-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
373 lines (309 loc) · 11 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"
"unicode/utf8"
"github.com/bitrise-io/go-steputils/input"
"github.com/bitrise-io/go-steputils/v2/stepconf"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/retry"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/env"
"github.com/bitrise-io/go-utils/v2/log/colorstring"
"github.com/bitrise-io/go-xcode/certificateutil"
"github.com/bitrise-io/go-xcode/v2/autocodesign/certdownloader"
"github.com/bitrise-io/go-xcode/v2/autocodesign/codesignasset"
"github.com/bitrise-io/go-xcode/v2/autocodesign/keychain"
"github.com/bitrise-io/go-xcode/v2/autocodesign/profiledownloader"
"github.com/hashicorp/go-retryablehttp"
)
// Config ...
type Config struct {
CertificateURL string
CertificatePassphrase string
ProvisioningProfileURL string
InstallDefaults string
DefaultCertificateURL string
DefaultCertificatePassphrase string
DefaultProvisioningProfileURL string
KeychainPath string
KeychainPassword string
Verbose bool
}
func createConfigFromEnvs() Config {
return Config{
CertificateURL: os.Getenv("certificate_url"),
CertificatePassphrase: os.Getenv("certificate_passphrase"),
ProvisioningProfileURL: os.Getenv("provisioning_profile_url"),
InstallDefaults: os.Getenv("install_defaults"),
DefaultCertificateURL: os.Getenv("default_certificate_url"),
DefaultCertificatePassphrase: os.Getenv("default_certificate_passphrase"),
DefaultProvisioningProfileURL: os.Getenv("default_provisioning_profile_url"),
KeychainPath: os.Getenv("keychain_path"),
KeychainPassword: os.Getenv("keychain_password"),
Verbose: os.Getenv("verbose") == "true",
}
}
func secureInput(str string) string {
if str == "" {
return ""
}
secureStr := func(s string, show int) string {
runeCount := utf8.RuneCountInString(s)
if runeCount < 6 || show == 0 {
return strings.Repeat("*", 3)
}
if show*4 > runeCount {
show = 1
}
sec := fmt.Sprintf("%s%s%s", s[0:show], strings.Repeat("*", 3), s[len(s)-show:])
return sec
}
prefix := ""
cont := str
sec := secureStr(cont, 0)
if strings.HasPrefix(str, "file://") {
prefix = "file://"
cont = strings.TrimPrefix(str, prefix)
sec = secureStr(cont, 3)
} else if strings.HasPrefix(str, "http://www.") {
prefix = "http://www."
cont = strings.TrimPrefix(str, prefix)
sec = secureStr(cont, 3)
} else if strings.HasPrefix(str, "https://www.") {
prefix = "https://www."
cont = strings.TrimPrefix(str, prefix)
sec = secureStr(cont, 3)
} else if strings.HasPrefix(str, "http://") {
prefix = "http://"
cont = strings.TrimPrefix(str, prefix)
sec = secureStr(cont, 3)
} else if strings.HasPrefix(str, "https://") {
prefix = "https://"
cont = strings.TrimPrefix(str, prefix)
sec = secureStr(cont, 3)
}
return prefix + sec
}
func (c Config) print() {
fmt.Println()
log.Infof("Inputs:")
log.Printf(" - CertificateURL: %s", secureInput(c.CertificateURL))
log.Printf(" - CertificatePassphrase: %s", secureInput(c.CertificatePassphrase))
log.Printf(" - ProvisioningProfileURL: %s", secureInput(c.ProvisioningProfileURL))
log.Printf(" - InstallDefaults: %s", c.InstallDefaults)
log.Printf(" - DefaultCertificateURL: %s", secureInput(c.DefaultCertificateURL))
log.Printf(" - DefaultCertificatePassphrase: %s", secureInput(c.DefaultCertificatePassphrase))
log.Printf(" - DefaultProvisioningProfileURL: %s", secureInput(c.DefaultProvisioningProfileURL))
log.Printf(" - KeychainPath: %s", c.KeychainPath)
log.Printf(" - KeychainPassword: %s", secureInput(c.KeychainPassword))
}
func (c Config) validate() error {
if err := input.ValidateWithOptions(c.InstallDefaults, "yes", "no"); err != nil {
return fmt.Errorf("issue with input InstallDefaults: %s", err)
}
if err := input.ValidateIfNotEmpty(c.KeychainPath); err != nil {
return fmt.Errorf("issue with input KeychainPath: %s", err)
}
if err := input.ValidateIfNotEmpty(c.KeychainPassword); err != nil {
return fmt.Errorf("issue with input KeychainPassword: %s", err)
}
return nil
}
func strip(str string) string {
str = strings.TrimSpace(str)
return strings.Trim(str, "\"")
}
func splitAndStrip(str, sep string) []string {
items := []string{}
split := strings.Split(str, sep)
for _, item := range split {
item = strings.TrimSpace(item)
item = strings.Trim(item, "\"")
if item != "" {
items = append(items, item)
}
}
return items
}
func splitAndTrimSpace(str, sep string) []string {
items := []string{}
split := strings.Split(str, sep)
for _, item := range split {
item = strings.TrimSpace(item)
if item != "" {
items = append(items, item)
}
}
return items
}
func appendWithoutDuplicatesAndKeepOrder(items []string, item string) []string {
result := []string{}
resultMap := map[string]bool{}
list := append(items, item)
for _, i := range list {
exist := resultMap[i]
if !exist {
result = append(result, i)
resultMap[i] = true
}
}
return result
}
func printCertificateInfo(info certificateutil.CertificateInfoModel) {
log.Printf(colorstring.Magenta(info.CommonName))
log.Printf("Serial: %s", info.Serial)
log.Printf("Team: \t%s (%s)", info.TeamName, info.TeamID)
log.Printf("Expiry: %s", info.EndDate)
if err := info.CheckValidity(); err != nil {
log.Errorf("[X] %s", err)
}
}
func failF(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func failE(err error) {
log.Errorf(err.Error())
os.Exit(1)
}
func main() {
configs := createConfigFromEnvs()
configs.print()
if err := configs.validate(); err != nil {
failF("Issue with inputs: %s", err)
}
log.SetEnableDebugLog(configs.Verbose)
fmt.Println()
// Collect Certificates
var certificateURLPassphraseMap []certdownloader.CertificateAndPassphrase
if configs.CertificateURL != "" {
certificateURLs := splitAndTrimSpace(configs.CertificateURL, "|")
// Do not splitAndTrimSpace passphrases, since a passphrase might be empty!
certificatePassphrases := strings.Split(configs.CertificatePassphrase, "|")
if len(certificateURLs) != len(certificatePassphrases) {
failF(
"Certificate URL count: (%d), is not equal to Certificate passphrase count: (%d).\n"+
"This could be because one of your passphrases contains a pipe character (\"|\") "+
"which is not supported, as it is used as the delimiter in the step input.",
len(certificateURLs),
len(certificatePassphrases),
)
}
for i := 0; i < len(certificateURLs); i++ {
certificateURL := certificateURLs[i]
certificatePassphrase := certificatePassphrases[i]
certificateURLPassphraseMap = append(certificateURLPassphraseMap, certdownloader.CertificateAndPassphrase{
URL: certificateURL,
Passphrase: certificatePassphrase,
})
}
}
if configs.DefaultCertificateURL != "" && configs.InstallDefaults == "yes" {
log.Printf("Default Certificate given")
certificateURLPassphraseMap = append(certificateURLPassphraseMap, certdownloader.CertificateAndPassphrase{
URL: configs.DefaultCertificateURL,
Passphrase: configs.DefaultCertificatePassphrase,
})
}
certificateCount := len(certificateURLPassphraseMap)
log.Printf("Provided Certificate count: %d", certificateCount)
if certificateCount == 0 {
log.Warnf("No Certificate provided")
}
// Collect Provisioning Profiles
provisioningProfileURLs := splitAndTrimSpace(configs.ProvisioningProfileURL, "|")
if configs.DefaultProvisioningProfileURL != "" && configs.InstallDefaults == "yes" {
log.Printf("Default Provisioning Profile given")
provisioningProfileURLs = append(provisioningProfileURLs, configs.DefaultProvisioningProfileURL)
}
profileCount := len(provisioningProfileURLs)
log.Printf("Provided Provisioning Profile count: %d", profileCount)
if profileCount == 0 {
log.Warnf("No Provisioning Profile provided")
}
keychainWriter, err := keychain.New(configs.KeychainPath, stepconf.Secret(configs.KeychainPassword), command.NewFactory(env.NewRepository()))
if err != nil {
failE(fmt.Errorf("Failed to open Keychain: %w", err))
}
retryHTTPClient := retry.NewHTTPClient()
retryHTTPClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
log.Debugf("HTTP retry: %s", err)
if resp != nil && resp.StatusCode == http.StatusNotFound {
log.Debugf("Received HTTP 404, retrying request: %s %s", resp.Request.Method, resp.Request.URL)
return true, nil
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}
httpClient := retryHTTPClient.StandardClient()
certDownloader := certdownloader.NewDownloader(certificateURLPassphraseMap, httpClient)
profileDownloader := profiledownloader.New(provisioningProfileURLs, httpClient)
assetInstaller := codesignasset.NewWriter(*keychainWriter)
fmt.Println()
log.Infof("Downloading Certificates...")
certDownloadStart := time.Now()
certificates, err := certDownloader.GetCertificates()
if err != nil {
failE(fmt.Errorf("Download failed: %w", err))
}
log.Printf("Download took %s", time.Since(certDownloadStart).Round(time.Millisecond))
if len(certificates) == 1 {
log.Donef("1 certificate downloaded")
} else {
log.Donef("%d certificates downloaded", len(certificates))
}
fmt.Println()
log.Infof("Installing downloaded certificates...")
certInstallStart := time.Now()
for i, cert := range certificates {
log.Printf("%d/%d:", i+1, len(certificates))
printCertificateInfo(cert)
if err := assetInstaller.InstallCertificate(cert); err != nil {
failE(fmt.Errorf("Failed to install certificate: %w", err))
}
fmt.Println()
}
log.Printf("Installation took %s", time.Since(certInstallStart).Round(time.Millisecond))
log.Donef("Certificates installed.")
fmt.Println()
log.Infof("Downloading Provisioning Profiles...")
profileDownloadStart := time.Now()
profiles, err := profileDownloader.GetProfiles()
if err != nil {
failE(fmt.Errorf("Download failed: %w", err))
}
log.Printf("Download took %s", time.Since(profileDownloadStart).Round(time.Millisecond))
if len(profiles) == 1 {
log.Donef("1 Provisioning Profile downloaded.")
} else {
log.Donef("%d Provisioning Profiles downloaded.", len(profiles))
}
fmt.Println()
log.Infof("Installing Provisioning Profiles...")
profileInstallStart := time.Now()
for i, profile := range profiles {
log.Printf("%d/%d:", i+1, len(profiles))
if configs.Verbose {
log.Debugf("%s", profile.Info.String(certificates...))
} else {
log.Printf("%s", colorstring.Magenta(profile.Info.Name))
log.Printf("Type: \t\t%s", profile.Info.Type)
log.Printf("Expiry: \t%s", profile.Info.ExpirationDate)
log.Printf("Bundle ID: \t%s", profile.Info.BundleID)
log.Printf("Included certificates:")
for _, cert := range profile.Info.DeveloperCertificates {
log.Printf("- %s", cert.CommonName)
}
}
fmt.Println()
if err := assetInstaller.InstallProfile(profile.Profile); err != nil {
failE(fmt.Errorf("Failed to install Provisioning Profile: %w", err))
}
}
log.Printf("Installation took %s", time.Since(profileInstallStart).Round(time.Millisecond))
log.Donef("Provisioning Profiles installed.")
}