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.
93 lines
3.2 KiB
Go
93 lines
3.2 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
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/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
)
|
|
|
|
type auditTasks struct {
|
|
storecontract.TaskStore
|
|
failInsert bool
|
|
recorded []events.Event
|
|
}
|
|
|
|
func (*auditTasks) TaskByID(_ context.Context, id int64) (*task.Task, error) {
|
|
return &task.Task{ID: id, Identity: "human", Workflow: "deploy", WorkflowDefinitionYAML: "id: deploy\nidentity: execution-agent\n"}, nil
|
|
}
|
|
|
|
func (a *auditTasks) InsertEvent(_ context.Context, e events.Event) (int64, error) {
|
|
if a.failInsert {
|
|
return 0, errors.New("store down")
|
|
}
|
|
a.recorded = append(a.recorded, e)
|
|
return 1, nil
|
|
}
|
|
|
|
// TestIdentityStartIsAudited pins that a run credential is only issued with
|
|
// its identity-start audit written, stamped by the server, and that a start
|
|
// whose audit fails leaves no usable credential.
|
|
func TestIdentityStartIsAudited(t *testing.T) {
|
|
token := strings.Repeat("cd", 32)
|
|
tests := []struct {
|
|
name string
|
|
failInsert bool
|
|
}{
|
|
{"audited start issues the credential", false},
|
|
{"unaudited start is undone", true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tasks := &auditTasks{failInsert: tt.failInsert}
|
|
grants := &TaskGrants{Store: credentials{}}
|
|
s := &taskGrantServer{server: &server{deps: Deps{Tasks: tasks, Grants: grants, RootIdentity: "root", Identities: executionIdentityRepo{}}}}
|
|
ctx := access.WithActor(context.Background(), "archied")
|
|
_, err := s.RegisterTaskGrant(ctx, &pb.RegisterTaskGrantRequest{TaskId: 7, Token: token, LifetimeSeconds: 60})
|
|
live, _ := grants.taskFor(ctx, token)
|
|
if tt.failInsert {
|
|
if err == nil || live != 0 {
|
|
t.Fatalf("err %v, credential live for task %d", err, live)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || live != 7 || len(tasks.recorded) != 1 {
|
|
t.Fatalf("err %v live %d events %d", err, live, len(tasks.recorded))
|
|
}
|
|
e := tasks.recorded[0]
|
|
if e.Kind != events.KindIdentityStarted || e.ActorID != "archied" || e.PrincipalID != "execution-agent" || e.Data["requested_by"] != "human" {
|
|
t.Fatalf("event %+v", e)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type executionIdentityRepo struct{ identity.Repository }
|
|
|
|
func (executionIdentityRepo) Get(context.Context, identity.IdentityID) (identity.Identity, error) {
|
|
return identity.Identity{ID: "execution-agent", Kind: identity.KindBot, Lifecycle: identity.LifecycleActive}, nil
|
|
}
|
|
|
|
func TestCredentialUsesPinnedExecutionIdentity(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
definition, want string
|
|
denied bool
|
|
}{
|
|
{"id: x\nidentity: execution-agent\n", "execution-agent", false},
|
|
{"id: x\n", "", true},
|
|
} {
|
|
task := &task.Task{Identity: "requester", WorkflowDefinitionYAML: tt.definition}
|
|
got, err := executionReference(task)
|
|
if (err != nil) != tt.denied || got != tt.want || task.Identity != "requester" {
|
|
t.Fatalf("got %q, %v; requester %q", got, err, task.Identity)
|
|
}
|
|
}
|
|
}
|