Skip to content

Commit

Permalink
cmd/build: exit with failure on empty signing key (#5297)
Browse files Browse the repository at this point in the history
Fixes #4972.

Signed-off-by: Mathis Joffre <mariusjoffre@gmail.com>
  • Loading branch information
Joffref committed Nov 2, 2022
1 parent c067f1d commit a855ab1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 10 additions & 5 deletions cmd/build.go
Expand Up @@ -261,7 +261,10 @@ func dobuild(params buildParams, args []string) error {
return err
}

bsc := buildSigningConfig(params.key, params.algorithm, params.claimsFile, params.plugin)
bsc, err := buildSigningConfig(params.key, params.algorithm, params.claimsFile, params.plugin)
if err != nil {
return err
}

if bvc != nil || bsc != nil {
if !params.bundleMode {
Expand Down Expand Up @@ -346,10 +349,12 @@ func buildVerificationConfig(pubKey, pubKeyID, alg, scope string, excludeFiles [
return bundle.NewVerificationConfig(map[string]*keys.Config{pubKeyID: keyConfig}, pubKeyID, scope, excludeFiles), nil
}

func buildSigningConfig(key, alg, claimsFile, plugin string) *bundle.SigningConfig {
func buildSigningConfig(key, alg, claimsFile, plugin string) (*bundle.SigningConfig, error) {
if key == "" && (plugin != "" || claimsFile != "" || alg != "") {
return nil, fmt.Errorf("specify the secret (HMAC) or path of the PEM file containing the private key (RSA and ECDSA)")
}
if key == "" {
return nil
return nil, nil
}

return bundle.NewSigningConfig(key, alg, claimsFile).WithPlugin(plugin)
return bundle.NewSigningConfig(key, alg, claimsFile).WithPlugin(plugin), nil
}
5 changes: 4 additions & 1 deletion cmd/sign.go
Expand Up @@ -169,7 +169,10 @@ func doSign(args []string, params signCmdParams) error {
return err
}

signingConfig := buildSigningConfig(params.key, params.algorithm, params.claimsFile, params.plugin)
signingConfig, err := buildSigningConfig(params.key, params.algorithm, params.claimsFile, params.plugin)
if err != nil {
return err
}

token, err := bundle.GenerateSignedToken(files, signingConfig, "")
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/sign_test.go
Expand Up @@ -146,6 +146,11 @@ func TestValidateSignParams(t *testing.T) {
newSignCmdParams(),
true, fmt.Errorf("specify the secret (HMAC) or path of the PEM file containing the private key (RSA and ECDSA)"),
},
"empty_signing_key": {
[]string{"foo"},
signCmdParams{key: "", bundleMode: true},
true, fmt.Errorf("specify the secret (HMAC) or path of the PEM file containing the private key (RSA and ECDSA)"),
},
"non_bundle_mode": {
[]string{"foo"},
signCmdParams{key: "foo"},
Expand Down

0 comments on commit a855ab1

Please sign in to comment.