/
main.go
439 lines (382 loc) · 12.7 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/avast/retry-go/v4"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
httpretry "github.com/bitrise-io/go-utils/retry"
"github.com/bitrise-io/go-utils/sliceutil"
"github.com/bitrise-io/go-xcode/appleauth"
"github.com/bitrise-io/go-xcode/devportalservice"
"github.com/bitrise-io/go-xcode/ipa"
"github.com/bitrise-io/go-xcode/plistutil"
"github.com/bitrise-io/go-xcode/utility"
shellquote "github.com/kballard/go-shellquote"
)
// Config ...
type Config struct {
BitriseConnection string `env:"connection,opt[automatic,api_key,apple_id,off]"`
AppleID string `env:"itunescon_user"`
Password stepconf.Secret `env:"password"`
AppSpecificPassword stepconf.Secret `env:"app_password"`
APIKeyPath stepconf.Secret `env:"api_key_path"`
APIIssuer string `env:"api_issuer"`
IpaPath string `env:"ipa_path"`
PkgPath string `env:"pkg_path"`
Platform string `env:"platform,opt[auto,ios,macos,tvos]"`
ItunesConnectUser string `env:"itunescon_user"`
AdditionalParams string `env:"altool_options"`
RetryTimes string `env:"retries"`
// Used to get Bitrise Apple Developer Portal Connection
BuildURL string `env:"BITRISE_BUILD_URL"`
BuildAPIToken stepconf.Secret `env:"BITRISE_BUILD_API_TOKEN"`
}
type platformType string
const (
iOS platformType = "ios"
tvOS platformType = "appletvos"
macOS platformType = "macos"
)
func (cfg Config) validateArtifact() error {
cfg.IpaPath = strings.TrimSpace(cfg.IpaPath)
cfg.PkgPath = strings.TrimSpace(cfg.PkgPath)
var (
deployIPA = cfg.IpaPath != ""
deployPKG = cfg.PkgPath != ""
)
if deployIPA == deployPKG {
return fmt.Errorf("one artifact is required and only one is allowed, either provide ipa_path or pkg_path")
}
return nil
}
// getPlatformType maps platform to an altool parameter
//
// -t, --type {macos | ios | appletvos} Specify the platform of the file, or of the host app when using --upload-hosted-content. (Output by 'xcrun altool -h')
//
// if 'auto' is selected the 'DTPlatformName' is read from Info.plist
func getPlatformType(ipaPath, platform string) platformType {
fallback := func() platformType {
log.Warnf("Failed to analyze %s, fallback platform type to ios", ipaPath)
return iOS
}
switch platform {
case "auto":
// *.pkg -> macos
if ipaPath == "" {
return macOS
}
plistPath, err := ipa.UnwrapEmbeddedInfoPlist(ipaPath)
if err != nil {
return fallback()
}
plist, err := plistutil.NewPlistDataFromFile(plistPath)
if err != nil {
return fallback()
}
platform, ok := plist.GetString("DTPlatformName")
if !ok {
return fallback()
}
switch platform {
case "appletvos", "appletvsimulator":
return tvOS
case "macosx":
return macOS
case "iphoneos", "iphonesimulator", "watchos", "watchsimulator":
return iOS
default:
return fallback()
}
case "ios":
return iOS
case "macos":
return macOS
case "tvos":
return tvOS
default:
return fallback()
}
}
func parseAuthSources(connection string) ([]appleauth.Source, error) {
switch connection {
case "automatic":
return []appleauth.Source{
&appleauth.ConnectionAPIKeySource{},
&appleauth.ConnectionAppleIDSource{},
&appleauth.InputAPIKeySource{},
&appleauth.InputAppleIDSource{},
}, nil
case "api_key":
return []appleauth.Source{&appleauth.ConnectionAPIKeySource{}}, nil
case "apple_id":
return []appleauth.Source{&appleauth.ConnectionAppleIDSource{}}, nil
case "off":
return []appleauth.Source{
&appleauth.InputAPIKeySource{},
&appleauth.InputAppleIDSource{},
}, nil
default:
return nil, fmt.Errorf("invalid connection input: %s", connection)
}
}
const notConnected = `Connected Apple Developer Portal Account not found.
Most likely because there is no Apple Developer Portal Account connected to the build.
Read more: https://devcenter.bitrise.io/getting-started/configuring-bitrise-steps-that-require-apple-developer-account-data/`
func handleSessionDataError(err error) {
if err == nil {
return
}
if networkErr, ok := err.(devportalservice.NetworkError); ok && networkErr.Status == http.StatusUnauthorized {
fmt.Println()
log.Warnf("%s", "Unauthorized to query Connected Apple Developer Portal Account. This happens by design, with a public app's PR build, to protect secrets.")
return
}
fmt.Println()
log.Errorf("Failed to activate Bitrise Apple Developer Portal connection: %s", err)
log.Warnf("Read more: https://devcenter.bitrise.io/getting-started/configuring-bitrise-steps-that-require-apple-developer-account-data/")
}
func getKeyPath(keyID string, keyPaths []string) (string, error) {
certName := fmt.Sprintf("AuthKey_%s.p8", keyID)
for _, path := range keyPaths {
certPath := filepath.Join(path, certName)
switch exists, err := pathutil.IsPathExists(certPath); {
case err != nil:
return "", err
case exists:
return certPath, os.ErrExist
}
}
return filepath.Join(keyPaths[0], certName), nil
}
func writeAPIKey(privateKey, keyID string) error {
// see these in the altool's man page
var keyPaths = []string{
filepath.Join(os.Getenv("HOME"), ".appstoreconnect/private_keys"),
filepath.Join(os.Getenv("HOME"), ".private_keys"),
filepath.Join(os.Getenv("HOME"), "private_keys"),
"./private_keys",
}
keyPath, err := getKeyPath(keyID, keyPaths)
if err != nil {
if err == os.ErrExist {
return nil
}
return err
}
if err := os.MkdirAll(filepath.Dir(keyPath), 0777); err != nil {
return err
}
return fileutil.WriteStringToFile(keyPath, privateKey)
}
const typeKey = "--type"
func main() {
var cfg Config
if err := stepconf.Parse(&cfg); err != nil {
failf("Error: %s", err)
}
stepconf.Print(cfg)
fmt.Println()
if err := cfg.validateArtifact(); err != nil {
failf("Input error: %s", err)
}
authInputs := appleauth.Inputs{
Username: cfg.ItunesConnectUser,
Password: string(cfg.Password),
AppSpecificPassword: string(cfg.AppSpecificPassword),
APIIssuer: cfg.APIIssuer,
APIKeyPath: string(cfg.APIKeyPath),
}
if err := authInputs.Validate(); err != nil {
failf("Issue with authentication related inputs: %v", err)
}
xcodeVersion, err := utility.GetXcodeVersion()
if err != nil {
failf("Failed to determine Xcode version: %s", err)
}
//
// Select and fetch Apple authenication source
authSources, err := parseAuthSources(cfg.BitriseConnection)
if err != nil {
failf("Invalid input: unexpected value for Bitrise Apple Developer Connection (%s)", cfg.BitriseConnection)
}
var devportalConnectionProvider *devportalservice.BitriseClient
if cfg.BuildURL != "" && cfg.BuildAPIToken != "" {
devportalConnectionProvider = devportalservice.NewBitriseClient(httpretry.NewHTTPClient().StandardClient(), cfg.BuildURL, string(cfg.BuildAPIToken))
} else {
fmt.Println()
log.Warnf("Connected Apple Developer Portal Account not found. Step is not running on bitrise.io: BITRISE_BUILD_URL and BITRISE_BUILD_API_TOKEN envs are not set")
}
var conn *devportalservice.AppleDeveloperConnection
if cfg.BitriseConnection != "off" && devportalConnectionProvider != nil {
var err error
conn, err = devportalConnectionProvider.GetAppleDeveloperConnection()
if err != nil {
handleSessionDataError(err)
}
if conn != nil && (conn.APIKeyConnection == nil && conn.AppleIDConnection == nil) {
fmt.Println()
log.Debugf("%s", notConnected)
}
}
authConfig, err := appleauth.Select(conn, authSources, authInputs)
if err != nil {
failf("Could not configure Apple Service authentication: %v", err)
}
if authConfig.AppleID != nil && authConfig.AppleID.AppSpecificPassword == "" {
log.Warnf("If 2FA enabled, Application-specific password is required when using Apple ID authentication.")
}
// Prepare command
var authParams []string
if authConfig.APIKey != nil {
if err := writeAPIKey(string(authConfig.APIKey.PrivateKey), authConfig.APIKey.KeyID); err != nil {
failf("Failed to prepare certificate for authentication, error: %s", err)
}
authParams = []string{"--apiKey", authConfig.APIKey.KeyID, "--apiIssuer", authConfig.APIKey.IssuerID}
} else {
password := string(authConfig.AppleID.Password)
if string(authConfig.AppleID.AppSpecificPassword) != "" {
password = string(authConfig.AppleID.AppSpecificPassword)
}
authParams = []string{"--username", authConfig.AppleID.Username, "--password", password}
}
filePth := cfg.IpaPath
if filePth == "" {
filePth = cfg.PkgPath
}
if filePth == "" {
failf("Either IPA path or PKG path has to be provided")
}
additionalParams, err := shellquote.Split(cfg.AdditionalParams)
if err != nil {
failf("Failed to parse additional parameters, error: %s", err)
}
uploadParams := []string{"--upload-app", "-f", filePth}
// Platform type parameter was introduced in Xcode 13
if xcodeVersion.MajorVersion >= 13 && !sliceutil.IsStringInSlice(typeKey, additionalParams) {
uploadParams = append(uploadParams, typeKey, string(getPlatformType(cfg.IpaPath, cfg.Platform)))
}
altoolParams := append([]string{"altool"}, uploadParams...)
altoolParams = append(altoolParams, authParams...)
altoolParams = append(altoolParams, additionalParams...)
out, err := uploadWithRetry(newAltoolUploader(altoolParams, filePth, authConfig), cfg.RetryTimes)
if err != nil {
failf("Uploading IPA failed: %s", err)
}
if matches := regexp.MustCompile(`(?i)Generated JWT: (.*)`).FindStringSubmatch(out); len(matches) == 2 {
out = strings.Replace(out, matches[1], "[REDACTED]", -1)
}
fmt.Println(out)
log.Donef("IPA uploaded")
}
type uploader interface {
upload() (string, string, error)
}
type altoolUploader struct {
altoolParams []string
filePth string
authConfig appleauth.Credentials
}
func newAltoolUploader(altoolParams []string, filePth string, authConfig appleauth.Credentials) uploader {
return altoolUploader{altoolParams: altoolParams, filePth: filePth, authConfig: authConfig}
}
func (a altoolUploader) upload() (string, string, error) {
cmd := command.New("xcrun", a.altoolParams...)
var sb bytes.Buffer
var eb bytes.Buffer
cmd.SetStdout(&sb)
cmd.SetStderr(&eb)
fileName := filepath.Base(a.filePth)
log.Infof("Uploading - %s ...", fileName)
commandStr := cmd.PrintableCommandArgs()
authConfig := a.authConfig
if authConfig.APIKey == nil {
if authConfig.AppleID.Password != "" {
commandStr = strings.Replace(commandStr, authConfig.AppleID.Password, "[REDACTED]", -1)
}
if authConfig.AppleID.AppSpecificPassword != "" {
commandStr = strings.Replace(commandStr, authConfig.AppleID.AppSpecificPassword, "[REDACTED]", -1)
}
}
log.Printf("$ %s", commandStr)
err := cmd.Run()
ioString := sb.String()
errorString := eb.String()
if errorString != "" {
log.Errorf("%s", errorString)
}
if err != nil {
return ioString, errorString, err
}
return ioString, errorString, nil
}
func uploadWithRetry(uploader uploader, retryTimes string, opts ...retry.Option) (string, error) {
var regexList = []string{
// https://bitrise.atlassian.net/browse/STEP-1190
`(?s).*Unable to determine the application using bundleId.*-19201.*`,
`(?s).*Unable to determine app platform for 'Undefined' software type.*1194.*`,
`(?s).*TransporterService.*error occurred trying to read the bundle.*-18000.*`,
`(?s).*Unable to authenticate.*-19209.*`,
`(?s).*server returned an invalid response.*try your request again.*`,
`(?s).*The request timed out.*`,
}
var result string
parsedRetryTimes, err := strconv.ParseInt(retryTimes, 10, 32)
attempts := uint(parsedRetryTimes)
if err != nil {
attempts = uint(10)
}
mOpts := []retry.Option{
retry.Attempts(attempts),
retry.Delay(300 * time.Millisecond),
retry.LastErrorOnly(true),
retry.OnRetry(func(n uint, err error) {
if n == 0 {
log.Warnf("Upload failed, but we recognized it as possibly recoverable error, retrying...")
} else if n != attempts-1 {
log.Warnf("Attempt %d failed, retrying...", n+1)
} else {
log.Warnf("Attempt %d failed", attempts)
}
}),
}
mOpts = append(mOpts, opts...)
err = retry.Do(
func() error {
r, errorString, err := uploader.upload()
if err != nil {
for _, re := range regexList {
matched, err2 := regexp.MatchString(re, errorString)
if err2 != nil {
log.Warnf("Couldn't match %s with regex %s", errorString, re)
continue
}
if matched {
return err
}
}
return retry.Unrecoverable(err)
}
result = r
return nil
},
mOpts...)
if err != nil {
return "", err
}
return result, nil
}
func failf(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}