{"action":"created","comment":{"url":"https://api.github.com/repos/docker/notary/pulls/comments/34967760","id":34967760,"diff_hunk":"@@ -106,64 +110,122 @@ func (s *KeyMemoryStore) ListKeys() []string {\n \treturn listKeys(s)\n }\n \n-func addKey(s LimitedFileStore, name string, privKey data.PrivateKey) error {\n+// RemoveKey removes the key from the keystore\n+func (s *KeyMemoryStore) RemoveKey(name string) error {\n+\treturn removeKey(s, name)\n+}\n+\n+func addKey(s LimitedFileStore, passphraseRetriever PassphraseRetriever, name, alias string, privKey data.PrivateKey) error {\n \tpemPrivKey, err := KeyToPEM(privKey)\n \tif err != nil {\n \t\treturn err\n \t}\n \n-\treturn s.Add(name, pemPrivKey)\n-}\n-\n-func getKey(s LimitedFileStore, name string) (data.PrivateKey, error) {\n-\tkeyBytes, err := s.Get(name)\n-\tif err != nil {\n-\t\treturn nil, err\n+\tattempts := 0\n+\tpassphrase := \"\"\n+\tgiveup := false\n+\tfor {\n+\t\tpassphrase, giveup, err = passphraseRetriever(name, alias, true, attempts)\n+\t\tif err != nil {\n+\t\t\tattempts++\n+\t\t\tcontinue\n+\t\t}\n+\t\tif giveup {\n+\t\t\treturn errors.New(\"obtaining passphrase failed\")\n+\t\t}\n+\t\tif attempts > 10 {\n+\t\t\treturn errors.New(\"sanity check on number of passphrase attempts exceeded\")\n+\t\t}\n+\t\tbreak\n \t}\n \n-\t// Convert PEM encoded bytes back to a PrivateKey\n-\tprivKey, err := ParsePEMPrivateKey(keyBytes, \"\")\n-\tif err != nil {\n-\t\treturn nil, err\n+\tif passphrase != \"\" {\n+\t\tpemPrivKey, err = EncryptPrivateKey(privKey, passphrase)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n \t}\n \n-\treturn privKey, nil\n+\treturn s.Add(name+\"_\"+alias, pemPrivKey)\n }\n \n-func addEncryptedKey(s LimitedFileStore, name string, privKey data.PrivateKey, passphrase string) error {\n-\tencryptedPrivKey, err := EncryptPrivateKey(privKey, passphrase)\n-\tif err != nil {\n-\t\treturn err\n+func getKeyAlias(s LimitedFileStore, keyID string) (string, error) {\n+\tfiles := s.ListFiles(true)\n+\tname := strings.TrimSpace(strings.TrimSuffix(filepath.Base(keyID), filepath.Ext(keyID)))\n+\n+\tfor _, file := range files {\n+\t\tlastPathSeparator := strings.LastIndexAny(file, \"/\\\\\")\n+\t\tfilename := file[lastPathSeparator+1:]\n+\n+\t\tif strings.HasPrefix(filename, name) {\n+\t\t\taliasPlusDotKey := strings.TrimPrefix(filename, name+\"_\")\n+\t\t\tretVal := strings.TrimSuffix(aliasPlusDotKey, \".\"+keyExtension)\n+\t\t\treturn retVal, nil\n+\t\t}\n \t}\n \n-\treturn s.Add(name, encryptedPrivKey)\n+\treturn \"\", fmt.Errorf(\"keyId %s has no alias\", name)\n }\n \n-func getDecryptedKey(s LimitedFileStore, name string, passphrase string) (data.PrivateKey, error) {\n-\tkeyBytes, err := s.Get(name)\n+// GetKey returns the PrivateKey given a KeyID\n+func getKey(s LimitedFileStore, passphraseRetriever PassphraseRetriever, name string) (data.PrivateKey, error) {\n+\tkeyAlias, err := getKeyAlias(s, name)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n \n-\t// Gets an unencrypted PrivateKey.\n-\tprivKey, err := ParsePEMPrivateKey(keyBytes, passphrase)\n+\tkeyBytes, err := s.Get(name + \"_\" + keyAlias)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n \n+\t// See if the key is encrypted. If its encrypted we'll fail to parse the private key\n+\tprivKey, err := ParsePEMPrivateKey(keyBytes, \"\")\n+\tif err != nil {\n+\t\t// We need to decrypt the key, lets get a passphrase\n+\t\tattempts := 0\n+\t\tfor {","path":"trustmanager/keyfilestore.go","position":234,"original_position":234,"commit_id":"e125b7c262f45ca2dc4e2fbd4776d58d31959734","original_commit_id":"e125b7c262f45ca2dc4e2fbd4776d58d31959734","user":{"login":"endophage","id":1302091,"avatar_url":"https://avatars.githubusercontent.com/u/1302091?v=3","gravatar_id":"","url":"https://api.github.com/users/endophage","html_url":"https://github.com/endophage","followers_url":"https://api.github.com/users/endophage/followers","following_url":"https://api.github.com/users/endophage/following{/other_user}","gists_url":"https://api.github.com/users/endophage/gists{/gist_id}","starred_url":"https://api.github.com/users/endophage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/endophage/subscriptions","organizations_url":"https://api.github.com/users/endophage/orgs","repos_url":"https://api.github.com/users/endophage/repos","events_url":"https://api.github.com/users/endophage/events{/privacy}","received_events_url":"https://api.github.com/users/endophage/received_events","type":"User","site_admin":false},"body":"`for attempts := 0; ; attempts++ {`","created_at":"2015-07-20T04:09:50Z","updated_at":"2015-07-20T04:09:50Z","html_url":"https://github.com/docker/notary/pull/72#discussion_r34967760","pull_request_url":"https://api.github.com/repos/docker/notary/pulls/72","_links":{"self":{"href":"https://api.github.com/repos/docker/notary/pulls/comments/34967760"},"html":{"href":"https://github.com/docker/notary/pull/72#discussion_r34967760"},"pull_request":{"href":"https://api.github.com/repos/docker/notary/pulls/72"}}},"pull_request":{"url":"https://api.github.com/repos/docker/notary/pulls/72","id":40203982,"html_url":"https://github.com/docker/notary/pull/72","diff_url":"https://github.com/docker/notary/pull/72.diff","patch_url":"https://github.com/docker/notary/pull/72.patch","issue_url":"https://api.github.com/repos/docker/notary/issues/72","number":72,"state":"open","locked":false,"title":"Update keyfilestore to use passwordRetriever","user":{"login":"NathanMcCauley","id":695682,"avatar_url":"https://avatars.githubusercontent.com/u/695682?v=3","gravatar_id":"","url":"https://api.github.com/users/NathanMcCauley","html_url":"https://github.com/NathanMcCauley","followers_url":"https://api.github.com/users/NathanMcCauley/followers","following_url":"https://api.github.com/users/NathanMcCauley/following{/other_user}","gists_url":"https://api.github.com/users/NathanMcCauley/gists{/gist_id}","starred_url":"https://api.github.com/users/NathanMcCauley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NathanMcCauley/subscriptions","organizations_url":"https://api.github.com/users/NathanMcCauley/orgs","repos_url":"https://api.github.com/users/NathanMcCauley/repos","events_url":"https://api.github.com/users/NathanMcCauley/events{/privacy}","received_events_url":"https://api.github.com/users/NathanMcCauley/received_events","type":"User","site_admin":false},"body":"WIP, do not merge. Will need to rebase after #71 \r\n\r\nWill allow us to have a callback for password protection of all signing keys.\r\n\r\nCC @diogomonica @aaronlehmann \r\n\r\nSigned-off-by: Nathan McCauley <nathan.mccauley@docker.com>","created_at":"2015-07-17T06:38:46Z","updated_at":"2015-07-20T04:09:50Z","closed_at":null,"merged_at":null,"merge_commit_sha":"443d12355c0fec8e4e5f77c416008a2c29610052","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/docker/notary/pulls/72/commits","review_comments_url":"https://api.github.com/repos/docker/notary/pulls/72/comments","review_comment_url":"https://api.github.com/repos/docker/notary/pulls/comments{/number}","comments_url":"https://api.github.com/repos/docker/notary/issues/72/comments","statuses_url":"https://api.github.com/repos/docker/notary/statuses/e125b7c262f45ca2dc4e2fbd4776d58d31959734","head":{"label":"docker:passphrase_callbacks","ref":"passphrase_callbacks","sha":"e125b7c262f45ca2dc4e2fbd4776d58d31959734","user":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"repo":{"id":37743436,"name":"notary","full_name":"docker/notary","owner":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/docker/notary","description":"","fork":false,"url":"https://api.github.com/repos/docker/notary","forks_url":"https://api.github.com/repos/docker/notary/forks","keys_url":"https://api.github.com/repos/docker/notary/keys{/key_id}","collaborators_url":"https://api.github.com/repos/docker/notary/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/docker/notary/teams","hooks_url":"https://api.github.com/repos/docker/notary/hooks","issue_events_url":"https://api.github.com/repos/docker/notary/issues/events{/number}","events_url":"https://api.github.com/repos/docker/notary/events","assignees_url":"https://api.github.com/repos/docker/notary/assignees{/user}","branches_url":"https://api.github.com/repos/docker/notary/branches{/branch}","tags_url":"https://api.github.com/repos/docker/notary/tags","blobs_url":"https://api.github.com/repos/docker/notary/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/docker/notary/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/docker/notary/git/refs{/sha}","trees_url":"https://api.github.com/repos/docker/notary/git/trees{/sha}","statuses_url":"https://api.github.com/repos/docker/notary/statuses/{sha}","languages_url":"https://api.github.com/repos/docker/notary/languages","stargazers_url":"https://api.github.com/repos/docker/notary/stargazers","contributors_url":"https://api.github.com/repos/docker/notary/contributors","subscribers_url":"https://api.github.com/repos/docker/notary/subscribers","subscription_url":"https://api.github.com/repos/docker/notary/subscription","commits_url":"https://api.github.com/repos/docker/notary/commits{/sha}","git_commits_url":"https://api.github.com/repos/docker/notary/git/commits{/sha}","comments_url":"https://api.github.com/repos/docker/notary/comments{/number}","issue_comment_url":"https://api.github.com/repos/docker/notary/issues/comments{/number}","contents_url":"https://api.github.com/repos/docker/notary/contents/{+path}","compare_url":"https://api.github.com/repos/docker/notary/compare/{base}...{head}","merges_url":"https://api.github.com/repos/docker/notary/merges","archive_url":"https://api.github.com/repos/docker/notary/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/docker/notary/downloads","issues_url":"https://api.github.com/repos/docker/notary/issues{/number}","pulls_url":"https://api.github.com/repos/docker/notary/pulls{/number}","milestones_url":"https://api.github.com/repos/docker/notary/milestones{/number}","notifications_url":"https://api.github.com/repos/docker/notary/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/docker/notary/labels{/name}","releases_url":"https://api.github.com/repos/docker/notary/releases{/id}","created_at":"2015-06-19T20:07:53Z","updated_at":"2015-07-20T01:16:44Z","pushed_at":"2015-07-20T00:40:20Z","git_url":"git://github.com/docker/notary.git","ssh_url":"git@github.com:docker/notary.git","clone_url":"https://github.com/docker/notary.git","svn_url":"https://github.com/docker/notary","homepage":null,"size":20543,"stargazers_count":312,"watchers_count":312,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":22,"mirror_url":null,"open_issues_count":20,"forks":22,"open_issues":20,"watchers":312,"default_branch":"master"}},"base":{"label":"docker:master","ref":"master","sha":"9d31d343f3de7c61a6cb9fcb789f8b71d85d30c9","user":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"repo":{"id":37743436,"name":"notary","full_name":"docker/notary","owner":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/docker/notary","description":"","fork":false,"url":"https://api.github.com/repos/docker/notary","forks_url":"https://api.github.com/repos/docker/notary/forks","keys_url":"https://api.github.com/repos/docker/notary/keys{/key_id}","collaborators_url":"https://api.github.com/repos/docker/notary/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/docker/notary/teams","hooks_url":"https://api.github.com/repos/docker/notary/hooks","issue_events_url":"https://api.github.com/repos/docker/notary/issues/events{/number}","events_url":"https://api.github.com/repos/docker/notary/events","assignees_url":"https://api.github.com/repos/docker/notary/assignees{/user}","branches_url":"https://api.github.com/repos/docker/notary/branches{/branch}","tags_url":"https://api.github.com/repos/docker/notary/tags","blobs_url":"https://api.github.com/repos/docker/notary/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/docker/notary/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/docker/notary/git/refs{/sha}","trees_url":"https://api.github.com/repos/docker/notary/git/trees{/sha}","statuses_url":"https://api.github.com/repos/docker/notary/statuses/{sha}","languages_url":"https://api.github.com/repos/docker/notary/languages","stargazers_url":"https://api.github.com/repos/docker/notary/stargazers","contributors_url":"https://api.github.com/repos/docker/notary/contributors","subscribers_url":"https://api.github.com/repos/docker/notary/subscribers","subscription_url":"https://api.github.com/repos/docker/notary/subscription","commits_url":"https://api.github.com/repos/docker/notary/commits{/sha}","git_commits_url":"https://api.github.com/repos/docker/notary/git/commits{/sha}","comments_url":"https://api.github.com/repos/docker/notary/comments{/number}","issue_comment_url":"https://api.github.com/repos/docker/notary/issues/comments{/number}","contents_url":"https://api.github.com/repos/docker/notary/contents/{+path}","compare_url":"https://api.github.com/repos/docker/notary/compare/{base}...{head}","merges_url":"https://api.github.com/repos/docker/notary/merges","archive_url":"https://api.github.com/repos/docker/notary/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/docker/notary/downloads","issues_url":"https://api.github.com/repos/docker/notary/issues{/number}","pulls_url":"https://api.github.com/repos/docker/notary/pulls{/number}","milestones_url":"https://api.github.com/repos/docker/notary/milestones{/number}","notifications_url":"https://api.github.com/repos/docker/notary/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/docker/notary/labels{/name}","releases_url":"https://api.github.com/repos/docker/notary/releases{/id}","created_at":"2015-06-19T20:07:53Z","updated_at":"2015-07-20T01:16:44Z","pushed_at":"2015-07-20T00:40:20Z","git_url":"git://github.com/docker/notary.git","ssh_url":"git@github.com:docker/notary.git","clone_url":"https://github.com/docker/notary.git","svn_url":"https://github.com/docker/notary","homepage":null,"size":20543,"stargazers_count":312,"watchers_count":312,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":22,"mirror_url":null,"open_issues_count":20,"forks":22,"open_issues":20,"watchers":312,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/docker/notary/pulls/72"},"html":{"href":"https://github.com/docker/notary/pull/72"},"issue":{"href":"https://api.github.com/repos/docker/notary/issues/72"},"comments":{"href":"https://api.github.com/repos/docker/notary/issues/72/comments"},"review_comments":{"href":"https://api.github.com/repos/docker/notary/pulls/72/comments"},"review_comment":{"href":"https://api.github.com/repos/docker/notary/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/docker/notary/pulls/72/commits"},"statuses":{"href":"https://api.github.com/repos/docker/notary/statuses/e125b7c262f45ca2dc4e2fbd4776d58d31959734"}}}}