-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
447 additions
and
91 deletions.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package v1beta1 | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
) | ||
|
||
const RoleAdmin = "Admin" | ||
|
||
func CanForceRun(email string, groups []string, module *Module) bool { | ||
// remove empty values | ||
groups = slices.DeleteFunc(groups, func(g string) bool { | ||
return strings.TrimSpace(g) == "" | ||
}) | ||
|
||
for _, rbac := range module.Spec.RBAC { | ||
// only module "Admins" are allowed to do force run | ||
if rbac.Role == RoleAdmin { | ||
for _, subject := range rbac.Subjects { | ||
// check if email matching and its not a empty value | ||
if subject.Kind == "User" && | ||
subject.Name == email && | ||
strings.TrimSpace(email) != "" { | ||
return true | ||
} | ||
if subject.Kind == "Group" && | ||
slices.Contains(groups, subject.Name) { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
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 |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package v1beta1 | ||
|
||
import "testing" | ||
|
||
func TestCanForceRun(t *testing.T) { | ||
type args struct { | ||
email string | ||
groups []string | ||
module *Module | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
"no rbac", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{Spec: ModuleSpec{RepoName: "test"}}, | ||
}, | ||
false, | ||
}, { | ||
"no Admin role", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{Role: "View", Subjects: []Subject{{Kind: "User", Name: "u1"}}}}, | ||
}}, | ||
}, | ||
false, | ||
}, { | ||
"email or group doesn't match", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group3"}, | ||
}}}}}, | ||
}, | ||
false, | ||
}, { | ||
"email match", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u1"}, | ||
{Kind: "Group", Name: "group3"}, | ||
}}}}}, | ||
}, | ||
true, | ||
}, { | ||
"email match", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group3"}, | ||
{Kind: "User", Name: "u1"}, | ||
}}}}}, | ||
}, | ||
true, | ||
}, { | ||
"empty email match", | ||
args{ | ||
" ", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: " "}, | ||
{Kind: "Group", Name: "group3"}, | ||
{Kind: "User", Name: "u1"}, | ||
}}}}}, | ||
}, | ||
false, | ||
}, { | ||
"group match", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group2"}, | ||
}}}}}, | ||
}, | ||
true, | ||
}, { | ||
"empty group match", | ||
args{ | ||
"u1", []string{" ", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: " "}, | ||
}}}}}, | ||
}, | ||
false, | ||
}, { | ||
"with multiple roles", | ||
args{ | ||
"u1", []string{"group1", "group2"}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "View", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group2"}, | ||
}}, { | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group2"}, | ||
}}, | ||
}}}, | ||
}, | ||
true, | ||
}, { | ||
"no_user_groups", | ||
args{ | ||
"u1", []string{}, | ||
&Module{ | ||
Spec: ModuleSpec{ | ||
RBAC: []RBAC{{ | ||
Role: "View", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group2"}, | ||
}}, { | ||
Role: "Admin", | ||
Subjects: []Subject{ | ||
{Kind: "User", Name: "u3"}, | ||
{Kind: "Group", Name: "group2"}, | ||
}}, | ||
}}}, | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := CanForceRun(tt.args.email, tt.args.groups, tt.args.module); got != tt.want { | ||
t.Errorf("CanForceRun() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.