mirror of https://github.com/docker/docs.git
simplify export logic with new keymap
Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
parent
0f39dd7aa8
commit
c41cee3e5d
|
@ -263,27 +263,12 @@ func (k *keyCommander) keysExport(cmd *cobra.Command, args []string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Search for this key in all of our keystores, determine whether this key has a GUN
|
||||
keyGun := ""
|
||||
keyRole := ""
|
||||
for _, store := range ks {
|
||||
for keypath, role := range store.ListKeys() {
|
||||
if filepath.Base(keypath) == keyID {
|
||||
keyRole = role
|
||||
if role == data.CanonicalRootRole {
|
||||
continue
|
||||
}
|
||||
dirPath := filepath.Dir(keypath)
|
||||
if dirPath != "." { // no gun
|
||||
keyGun = dirPath
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
cs := cryptoservice.NewCryptoService("", ks...)
|
||||
keyInfo, err := cs.GetKeyInfo(keyID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not retrieve info for key %s", keyID)
|
||||
}
|
||||
|
||||
cs := cryptoservice.NewCryptoService(keyGun, ks...)
|
||||
|
||||
exportFile, err := os.Create(exportFilename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating output file: %v", err)
|
||||
|
@ -294,12 +279,12 @@ func (k *keyCommander) keysExport(cmd *cobra.Command, args []string) error {
|
|||
exportRetriever := k.getRetriever()
|
||||
err = cs.ExportKeyReencrypt(exportFile, keyID, exportRetriever)
|
||||
} else {
|
||||
err = cs.ExportKey(exportFile, keyID, keyRole)
|
||||
err = cs.ExportKey(exportFile, keyID, keyInfo.Role)
|
||||
}
|
||||
exportFile.Close()
|
||||
if err != nil {
|
||||
os.Remove(exportFilename)
|
||||
return fmt.Errorf("Error exporting %s key: %v", keyRole, err)
|
||||
return fmt.Errorf("Error exporting %s key: %v", keyInfo.Role, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -41,9 +41,6 @@ func (cs *CryptoService) ExportKey(dest io.Writer, keyID, role string) error {
|
|||
err error
|
||||
)
|
||||
|
||||
if role != data.CanonicalRootRole {
|
||||
keyID = filepath.Join(cs.gun, keyID)
|
||||
}
|
||||
for _, ks := range cs.keyStores {
|
||||
pemBytes, err = ks.ExportKey(keyID)
|
||||
if err != nil {
|
||||
|
|
|
@ -13,12 +13,6 @@ import (
|
|||
"github.com/docker/notary/tuf/data"
|
||||
)
|
||||
|
||||
const (
|
||||
rootKeysSubdir = "root_keys"
|
||||
nonRootKeysSubdir = "tuf_keys"
|
||||
privDir = "private"
|
||||
)
|
||||
|
||||
type keyInfoMap map[string]KeyInfo
|
||||
|
||||
// KeyFileStore persists and manages private keys on disk
|
||||
|
@ -73,10 +67,10 @@ func generateKeyInfoMap(s LimitedFileStore) map[string]KeyInfo {
|
|||
for _, keyPath := range s.ListFiles() {
|
||||
// Remove the prefix of the directory from the filename for GUN/role/ID parsing
|
||||
var keyIDAndGun, keyRole string
|
||||
if strings.HasPrefix(keyPath, rootKeysSubdir+"/") {
|
||||
keyIDAndGun = strings.TrimPrefix(keyPath, rootKeysSubdir+"/")
|
||||
if strings.HasPrefix(keyPath, notary.RootKeysSubdir+"/") {
|
||||
keyIDAndGun = strings.TrimPrefix(keyPath, notary.RootKeysSubdir+"/")
|
||||
} else {
|
||||
keyIDAndGun = strings.TrimPrefix(keyPath, nonRootKeysSubdir+"/")
|
||||
keyIDAndGun = strings.TrimPrefix(keyPath, notary.NonRootKeysSubdir+"/")
|
||||
}
|
||||
|
||||
// Separate the ID and GUN (can be empty) from the filepath
|
||||
|
@ -206,6 +200,9 @@ func (s *KeyFileStore) RemoveKey(name string) error {
|
|||
// ExportKey exports the encrypted bytes from the keystore and writes it to
|
||||
// dest.
|
||||
func (s *KeyFileStore) ExportKey(name string) ([]byte, error) {
|
||||
if keyInfo, ok := s.keyInfoMap[name]; ok {
|
||||
name = filepath.Join(keyInfo.Gun, name)
|
||||
}
|
||||
keyBytes, _, err := getRawKey(s, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue