diff --git a/go.mod b/go.mod index a65ba91d9..2bdcab980 100644 --- a/go.mod +++ b/go.mod @@ -84,6 +84,7 @@ require ( github.com/google/uuid v1.6.0 github.com/hashicorp/go-hclog v1.6.3 github.com/hashicorp/go-plugin v1.8.0 + github.com/hiddeco/sshsig v0.2.0 github.com/jackc/pgx/v5 v5.11.0 github.com/klauspost/compress v1.20.3 github.com/moby/moby/api v1.56.1 diff --git a/go.sum b/go.sum index dc5645d93..0d03f3626 100644 --- a/go.sum +++ b/go.sum @@ -104,6 +104,8 @@ github.com/hashicorp/go-plugin v1.8.0 h1:ie8S6RRY8RvB2usYZv+AAZ/wBvx2AU5p5QeP5j/ github.com/hashicorp/go-plugin v1.8.0/go.mod h1:BExt6KEaIYx804z8k4gRzRLEvxKVb+kn0NMcihqOqb8= github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8= github.com/hashicorp/yamux v0.1.2/go.mod h1:C+zze2n6e/7wshOZep2A70/aQU6QBRWJO/G6FT1wIns= +github.com/hiddeco/sshsig v0.2.0 h1:gMWllgKCITXdydVkDL+Zro0PU96QI55LwUwebSwNTSw= +github.com/hiddeco/sshsig v0.2.0/go.mod h1:nJc98aGgiH6Yql2doqH4CTBVHexQA40Q+hMMLHP4EqE= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= diff --git a/internal/app/archied/repository_access.go b/internal/app/archied/repository_access.go index 38eb034eb..626947860 100644 --- a/internal/app/archied/repository_access.go +++ b/internal/app/archied/repository_access.go @@ -6,6 +6,8 @@ import ( "fmt" "strings" + "github.com/go-git/go-git/v6" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/daemon" "github.com/samcharles93/archie-core/internal/domain/access" @@ -107,8 +109,18 @@ func (b *boot) openRepositoryConnection(ctx context.Context, principal access.Pr } } cfg := b.cfgHolder.Get() - botUser, botEmail := commitIdentity(cfg, connection.Type, username) - out.Trees = &worktree.Manager{WorkDir: worktree.OrgWorkDir(cfg.WorkDir, string(principal.Org)), Token: token, AuthUser: username, BaseURL: host, BotUser: botUser, BotEmail: botEmail, Log: b.log} + botUser, botEmail := commitIdentity(cfg, connection, username) + var signer git.Signer + if connection.SigningKey.Key != "" { + key, err := b.secrets.Resolve(connection.SigningKey) + if err != nil || key == "" { + return failure("signing_key_unavailable", fmt.Sprintf("The signing key for %s is unavailable. Check its secret reference.", connection.Name)) + } + if signer, err = worktree.NewSSHSigner([]byte(key)); err != nil { + return failure("signing_key_invalid", fmt.Sprintf("The signing key for %s is not an OpenSSH private key.", connection.Name)) + } + } + out.Trees = &worktree.Manager{WorkDir: worktree.OrgWorkDir(cfg.WorkDir, string(principal.Org)), Token: token, AuthUser: username, BaseURL: host, BotUser: botUser, BotEmail: botEmail, Signer: signer, Log: b.log} out.Forge = b.connectionForge(ctx, connection, principal.Org, token) if operation == config.OperationPR { if _, disabled := out.Forge.(*forge.NoopForge); disabled { @@ -118,10 +130,12 @@ func (b *boot) openRepositoryConnection(ctx context.Context, principal access.Pr return out, nil } -// commitIdentity is the author of commits made through a connection. A -// control-plane connection leaves the root bot fields empty, so its account -// signs the commits instead of nobody. -func commitIdentity(cfg config.Config, forgeType, account string) (string, string) { +// commitIdentity is the author of commits made through a connection: its +// configured commit identity, else the root bot fields, else its account. +func commitIdentity(cfg config.Config, connection config.ForgeConnection, account string) (string, string) { + if connection.CommitName != "" { + return connection.CommitName, configuration.DefaultBotEmail(connection.Type, connection.CommitName, connection.CommitEmail) + } if cfg.BotUser != "" { return cfg.BotUser, cfg.BotEmail } @@ -129,5 +143,5 @@ func commitIdentity(cfg config.Config, forgeType, account string) (string, strin if user == "" { user = "archie" } - return user, configuration.DefaultBotEmail(forgeType, user, "") + return user, configuration.DefaultBotEmail(connection.Type, user, connection.CommitEmail) } diff --git a/internal/config/config.go b/internal/config/config.go index 009eddf51..a802d6881 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -64,25 +64,32 @@ func ValidOperation(op string) bool { return slices.Contains(Operations(), op) } // instance's root configuration. type ForgeConnection struct { // ID is the stable reference a repository binding and a permission name. - ID string `toml:"id" json:"id" yaml:"id"` + ID string `json:"id"` // Name is the operator-facing label. - Name string `toml:"name" json:"name" yaml:"name"` + Name string `json:"name"` // Type selects the forge implementation: the name of an enabled forge // extension, or the compiled-in forge of that name. - Type string `toml:"type" json:"type" yaml:"type"` + Type string `json:"type"` // Host is the forge's host, e.g. "github.com". - Host string `toml:"host" json:"host" yaml:"host"` + Host string `json:"host"` // Account is the external account the credential authenticates as. It is // recorded, and checked against the account a verification actually // reached; it is never used to infer authority. - Account string `toml:"account" json:"account" yaml:"account"` + Account string `json:"account"` // Token references the credential. The connection records the reference, // never a token value. - Token SecretRef `toml:"token" json:"token" yaml:"token"` + Token SecretRef `json:"token"` + // CommitName and CommitEmail author commits made through the connection. + // Empty falls back to the account and the forge's default email. + CommitName string `json:"commit_name,omitempty"` + CommitEmail string `json:"commit_email,omitempty"` + // SigningKey references an OpenSSH private key that signs those commits. + // Unset leaves them unsigned. + SigningKey SecretRef `json:"signing_key,omitzero"` // Permits names, per execution identity ID, the operations that identity // may perform through this connection. An identity with no entry may // perform none: the connection itself is not a grant. - Permits map[string][]string `toml:"permits" json:"permits,omitempty" yaml:"permits"` + Permits map[string][]string `json:"permits,omitempty"` } // Permitted reports whether the identity may perform op through this diff --git a/internal/infrastructure/controlplanestore/forge_connections.go b/internal/infrastructure/controlplanestore/forge_connections.go index bd6fe8131..4626d678c 100644 --- a/internal/infrastructure/controlplanestore/forge_connections.go +++ b/internal/infrastructure/controlplanestore/forge_connections.go @@ -118,6 +118,9 @@ func validateForgeConnection(c config.ForgeConnection) error { if (c.Token.Engine == "") != (c.Token.Key == "") { return fmt.Errorf("connection %q: the credential reference needs both an engine and a key", c.ID) } + if (c.SigningKey.Engine == "") != (c.SigningKey.Key == "") { + return fmt.Errorf("connection %q: the signing key reference needs both an engine and a key", c.ID) + } for _, id := range slices.Sorted(maps.Keys(c.Permits)) { operations := c.Permits[id] if strings.TrimSpace(id) == "" { diff --git a/internal/worktree/sign.go b/internal/worktree/sign.go new file mode 100644 index 000000000..0c7c5ca6e --- /dev/null +++ b/internal/worktree/sign.go @@ -0,0 +1,33 @@ +package worktree + +import ( + "bytes" + "context" + "fmt" + "io" + + "github.com/go-git/go-git/v6" + "github.com/hiddeco/sshsig" + "golang.org/x/crypto/ssh" +) + +// sshSigner signs commits the way `git -c gpg.format=ssh` does, so forges +// verify them against the account's SSH signing key. +type sshSigner struct{ key ssh.Signer } + +// NewSSHSigner parses an OpenSSH private key for commit signing. +func NewSSHSigner(privateKey []byte) (git.Signer, error) { + key, err := ssh.ParsePrivateKey(privateKey) + if err != nil { + return nil, fmt.Errorf("parse signing key: %w", err) + } + return sshSigner{key: key}, nil +} + +func (s sshSigner) Sign(_ context.Context, message io.Reader) ([]byte, error) { + sig, err := sshsig.Sign(message, s.key, sshsig.HashSHA512, "git") + if err != nil { + return nil, fmt.Errorf("ssh sign: %w", err) + } + return bytes.TrimSpace(sshsig.Armor(sig)), nil +} diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index a1d0b54b4..3d064b99a 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -45,6 +45,8 @@ type Manager struct { AuthUser string BotUser string BotEmail string + // Signer signs bot commits; nil leaves them unsigned. + Signer git.Signer // BaseURL overrides the forge host. Set from config [forge].host. // Empty falls back to https://github.com. BaseURL string @@ -574,7 +576,7 @@ func (m *Manager) setIdentity(r *git.Repository) error { } cfg.User.Name = m.BotUser cfg.User.Email = m.BotEmail - // Disable commit signing; go-git cannot sign. + // go-git signs through Signer; a user-level gpgsign must not apply. cfg.Raw.Section("commit").SetOption("gpgsign", "false") if err := r.SetConfig(cfg); err != nil { return fmt.Errorf("write repo config: %w", err) @@ -627,7 +629,7 @@ func (m *Manager) CommitAll(ctx context.Context, dir, message string) (bool, err return false, err } sig := m.signature() - if _, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig}); err != nil { + if _, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig, Signer: m.Signer}); err != nil { return false, fmt.Errorf("commit: %w", err) } return true, nil diff --git a/ui/src/settings/ConnectionCard.vue b/ui/src/settings/ConnectionCard.vue index 47e4770e6..d99f47c4d 100644 --- a/ui/src/settings/ConnectionCard.vue +++ b/ui/src/settings/ConnectionCard.vue @@ -98,6 +98,29 @@ const unavailable = computed(() => !!connection.value.type && !extensions.forges
+ +
+ + +
+
+ +
+
; }