-
Notifications
You must be signed in to change notification settings - Fork 510
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
Notary repository lazy initialization #1105
Changes from all commits
c046614
9a4d4b8
d11ad89
dd059a6
e556e94
5a9636a
52a07d3
1da4d7f
4182ca0
afc4985
1702384
6a1f1c1
2e8d5ad
9ff8415
73f678c
556e2b4
7e74573
d6ba9f9
dd1ef8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,7 @@ func (t *tufCommander) AddToCommand(cmd *cobra.Command) { | |
cmd.AddCommand(cmdReset) | ||
|
||
cmd.AddCommand(cmdTUFPublishTemplate.ToCommand(t.tufPublish)) | ||
|
||
cmd.AddCommand(cmdTUFLookupTemplate.ToCommand(t.tufLookup)) | ||
|
||
cmdTUFList := cmdTUFListTemplate.ToCommand(t.tufList) | ||
|
@@ -385,6 +386,35 @@ func (t *tufCommander) tufDeleteGUN(cmd *cobra.Command, args []string) error { | |
return nil | ||
} | ||
|
||
func importRootKey(cmd *cobra.Command, rootKey string, nRepo *notaryclient.NotaryRepository, retriever notary.PassRetriever) ([]string, error) { | ||
var rootKeyList []string | ||
|
||
if rootKey != "" { | ||
privKey, err := readKey(data.CanonicalRootRole, rootKey, retriever) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = nRepo.CryptoService.AddKey(data.CanonicalRootRole, "", privKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error importing key: %v", err) | ||
} | ||
rootKeyList = []string{privKey.ID()} | ||
} else { | ||
rootKeyList = nRepo.CryptoService.ListKeys(data.CanonicalRootRole) | ||
} | ||
|
||
if len(rootKeyList) > 0 { | ||
// Chooses the first root key available, which is initialization specific | ||
// but should return the HW one first. | ||
rootKeyID := rootKeyList[0] | ||
cmd.Printf("Root key found, using: %s\n", rootKeyID) | ||
|
||
return []string{rootKeyID}, nil | ||
} | ||
|
||
return []string{}, nil | ||
} | ||
|
||
func (t *tufCommander) tufInit(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
cmd.Usage() | ||
|
@@ -413,38 +443,12 @@ func (t *tufCommander) tufInit(cmd *cobra.Command, args []string) error { | |
return err | ||
} | ||
|
||
var rootKeyList []string | ||
|
||
if t.rootKey != "" { | ||
privKey, err := readKey(data.CanonicalRootRole, t.rootKey, t.retriever) | ||
if err != nil { | ||
return err | ||
} | ||
err = nRepo.CryptoService.AddKey(data.CanonicalRootRole, "", privKey) | ||
if err != nil { | ||
return fmt.Errorf("Error importing key: %v", err) | ||
} | ||
rootKeyList = []string{privKey.ID()} | ||
} else { | ||
rootKeyList = nRepo.CryptoService.ListKeys(data.CanonicalRootRole) | ||
} | ||
|
||
var rootKeyID string | ||
if len(rootKeyList) < 1 { | ||
cmd.Println("No root keys found. Generating a new root key...") | ||
rootPublicKey, err := nRepo.CryptoService.Create(data.CanonicalRootRole, "", data.ECDSAKey) | ||
if err != nil { | ||
return err | ||
} | ||
rootKeyID = rootPublicKey.ID() | ||
} else { | ||
// Chooses the first root key available, which is initialization specific | ||
// but should return the HW one first. | ||
rootKeyID = rootKeyList[0] | ||
cmd.Printf("Root key found, using: %s\n", rootKeyID) | ||
rootKeyIDs, err := importRootKey(cmd, t.rootKey, nRepo, t.retriever) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = nRepo.Initialize([]string{rootKeyID}); err != nil { | ||
if err = nRepo.Initialize(rootKeyIDs); err != nil { | ||
return err | ||
} | ||
|
||
|
@@ -686,7 +690,7 @@ func (t *tufCommander) tufPublish(cmd *cobra.Command, args []string) error { | |
return err | ||
} | ||
|
||
return publishAndPrintToCLI(cmd, nRepo, gun) | ||
return publishAndPrintToCLI(cmd, nRepo) | ||
} | ||
|
||
func (t *tufCommander) tufRemove(cmd *cobra.Command, args []string) error { | ||
|
@@ -1031,14 +1035,14 @@ func maybeAutoPublish(cmd *cobra.Command, doPublish bool, gun data.GUN, config * | |
return err | ||
} | ||
|
||
cmd.Println("Auto-publishing changes to", gun) | ||
return publishAndPrintToCLI(cmd, nRepo, gun) | ||
cmd.Println("Auto-publishing changes to", nRepo.GetGUN()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah you're right, the getter isn't needed - for some reason thought I had seen it somewhere else 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Providing a different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need the getter because we use the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about the case where this function might be called elsewhere in the same package, we have no "contract" on the fact that the gun in the argument is the same as the one used for this repo. It's just less error-prone if we don't allow someone to provide an unrelated gun. For now it's only used properly because we get it from this same repo but people might call it in a different way.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the gun passed in to the constructor of the repo, wouldn't that gun and the gun on the repo by contract of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update: unless you mean the contract between the gun and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the contract at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok, that makes sense. |
||
return publishAndPrintToCLI(cmd, nRepo) | ||
} | ||
|
||
func publishAndPrintToCLI(cmd *cobra.Command, nRepo *notaryclient.NotaryRepository, gun data.GUN) error { | ||
func publishAndPrintToCLI(cmd *cobra.Command, nRepo *notaryclient.NotaryRepository) error { | ||
if err := nRepo.Publish(); err != nil { | ||
return err | ||
} | ||
cmd.Printf("Successfully published changes for repository %s\n", gun) | ||
cmd.Printf("Successfully published changes for repository %s\n", nRepo.GetGUN()) | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great cleanup! Could we add some debug logs for these cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(fixed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: I'm wondering if this should be logged as
Info
so that users can see it. Mainly because while trying this out, I'm seeing something like:And there's not a lot of difference between "passphrase for root key" and "passphrase for new targets key", etc. It may be useful for users to see a note about what's going on, instead of wondering why they're being asked for all these passphrases (since it's hard to distinguish between the output of the lazy init vs publishing an actual change).
Possibly the log message could be "No TUF data found locally or remotely - initializing repository %s for the first time" or something? (also totally cool with "from scratch", but "first time" might make it more clear to users since we document how notary generates keys when initializing repos for the first time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good point, I didn't realize how disturbing it might be regarding the passphrases.
Will fix!