One gRPC service per domain in state.v1 (tasks, executions, accounts, identities, packages, captures, bindings, access, orgs, alerts and so on), each with its own server type in staterpc. RPC and message names and numbers are unchanged.
89 lines
3.6 KiB
Go
89 lines
3.6 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
type accountPolicyStore struct {
|
|
access.PolicyStore
|
|
policies []access.Policy
|
|
}
|
|
|
|
func (s accountPolicyStore) ListPolicies(context.Context) ([]access.Policy, error) {
|
|
return s.policies, nil
|
|
}
|
|
|
|
type accountRegistrationStore struct{ identity.Accounts }
|
|
|
|
func (accountRegistrationStore) RegisterAccount(context.Context, identity.AccountRegistration, bool) (identity.Identity, error) {
|
|
return identity.Identity{ID: "created"}, nil
|
|
}
|
|
|
|
func TestAccountManagementUsesPolicy(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
role org.Role
|
|
extra string
|
|
anonymous bool
|
|
want codes.Code
|
|
}{
|
|
{name: "administrator relationship permits", role: org.RoleAdmin, want: codes.OK},
|
|
{name: "developer has no grant", role: org.RoleDeveloper, want: codes.PermissionDenied},
|
|
{name: "explicit identity policy permits without admin role", role: org.RoleDeveloper, extra: `permit(principal == Archie::Identity::"alice", action == Archie::Action::"manage_members", resource);`, want: codes.OK},
|
|
{name: "explicit forbid wins over admin role", role: org.RoleAdmin, extra: `forbid(principal,action == Archie::Action::"manage_members",resource);`, want: codes.PermissionDenied},
|
|
{name: "unauthenticated service has no user grant", anonymous: true, want: codes.PermissionDenied},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx := t.Context()
|
|
if !tc.anonymous {
|
|
ctx = access.WithPrincipal(ctx, access.Principal{IdentityID: "alice", Org: "acme", Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tc.role}}})
|
|
}
|
|
policies := access.ShippedOrgPolicies("acme")
|
|
if tc.extra != "" {
|
|
policies = append(policies, access.Policy{ID: "custom-members", Level: access.LevelOrg, OrgID: "acme", Text: tc.extra})
|
|
}
|
|
srv := &accountServer{server: &server{deps: Deps{Accounts: accountRegistrationStore{}, Policies: accountPolicyStore{policies: policies}}}}
|
|
_, err := srv.RegisterAccount(ctx, &pb.RegisterAccountRequest{Email: "new@example.com", Role: "viewer"})
|
|
if got := status.Code(err); got != tc.want {
|
|
t.Fatalf("code=%s want=%s err=%v", got, tc.want, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTaskCredentialsCannotManageAccounts(t *testing.T) {
|
|
for _, method := range []string{
|
|
pb.AccountService_AccountSetup_FullMethodName,
|
|
pb.AccountService_RegisterAccount_FullMethodName,
|
|
pb.AccountService_SignInAccount_FullMethodName,
|
|
pb.AccountService_ResolveAccountSession_FullMethodName,
|
|
pb.AccountService_SignOutAccount_FullMethodName,
|
|
pb.AccountService_ChangeAccountPassword_FullMethodName,
|
|
pb.AccountService_GetAuthenticationSettings_FullMethodName,
|
|
pb.AccountService_SaveAuthenticationSettings_FullMethodName,
|
|
pb.AccountService_BeginPasskey_FullMethodName,
|
|
pb.AccountService_FinishPasskey_FullMethodName,
|
|
pb.AccountService_ListPasskeys_FullMethodName,
|
|
pb.AccountService_DeletePasskey_FullMethodName,
|
|
pb.AccountService_OpenAccountSession_FullMethodName,
|
|
pb.AccountService_RequireFreshAccountSession_FullMethodName,
|
|
pb.AccountService_CreateAccountRecovery_FullMethodName,
|
|
pb.AccountService_RecoverAccount_FullMethodName,
|
|
} {
|
|
t.Run(method, func(t *testing.T) {
|
|
if authorizesTaskScopedCall(method, nil, 7) {
|
|
t.Fatal("task credential authorizes account operation")
|
|
}
|
|
})
|
|
}
|
|
}
|