add non-root passwd functionality

Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
Riyaz Faizullabhoy
2016-02-03 16:11:09 -08:00
parent 70c7a8a16d
commit 1c4d02455b
3 changed files with 37 additions and 10 deletions

View File

@@ -1169,17 +1169,36 @@ func TestClientKeyPassphraseChange(t *testing.T) {
server := setupServer()
defer server.Close()
target := "sdgkadga"
tempFile, err := ioutil.TempFile("/tmp", "targetfile")
assert.NoError(t, err)
tempFile.Close()
defer os.Remove(tempFile.Name())
// -- tests --
_, err := runCommand(t, tempDir, "-s", server.URL, "init", "gun1")
_, err = runCommand(t, tempDir, "-s", server.URL, "init", "gun1")
assert.NoError(t, err)
// we should have three keys stored locally in total: root, targets, snapshot
rootIDs, _ := assertNumKeys(t, tempDir, 1, 2, true)
rootIDs, signingIDs := assertNumKeys(t, tempDir, 1, 2, true)
for _, keyID := range signingIDs {
// try changing the private key passphrase
_, err = runCommand(t, tempDir, "-s", server.URL, "key", "passwd", keyID)
assert.NoError(t, err)
// assert that the signing keys (number and IDs) didn't change
_, signingIDs = assertNumKeys(t, tempDir, 1, 2, true)
assert.Contains(t, signingIDs, keyID)
// make sure we can still publish with this signing key
assertSuccessfullyPublish(t, tempDir, server.URL, "gun1", target, tempFile.Name())
}
// only one rootID, try changing the private key passphrase
rootID := rootIDs[0]
_, err = runCommand(t, tempDir, "-s", server.URL, "key", "passwd", rootID)
assert.NoError(t, err)
// make sure we can init a new repo with this key
_, err = runCommand(t, tempDir, "-s", server.URL, "init", "gun2")
assert.NoError(t, err)

View File

@@ -78,8 +78,8 @@ var cmdKeyRemoveTemplate = usageTemplate{
var cmdKeyPasswdTemplate = usageTemplate{
Use: "passwd [ keyID ]",
Short: "Changes the passphrase for the root key with the given keyID.",
Long: "Changes the passphrase for the root key with the given keyID. Will require validation of the old passphrase.",
Short: "Changes the passphrase for the key with the given keyID.",
Long: "Changes the passphrase for the key with the given keyID. Will require validation of the old passphrase.",
}
type keyCommander struct {
@@ -502,7 +502,7 @@ func (k *keyCommander) keyRemove(cmd *cobra.Command, args []string) error {
func (k *keyCommander) keyPassphraseChange(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
cmd.Usage()
return fmt.Errorf("must specify the key ID of the root key to change the passphrase of")
return fmt.Errorf("must specify the key ID of the key to change the passphrase of")
}
config, err := k.configGetter()
@@ -521,18 +521,26 @@ func (k *keyCommander) keyPassphraseChange(cmd *cobra.Command, args []string) er
return fmt.Errorf("invalid key ID provided: %s", keyID)
}
// We only allow for changing the root key, so use no gun
cs := cryptoservice.NewCryptoService("", ks...)
// Find the key's GUN by ID, in case it is a non-root key
var keyGUN string
for _, store := range ks {
for keypath := range store.ListKeys() {
if filepath.Base(keypath) == keyID {
keyGUN = filepath.Dir(keypath)
}
}
}
cs := cryptoservice.NewCryptoService(keyGUN, ks...)
privKey, role, err := cs.GetPrivateKey(keyID)
if err != nil {
return fmt.Errorf("could not retrieve local root key for key ID provided: %s", keyID)
return fmt.Errorf("could not retrieve local key for key ID provided: %s", keyID)
}
// Must use a different passphrase retriever to avoid caching the
// unlocking passphrase and reusing that.
passChangeRetriever := k.getRetriever()
keyStore, err := trustmanager.NewKeyFileStore(config.GetString("trust_dir"), passChangeRetriever)
err = keyStore.AddKey(keyID, role, privKey)
err = keyStore.AddKey(filepath.Join(keyGUN, keyID), role, privKey)
if err != nil {
return err
}

View File

@@ -431,5 +431,5 @@ func TestChangeKeyPassphraseNonexistentID(t *testing.T) {
// Valid ID size, but does not exist as a key ID
err := k.keyPassphraseChange(&cobra.Command{}, []string{strings.Repeat("x", notary.Sha256HexSize)})
assert.Error(t, err)
assert.Contains(t, err.Error(), "could not retrieve local root key for key ID provided")
assert.Contains(t, err.Error(), "could not retrieve local key for key ID provided")
}