package controlplane import ( "encoding/json" "errors" "testing" "github.com/samcharles93/archie-core/internal/config" ) 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: the root connection takes no permit entry, so no execution identity // reaches it. func TestConnectionMigrationKeepsTheFilesGrants(t *testing.T) { cfg := config.Config{ Forge: config.Forge{Type: "github", Host: "github.com", Token: config.SecretRef{Engine: "env", Key: "ROOT_TOKEN"}}, } 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) != 1 { t.Fatalf("seeded %d connections, want the root's alone: %+v", len(connections), connections) } root := connections[0] 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.Name != "github.com" { t.Fatalf("root connection name = %q, want the host an operator recognises, not an internal label", root.Name) } if root.Token.Key != "ROOT_TOKEN" { t.Fatalf("root connection credential = %+v, want the file's reference", root.Token) } }