Improve grant validation (#3081)

This adds several relationships into the resource package that are then
made use of to improve the grant validation that runs during Parse. By
understanding mappings between ID and resource type the validation
checks can far better align with actual allowed grant formats so we can
find several common misunderstandings before they simply result in an
authorization failure.

In the future some of these functions could be used in the logic in the
actual ACL checks but it's not currently necessary and I don't want to
lump that change in with this.

Note that the tests have not meaningfully changed, but they _have_ had
to be updated because the stricter validation is actually catching
issues.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2023-03-16 10:11:55 -04:00
1 parent ef37369c2b
commit fc664eb35f
12 files changed
+380 -222

No files matched your search

+45 -1
View File
@@ -36,7 +36,7 @@ const (
// * The scopes service collection actions for appropriate scopes
// * The Test_AnonRestrictions test: update the following line to include the last resource:
// for i := resource.Type(1); i <= resource.<Resource>; i++ {
// * The prefixes and top level prefixes in globals/prefixes.go
// * The prefixes and mappings in globals/prefixes.go
)
func (r Type) MarshalJSON() ([]byte, error) {
@@ -99,3 +99,47 @@ var Map = map[string]Type{
CredentialLibrary.String(): CredentialLibrary,
Credential.String(): Credential,
}
// Parent returns the parent type for a given type; if there is no parent, it
// returns the incoming type
func Parent(in Type) Type {
switch in {
case Account, ManagedGroup:
return AuthMethod
case HostSet, Host:
return HostCatalog
case CredentialLibrary, Credential:
return CredentialStore
}
return in
}
// HasChildTypes indicates whether this is a type that has child resource types;
// it's essentially the inverse of Parent
func HasChildTypes(in Type) bool {
switch in {
case AuthMethod, HostCatalog, CredentialStore:
return true
}
return false
}
// TopLevelType indicates whether this is a type that supports collection
// actions, e.g. Create/List
func TopLevelType(typ Type) bool {
switch typ {
case AuthMethod,
AuthToken,
CredentialStore,
Group,
HostCatalog,
Role,
Scope,
Session,
Target,
User,
Worker:
return true
}
return false
}
+59 -24
View File
@@ -11,90 +11,125 @@ import (
func Test_Resource(t *testing.T) {
tests := []struct {
typeString string
want Type
typeString string
want Type
topLevelType bool
hasChildTypes bool
parent Type
}{
{
typeString: "unknown",
want: Unknown,
},
{
typeString: "scope",
want: Scope,
typeString: "scope",
want: Scope,
topLevelType: true,
},
{
typeString: "user",
want: User,
typeString: "user",
want: User,
topLevelType: true,
},
{
typeString: "group",
want: Group,
typeString: "group",
want: Group,
topLevelType: true,
},
{
typeString: "role",
want: Role,
typeString: "role",
want: Role,
topLevelType: true,
},
{
typeString: "auth-method",
want: AuthMethod,
typeString: "auth-method",
want: AuthMethod,
topLevelType: true,
hasChildTypes: true,
},
{
typeString: "account",
want: Account,
parent: AuthMethod,
},
{
typeString: "auth-token",
want: AuthToken,
typeString: "auth-token",
want: AuthToken,
topLevelType: true,
},
{
typeString: "*",
want: All,
},
{
typeString: "host-catalog",
want: HostCatalog,
typeString: "host-catalog",
want: HostCatalog,
topLevelType: true,
hasChildTypes: true,
},
{
typeString: "host-set",
want: HostSet,
parent: HostCatalog,
},
{
typeString: "host",
want: Host,
parent: HostCatalog,
},
{
typeString: "target",
want: Target,
typeString: "target",
want: Target,
topLevelType: true,
},
{
typeString: "controller",
want: Controller,
},
{
typeString: "worker",
want: Worker,
typeString: "worker",
want: Worker,
topLevelType: true,
},
{
typeString: "session",
want: Session,
typeString: "session",
want: Session,
topLevelType: true,
},
{
typeString: "managed-group",
want: ManagedGroup,
parent: AuthMethod,
},
{
typeString: "credential-store",
want: CredentialStore,
typeString: "credential-store",
want: CredentialStore,
topLevelType: true,
hasChildTypes: true,
},
{
typeString: "credential-library",
want: CredentialLibrary,
parent: CredentialStore,
},
{
typeString: "credential",
want: Credential,
parent: CredentialStore,
},
}
for _, tt := range tests {
t.Run(tt.typeString, func(t *testing.T) {
assert.Equalf(t, tt.want, Map[tt.typeString], "unexpected type for %s", tt.typeString)
assert.Equalf(t, tt.typeString, tt.want.String(), "unexpected string for %s", tt.typeString)
assert.Equalf(t, tt.topLevelType, TopLevelType(tt.want), "unexpected top level type types for %s", tt.typeString)
assert.Equalf(t, tt.hasChildTypes, HasChildTypes(tt.want), "unexpected has child types for %s", tt.typeString)
parent := Parent(tt.want)
if tt.parent == Unknown {
assert.Equal(t, tt.want, parent)
} else {
assert.Equal(t, tt.parent, parent)
}
})
}
}