44 lines
2.2 KiB
Go
44 lines
2.2 KiB
Go
package domain
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.cin.su/m/Assistant/contracts/authority"
|
|
"git.cin.su/m/Assistant/contracts/identity"
|
|
)
|
|
|
|
func delegatedGrant() authority.DelegatedControlPlaneGrant {
|
|
return authority.DelegatedControlPlaneGrant{ID: "grant-1", ServiceActorID: "service-actor", ServicePrincipalID: "service-principal", IssuerPrincipalID: "owner", TaskID: "GEN2-COGNITIVE-003", TaskRevision: 4, Operations: []authority.DelegatedControlPlaneOperation{authority.DelegatedOperationCanaryCognitiveBinding}, Resource: authority.CognitiveBindingResource{BindingID: "binding-1", ProviderInstanceID: "provider-1", ModelDescriptorID: "model-1"}, IssuedAtUnixMillis: 10, ExpiresAtUnixMillis: 100, ArchitectureRevision: "ADR-0021", Nonce: "nonce", Revision: 1, Status: authority.DelegatedGrantActive}
|
|
}
|
|
|
|
func TestDelegatedControlPlaneGrantIsExactAndTimeBound(t *testing.T) {
|
|
grant := delegatedGrant()
|
|
binding := identity.PrincipalBinding{ActorID: "service-actor", PrincipalID: "service-principal", Active: true}
|
|
request := authority.DelegatedControlPlaneRequest{GrantID: grant.ID, TaskID: grant.TaskID, TaskRevision: grant.TaskRevision, Operation: authority.DelegatedOperationCanaryCognitiveBinding, Resource: grant.Resource}
|
|
if err := ValidateDelegatedControlPlaneGrant(20, binding, grant, request); err != nil {
|
|
t.Fatalf("valid delegation denied: %v", err)
|
|
}
|
|
request.Resource.BindingID = "other"
|
|
if err := ValidateDelegatedControlPlaneGrant(20, binding, grant, request); err == nil {
|
|
t.Fatal("different resource was allowed")
|
|
}
|
|
request.Resource = grant.Resource
|
|
if err := ValidateDelegatedControlPlaneGrant(100, binding, grant, request); err == nil {
|
|
t.Fatal("expired grant was allowed")
|
|
}
|
|
grant.Status = authority.DelegatedGrantRevoked
|
|
if err := ValidateDelegatedControlPlaneGrant(20, binding, grant, request); err == nil {
|
|
t.Fatal("revoked grant was allowed")
|
|
}
|
|
}
|
|
|
|
func TestDelegatedGrantCannotSelfRevokeOrBroaden(t *testing.T) {
|
|
grant := delegatedGrant()
|
|
if CanIssuerRevokeDelegatedGrant("service-principal", grant) {
|
|
t.Fatal("service principal may not revoke its own grant")
|
|
}
|
|
if !CanIssuerRevokeDelegatedGrant("owner", grant) {
|
|
t.Fatal("issuer could not revoke active grant")
|
|
}
|
|
}
|