Skip to content

Commit

Permalink
Move private keys retrieval from a crypto service to a helper function
Browse files Browse the repository at this point in the history
Signed-off-by: Nassim 'Nass' Eddequiouaq <[email protected]>
  • Loading branch information
n4ss committed Mar 2, 2017
1 parent fd3ad27 commit 348cde4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
31 changes: 6 additions & 25 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,10 @@ func rootCertKey(gun data.GUN, privKey data.PrivateKey) (data.PublicKey, error)
// result is only stored on local disk, not published to the server. To do that,
// use r.Publish() eventually.
func (r *NotaryRepository) Initialize(rootKeyIDs []string, serverManagedRoles ...data.RoleName) error {
privKeys := make([]data.PrivateKey, 0, len(rootKeyIDs))
for _, keyID := range rootKeyIDs {
privKey, _, err := r.CryptoService.GetPrivateKey(keyID)
if err != nil {
return err
}
privKeys = append(privKeys, privKey)
}
if len(privKeys) == 0 {
var rootKeyID string
rootKeyList := r.CryptoService.ListKeys(data.CanonicalRootRole)
if len(rootKeyList) == 0 {
rootPublicKey, err := r.CryptoService.Create(data.CanonicalRootRole, "", data.ECDSAKey)
if err != nil {
return err
}
rootKeyID = rootPublicKey.ID()
} else {
rootKeyID = rootKeyList[0]
}
privKey, _, err := r.CryptoService.GetPrivateKey(rootKeyID)
if err != nil {
return err
}
privKeys = append(privKeys, privKey)

privKeys, err := getAllPrivKeys(rootKeyIDs, r.CryptoService)
if err != nil {
return err
}

// currently we only support server managing timestamps and snapshots, and
Expand Down Expand Up @@ -872,6 +851,8 @@ func (r *NotaryRepository) initializeFromCache() error {
err.Error())
return err
}

return nil
}

// saveMetadata saves contents of r.tufRepo onto the local disk, creating
Expand Down
37 changes: 36 additions & 1 deletion client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"time"

"github.com/Sirupsen/logrus"
"github.com/docker/distribution/registry/storage/cache"
"github.com/docker/notary/client/changelist"
store "github.com/docker/notary/storage"
"github.com/docker/notary/tuf"
"github.com/docker/notary/tuf/data"
"github.com/docker/notary/tuf/signed"
"github.com/docker/notary/tuf/utils"
)

Expand Down Expand Up @@ -290,3 +290,38 @@ func isMetaCached(cache store.MetadataStore) (bool, error) {

return false, nil
}

func getAllPrivKeys(rootKeyIDs []string, cryptoService signed.CryptoService) ([]data.PrivateKey, error) {
if cryptoService == nil {
return nil, fmt.Errorf("no crypto service available to get private keys from")
}

privKeys := make([]data.PrivateKey, 0, len(rootKeyIDs))
for _, keyID := range rootKeyIDs {
privKey, _, err := cryptoService.GetPrivateKey(keyID)
if err != nil {
return nil, err
}
privKeys = append(privKeys, privKey)
}
if len(privKeys) == 0 {
var rootKeyID string
rootKeyList := cryptoService.ListKeys(data.CanonicalRootRole)
if len(rootKeyList) == 0 {
rootPublicKey, err := cryptoService.Create(data.CanonicalRootRole, "", data.ECDSAKey)
if err != nil {
return nil, err
}
rootKeyID = rootPublicKey.ID()
} else {
rootKeyID = rootKeyList[0]
}
privKey, _, err := cryptoService.GetPrivateKey(rootKeyID)
if err != nil {
return nil, err
}
privKeys = append(privKeys, privKey)
}

return privKeys, nil
}

0 comments on commit 348cde4

Please sign in to comment.