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

return errors for invalid key pem blocks #1260

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions cmd/notary/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ func TestClientKeyImport(t *testing.T) {

// import the key
_, err = runCommand(t, tempDir, "key", "import", tempFile6.Name())
require.NoError(t, err)
require.EqualError(t, err, "failed to import all keys: invalid key pem block")

// if there is hardware available, root will only be on hardware, and not
// on disk
Expand Down Expand Up @@ -2720,7 +2720,7 @@ func TestClientKeyImport(t *testing.T) {

// import the key
_, err = runCommand(t, tempDir, "key", "import", tempFile8.Name())
require.NoError(t, err)
require.EqualError(t, err, "failed to import all keys: invalid key pem block")

// if there is hardware available, root will only be on hardware, and not
// on disk
Expand Down
4 changes: 1 addition & 3 deletions cmd/notary/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,9 @@ func TestKeyGeneration(t *testing.T) {
require.NoError(t, err)
privK, err := utils.ParsePEMPrivateKey(priv, testPassphrase)
require.NoError(t, err)

// the ID is only generated from the public part of the key so they should be identical
require.Equal(t, pubK.ID(), privK.ID())

_, err = runCommand(t, tempDir, "key", "import", filepath.Join(tempDir, "testkeys-key.pem"))
require.NoError(t, err)

require.EqualError(t, err, "failed to import all keys: invalid key pem block")
}
10 changes: 8 additions & 2 deletions trustmanager/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trustmanager
import (
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -100,8 +101,9 @@ func ImportKeys(from io.Reader, to []Importer, fallbackRole string, fallbackGUN
return err
}
var (
writeTo string
toWrite []byte
writeTo string
toWrite []byte
errBlocks []string
)
for block, rest := pem.Decode(data); block != nil; block, rest = pem.Decode(rest) {
handleLegacyPath(block)
Expand All @@ -110,6 +112,7 @@ func ImportKeys(from io.Reader, to []Importer, fallbackRole string, fallbackGUN
loc, err := checkValidity(block)
if err != nil {
// already logged in checkValidity
errBlocks = append(errBlocks, err.Error())
continue
}

Expand Down Expand Up @@ -157,6 +160,9 @@ func ImportKeys(from io.Reader, to []Importer, fallbackRole string, fallbackGUN
if toWrite != nil { // close out final iteration if there's data left
return importToStores(to, writeTo, toWrite)
}
if len(errBlocks) > 0 {
return fmt.Errorf("failed to import all keys: %s", strings.Join(errBlocks, ", "))
}
return nil
}

Expand Down