-
Notifications
You must be signed in to change notification settings - Fork 903
test: Add unit test for rbac Authorize()
function
#853
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
Changes from 1 commit
ab61328
03e4d0f
9981291
3ab32da
e1d5893
84a90f3
1fac0d9
1e3aac0
e977e84
00a7c3f
1f04c01
fbf4db1
4946897
7e6cc66
a0017e5
4b110b3
65ef4e3
d294786
c1f8945
01f3d40
de7de6e
4c86e44
30c6568
a419a65
bbd1c4c
def010f
c4ee590
84e3ab9
c2eec18
5a2834a
913d141
2804b92
5698938
75ed8ef
b2db661
26ef1e6
19aba30
ceee9cd
ee8bf04
44c02a1
dfb9ad1
e482d2c
a4e038f
9918c16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package authz | ||
|
||
import "strings" | ||
|
||
type permLevel string | ||
|
||
const ( | ||
LevelWildcard permLevel = "*" | ||
LevelSite permLevel = "site" | ||
LevelOrg permLevel = "org" | ||
LevelUser permLevel = "user" | ||
) | ||
|
||
var PermissionLevels = [4]permLevel{LevelWildcard, LevelSite, LevelOrg, LevelUser} | ||
|
||
type Permission struct { | ||
// Sign is positive or negative. | ||
// True = Positive, False = negative | ||
Sign bool | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Level permLevel | ||
// LevelID is used for identifying a particular org. | ||
// org:1234 | ||
LevelID string | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ResourceType string | ||
ResourceID string | ||
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. All of our resource identifiers are UUIDs, can this be one too? 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. I thought some 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 is true! All v1 IDs have been converted to UUIDs. |
||
Action string | ||
} | ||
|
||
// String returns the <level>.<resource_type>.<id>.<action> string formatted permission. | ||
// A string builder is used to be the most efficient. | ||
func (p Permission) String() string { | ||
var s strings.Builder | ||
// This could be 1 more than the actual capacity. But being 1 byte over for capacity is ok. | ||
s.Grow(1 + 4 + len(p.Level) + len(p.LevelID) + len(p.ResourceType) + len(p.ResourceID) + len(p.Action)) | ||
if p.Sign { | ||
s.WriteRune('+') | ||
} else { | ||
s.WriteRune('-') | ||
} | ||
s.WriteString(string(p.Level)) | ||
if p.LevelID != "" { | ||
s.WriteRune(':') | ||
s.WriteString(p.LevelID) | ||
} | ||
s.WriteRune('.') | ||
s.WriteString(p.ResourceType) | ||
s.WriteRune('.') | ||
s.WriteString(p.ResourceID) | ||
s.WriteRune('.') | ||
s.WriteString(p.Action) | ||
return s.String() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package testdata | ||
|
||
type permissionSet string | ||
|
||
const ( | ||
SetPositive permissionSet = "j" | ||
SetNegative permissionSet = "j!" | ||
SetNeutral permissionSet = "a" | ||
) | ||
|
||
var ( | ||
PermissionSets = []permissionSet{SetPositive, SetNegative, SetNeutral} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package testdata | ||
|
||
import ( | ||
. "github.com/coder/coder/coderd/authz" | ||
) | ||
|
||
type level string | ||
|
||
const ( | ||
otherOption = "other" | ||
|
||
levelWild level = "*" | ||
levelSite level = "site" | ||
levelOrg level = "org" | ||
levelOrgMem level = "org:mem" | ||
// levelOrgAll is a helper to get both org levels above | ||
levelOrgAll level = "org:*" | ||
levelUser level = "user" | ||
) | ||
|
||
var ( | ||
PermissionTypes = []bool{true, false} | ||
Levels = PermissionLevels | ||
LevelIDs = []string{"", "mem"} | ||
ResourceTypes = []string{"resource", "*", otherOption} | ||
ResourceIDs = []string{"rid", "*", otherOption} | ||
Actions = []string{"action", "*", otherOption} | ||
) | ||
|
||
func AllPermissions() Set { | ||
all := make(Set, 0, 2*len(Levels)*len(LevelIDs)*len(ResourceTypes)*len(ResourceIDs)*len(Actions)) | ||
for _, p := range PermissionTypes { | ||
for _, l := range Levels { | ||
for _, lid := range LevelIDs { | ||
for _, t := range ResourceTypes { | ||
for _, i := range ResourceIDs { | ||
for _, a := range Actions { | ||
all = append(all, &Permission{ | ||
Sign: p, | ||
Level: l, | ||
LevelID: lid, | ||
ResourceType: t, | ||
ResourceID: i, | ||
Action: a, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return all | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package testdata | ||
|
||
import ( | ||
. "github.com/coder/coder/coderd/authz" | ||
) | ||
|
||
var _ Permission | ||
|
||
type Role struct { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package testdata | ||
|
||
import ( | ||
"strings" | ||
|
||
. "github.com/coder/coder/coderd/authz" | ||
) | ||
|
||
type Set []*Permission | ||
|
||
func (s Set) String() string { | ||
var str strings.Builder | ||
sep := "" | ||
for _, v := range s { | ||
str.WriteString(sep) | ||
str.WriteString(v.String()) | ||
sep = ", " | ||
} | ||
return str.String() | ||
} |
Uh oh!
There was an error while loading. Please reload this page.