-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg/validation: add selector validation #288
Merged
k8s-ci-robot
merged 2 commits into
kubernetes-sigs:master
from
ahmetb:nil-platforms-and-selectors
Jul 22, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,47 +25,36 @@ import ( | |
) | ||
|
||
func Test_IsSafePluginName(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
name string | ||
pluginName string | ||
want bool | ||
}{ | ||
{ | ||
name: "secure name", | ||
args: args{ | ||
name: "foo-bar", | ||
}, | ||
want: true, | ||
name: "secure name", | ||
pluginName: "foo-bar", | ||
want: true, | ||
}, | ||
{ | ||
name: "insecure path name", | ||
args: args{ | ||
name: "/foo-bar", | ||
}, | ||
want: false, | ||
name: "insecure path name", | ||
pluginName: "/foo-bar", | ||
want: false, | ||
}, | ||
{ | ||
name: "relative name", | ||
args: args{ | ||
name: "..foo-bar", | ||
}, | ||
want: false, | ||
name: "relative name", | ||
pluginName: "..foo-bar", | ||
want: false, | ||
}, | ||
{ | ||
name: "bad windows name", | ||
args: args{ | ||
name: "nul", | ||
}, | ||
want: false, | ||
name: "bad windows name", | ||
pluginName: "nul", | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsSafePluginName(tt.args.name); got != tt.want { | ||
t.Errorf("IsSafePluginName() = %v, want %v", got, tt.want) | ||
if got := IsSafePluginName(tt.pluginName); got != tt.want { | ||
t.Errorf("IsSafePluginName(%s) = %v, want %v", tt.pluginName, got, tt.want) | ||
} | ||
}) | ||
} | ||
|
@@ -204,14 +193,85 @@ func TestValidatePlatform(t *testing.T) { | |
platform: testutil.NewPlatform().WithBin("").V(), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid platform selector", | ||
platform: testutil.NewPlatform().WithSelector(&metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"unsupported-field": "orange"}}).V(), | ||
wantErr: true, | ||
}, | ||
// TODO(ahmetb): add test case "bin field outside the plugin installation directory" | ||
// by testing .WithBin("foo/../../../malicious-file"). | ||
// It appears like currently we're allowing this. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := ValidatePlatform(tt.platform); (err != nil) != tt.wantErr { | ||
t.Errorf("ValidatePlatform() error = %v, wantErr %v", err, tt.wantErr) | ||
if err := validatePlatform(tt.platform); (err != nil) != tt.wantErr { | ||
t.Errorf("validatePlatform() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_validateSelector(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
sel *metav1.LabelSelector | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "nil selector", | ||
sel: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test does not pass, and should be added: {
name: "wildcard selector",
sel: &metav1.LabelSelector{},
wantErr: true,
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a great catch. Added a commit. |
||
name: "empty (wildcard) selector", | ||
sel: &metav1.LabelSelector{}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "valid matchLabels", | ||
sel: &metav1.LabelSelector{MatchLabels: map[string]string{"os": "foo", "arch": "bar"}}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid matchExpressions", | ||
sel: &metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{Key: "os", | ||
Operator: metav1.LabelSelectorOpIn, | ||
Values: []string{"apple", "orange"}, | ||
}}}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "empty matchLabels", | ||
sel: &metav1.LabelSelector{MatchLabels: map[string]string{}}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "empty matchExpressions", | ||
sel: &metav1.LabelSelector{MatchExpressions: []metav1.LabelSelectorRequirement{}}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "unsupported key in matchLabels", | ||
sel: &metav1.LabelSelector{MatchLabels: map[string]string{"unsupported-key": "value"}}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "unsupported key in matchExpressions", | ||
sel: &metav1.LabelSelector{ | ||
MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{Key: "unsupported-key", | ||
Operator: metav1.LabelSelectorOpIn, | ||
Values: []string{"apple", "orange"}}}}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := validateSelector(tt.sel); (err != nil) != tt.wantErr { | ||
t.Errorf("validateSelector() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be on the safe side, this should be
Otherwise,
sel.MatchLabels
could be non-nil, but empty. And that would match everything, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have a check for that case below.
I wanted to distinguish error messages for these:
which means different things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see.