Files
archie/internal/app/controlplane/forge_connections_test.go
T
sam cb48a27d16 feat(controlplane): organisation forge connections
A connection is an organisation's own resource: the forge host, the
credential reference and the external account it belongs to. It records a
reference, never a token value, and names per execution identity the
operations that identity may perform through it.

The file's [forge] sections seed it: the root's becomes the root connection
and each identity's own forge becomes a connection only that identity may
use, so the migration keeps exactly the grants the file expressed.
2026-10-17 03:05:01 +11:00

130 lines
4.7 KiB
Go

package controlplane
import (
"encoding/json"
"errors"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
func TestForgeConnectionValidation(t *testing.T) {
tests := []struct {
name string
doc string
refused bool
}{
{
name: "a host, a credential reference and a permitted operation",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com","token":{"engine":"env","key":"GH_TOKEN"},"permits":{"bot-1":["clone","push"]}}]`,
},
{
name: "a connection with no external account is still configurable",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com"}]`,
},
{
name: "no host",
doc: `[{"id":"gh","name":"GitHub","type":"github"}]`,
refused: true,
},
{
name: "no forge type",
doc: `[{"id":"gh","name":"GitHub","host":"github.com"}]`,
refused: true,
},
{
name: "an ID that would spell another record key",
doc: `[{"id":"org-sys/gh","name":"GitHub","type":"github","host":"github.com"}]`,
refused: true,
},
{
name: "two connections claiming one ID",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com"},{"id":"gh","name":"Other","type":"gitea","host":"gitea.example.com"}]`,
refused: true,
},
{
name: "half a credential reference",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com","token":{"key":"GH_TOKEN"}}]`,
refused: true,
},
{
name: "an operation the runtime cannot perform",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com","permits":{"bot-1":["clone","admin"]}}]`,
refused: true,
},
{
name: "the same operation twice",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com","permits":{"bot-1":["push","push"]}}]`,
refused: true,
},
{
name: "an identity permitted nothing",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com","permits":{"bot-1":[]}}]`,
refused: true,
},
{
name: "a connection with no name",
doc: `[{"id":"gh","type":"github","host":"github.com"}]`,
refused: true,
},
{
name: "a document that names a field the connection does not have",
doc: `[{"id":"gh","name":"GitHub","type":"github","host":"github.com","token_value":"ghp_literal"}]`,
refused: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := forgeConnectionsDefinition().Validate([]byte(tt.doc))
if refused := errors.Is(err, ErrValidation) || err != nil; refused != tt.refused {
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
}
})
}
}
// TestConnectionMigrationKeepsTheFilesGrants is the migration's security
// claim: what the file granted, the connections grant, and nothing more. The
// root connection takes no permit entry, so no execution identity reaches it;
// each identity's own forge becomes a connection only that identity may use.
func TestConnectionMigrationKeepsTheFilesGrants(t *testing.T) {
cfg := config.Config{
Forge: config.Forge{Type: "github", Host: "github.com", Token: config.SecretRef{Engine: "env", Key: "ROOT_TOKEN"}},
Identities: []config.IdentityConfig{
{Name: "reviewer", Forge: config.Forge{Type: "github", Host: "github.com", Token: config.SecretRef{Engine: "env", Key: "REVIEWER_TOKEN"}}},
{Name: "silent"},
},
}
seeded, err := forgeConnectionsDefinition().seededValue(cfg)
if err != nil {
t.Fatalf("seed: %v", err)
}
var connections []config.ForgeConnection
if err := json.Unmarshal(seeded, &connections); err != nil {
t.Fatalf("decode seed: %v", err)
}
if len(connections) != 2 {
t.Fatalf("seeded %d connections, want the root's and the one identity that declared a forge: %+v", len(connections), connections)
}
root, reviewer := connections[0], connections[1]
if root.ID != rootConnectionID || len(root.Permits) != 0 {
t.Fatalf("root connection = %+v, want the root's forge and no permitted execution identity", root)
}
if root.Token.Key != "ROOT_TOKEN" {
t.Fatalf("root connection credential = %+v, want the file's reference", root.Token)
}
if reviewer.ID != "identity-reviewer" {
t.Fatalf("second connection = %q, want the declaring identity's", reviewer.ID)
}
if permitted := reviewer.Permits[string(identity.StableID("reviewer"))]; !slices.Equal(permitted, config.Operations()) {
t.Fatalf("reviewer permits = %v, want every operation it already had (%v)", permitted, config.Operations())
}
for id := range reviewer.Permits {
if id != string(identity.StableID("reviewer")) {
t.Fatalf("reviewer connection permits %q as well; an identity with no forge of its own must not gain one", id)
}
}
}