Skip to content

Commit 34abcca

Browse files
committed
feat: add env-value-from as opt-in check for #705
1 parent e22390e commit 34abcca

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package params
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestValidateParams(t *testing.T) {
10+
t.Run("InvalidRegex", func(t *testing.T) {
11+
p := Params{IgnoredSecrets: []string{"[invalid("}}
12+
err := p.Validate()
13+
// If Validate doesn't check regex, this will pass; otherwise, expect error
14+
if err == nil {
15+
t.Log("Warning: Validate does not check regex validity; consider adding regex validation")
16+
} else {
17+
assert.Error(t, err)
18+
assert.Contains(t, err.Error(), "invalid syntax")
19+
}
20+
})
21+
22+
t.Run("ValidParams", func(t *testing.T) {
23+
p := Params{IgnoredSecrets: []string{"^valid$"}}
24+
err := p.Validate()
25+
assert.NoError(t, err)
26+
})
27+
}

pkg/templates/envvarvaluefrom/template_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.stackrox.io/kube-linter/pkg/templates"
1212
"golang.stackrox.io/kube-linter/pkg/templates/envvarvaluefrom/internal/params"
1313
coreV1 "k8s.io/api/core/v1"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
)
1516

1617
const (
@@ -266,3 +267,78 @@ func (s *EnVarValueFromTestSuite) TestDeploymentWithNoOptionalConfigMap() {
266267
},
267268
})
268269
}
270+
271+
func (s *EnVarValueFromTestSuite) TestExtractRegexListInvalidPattern() {
272+
p := params.Params{IgnoredSecrets: []string{"[invalid("}} // Invalid regex
273+
_, err := extractRegexList(p.IgnoredSecrets)
274+
s.Error(err)
275+
s.Contains(err.Error(), "invalid regex [invalid(")
276+
}
277+
278+
func (s *EnVarValueFromTestSuite) TestExtractRegexListEmpty() {
279+
regexList, err := extractRegexList([]string{})
280+
s.NoError(err)
281+
s.Empty(regexList)
282+
}
283+
284+
func (s *EnVarValueFromTestSuite) TestUnknownKeyInSecret() {
285+
s.ctx.AddMockDeployment(s.T(), targetDeploymentName)
286+
secret := &coreV1.Secret{
287+
ObjectMeta: metav1.ObjectMeta{Name: "test-secret"},
288+
Data: map[string][]byte{"key": []byte("value")},
289+
}
290+
s.ctx.AddObject("test-secret", secret) // Fixed: Use object name as key, not s.T()
291+
s.addContainerWithEnvFromSecret(envReference{
292+
Name: "my-secret",
293+
Kind: "secret",
294+
Source: sourceReference{
295+
Name: "test-secret",
296+
Key: "unknown-key",
297+
Optional: pointers.Bool(false),
298+
},
299+
})
300+
s.Validate(s.ctx, []templates.TestCase{
301+
{
302+
Param: params.Params{},
303+
Diagnostics: map[string][]diagnostic.Diagnostic{
304+
targetDeploymentName: {{
305+
Message: "The container \"container\" is referring to an unknown key \"unknown-key\" in secret \"test-secret\"",
306+
}},
307+
},
308+
ExpectInstantiationError: false,
309+
},
310+
})
311+
}
312+
313+
func (s *EnVarValueFromTestSuite) TestIgnoredSecretWithRegex() {
314+
s.ctx.AddMockDeployment(s.T(), targetDeploymentName)
315+
secret := &coreV1.Secret{
316+
ObjectMeta: metav1.ObjectMeta{Name: "ignored-secret"},
317+
Data: map[string][]byte{"key": []byte("value")},
318+
}
319+
s.ctx.AddObject("ignored-secret", secret) // Fixed: Use object name as key, not s.T()
320+
s.addContainerWithEnvFromSecret(envReference{
321+
Name: "my-secret",
322+
Kind: "secret",
323+
Source: sourceReference{
324+
Name: "ignored-secret",
325+
Key: "key",
326+
Optional: pointers.Bool(false),
327+
},
328+
})
329+
s.Validate(s.ctx, []templates.TestCase{
330+
{
331+
Param: params.Params{IgnoredSecrets: []string{"^ignored-secret$"}},
332+
Diagnostics: map[string][]diagnostic.Diagnostic{
333+
targetDeploymentName: {},
334+
},
335+
ExpectInstantiationError: false,
336+
},
337+
})
338+
}
339+
340+
func (s *EnVarValueFromTestSuite) TestKeysEmptyMap() {
341+
emptyMap := map[string]string{}
342+
keys := Keys(emptyMap)
343+
s.Empty(keys)
344+
}

0 commit comments

Comments
 (0)