Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to error messages #78

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func validate(cfg configs) error {
func main() {
var cfg configs
if err := stepconf.Parse(&cfg); err != nil {
failf("Issue with input: %s", err)
failf("Process config: failed to parse input: %s", err)
}
pageAlignConfig := parsePageAlign(cfg.PageAlign)

Expand All @@ -250,13 +250,13 @@ func main() {
fmt.Println()

if err := validate(cfg); err != nil {
failf("Issue with input: %s", err)
failf("Process config: failed to validate input: %s", err)
}

// Download keystore
tmpDir, err := pathutil.NormalizedOSTempDirPath("bitrise-sign-build-artifact")
if err != nil {
failf("Failed to create tmp dir, error: %s", err)
failf("Run: failed to create tmp dir: %s", err)
}

keystorePath := ""
Expand All @@ -265,20 +265,20 @@ func main() {
var err error
keystorePath, err = pathutil.AbsPath(pth)
if err != nil {
failf("Failed to expand path (%s), error: %s", pth, err)
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("Failed to download keystore, error: %s", err)
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("Failed to create keystore helper, error: %s", err)
failf("Run: failed to create keystore helper: %s", err)
}
// ---

Expand All @@ -288,24 +288,24 @@ func main() {

androidSDK, err := sdk.New(androidHome)
if err != nil {
failf("failed to create sdk model, error: %s", err)
failf("Run: failed to create SDK model: %s", err)
}

aapt, err := androidSDK.LatestBuildToolPath("aapt")
if err != nil {
failf("Failed to find aapt path, error: %s", err)
failf("Run: failed to find AAPT path: %s", err)
}
log.Printf("aapt: %s", aapt)

zipalign, err := androidSDK.LatestBuildToolPath("zipalign")
if err != nil {
failf("Failed to find zipalign path, error: %s", err)
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("Failed to create keystore helper, error: %s", err)
failf("Run: failed to create signature configuration: %s", err)
}
// ---

Expand Down Expand Up @@ -334,21 +334,21 @@ func main() {
// unsign build artifact
unsignedBuildArtifactPth := filepath.Join(tmpDir, "unsigned"+artifactExt)
if err := command.CopyFile(buildArtifactPath, unsignedBuildArtifactPth); err != nil {
failf("Failed to copy build artifact, error: %s", err)
failf("Run: failed to copy build artifact: %s", err)
}

signAAB := strings.EqualFold(artifactExt, ".aab")

if signAAB || !cfg.UseAPKSigner {
isSigned, err := isBuildArtifactSigned(aapt, unsignedBuildArtifactPth)
if err != nil {
failf("Failed to check if build artifact is signed, error: %s", err)
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("Failed to unsign Build Artifact, error: %s", err)
failf("Run: failed to un-sign Build Artifact: %s", err)
}
fmt.Println()
} else {
Expand All @@ -370,9 +370,6 @@ func main() {
signedAPKPaths = append(signedAPKPaths, fullPath)
}

if err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was checking an err instance assigned waaay earlier, I don't think we need this here.

failf("Failed to sign artifact, error: %s", err)
}
fmt.Println()
// ---
}
Expand Down Expand Up @@ -402,19 +399,19 @@ func signJarSigner(zipalign, tmpDir string, unsignedBuildArtifactPth string, bui
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("Failed to sign Build Artifact, error: %s", err)
failf("Run: failed to sign Build Artifact: %s", err)
}
fmt.Println()

log.Infof("Verify Build Artifact")
if err := keystore.VerifyBuildArtifact(unalignedBuildArtifactPth); err != nil {
failf("Failed to verify Build Artifact, error: %s", err)
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("Failed to zipalign Build Artifact: %s", err)
failf("Run: failed to zipalign Build Artifact: %s", err)
}

return fullPath
Expand All @@ -423,7 +420,7 @@ func signJarSigner(zipalign, tmpDir string, unsignedBuildArtifactPth string, bui
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("Failed to zipalign Build Artifact, error: %s", err)
failf("Run: failed to zipalign Build Artifact: %s", err)
}

signedArtifactName := fmt.Sprintf("%s-bitrise-signed%s", buildArtifactBasename, artifactExt)
Expand All @@ -437,14 +434,14 @@ func signAPK(zipalign, unsignedBuildArtifactPth, buildArtifactDir, buildArtifact
log.Infof("Sign Build Artifact with APKSigner: %s", alignedPath)
err = apkSigner.SignBuildArtifact(alignedPath, fullPath)
if err != nil {
failf("Failed to build artifact, error: %s", err)
failf("Run: failed to build artifact: %s", err)
}

fmt.Println()
log.Infof("Verify Build Artifact")
err = apkSigner.VerifyBuildArtifact(fullPath)
if err != nil {
failf("Failed to build artifact, error: %s", err)
failf("Run: failed to build artifact: %s", err)
}

return fullPath
Expand Down