Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

Commit 92ccbcb

Browse files
committed
support multiple notarize blocks, more validation in gon
1 parent 1d9cee1 commit 92ccbcb

File tree

8 files changed

+191
-70
lines changed

8 files changed

+191
-70
lines changed

cmd/gon/item.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type item struct {
1818
// Path is the path to the file to notarize.
1919
Path string
2020

21+
// BundleId is the bundle ID to use for this notarization.
22+
BundleId string
23+
2124
// Staple is true if we should perform stapling on this file. Not
2225
// all files support stapling so the default depends on the type of file.
2326
Staple bool
@@ -54,10 +57,16 @@ type processOptions struct {
5457
func (i *item) notarize(ctx context.Context, opts *processOptions) error {
5558
lock := opts.OutputLock
5659

60+
// The bundle ID defaults to the root one
61+
bundleId := i.BundleId
62+
if bundleId == "" {
63+
bundleId = opts.Config.BundleId
64+
}
65+
5766
// Start notarization
5867
_, err := notarize.Notarize(ctx, &notarize.Options{
5968
File: i.Path,
60-
BundleId: opts.Config.BundleId,
69+
BundleId: bundleId,
6170
Username: opts.Config.AppleId.Username,
6271
Password: opts.Config.AppleId.Password,
6372
Provider: opts.Config.AppleId.Provider,

cmd/gon/main.go

Lines changed: 101 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -81,83 +81,123 @@ func realMain() int {
8181

8282
// Notarize is an alternative to "Source", where you specify
8383
// a single .pkg or .zip that is ready for notarization and stapling
84-
if cfg.Notarize != nil {
85-
items = append(items, &item{Path: cfg.Notarize.Package, Staple: cfg.Notarize.Staple})
84+
if len(cfg.Notarize) > 0 {
85+
for _, c := range cfg.Notarize {
86+
items = append(items, &item{
87+
Path: c.Path,
88+
BundleId: c.BundleId,
89+
Staple: c.Staple,
90+
})
91+
}
92+
}
93+
94+
if len(cfg.Source) > 0 {
95+
if cfg.Sign == nil {
96+
color.New(color.Bold, color.FgRed).Fprintf(os.Stdout,
97+
"❗️ `sign` configuration required with `source` set\n")
98+
color.New(color.FgRed).Fprintf(os.Stdout,
99+
"When you set the `source` configuration, you must also specify the\n"+
100+
"`sign` configuration to sign the input files.\n")
101+
return 1
102+
}
103+
} else {
104+
if cfg.Zip != nil {
105+
color.New(color.Bold, color.FgRed).Fprintf(os.Stdout,
106+
"❗️ `zip` can only be set while `source` is also set\n")
107+
color.New(color.FgRed).Fprintf(os.Stdout,
108+
"Zip packaging is only supported when `source` is specified. This is\n"+
109+
"because the `zip` option packages the source files. If there are no\n"+
110+
"source files specified, then there is nothing to package.\n")
111+
return 1
112+
}
113+
114+
if cfg.Dmg != nil {
115+
color.New(color.Bold, color.FgRed).Fprintf(os.Stdout,
116+
"❗️ `dmg` can only be set while `source` is also set\n")
117+
color.New(color.FgRed).Fprintf(os.Stdout,
118+
"Dmg packaging is only supported when `source` is specified. This is\n"+
119+
"because the `dmg` option packages the source files. If there are no\n"+
120+
"source files specified, then there is nothing to package.\n")
121+
return 1
122+
}
86123
}
87124

88125
// If we have no items to sign then its probably an error
89-
if len(cfg.Source) == 0 && cfg.Notarize == nil {
126+
if len(cfg.Source) == 0 && len(cfg.Notarize) == 0 {
90127
color.New(color.Bold, color.FgRed).Fprintf(os.Stdout, "❗️ No source files specified\n")
91128
color.New(color.FgRed).Fprintf(os.Stdout,
92129
"Your configuration had an empty 'source' and empty 'notarize' values. This must be populated with\n"+
93130
"at least one file to sign, package, and notarize.\n")
94131
return 1
95132
}
96133

97-
if len(cfg.Source) > 0 && cfg.Sign != nil {
98-
// Perform codesigning
99-
color.New(color.Bold).Fprintf(os.Stdout, "==> %s Signing files...\n", iconSign)
100-
err = sign.Sign(context.Background(), &sign.Options{
101-
Files: cfg.Source,
102-
Identity: cfg.Sign.ApplicationIdentity,
103-
Logger: logger.Named("sign"),
104-
})
105-
if err != nil {
106-
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error signing files:\n\n%s\n", err))
107-
return 1
134+
// If we're in source mode, then sign & package as configured
135+
if len(cfg.Source) > 0 {
136+
if cfg.Sign != nil {
137+
// Perform codesigning
138+
color.New(color.Bold).Fprintf(os.Stdout, "==> %s Signing files...\n", iconSign)
139+
err = sign.Sign(context.Background(), &sign.Options{
140+
Files: cfg.Source,
141+
Identity: cfg.Sign.ApplicationIdentity,
142+
Logger: logger.Named("sign"),
143+
})
144+
if err != nil {
145+
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error signing files:\n\n%s\n", err))
146+
return 1
147+
}
148+
color.New(color.Bold, color.FgGreen).Fprintf(os.Stdout, " Code signing successful\n")
108149
}
109-
color.New(color.Bold, color.FgGreen).Fprintf(os.Stdout, " Code signing successful\n")
110-
}
111150

112-
// Create a zip
113-
if len(cfg.Source) > 0 && cfg.Zip != nil {
114-
color.New(color.Bold).Fprintf(os.Stdout, "==> %s Creating Zip archive...\n", iconPackage)
115-
err = zip.Zip(context.Background(), &zip.Options{
116-
Files: cfg.Source,
117-
OutputPath: cfg.Zip.OutputPath,
118-
})
119-
if err != nil {
120-
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error creating zip archive:\n\n%s\n", err))
121-
return 1
151+
// Create a zip
152+
if cfg.Zip != nil {
153+
color.New(color.Bold).Fprintf(os.Stdout, "==> %s Creating Zip archive...\n", iconPackage)
154+
err = zip.Zip(context.Background(), &zip.Options{
155+
Files: cfg.Source,
156+
OutputPath: cfg.Zip.OutputPath,
157+
})
158+
if err != nil {
159+
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error creating zip archive:\n\n%s\n", err))
160+
return 1
161+
}
162+
color.New(color.Bold, color.FgGreen).Fprintf(os.Stdout, " Zip archive created with signed files\n")
163+
164+
// Queue to notarize
165+
items = append(items, &item{Path: cfg.Zip.OutputPath})
122166
}
123-
color.New(color.Bold, color.FgGreen).Fprintf(os.Stdout, " Zip archive created with signed files\n")
124167

125-
// Queue to notarize
126-
items = append(items, &item{Path: cfg.Zip.OutputPath})
127-
}
168+
// Create a dmg
169+
if cfg.Dmg != nil && cfg.Sign != nil {
170+
// First create the dmg itself. This passes in the signed files.
171+
color.New(color.Bold).Fprintf(os.Stdout, "==> %s Creating dmg...\n", iconPackage)
172+
color.New().Fprintf(os.Stdout, " This will open Finder windows momentarily.\n")
173+
err = dmg.Dmg(context.Background(), &dmg.Options{
174+
Files: cfg.Source,
175+
OutputPath: cfg.Dmg.OutputPath,
176+
VolumeName: cfg.Dmg.VolumeName,
177+
Logger: logger.Named("dmg"),
178+
})
179+
if err != nil {
180+
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error creating dmg:\n\n%s\n", err))
181+
return 1
182+
}
183+
color.New().Fprintf(os.Stdout, " Dmg file created: %s\n", cfg.Dmg.OutputPath)
184+
185+
// Next we need to sign the actual DMG as well
186+
color.New().Fprintf(os.Stdout, " Signing dmg...\n")
187+
err = sign.Sign(context.Background(), &sign.Options{
188+
Files: []string{cfg.Dmg.OutputPath},
189+
Identity: cfg.Sign.ApplicationIdentity,
190+
Logger: logger.Named("dmg"),
191+
})
192+
if err != nil {
193+
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error signing dmg:\n\n%s\n", err))
194+
return 1
195+
}
196+
color.New(color.Bold, color.FgGreen).Fprintf(os.Stdout, " Dmg created and signed\n")
128197

129-
// Create a dmg
130-
if len(cfg.Source) > 0 && cfg.Dmg != nil && cfg.Sign != nil {
131-
// First create the dmg itself. This passes in the signed files.
132-
color.New(color.Bold).Fprintf(os.Stdout, "==> %s Creating dmg...\n", iconPackage)
133-
color.New().Fprintf(os.Stdout, " This will open Finder windows momentarily.\n")
134-
err = dmg.Dmg(context.Background(), &dmg.Options{
135-
Files: cfg.Source,
136-
OutputPath: cfg.Dmg.OutputPath,
137-
VolumeName: cfg.Dmg.VolumeName,
138-
Logger: logger.Named("dmg"),
139-
})
140-
if err != nil {
141-
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error creating dmg:\n\n%s\n", err))
142-
return 1
143-
}
144-
color.New().Fprintf(os.Stdout, " Dmg file created: %s\n", cfg.Dmg.OutputPath)
145-
146-
// Next we need to sign the actual DMG as well
147-
color.New().Fprintf(os.Stdout, " Signing dmg...\n")
148-
err = sign.Sign(context.Background(), &sign.Options{
149-
Files: []string{cfg.Dmg.OutputPath},
150-
Identity: cfg.Sign.ApplicationIdentity,
151-
Logger: logger.Named("dmg"),
152-
})
153-
if err != nil {
154-
fmt.Fprintf(os.Stdout, color.RedString("❗️ Error signing dmg:\n\n%s\n", err))
155-
return 1
198+
// Queue to notarize
199+
items = append(items, &item{Path: cfg.Dmg.OutputPath, Staple: true})
156200
}
157-
color.New(color.Bold, color.FgGreen).Fprintf(os.Stdout, " Dmg created and signed\n")
158-
159-
// Queue to notarize
160-
items = append(items, &item{Path: cfg.Dmg.OutputPath, Staple: true})
161201
}
162202

163203
// If we have no items to notarize then its probably an error in the configuration.

internal/config/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Config struct {
1212

1313
// Notarize is a single file (usually a .pkg installer or zip)
1414
// that is ready for notarization as-is
15-
Notarize *Notarize `hcl:"notarize,block"`
15+
Notarize []Notarize `hcl:"notarize,block"`
1616

1717
// Sign are the settings for code-signing the binaries.
1818
Sign *Sign `hcl:"sign,block"`
@@ -47,10 +47,18 @@ type AppleId struct {
4747
Provider string `hcl:"provider,optional"`
4848
}
4949

50-
// Options for notarizing a pre-built .pkg or .zip
50+
// NOtarize are the options for notarizing a pre-built file.
5151
type Notarize struct {
52-
Package string `hcl:"package"`
53-
Staple bool `hcl:"staple"`
52+
// Path is the path to the file to notarize. This can be any supported
53+
// filetype (dmg, pkg, app, zip).
54+
Path string `hcl:"path"`
55+
56+
// BundleId is the bundle ID to use for notarizing this package.
57+
// If this isn't specified then the root bundle_id is inherited.
58+
BundleId string `hcl:"bundle_id"`
59+
60+
// Staple, if true will staple the notarization ticket to the file.
61+
Staple bool `hcl:"staple,optional"`
5462
}
5563

5664
// Sign are the options for codesigning the binaries.

internal/config/testdata/basic.hcl.golden

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
(string) (len=11) "./terraform"
44
},
55
BundleId: (string) (len=28) "com.mitchellh.test.terraform",
6-
Sign: (config.Sign) {
6+
Notarize: ([]config.Notarize) <nil>,
7+
Sign: (*config.Sign)({
78
ApplicationIdentity: (string) (len=3) "foo"
8-
},
9+
}),
910
AppleId: (config.AppleId) {
1011
Username: (string) (len=21) "mitchellh@example.com",
1112
Password: (string) (len=5) "hello",

internal/config/testdata/notarize.hcl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ source = []
22
bundle_id = "com.example.terraform"
33

44
notarize {
5-
package = "/path/to/terraform.pkg"
6-
staple = true
5+
path = "/path/to/terraform.pkg"
6+
bundle_id = "foo.bar"
77
}
88

99
apple_id {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(*config.Config)({
2+
Source: ([]string) {
3+
},
4+
BundleId: (string) (len=21) "com.example.terraform",
5+
Notarize: ([]config.Notarize) (len=1 cap=1) {
6+
(config.Notarize) {
7+
Path: (string) (len=22) "/path/to/terraform.pkg",
8+
BundleId: (string) (len=7) "foo.bar",
9+
Staple: (bool) false
10+
}
11+
},
12+
Sign: (*config.Sign)(<nil>),
13+
AppleId: (config.AppleId) {
14+
Username: (string) (len=21) "mitchellh@example.com",
15+
Password: (string) (len=5) "hello",
16+
Provider: (string) ""
17+
},
18+
Zip: (*config.Zip)(<nil>),
19+
Dmg: (*config.Dmg)(<nil>)
20+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
source = []
2+
bundle_id = ""
3+
4+
notarize {
5+
path = "/path/to/terraform.pkg"
6+
bundle_id = "foo.bar"
7+
}
8+
9+
notarize {
10+
path = "/path/to/terraform.pkg"
11+
bundle_id = "foo.bar"
12+
staple = true
13+
}
14+
15+
apple_id {
16+
username = "mitchellh@example.com"
17+
password = "hello"
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(*config.Config)({
2+
Source: ([]string) {
3+
},
4+
BundleId: (string) "",
5+
Notarize: ([]config.Notarize) (len=2 cap=2) {
6+
(config.Notarize) {
7+
Path: (string) (len=22) "/path/to/terraform.pkg",
8+
BundleId: (string) (len=7) "foo.bar",
9+
Staple: (bool) false
10+
},
11+
(config.Notarize) {
12+
Path: (string) (len=22) "/path/to/terraform.pkg",
13+
BundleId: (string) (len=7) "foo.bar",
14+
Staple: (bool) true
15+
}
16+
},
17+
Sign: (*config.Sign)(<nil>),
18+
AppleId: (config.AppleId) {
19+
Username: (string) (len=21) "mitchellh@example.com",
20+
Password: (string) (len=5) "hello",
21+
Provider: (string) ""
22+
},
23+
Zip: (*config.Zip)(<nil>),
24+
Dmg: (*config.Dmg)(<nil>)
25+
})

0 commit comments

Comments
 (0)