Skip to content

Commit 42fc4f0

Browse files
authored
fix: Correct how configmap and secrets paths are parsed in the CLI (#1375)
* fix: Correct how configmap and secrets paths are parsed in the CLI * Add some garbage data to secrets and configmaps
1 parent 312e467 commit 42fc4f0

File tree

10 files changed

+106
-13
lines changed

10 files changed

+106
-13
lines changed

internal/specs/specs.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,58 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
8282

8383
for _, v := range args {
8484
if strings.HasPrefix(v, "secret/") {
85-
// format secret/namespace-name/secret-name
85+
// format secret/namespace-name/secret-name[/data-key]
8686
pathParts := strings.Split(v, "/")
87-
if len(pathParts) != 3 {
88-
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Errorf("secret path %q must have 3 components", v))
87+
if len(pathParts) > 4 {
88+
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Errorf("secret path %s must have at most 4 components", v))
89+
}
90+
if len(pathParts) < 3 {
91+
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Errorf("secret path %s must have at least 3 components", v))
8992
}
9093

9194
data, err := LoadFromSecret(ctx, client, pathParts[1], pathParts[2])
9295
if err != nil {
9396
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Wrap(err, "failed to get spec from secret"))
9497
}
9598

96-
// Append all data in the secret. Some may not be specs, but that's ok. They will be ignored.
97-
for _, spec := range data {
98-
rawSpecs = append(rawSpecs, string(spec))
99+
// If we have a key defined, then load specs from that key only.
100+
if len(pathParts) == 4 {
101+
spec, ok := data[pathParts[3]]
102+
if ok {
103+
rawSpecs = append(rawSpecs, string(spec))
104+
}
105+
} else {
106+
// Append all data in the secret. Some may not be specs, but that's ok. They will be ignored.
107+
for _, spec := range data {
108+
rawSpecs = append(rawSpecs, string(spec))
109+
}
99110
}
100111
} else if strings.HasPrefix(v, "configmap/") {
101-
// format configmap/namespace-name/configmap-name
112+
// format configmap/namespace-name/configmap-name[/data-key]
102113
pathParts := strings.Split(v, "/")
103-
if len(pathParts) != 3 {
104-
return nil, errors.Errorf("configmap path %q must have 3 components", v)
114+
if len(pathParts) > 4 {
115+
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Errorf("configmap path %s must have at most 4 components", v))
116+
}
117+
if len(pathParts) < 3 {
118+
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Errorf("configmap path %s must have at least 3 components", v))
105119
}
106120

107121
data, err := LoadFromConfigMap(ctx, client, pathParts[1], pathParts[2])
108122
if err != nil {
109-
return nil, errors.Wrap(err, "failed to get spec from configmap")
123+
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, errors.Wrap(err, "failed to get spec from configmap"))
110124
}
111125

112-
// Append all data in the configmap. Some may not be specs, but that's ok. They will be ignored.
113-
for _, spec := range data {
114-
rawSpecs = append(rawSpecs, spec)
126+
// If we have a key defined, then load specs from that key only.
127+
if len(pathParts) == 4 {
128+
spec, ok := data[pathParts[3]]
129+
if ok {
130+
rawSpecs = append(rawSpecs, spec)
131+
}
132+
} else {
133+
// Append all data in the configmap. Some may not be specs, but that's ok. They will be ignored.
134+
for _, spec := range data {
135+
rawSpecs = append(rawSpecs, spec)
136+
}
115137
}
116138
} else if _, err := os.Stat(v); err == nil {
117139
b, err := os.ReadFile(v)

test/validate-support-bundle-e2e.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,45 @@ if ! grep "labelled-support-bundle-4 \*\*\*HIDDEN\*\*\*" "$tmpdir/$bundle_direct
111111
echo "Hidden content not found in redacted echo-hi-4 file"
112112
exit 1
113113
fi
114+
115+
echo "======= Generating support bundle from k8s secret/<namespace-name>/<secret-name>/<data-key> ======"
116+
recreate_tmpdir
117+
kubectl apply -f "$PRJ_ROOT/testdata/supportbundle/labelled-specs"
118+
./bin/support-bundle -v1 --interactive=false secret/default/labelled-support-bundle-1/custom-spec-key \
119+
--redactors configmap/default/labelled-redactor-spec-1/customer-redactor-spec \
120+
--output=$tmpdir/$bundle_archive_name
121+
if [ $? -ne 0 ]; then
122+
echo "support-bundle command failed"
123+
exit $?
124+
fi
125+
126+
if ! tar -xvzf $tmpdir/$bundle_archive_name --directory $tmpdir; then
127+
echo "A valid support bundle archive was not generated"
128+
exit 1
129+
fi
130+
131+
if ! grep "custom-spec-key \*\*\*HIDDEN\*\*\*" "$tmpdir/$bundle_directory_name/echo-hi-3"; then
132+
echo "$(cat $tmpdir/$bundle_directory_name/echo-hi-3)"
133+
echo "Hidden content not found in redacted echo-hi-3 file"
134+
exit 1
135+
fi
136+
137+
echo "======= Generating support bundle from k8s configmap/<namespace-name>/<configmap-name> ======"
138+
recreate_tmpdir
139+
kubectl apply -f "$PRJ_ROOT/testdata/supportbundle/labelled-specs"
140+
./bin/support-bundle -v1 --interactive=false configmap/labelled-specs/labelled-support-bundle-2 --output=$tmpdir/$bundle_archive_name
141+
if [ $? -ne 0 ]; then
142+
echo "support-bundle command failed"
143+
exit $?
144+
fi
145+
146+
if ! tar -xvzf $tmpdir/$bundle_archive_name --directory $tmpdir; then
147+
echo "A valid support bundle archive was not generated"
148+
exit 1
149+
fi
150+
151+
if ! grep "labelled-support-bundle-2 REDACT" "$tmpdir/$bundle_directory_name/echo-hi-2"; then
152+
echo "$(cat $tmpdir/$bundle_directory_name/echo-hi-2)"
153+
echo "Hidden content not found in redacted echo-hi-2 file"
154+
exit 1
155+
fi

testdata/supportbundle/labelled-specs/redact-spec-1.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ data:
1616
removals:
1717
values:
1818
- REDACT FIRST TEXT PLEASE
19+
customer-redactor-spec: |
20+
apiVersion: troubleshoot.sh/v1beta2
21+
kind: Redactor
22+
metadata:
23+
name: labelled-redactor-spec-1
24+
spec:
25+
redactors:
26+
- name: redact-text-1
27+
removals:
28+
values:
29+
- REDACT FIRST TEXT PLEASE
30+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/redact-spec-2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ stringData:
1717
removals:
1818
values:
1919
- REDACT SECOND TEXT PLEASE
20+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/redact-spec-3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ data:
1616
removals:
1717
values:
1818
- REDACT FIRST TEXT PLEASE
19+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/redact-spec-4.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ stringData:
1717
removals:
1818
values:
1919
- REDACT SECOND TEXT PLEASE
20+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/sb-spec-1.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ stringData:
1515
- data:
1616
name: echo-hi-1
1717
data: "I am labelled-support-bundle-1 REDACT FIRST TEXT PLEASE"
18+
custom-spec-key: |
19+
apiVersion: troubleshoot.sh/v1beta2
20+
kind: SupportBundle
21+
metadata:
22+
name: custom-spec-key
23+
spec:
24+
collectors:
25+
- data:
26+
name: echo-hi-3
27+
data: "I am custom-spec-key REDACT FIRST TEXT PLEASE"
28+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/sb-spec-2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ data:
1616
- data:
1717
name: echo-hi-2
1818
data: "I am labelled-support-bundle-2 REDACT SECOND TEXT PLEASE"
19+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/sb-spec-3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ stringData:
1515
- data:
1616
name: echo-hi-3
1717
data: "I am labelled-support-bundle-3 REDACT FIRST TEXT PLEASE"
18+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

testdata/supportbundle/labelled-specs/sb-spec-4.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ data:
1616
- data:
1717
name: echo-hi-4
1818
data: "I am labelled-support-bundle-4 REDACT SECOND TEXT PLEASE"
19+
garbagge: MWdRRTlBRi9YNzB3eUE2VEgvWjdhRFVUR1UvRmU3TXdUR3Q4cnE4Nkti

0 commit comments

Comments
 (0)