-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
512 lines (432 loc) · 16.2 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/bitrise-io/go-android/sdk"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-steplib/steps-sign-apk/keystore"
)
var signingFileExts = []string{".mf", ".rsa", ".dsa", ".ec", ".sf"}
// -----------------------
// --- Models
// -----------------------
type configs struct {
BuildArtifactPath string `env:"android_app,required"`
KeystoreURL string `env:"keystore_url,required"`
KeystorePassword string `env:"keystore_password,required"`
KeystoreAlias string `env:"keystore_alias,required"`
PrivateKeyPassword string `env:"private_key_password"`
OutputName string `env:"output_name"`
VerboseLog bool `env:"verbose_log,opt[true,false]"`
PageAlign string `env:"page_align,opt[automatic,true,false]"`
SignerScheme string `env:"signer_scheme,opt[automatic,v2,v3,v4]"`
DebuggablePermitted string `env:"debuggable_permitted,opt[true,false]"`
SignerTool string `env:"signer_tool,opt[automatic,apksigner,jarsigner]"`
// Deprecated
APKPath string `env:"apk_path"`
}
type codeSignerTool string
const (
apksignerSignerTool codeSignerTool = "apksigner"
jarsignerSignerTool codeSignerTool = "jarsigner"
automaticSignerTool codeSignerTool = "automatic"
)
type pageAlignStatus int
const (
pageAlignInvalid pageAlignStatus = iota + 1
pageAlignAuto
pageAlignYes
pageAlignNo
)
func parsePageAlign(s string) pageAlignStatus {
switch s {
case "automatic":
return pageAlignAuto
case "true":
return pageAlignYes
case "false":
return pageAlignNo
default:
return pageAlignInvalid
}
}
func splitElements(list []string, sep string) (s []string) {
for _, e := range list {
s = append(s, strings.Split(e, sep)...)
}
return
}
func parseAppList(list string) (apps []string) {
list = strings.TrimSpace(list)
if len(list) == 0 {
return nil
}
s := []string{list}
for _, sep := range []string{"\n", `\n`, "|"} {
s = splitElements(s, sep)
}
for _, app := range s {
app = strings.TrimSpace(app)
if len(app) > 0 {
apps = append(apps, app)
}
}
return
}
// -----------------------
// --- Functions
// -----------------------
func download(url, pth string) error {
out, err := os.Create(pth)
if err != nil {
return err
}
defer func() {
if err := out.Close(); err != nil {
log.Warnf("Failed to close file: %s, error: %s", out, err)
}
}()
resp, err := http.Get(url)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Warnf("Failed to close response body, error: %s", err)
}
}()
_, err = io.Copy(out, resp.Body)
return err
}
func listFilesInBuildArtifact(aapt, pth string) ([]string, error) {
cmdSlice := []string{aapt, "list", pth}
out, err := keystore.ExecuteForOutput(cmdSlice)
if err != nil {
return []string{}, err
}
return strings.Split(out, "\n"), nil
}
func filterMETAFiles(fileList []string) []string {
metaFiles := []string{}
for _, file := range fileList {
if strings.HasPrefix(file, "META-INF/") {
metaFiles = append(metaFiles, file)
}
}
return metaFiles
}
func filterSigningFiles(fileList []string) []string {
var signingFiles []string
for _, file := range fileList {
ext := filepath.Ext(file)
for _, signExt := range signingFileExts {
if strings.EqualFold(ext, signExt) {
signingFiles = append(signingFiles, file)
}
}
}
return signingFiles
}
func removeFilesFromBuildArtifact(aapt, pth string, files []string) error {
cmdSlice := append([]string{aapt, "remove", pth}, files...)
prinatableCmd := command.PrintableCommandArgs(false, cmdSlice)
log.Printf("=> %s", prinatableCmd)
out, err := keystore.ExecuteForOutput(cmdSlice)
if err != nil && errorutil.IsExitStatusError(err) {
return errors.New(out)
}
return err
}
func isBuildArtifactSigned(aapt, pth string) (bool, error) {
filesInBuildArtifact, err := listFilesInBuildArtifact(aapt, pth)
if err != nil {
return false, err
}
metaFiles := filterMETAFiles(filesInBuildArtifact)
for _, metaFile := range metaFiles {
ext := filepath.Ext(metaFile)
if strings.EqualFold(ext, ".dsa") || strings.EqualFold(ext, ".rsa") {
return true, nil
}
}
return false, nil
}
func unsignBuildArtifact(aapt, pth string) error {
filesInBuildArtifact, err := listFilesInBuildArtifact(aapt, pth)
if err != nil {
return err
}
metaFiles := filterMETAFiles(filesInBuildArtifact)
signingFiles := filterSigningFiles(metaFiles)
if len(signingFiles) == 0 {
log.Printf("Build Artifact is not signed")
return nil
}
return removeFilesFromBuildArtifact(aapt, pth, signingFiles)
}
func prettyBuildArtifactBasename(buildArtifactPth string) string {
buildArtifactBasenameWithExt := path.Base(buildArtifactPth)
buildArtifactExt := filepath.Ext(buildArtifactBasenameWithExt)
buildArtifactBasename := strings.TrimSuffix(buildArtifactBasenameWithExt, buildArtifactExt)
buildArtifactBasename = strings.TrimSuffix(buildArtifactBasename, "-unsigned")
return buildArtifactBasename
}
func failf(format string, v ...interface{}) {
log.Errorf(format, v)
os.Exit(1)
}
func handleDeprecatedInputs(cfg *configs) {
if cfg.APKPath != "" {
log.Warnf("step input 'APK file path' (apk_path) is deprecated and will be removed on 20 August 2019, use 'APK or App Bundle file path' (android_app) instead!")
cfg.BuildArtifactPath = cfg.APKPath
}
}
func validate(cfg configs) error {
buildArtifactPaths := parseAppList(cfg.BuildArtifactPath)
for _, buildArtifactPath := range buildArtifactPaths {
if exist, err := pathutil.IsPathExists(buildArtifactPath); err != nil {
return fmt.Errorf("failed to check if BuildArtifactPath exist at: %s, error: %s", buildArtifactPath, err)
} else if !exist {
return fmt.Errorf("BuildArtifactPath not exist at: %s", buildArtifactPath)
}
artifactExt := path.Ext(buildArtifactPath)
signAAB := strings.EqualFold(artifactExt, ".aab")
if cfg.SignerTool == "apksigner" && signAAB {
failf("signer tool apksigner does not support signing AABs, please use automatic or jarsigner instead")
}
}
return nil
}
// -----------------------
// --- Main
// -----------------------
func main() {
var cfg configs
if err := stepconf.Parse(&cfg); err != nil {
failf("Process config: failed to parse input: %s", err)
}
pageAlignConfig := parsePageAlign(cfg.PageAlign)
stepconf.Print(cfg)
log.SetEnableDebugLog(cfg.VerboseLog)
handleDeprecatedInputs(&cfg)
fmt.Println()
if err := validate(cfg); err != nil {
failf("Process config: failed to validate input: %s", err)
}
// Download keystore
tmpDir, err := pathutil.NormalizedOSTempDirPath("bitrise-sign-build-artifact")
if err != nil {
failf("Run: failed to create tmp dir: %s", err)
}
keystorePath := ""
if strings.HasPrefix(cfg.KeystoreURL, "file://") {
pth := strings.TrimPrefix(cfg.KeystoreURL, "file://")
var err error
keystorePath, err = pathutil.AbsPath(pth)
if err != nil {
failf("Run: failed to expand path (%s): %s", pth, err)
}
} else {
log.Infof("Download keystore")
keystorePath = path.Join(tmpDir, "keystore.jks")
if err := download(cfg.KeystoreURL, keystorePath); err != nil {
failf("Run: failed to download keystore: %s", err)
}
}
log.Printf("using keystore at: %s", keystorePath)
keystore, err := keystore.NewHelper(keystorePath, cfg.KeystorePassword, cfg.KeystoreAlias)
if err != nil {
failf("Run: failed to create keystore helper: %s", err)
}
// ---
// Find Android tools
androidHome := os.Getenv("ANDROID_HOME")
log.Printf("android_home: %s", androidHome)
androidSDK, err := sdk.New(androidHome)
if err != nil {
failf("Run: failed to create SDK model: %s", err)
}
aapt, err := androidSDK.LatestBuildToolPath("aapt")
if err != nil {
failf("Run: failed to find AAPT path: %s", err)
}
log.Printf("aapt: %s", aapt)
zipalign, err := androidSDK.LatestBuildToolPath("zipalign")
if err != nil {
failf("Run: failed to find zipalign path: %s", err)
}
log.Printf("zipalign: %s", zipalign)
apkSigner, err := NewKeystoreSignatureConfiguration(keystorePath, cfg.KeystorePassword, cfg.KeystoreAlias, cfg.PrivateKeyPassword, cfg.DebuggablePermitted, cfg.SignerScheme)
if err != nil {
failf("Run: failed to create signature configuration: %s", err)
}
// ---
// Sign build artifacts
buildArtifactPaths := parseAppList(cfg.BuildArtifactPath)
signedAPKPaths := make([]string, 0)
signedAABPaths := make([]string, 0)
fmt.Println()
log.Infof("Signing %d Build Artifacts", len(buildArtifactPaths))
if len(buildArtifactPaths) > 1 && cfg.OutputName != "" {
log.Warnf("output_name is set and more than one artifact found, disabling artifact renaming as it would result in overwriting exported artifacts")
fmt.Println()
cfg.OutputName = ""
}
for i, buildArtifactPath := range buildArtifactPaths {
artifactExt := path.Ext(buildArtifactPath)
log.Donef("%d/%d signing %s", i+1, len(buildArtifactPaths), buildArtifactPath)
fmt.Println()
buildArtifactDir := path.Dir(buildArtifactPath)
buildArtifactBasename := prettyBuildArtifactBasename(buildArtifactPath)
// unsign build artifact
unsignedBuildArtifactPth := filepath.Join(tmpDir, "unsigned"+artifactExt)
if err := command.CopyFile(buildArtifactPath, unsignedBuildArtifactPth); err != nil {
failf("Run: failed to copy build artifact: %s", err)
}
signAAB := strings.EqualFold(artifactExt, ".aab")
signerTool := cfg.SignerTool
if signerTool == string(automaticSignerTool) {
if signAAB {
signerTool = string(jarsignerSignerTool)
} else {
signerTool = string(apksignerSignerTool)
}
}
if signerTool == string(jarsignerSignerTool) {
isSigned, err := isBuildArtifactSigned(aapt, unsignedBuildArtifactPth)
if err != nil {
failf("Run: failed to check if build artifact is signed: %s", err)
}
if isSigned {
log.Printf("Signature file (DSA or RSA) found in META-INF, unsigning the build artifact...")
if err := unsignBuildArtifact(aapt, unsignedBuildArtifactPth); err != nil {
failf("Run: failed to un-sign Build Artifact: %s", err)
}
fmt.Println()
} else {
log.Printf("No signature file (DSA or RSA) found in META-INF, skipping build artifact unsign...")
fmt.Println()
}
} else {
log.Printf("Skipping removal of existing signature as apksigner can re-sign already signed apk.")
}
var fullPath string
if signerTool == string(apksignerSignerTool) {
fullPath = signAPK(zipalign, unsignedBuildArtifactPth, buildArtifactDir, buildArtifactBasename, artifactExt, cfg.OutputName, apkSigner, pageAlignConfig)
} else {
fullPath = signJarSigner(zipalign, tmpDir, unsignedBuildArtifactPth, buildArtifactDir, buildArtifactBasename, artifactExt, cfg.PrivateKeyPassword, cfg.OutputName, keystore, pageAlignConfig)
}
if signAAB {
signedAABPaths = append(signedAABPaths, fullPath)
} else {
signedAPKPaths = append(signedAPKPaths, fullPath)
}
fmt.Println()
// ---
}
joinedAPKOutputPaths := strings.Join(signedAPKPaths, "|")
joinedAABOutputPaths := strings.Join(signedAABPaths, "|")
// APK
if len(signedAPKPaths) > 0 {
exportAPK(signedAPKPaths, joinedAPKOutputPaths)
} else {
log.Debugf("No Signed APK was exported - skip BITRISE_SIGNED_APK_PATH Environment Variable export")
log.Debugf("No Signed APK was exported - skip BITRISE_SIGNED_APK_PATH_LIST Environment Variable export")
}
// AAB
if len(signedAABPaths) > 0 {
exportAAB(signedAABPaths, joinedAABOutputPaths)
} else {
log.Debugf("No Signed AAB was exported - skip BITRISE_SIGNED_AAB_PATH Environment Variable export")
log.Debugf("No Signed AAB was exported - skip BITRISE_SIGNED_AAB_PATH_LIST Environment Variable export")
}
}
func signJarSigner(zipalign, tmpDir string, unsignedBuildArtifactPth string, buildArtifactDir string, buildArtifactBasename string, artifactExt string, privateKeyPassword string, outputName string, keystore keystore.Helper, pageAlignConfig pageAlignStatus) string {
// sign build artifact
unalignedBuildArtifactPth := filepath.Join(tmpDir, "unaligned"+artifactExt)
log.Infof("Sign Build Artifact with Jarsigner: %s", unsignedBuildArtifactPth)
if err := keystore.SignBuildArtifact(unsignedBuildArtifactPth, unalignedBuildArtifactPth, privateKeyPassword); err != nil {
failf("Run: failed to sign Build Artifact: %s", err)
}
fmt.Println()
log.Infof("Verify Build Artifact")
if err := keystore.VerifyBuildArtifact(unalignedBuildArtifactPth); err != nil {
failf("Run: failed to verify Build Artifact: %s", err)
}
fmt.Println()
fullPath, err := zipAlignArtifact(zipalign, unalignedBuildArtifactPth, buildArtifactDir, buildArtifactBasename, artifactExt, "signed", outputName, pageAlignConfig)
if err != nil {
failf("Run: failed to zipalign Build Artifact: %s", err)
}
return fullPath
}
func signAPK(zipalign, unsignedBuildArtifactPth, buildArtifactDir, buildArtifactBasename, artifactExt, outputName string, apkSigner SignatureConfiguration, pageAlignConfig pageAlignStatus) string {
alignedPath, err := zipAlignArtifact(zipalign, unsignedBuildArtifactPth, buildArtifactDir, buildArtifactBasename, artifactExt, "aligned", "", pageAlignConfig)
if err != nil {
failf("Run: failed to zipalign Build Artifact: %s", err)
}
signedArtifactName := fmt.Sprintf("%s-bitrise-signed%s", buildArtifactBasename, artifactExt)
if artifactName := fmt.Sprintf("%s%s", outputName, artifactExt); outputName != "" {
log.Printf("- Exporting (%s) as: %s", signedArtifactName, artifactName)
signedArtifactName = artifactName
}
fullPath := filepath.Join(buildArtifactDir, signedArtifactName)
fmt.Println()
log.Infof("Sign Build Artifact with APKSigner: %s", alignedPath)
err = apkSigner.SignBuildArtifact(alignedPath, fullPath)
if err != nil {
failf("Run: failed to build artifact: %s", err)
}
fmt.Println()
log.Infof("Verify Build Artifact")
err = apkSigner.VerifyBuildArtifact(fullPath)
if err != nil {
failf("Run: failed to build artifact: %s", err)
}
return fullPath
}
func exportAPK(signedAPKPaths []string, joinedAPKOutputPaths string) {
if err := tools.ExportEnvironmentWithEnvman("BITRISE_SIGNED_APK_PATH", signedAPKPaths[len(signedAPKPaths)-1]); err != nil {
log.Warnf("Failed to export APK (%s) error: %s", signedAPKPaths[len(signedAPKPaths)-1], err)
} else {
log.Donef("The Signed APK path is now available in the Environment Variable: BITRISE_SIGNED_APK_PATH (value: %s)", signedAPKPaths[len(signedAPKPaths)-1])
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_SIGNED_APK_PATH_LIST", joinedAPKOutputPaths); err != nil {
log.Warnf("Failed to export APK list (%s), error: %s", joinedAPKOutputPaths, err)
} else {
log.Donef("The Signed APK path list is now available in the Environment Variable: BITRISE_SIGNED_APK_PATH_LIST (value: %s)", joinedAPKOutputPaths)
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_APK_PATH", joinedAPKOutputPaths); err != nil {
log.Warnf("Failed to export APK list (%s), error: %s", joinedAPKOutputPaths, err)
} else {
log.Donef("The Signed APK path is now available in the Environment Variable: BITRISE_APK_PATH (value: %s)", joinedAPKOutputPaths)
}
}
func exportAAB(signedAABPaths []string, joinedAABOutputPaths string) {
if err := tools.ExportEnvironmentWithEnvman("BITRISE_SIGNED_AAB_PATH", signedAABPaths[len(signedAABPaths)-1]); err != nil {
log.Warnf("Failed to export AAB (%s), error: %s", signedAABPaths[len(signedAABPaths)-1], err)
} else {
log.Donef("The Signed AAB path is now available in the Environment Variable: BITRISE_SIGNED_AAB_PATH (value: %s)", signedAABPaths[len(signedAABPaths)-1])
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_SIGNED_AAB_PATH_LIST", joinedAABOutputPaths); err != nil {
log.Warnf("Failed to export AAB list (%s), error: %s", joinedAABOutputPaths, err)
} else {
log.Donef("The Signed AAB path list is now available in the Environment Variable: BITRISE_SIGNED_AAB_PATH_LIST (value: %s)", joinedAABOutputPaths)
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_AAB_PATH", joinedAABOutputPaths); err != nil {
log.Warnf("Failed to export AAB list (%s), error: %s", joinedAABOutputPaths, err)
} else {
log.Donef("The Signed AAB path is now available in the Environment Variable: BITRISE_AAB_PATH (value: %s)", joinedAABOutputPaths)
}
}