-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
sign.go
97 lines (85 loc) · 2.09 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sign
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/pipeline"
)
// Pipe for artifact signing.
type Pipe struct{}
func (Pipe) String() string {
return "signing artifacts"
}
// Default sets the Pipes defaults.
func (Pipe) Default(ctx *context.Context) error {
cfg := &ctx.Config.Sign
if cfg.Cmd == "" {
cfg.Cmd = "gpg"
}
if cfg.Signature == "" {
cfg.Signature = "${artifact}.sig"
}
if len(cfg.Args) == 0 {
cfg.Args = []string{"--output", "$signature", "--detach-sig", "$artifact"}
}
if cfg.Artifacts == "" {
cfg.Artifacts = "none"
}
return nil
}
// Run executes the Pipe.
func (Pipe) Run(ctx *context.Context) error {
switch ctx.Config.Sign.Artifacts {
case "checksum":
return sign(ctx, ctx.Checksums)
case "all":
return sign(ctx, ctx.Artifacts)
case "none":
return pipeline.Skip("artifact signing disabled")
default:
return fmt.Errorf("invalid list of artifacts to sign: %s", ctx.Config.Sign.Artifacts)
}
}
func sign(ctx *context.Context, artifacts []string) error {
var sigs []string
for _, a := range artifacts {
sig, err := signone(ctx, a)
if err != nil {
return err
}
sigs = append(sigs, sig)
}
for _, sig := range sigs {
ctx.AddArtifact(sig)
}
return nil
}
func signone(ctx *context.Context, artifact string) (string, error) {
cfg := ctx.Config.Sign
artifact = filepath.Join(ctx.Config.Dist, artifact)
env := map[string]string{
"artifact": artifact,
}
env["signature"] = expand(cfg.Signature, env)
var args []string
for _, a := range cfg.Args {
args = append(args, expand(a, env))
}
// The GoASTScanner flags this as a security risk.
// However, this works as intended. The nosec annotation
// tells the scanner to ignore this.
// #nosec
cmd := exec.Command(cfg.Cmd, args...)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("sign: %s failed with %q", cfg.Cmd, string(output))
}
return env["signature"], nil
}
func expand(s string, env map[string]string) string {
return os.Expand(s, func(key string) string {
return env[key]
})
}