Skip to content

Commit b6923ce

Browse files
author
ElasticClaw Factory
committed
feat(supportbundle): support secret/... redactor URIs
1 parent 1f1a21b commit b6923ce

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

pkg/supportbundle/load.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"k8s.io/klog/v2"
2121
)
2222

23+
// loadFromSecret is a package-level hook so tests can stub out cluster access.
24+
var loadFromSecret = specs.LoadFromSecret
25+
2326
// GetSupportBundleFromURI downloads and parses a support bundle from a URI and returns a SupportBundle object
2427
func GetSupportBundleFromURI(bundleURI string) (*troubleshootv1beta2.SupportBundle, error) {
2528
collectorContent, err := LoadSupportBundleSpec(bundleURI)
@@ -165,6 +168,29 @@ func LoadSupportBundleSpec(arg string) ([]byte, error) {
165168
}
166169

167170
func LoadRedactorSpec(arg string) ([]byte, error) {
171+
if strings.HasPrefix(arg, "secret/") {
172+
// format secret/namespace-name/secret-name[/data-key]
173+
pathParts := strings.Split(arg, "/")
174+
if len(pathParts) > 4 {
175+
return nil, errors.Errorf("secret path %s must have at most 4 components", arg)
176+
}
177+
if len(pathParts) < 3 {
178+
return nil, errors.Errorf("secret path %s must have at least 3 components", arg)
179+
}
180+
181+
dataKey := "redactor-spec"
182+
if len(pathParts) == 4 {
183+
dataKey = pathParts[3]
184+
}
185+
186+
spec, err := loadFromSecret(pathParts[1], pathParts[2], dataKey)
187+
if err != nil {
188+
return nil, errors.Wrap(err, "failed to get spec from secret")
189+
}
190+
191+
return spec, nil
192+
}
193+
168194
if strings.HasPrefix(arg, "configmap/") {
169195
// format configmap/namespace-name/configmap-name[/data-key]
170196
pathParts := strings.Split(arg, "/")

pkg/supportbundle/load_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package supportbundle
22

33
import (
4+
"fmt"
45
"reflect"
6+
"strings"
57
"testing"
68

79
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
@@ -92,3 +94,61 @@ spec:
9294
})
9395
}
9496
}
97+
98+
func TestLoadRedactorSpec(t *testing.T) {
99+
origLoadFromSecret := loadFromSecret
100+
defer func() { loadFromSecret = origLoadFromSecret }()
101+
102+
loadFromSecret = func(namespace, secretName, key string) ([]byte, error) {
103+
return []byte(fmt.Sprintf("namespace=%s,secret=%s,key=%s", namespace, secretName, key)), nil
104+
}
105+
106+
tests := []struct {
107+
name string
108+
uri string
109+
wantContent string
110+
wantErr string
111+
}{
112+
{
113+
name: "secret URI with default key",
114+
uri: "secret/default/my-redactor",
115+
wantContent: "namespace=default,secret=my-redactor,key=redactor-spec",
116+
},
117+
{
118+
name: "secret URI with custom key",
119+
uri: "secret/default/my-redactor/custom-key",
120+
wantContent: "namespace=default,secret=my-redactor,key=custom-key",
121+
},
122+
{
123+
name: "secret URI with too few components",
124+
uri: "secret/default",
125+
wantErr: "must have at least 3 components",
126+
},
127+
{
128+
name: "secret URI with too many components",
129+
uri: "secret/default/my-redactor/custom-key/extra",
130+
wantErr: "must have at most 4 components",
131+
},
132+
}
133+
134+
for _, tt := range tests {
135+
t.Run(tt.name, func(t *testing.T) {
136+
got, err := LoadRedactorSpec(tt.uri)
137+
if tt.wantErr != "" {
138+
if err == nil {
139+
t.Fatalf("LoadRedactorSpec() expected error, got nil")
140+
}
141+
if !strings.Contains(err.Error(), tt.wantErr) {
142+
t.Errorf("LoadRedactorSpec() error = %q, want containing %q", err.Error(), tt.wantErr)
143+
}
144+
return
145+
}
146+
if err != nil {
147+
t.Fatalf("LoadRedactorSpec() unexpected error = %v", err)
148+
}
149+
if string(got) != tt.wantContent {
150+
t.Errorf("LoadRedactorSpec() = %q, want %q", string(got), tt.wantContent)
151+
}
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)