-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update authz architecture to use Role for all user
- Loading branch information
Masayoshi Mizutani
committed
Feb 18, 2020
1 parent
15c0757
commit dda74ac
Showing
6 changed files
with
258 additions
and
16 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,155 @@ | ||
package main_test | ||
|
||
import ( | ||
"testing" | ||
|
||
main "github.com/m-mizutani/strix" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAuthzService(t *testing.T) { | ||
raw := `{ | ||
"users": [ | ||
{"user_id": "[email protected]", "role":"blue"}, | ||
{"user_id": "[email protected]", "role":"orange"}, | ||
{"user_id": "[email protected]", "role":"orange"} | ||
], | ||
"roles": [ | ||
{"name":"blue", "allowed_tags":[]}, | ||
{"name":"orange", "allowed_tags":["spell.1"]} | ||
], | ||
"rules": [ | ||
{"user_regex":"^delta@", "role":"blue"}, | ||
{"user_regex":"@example.com$", "role":"orange"} | ||
] | ||
}` | ||
|
||
authz, err := main.NewAuthzService([]byte(raw)) | ||
require.NoError(t, err) | ||
|
||
// Test default users and roles | ||
userA := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.NotNil(t, userA) | ||
assert.NotContains(t, main.AuthzUserAllowed(userA), "spell.1") | ||
|
||
userB := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.NotNil(t, userB) | ||
assert.Contains(t, main.AuthzUserAllowed(userB), "spell.1") | ||
|
||
userC := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.NotNil(t, userC) | ||
assert.Contains(t, main.AuthzUserAllowed(userC), "spell.1") | ||
|
||
// Test rules | ||
userD1 := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.NotNil(t, userD1) | ||
assert.Equal(t, 0, len(main.AuthzUserAllowed(userD1))) | ||
|
||
userD2 := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.NotNil(t, userD2) | ||
assert.Equal(t, 0, len(main.AuthzUserAllowed(userD2))) | ||
|
||
userD3 := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.Nil(t, userD3) | ||
|
||
userE1 := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.NotNil(t, userE1) | ||
assert.Contains(t, main.AuthzUserAllowed(userE1), "spell.1") | ||
|
||
userF1 := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.Nil(t, userF1) | ||
userF2 := main.AuthzServiceLookup(authz, "[email protected]") | ||
assert.Nil(t, userF2) | ||
} | ||
|
||
func TestAuthzServiceInvalidJSON(t *testing.T) { | ||
raw := `{ | ||
"users": [ | ||
{"user_id": "[email protected]", "role":"blue"} | ||
], | ||
"roles": [ | ||
{"name":"blue", "allowed_tags":[]} | ||
], | ||
"rules": [ | ||
{"user_regex":"^delta@", "role":"blue"} | ||
], | ||
}` // ^^^ invalid comma | ||
|
||
_, err := main.NewAuthzService([]byte(raw)) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestAuthzServiceDuplicatedRole(t *testing.T) { | ||
raw := `{ | ||
"users": [ | ||
{"user_id": "[email protected]", "role":"blue"} | ||
], | ||
"roles": [ | ||
{"name":"blue", "allowed_tags":[]}, | ||
{"name":"blue", "allowed_tags":["spell.1"]} | ||
] | ||
}` | ||
|
||
_, err := main.NewAuthzService([]byte(raw)) | ||
assert.EqualError(t, err, "Role 'blue' is duplicated") | ||
} | ||
|
||
func TestAuthzServiceDuplicatedUser(t *testing.T) { | ||
raw := `{ | ||
"users": [ | ||
{"user_id": "[email protected]", "role":"blue"}, | ||
{"user_id": "[email protected]", "role":"orange"} | ||
], | ||
"roles": [ | ||
{"name":"blue", "allowed_tags":[]}, | ||
{"name":"orange", "allowed_tags":["spell.1"]} | ||
] | ||
}` | ||
|
||
_, err := main.NewAuthzService([]byte(raw)) | ||
assert.EqualError(t, err, "User '[email protected]' is duplicated") | ||
} | ||
|
||
func TestAuthzServiceUserRoleNotFound(t *testing.T) { | ||
raw := `{ | ||
"users": [ | ||
{"user_id": "[email protected]", "role":"blue"}, | ||
{"user_id": "[email protected]", "role":"orange"} | ||
], | ||
"roles": [ | ||
{"name":"blue", "allowed_tags":[]} | ||
] | ||
}` | ||
|
||
_, err := main.NewAuthzService([]byte(raw)) | ||
assert.EqualError(t, err, "Role 'orange' of User '[email protected]' is not found") | ||
} | ||
|
||
func TestAuthzServiceRuleRoleNotFound(t *testing.T) { | ||
raw := `{ | ||
"roles": [ | ||
{"name":"blue", "allowed_tags":[]} | ||
], | ||
"rules": [ | ||
{"user_regex":"^delta@", "role":"orange"} | ||
] | ||
}` | ||
|
||
_, err := main.NewAuthzService([]byte(raw)) | ||
assert.EqualError(t, err, "Role 'orange' of Rule '^delta@' is not found") | ||
} | ||
|
||
func TestAuthzServiceRuleInavlidRegex(t *testing.T) { | ||
raw := `{ | ||
"roles": [ | ||
{"name":"orange", "allowed_tags":[]} | ||
], | ||
"rules": [ | ||
{"user_regex":"^[delta@", "role":"orange"} | ||
] | ||
}` | ||
|
||
_, err := main.NewAuthzService([]byte(raw)) | ||
assert.EqualError(t, err, "Fail to compile regex of a rule: ^[delta@") | ||
} |
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,13 @@ | ||
package main | ||
|
||
type AuthzUser authzUser | ||
|
||
var NewAuthzService = newAuthzService | ||
|
||
func AuthzServiceLookup(x *authzService, userID string) *AuthzUser { | ||
return (*AuthzUser)(x.lookup(userID)) | ||
} | ||
func AuthzUserAllowed(x *AuthzUser) []string { | ||
authz := (*authzUser)(x) | ||
return authz.rolePtr.AllowedTags | ||
} |
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