118 lines
5.4 KiB
Go
118 lines
5.4 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/daemon"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/forge"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore"
|
|
"github.com/samcharles93/archie-core/internal/worktree"
|
|
)
|
|
|
|
// repositoryAccess re-reads the org's binding and connection at the operation
|
|
// boundary. Neither a removed permit nor a rotated credential survives here.
|
|
func (b *boot) repositoryAccess(ctx context.Context, task *workflow.Task, operation string) (daemon.RepositoryAccess, error) {
|
|
var out daemon.RepositoryAccess
|
|
failure := func(code, message string) (daemon.RepositoryAccess, error) {
|
|
return daemon.RepositoryAccess{}, &forge.AccessError{Code: code, Connection: out.Repository.Connection, Operation: operation, Message: message}
|
|
}
|
|
if !config.ValidOperation(operation) {
|
|
return failure("operation_invalid", "Unsupported repository operation.")
|
|
}
|
|
principal, ok := access.PrincipalFromContext(ctx)
|
|
if !ok || principal.Org == "" || principal.IdentityID == "" || string(principal.Org) != task.OrgOrDefault() {
|
|
return failure("identity_unavailable", "The agent could not be identified.")
|
|
}
|
|
repository, err := b.repositoryBinding(ctx, task.Owner, task.Repo)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.Repository = repository
|
|
if out.Repository.Connection == "" {
|
|
return failure("connection_required", "Choose a connection for this repository.")
|
|
}
|
|
connections, err := b.repositoryConnections(ctx)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
for _, connection := range connections {
|
|
if connection.ID != out.Repository.Connection {
|
|
continue
|
|
}
|
|
return b.openRepositoryConnection(ctx, principal, out.Repository, connection, operation)
|
|
}
|
|
|
|
return failure("connection_missing", "The selected connection no longer exists. Choose another connection.")
|
|
}
|
|
|
|
func (b *boot) repositoryBinding(ctx context.Context, owner, name string) (config.Repo, error) {
|
|
var repos []config.Repo
|
|
_, found, err := b.controlPlane.Query(ctx, controlplanestore.RepositoryPoliciesKind, func(value []byte) error { return json.Unmarshal(value, &repos) })
|
|
if err != nil || !found {
|
|
return config.Repo{}, &forge.AccessError{Code: "configuration_unavailable", Message: "Repository settings could not be loaded. Try again."}
|
|
}
|
|
for _, repo := range repos {
|
|
if strings.EqualFold(repo.Owner, owner) && strings.EqualFold(repo.Name, name) {
|
|
return repo, nil
|
|
}
|
|
}
|
|
return config.Repo{}, &forge.AccessError{Code: "repository_not_configured", Message: "Add this repository to the organisation before starting a task."}
|
|
}
|
|
|
|
func (b *boot) repositoryConnections(ctx context.Context) ([]config.ForgeConnection, error) {
|
|
var connections []config.ForgeConnection
|
|
_, found, err := b.controlPlane.Query(ctx, controlplanestore.ForgeConnectionsKind, func(value []byte) error { return json.Unmarshal(value, &connections) })
|
|
if err != nil || !found {
|
|
return nil, &forge.AccessError{Code: "configuration_unavailable", Message: "Connection settings could not be loaded. Try again."}
|
|
}
|
|
return connections, nil
|
|
}
|
|
|
|
func (b *boot) connectionForge(ctx context.Context, connection config.ForgeConnection, owner org.OrgID, token string) forge.Forge {
|
|
if client, handled := b.forgeExt.open(access.WithoutPrincipal(ctx), connection.Type, string(owner)+"/"+connection.ID, connection.Host, token); handled {
|
|
return client
|
|
}
|
|
return forge.NewNoop(b.log)
|
|
}
|
|
|
|
func (b *boot) openRepositoryConnection(ctx context.Context, principal access.Principal, repository config.Repo, connection config.ForgeConnection, operation string) (daemon.RepositoryAccess, error) {
|
|
out := daemon.RepositoryAccess{Repository: repository, Connection: connection}
|
|
failure := func(code, message string) (daemon.RepositoryAccess, error) {
|
|
return daemon.RepositoryAccess{}, &forge.AccessError{Code: code, Connection: connection.ID, Operation: operation, Message: message}
|
|
}
|
|
if !connection.Permitted(string(principal.IdentityID), operation) {
|
|
return failure("operation_denied", fmt.Sprintf("Allow this agent to %s through %s before continuing.", operation, connection.Name))
|
|
}
|
|
token, err := b.secrets.Resolve(connection.Token)
|
|
if err != nil || token == "" {
|
|
return failure("credential_unavailable", fmt.Sprintf("The credential for %s is unavailable. Check its secret reference.", connection.Name))
|
|
}
|
|
host := strings.TrimRight(connection.Host, "/")
|
|
if !strings.Contains(host, "://") {
|
|
host = "https://" + host
|
|
}
|
|
username := connection.Account
|
|
if username == "" && connection.Type == "gitea" {
|
|
username, err = connectionAccount(ctx, connection.Type, host, token)
|
|
if err != nil {
|
|
return failure("account_unverified", "The forge account could not be verified. Check the credential and its permissions.")
|
|
}
|
|
}
|
|
cfg := b.cfgHolder.Get()
|
|
out.Trees = &worktree.Manager{WorkDir: worktree.OrgWorkDir(cfg.WorkDir, string(principal.Org)), Token: token, AuthUser: username, BaseURL: host, BotUser: cfg.BotUser, BotEmail: cfg.BotEmail, Log: b.log}
|
|
out.Forge = b.connectionForge(ctx, connection, principal.Org, token)
|
|
if operation == config.OperationPR {
|
|
if _, disabled := out.Forge.(*forge.NoopForge); disabled {
|
|
return failure("forge_unavailable", "Enable the forge extension for this connection.")
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|