|
| 1 | +package checkpoint |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/entireio/cli/cmd/entire/cli/logging" |
| 11 | + "github.com/go-git/go-git/v6/config" |
| 12 | + format "github.com/go-git/go-git/v6/plumbing/format/config" |
| 13 | + "github.com/go-git/go-git/v6/x/plugin" |
| 14 | + "github.com/go-git/x/plugin/objectsigner/auto" |
| 15 | + programsigner "github.com/go-git/x/plugin/objectsigner/program" |
| 16 | + sshagent "golang.org/x/crypto/ssh/agent" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + objectSignerLoader = loadObjectSigner |
| 21 | + scopeName = map[config.Scope]string{ |
| 22 | + config.GlobalScope: "global", |
| 23 | + config.SystemScope: "system", |
| 24 | + } |
| 25 | +) |
| 26 | + |
| 27 | +func loadObjectSigner(ctx context.Context) (plugin.Signer, bool) { |
| 28 | + cfgSource, err := plugin.Get(plugin.ConfigLoader()) |
| 29 | + if err != nil { |
| 30 | + // No config loader registered; signing not possible. |
| 31 | + return nil, false |
| 32 | + } |
| 33 | + |
| 34 | + sysCfg := loadScopedConfig(cfgSource, config.SystemScope) |
| 35 | + globalCfg := loadScopedConfig(cfgSource, config.GlobalScope) |
| 36 | + |
| 37 | + return loadObjectSignerFromConfigs(ctx, sysCfg, globalCfg) |
| 38 | +} |
| 39 | + |
| 40 | +func loadObjectSignerFromConfigs(ctx context.Context, sysCfg, globalCfg *config.Config) (plugin.Signer, bool) { |
| 41 | + // Merge system then global so that global settings take precedence. |
| 42 | + merged := config.Merge(sysCfg, globalCfg) |
| 43 | + |
| 44 | + if !merged.Commit.GpgSign.IsTrue() { |
| 45 | + return nil, false |
| 46 | + } |
| 47 | + |
| 48 | + if signer, ok := loadCustomProgramSigner(ctx, sysCfg, globalCfg, merged); ok { |
| 49 | + return signer, true |
| 50 | + } |
| 51 | + |
| 52 | + signer, err := auto.FromConfig(auto.Config{ |
| 53 | + SigningKey: merged.User.SigningKey, |
| 54 | + Format: auto.Format(merged.GPG.Format), |
| 55 | + SSHAgent: connectSSHAgent(ctx), |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + logging.Debug(ctx, "failed to create object signer", slog.String("error", err.Error())) |
| 59 | + return nil, false |
| 60 | + } |
| 61 | + |
| 62 | + return signer, true |
| 63 | +} |
| 64 | + |
| 65 | +func loadCustomProgramSigner(ctx context.Context, sysCfg, globalCfg *config.Config, merged config.Config) (plugin.Signer, bool) { |
| 66 | + signFormat := normalizeProgramFormat(merged.GPG.Format) |
| 67 | + |
| 68 | + // TODO: Replace with merged.GPG.Program once that is surfaced by go-git. |
| 69 | + programName, ok := customSignProgram(signFormat, rawConfig(sysCfg), rawConfig(globalCfg)) |
| 70 | + if !ok { |
| 71 | + return nil, false |
| 72 | + } |
| 73 | + |
| 74 | + signer, err := programsigner.New(signFormat, programName, merged.User.SigningKey) |
| 75 | + if err != nil { |
| 76 | + logging.Debug(ctx, "failed to create object signer from custom program", slog.String("error", err.Error())) |
| 77 | + return nil, false |
| 78 | + } |
| 79 | + |
| 80 | + logging.Debug( |
| 81 | + ctx, |
| 82 | + "using custom object signer program", |
| 83 | + slog.String("format", string(signFormat)), |
| 84 | + slog.String("program", programName), |
| 85 | + ) |
| 86 | + |
| 87 | + return signer, true |
| 88 | +} |
| 89 | + |
| 90 | +func rawConfig(cfg *config.Config) *format.Config { |
| 91 | + if cfg == nil { |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + return cfg.Raw |
| 96 | +} |
| 97 | + |
| 98 | +func normalizeProgramFormat(gitFormat string) programsigner.Format { |
| 99 | + switch auto.Format(gitFormat) { |
| 100 | + case "", auto.FormatOpenPGP: |
| 101 | + return programsigner.FormatOpenPGP |
| 102 | + case auto.FormatSSH: |
| 103 | + return programsigner.FormatSSH |
| 104 | + case auto.Format("x509"): |
| 105 | + return programsigner.FormatX509 |
| 106 | + default: |
| 107 | + return programsigner.Format(gitFormat) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// customSignProgram returns the effective custom signer program for signFormat. |
| 112 | +// Git supports both legacy OpenPGP gpg.program and format-specific |
| 113 | +// gpg.<format>.program settings; format-specific values override gpg.program |
| 114 | +// within the same scope, and later scopes override earlier scopes. |
| 115 | +func customSignProgram(signFormat programsigner.Format, raws ...*format.Config) (string, bool) { |
| 116 | + var programName string |
| 117 | + for _, raw := range raws { |
| 118 | + if raw == nil { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + if scopedProgram := signProgramFromRaw(signFormat, raw); scopedProgram != "" { |
| 123 | + programName = scopedProgram |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if programName == "" || programName == defaultSignProgram(signFormat) { |
| 128 | + return "", false |
| 129 | + } |
| 130 | + |
| 131 | + return programName, true |
| 132 | +} |
| 133 | + |
| 134 | +func signProgramFromRaw(signFormat programsigner.Format, raw *format.Config) string { |
| 135 | + if raw == nil { |
| 136 | + return "" |
| 137 | + } |
| 138 | + |
| 139 | + gpgSection := raw.Section("gpg") |
| 140 | + var programName string |
| 141 | + if signFormat == programsigner.FormatOpenPGP { |
| 142 | + programName = gpgSection.Option("program") |
| 143 | + } |
| 144 | + if formatProgram := gpgSection.Subsection(string(signFormat)).Option("program"); formatProgram != "" { |
| 145 | + programName = formatProgram |
| 146 | + } |
| 147 | + |
| 148 | + return programName |
| 149 | +} |
| 150 | + |
| 151 | +func defaultSignProgram(signFormat programsigner.Format) string { |
| 152 | + switch signFormat { |
| 153 | + case programsigner.FormatOpenPGP: |
| 154 | + return "gpg" |
| 155 | + case programsigner.FormatSSH: |
| 156 | + return "ssh-keygen" |
| 157 | + case programsigner.FormatX509: |
| 158 | + return "gpgsm" |
| 159 | + default: |
| 160 | + return "" |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// connectSSHAgent connects to the SSH agent via SSH_AUTH_SOCK. |
| 165 | +// Returns nil if the agent is unavailable. |
| 166 | +func connectSSHAgent(ctx context.Context) sshagent.Agent { |
| 167 | + sock := os.Getenv("SSH_AUTH_SOCK") |
| 168 | + if sock == "" { |
| 169 | + return nil |
| 170 | + } |
| 171 | + |
| 172 | + var d net.Dialer |
| 173 | + conn, err := d.DialContext(ctx, "unix", sock) |
| 174 | + if err != nil { |
| 175 | + return nil |
| 176 | + } |
| 177 | + |
| 178 | + return sshagent.NewClient(conn) |
| 179 | +} |
| 180 | + |
| 181 | +func loadScopedConfig(source plugin.ConfigSource, scope config.Scope) *config.Config { |
| 182 | + name := scopeName[scope] |
| 183 | + |
| 184 | + storer, err := source.Load(scope) |
| 185 | + if err != nil { |
| 186 | + fmt.Fprintf(os.Stderr, "warning: failed to load %s git config: %v\n", name, err) |
| 187 | + return config.NewConfig() |
| 188 | + } |
| 189 | + |
| 190 | + cfg, err := storer.Config() |
| 191 | + if err != nil { |
| 192 | + fmt.Fprintf(os.Stderr, "warning: failed to parse %s git config: %v\n", name, err) |
| 193 | + return config.NewConfig() |
| 194 | + } |
| 195 | + |
| 196 | + return cfg |
| 197 | +} |
0 commit comments