{"action":"created","comment":{"url":"https://api.github.com/repos/containers/image/pulls/comments/137899779","pull_request_review_id":61633825,"id":137899779,"diff_hunk":"@@ -0,0 +1,281 @@\n+package config\n+\n+import (\n+\t\"encoding/base64\"\n+\t\"encoding/json\"\n+\t\"fmt\"\n+\t\"io/ioutil\"\n+\t\"os\"\n+\t\"path/filepath\"\n+\t\"strings\"\n+\n+\thelperclient \"github.com/docker/docker-credential-helpers/client\"\n+\t\"github.com/docker/docker-credential-helpers/credentials\"\n+\t\"github.com/docker/docker/pkg/homedir\"\n+\t\"github.com/pkg/errors\"\n+)\n+\n+type dockerAuthConfig struct {\n+\tAuth string `json:\"auth,omitempty\"`\n+}\n+\n+type dockerConfigFile struct {\n+\tAuthConfigs map[string]dockerAuthConfig `json:\"auths\"`\n+\tCredHelpers map[string]string           `json:\"credHelpers,omitempty\"`\n+}\n+\n+const (\n+\tdockerHostname = \"docker.io\"\n+\tdockerRegistry = \"registry-1.docker.io\"\n+\n+\tauthCfg           = \".config/containers\"\n+\tauthCfgFileName   = \"auth.json\"\n+\tdockerCfg         = \".docker\"\n+\tdockerCfgFileName = \"config.json\"\n+\n+\taddAuth    = \"add\"\n+\tdeleteAuth = \"delete\"\n+)\n+\n+var (\n+\t// ErrNotLoggedIn is returned for users not logged into a registry\n+\t// that they are trying to logout of\n+\tErrNotLoggedIn = errors.Errorf(\"not logged in\")\n+)\n+\n+// SetAuthentication stores the username and password in the auth.json file\n+func SetAuthentication(registry, username, password string) error {\n+\tregistry = DockerHostNameToRegistry(registry)\n+\tpath, err := getPathToAuth()\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\treturn modifyJSON(registry, username, password, path, addAuth)\n+}\n+\n+// GetAuthentication returns the registry credentials stored in\n+// either auth.json file or .docker/config.json\n+func GetAuthentication(registry string) (string, string, error) {\n+\tregistry = DockerHostNameToRegistry(registry)\n+\t// Check auth.json file first\n+\tpath, err := getPathToAuth()\n+\tif err != nil {\n+\t\treturn \"\", \"\", errors.Wrapf(err, \"error getting path to auth file\")\n+\t}\n+\tusername, password, err := findAuthentication(registry, path)\n+\tif err != nil {\n+\t\treturn \"\", \"\", err\n+\t}\n+\tif username != \"\" && password != \"\" {\n+\t\treturn username, password, nil\n+\t}\n+\n+\t// check ~/.docker/config.json as secondary option\n+\tpath = filepath.Join(homedir.Get(), dockerCfg, dockerCfgFileName)\n+\tusername, password, err = findAuthentication(registry, path)\n+\tif err != nil {\n+\t\treturn \"\", \"\", err\n+\t}\n+\treturn username, password, nil\n+}\n+\n+// RemoveAuthentication deletes the credentials stored in auth.json\n+func RemoveAuthentication(registry string) error {\n+\tregistry = DockerHostNameToRegistry(registry)\n+\tpath, err := getPathToAuth()\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\treturn modifyJSON(registry, \"\", \"\", path, deleteAuth)\n+}\n+\n+// DockerHostNameToRegistry converts the docker hostname to the docker registry\n+func DockerHostNameToRegistry(registry string) string {\n+\tif registry == dockerHostname {\n+\t\treturn dockerRegistry\n+\t}\n+\treturn registry\n+}\n+\n+// getPath gets the path of the auth.json file since the user can set their own path\n+// by setting CRIO_AUTH_PATH\n+// If CRIO_AUTH_PATH is not set, the default path is ~/.config/containers/auth.json\n+func getPathToAuth() (string, error) {\n+\tif os.Getenv(\"CRIO_AUTH_PATH\") != \"\" {\n+\t\treturn filepath.Join(os.Getenv(\"CRIO_AUTH_PATH\"), authCfgFileName), nil\n+\t}\n+\tdir := filepath.Join(homedir.Get(), authCfg)\n+\tif _, err := os.Stat(dir); os.IsNotExist(err) {\n+\t\tif err = os.Mkdir(dir, 0700); err != nil {\n+\t\t\treturn \"\", errors.Wrapf(err, \"error creating directory %q\", dir)\n+\t\t}\n+\t}\n+\treturn filepath.Join(dir, authCfgFileName), nil\n+}\n+\n+// readJSONFile unmarshals the authentications stored in the auth.json file and returns it\n+// or returns an empty dockerConfigFile data structure if auth.json is empty\n+func readJSONFile(path string) (dockerConfigFile, error) {\n+\tvar auths dockerConfigFile\n+\n+\traw, err := ioutil.ReadFile(path)\n+\tif os.IsNotExist(err) {\n+\t\tauths.AuthConfigs = map[string]dockerAuthConfig{}\n+\t\treturn auths, nil\n+\t}\n+\n+\tif err = json.Unmarshal(raw, &auths); err != nil {\n+\t\treturn dockerConfigFile{}, errors.Wrapf(err, \"error unmarshaling JSON at %q\", path)\n+\t}\n+\n+\treturn auths, nil\n+}\n+\n+// addAuthToJSON either adds a new registry url with its encoded user:pass\n+// or modifies an existing registry url\n+func modifyJSON(registry, username, password, path, action string) error {\n+\tauths, err := readJSONFile(path)\n+\tif err != nil {\n+\t\treturn errors.Wrapf(err, \"error reading JSON file %q\", path)\n+\t}\n+\n+\tswitch action {\n+\tcase addAuth:\n+\t\tif len(auths.CredHelpers) > 0 {","path":"pkg/docker/config/config.go","position":144,"original_position":144,"commit_id":"c740ce29090f8656db311efc564eeb495ea3ea86","original_commit_id":"c740ce29090f8656db311efc564eeb495ea3ea86","user":{"login":"mtrmac","id":7052838,"avatar_url":"https://avatars2.githubusercontent.com/u/7052838?v=4","gravatar_id":"","url":"https://api.github.com/users/mtrmac","html_url":"https://github.com/mtrmac","followers_url":"https://api.github.com/users/mtrmac/followers","following_url":"https://api.github.com/users/mtrmac/following{/other_user}","gists_url":"https://api.github.com/users/mtrmac/gists{/gist_id}","starred_url":"https://api.github.com/users/mtrmac/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mtrmac/subscriptions","organizations_url":"https://api.github.com/users/mtrmac/orgs","repos_url":"https://api.github.com/users/mtrmac/repos","events_url":"https://api.github.com/users/mtrmac/events{/privacy}","received_events_url":"https://api.github.com/users/mtrmac/received_events","type":"User","site_admin":false},"body":"Shouldn’t this also be\r\n```go\r\nif ch, exists := auths.CredHelpers[registry]; exists\r\n```\r\n?","created_at":"2017-09-08T22:08:44Z","updated_at":"2017-09-08T22:22:57Z","html_url":"https://github.com/containers/image/pull/333#discussion_r137899779","pull_request_url":"https://api.github.com/repos/containers/image/pulls/333","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/containers/image/pulls/comments/137899779"},"html":{"href":"https://github.com/containers/image/pull/333#discussion_r137899779"},"pull_request":{"href":"https://api.github.com/repos/containers/image/pulls/333"}}},"pull_request":{"url":"https://api.github.com/repos/containers/image/pulls/333","id":138215966,"html_url":"https://github.com/containers/image/pull/333","diff_url":"https://github.com/containers/image/pull/333.diff","patch_url":"https://github.com/containers/image/pull/333.patch","issue_url":"https://api.github.com/repos/containers/image/issues/333","number":333,"state":"open","locked":false,"title":"Add authentication package to containers/image/pkg","user":{"login":"umohnani8","id":29377163,"avatar_url":"https://avatars0.githubusercontent.com/u/29377163?v=4","gravatar_id":"","url":"https://api.github.com/users/umohnani8","html_url":"https://github.com/umohnani8","followers_url":"https://api.github.com/users/umohnani8/followers","following_url":"https://api.github.com/users/umohnani8/following{/other_user}","gists_url":"https://api.github.com/users/umohnani8/gists{/gist_id}","starred_url":"https://api.github.com/users/umohnani8/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/umohnani8/subscriptions","organizations_url":"https://api.github.com/users/umohnani8/orgs","repos_url":"https://api.github.com/users/umohnani8/repos","events_url":"https://api.github.com/users/umohnani8/events{/privacy}","received_events_url":"https://api.github.com/users/umohnani8/received_events","type":"User","site_admin":false},"body":"This package is used in authenticating a user for kpod login\r\nand can be used for authentication in kpod push, pull etc.\r\n\r\nSigned-off-by: umohnani8 <umohnani@redhat.com>","created_at":"2017-08-29T15:37:44Z","updated_at":"2017-09-08T22:22:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c433c248ed8df161d34c862a04e993ae7d136e16","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/containers/image/pulls/333/commits","review_comments_url":"https://api.github.com/repos/containers/image/pulls/333/comments","review_comment_url":"https://api.github.com/repos/containers/image/pulls/comments{/number}","comments_url":"https://api.github.com/repos/containers/image/issues/333/comments","statuses_url":"https://api.github.com/repos/containers/image/statuses/c740ce29090f8656db311efc564eeb495ea3ea86","head":{"label":"umohnani8:authentication","ref":"authentication","sha":"c740ce29090f8656db311efc564eeb495ea3ea86","user":{"login":"umohnani8","id":29377163,"avatar_url":"https://avatars0.githubusercontent.com/u/29377163?v=4","gravatar_id":"","url":"https://api.github.com/users/umohnani8","html_url":"https://github.com/umohnani8","followers_url":"https://api.github.com/users/umohnani8/followers","following_url":"https://api.github.com/users/umohnani8/following{/other_user}","gists_url":"https://api.github.com/users/umohnani8/gists{/gist_id}","starred_url":"https://api.github.com/users/umohnani8/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/umohnani8/subscriptions","organizations_url":"https://api.github.com/users/umohnani8/orgs","repos_url":"https://api.github.com/users/umohnani8/repos","events_url":"https://api.github.com/users/umohnani8/events{/privacy}","received_events_url":"https://api.github.com/users/umohnani8/received_events","type":"User","site_admin":false},"repo":{"id":97640905,"name":"image","full_name":"umohnani8/image","owner":{"login":"umohnani8","id":29377163,"avatar_url":"https://avatars0.githubusercontent.com/u/29377163?v=4","gravatar_id":"","url":"https://api.github.com/users/umohnani8","html_url":"https://github.com/umohnani8","followers_url":"https://api.github.com/users/umohnani8/followers","following_url":"https://api.github.com/users/umohnani8/following{/other_user}","gists_url":"https://api.github.com/users/umohnani8/gists{/gist_id}","starred_url":"https://api.github.com/users/umohnani8/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/umohnani8/subscriptions","organizations_url":"https://api.github.com/users/umohnani8/orgs","repos_url":"https://api.github.com/users/umohnani8/repos","events_url":"https://api.github.com/users/umohnani8/events{/privacy}","received_events_url":"https://api.github.com/users/umohnani8/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/umohnani8/image","description":"Work with containers' images","fork":true,"url":"https://api.github.com/repos/umohnani8/image","forks_url":"https://api.github.com/repos/umohnani8/image/forks","keys_url":"https://api.github.com/repos/umohnani8/image/keys{/key_id}","collaborators_url":"https://api.github.com/repos/umohnani8/image/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/umohnani8/image/teams","hooks_url":"https://api.github.com/repos/umohnani8/image/hooks","issue_events_url":"https://api.github.com/repos/umohnani8/image/issues/events{/number}","events_url":"https://api.github.com/repos/umohnani8/image/events","assignees_url":"https://api.github.com/repos/umohnani8/image/assignees{/user}","branches_url":"https://api.github.com/repos/umohnani8/image/branches{/branch}","tags_url":"https://api.github.com/repos/umohnani8/image/tags","blobs_url":"https://api.github.com/repos/umohnani8/image/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/umohnani8/image/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/umohnani8/image/git/refs{/sha}","trees_url":"https://api.github.com/repos/umohnani8/image/git/trees{/sha}","statuses_url":"https://api.github.com/repos/umohnani8/image/statuses/{sha}","languages_url":"https://api.github.com/repos/umohnani8/image/languages","stargazers_url":"https://api.github.com/repos/umohnani8/image/stargazers","contributors_url":"https://api.github.com/repos/umohnani8/image/contributors","subscribers_url":"https://api.github.com/repos/umohnani8/image/subscribers","subscription_url":"https://api.github.com/repos/umohnani8/image/subscription","commits_url":"https://api.github.com/repos/umohnani8/image/commits{/sha}","git_commits_url":"https://api.github.com/repos/umohnani8/image/git/commits{/sha}","comments_url":"https://api.github.com/repos/umohnani8/image/comments{/number}","issue_comment_url":"https://api.github.com/repos/umohnani8/image/issues/comments{/number}","contents_url":"https://api.github.com/repos/umohnani8/image/contents/{+path}","compare_url":"https://api.github.com/repos/umohnani8/image/compare/{base}...{head}","merges_url":"https://api.github.com/repos/umohnani8/image/merges","archive_url":"https://api.github.com/repos/umohnani8/image/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/umohnani8/image/downloads","issues_url":"https://api.github.com/repos/umohnani8/image/issues{/number}","pulls_url":"https://api.github.com/repos/umohnani8/image/pulls{/number}","milestones_url":"https://api.github.com/repos/umohnani8/image/milestones{/number}","notifications_url":"https://api.github.com/repos/umohnani8/image/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/umohnani8/image/labels{/name}","releases_url":"https://api.github.com/repos/umohnani8/image/releases{/id}","deployments_url":"https://api.github.com/repos/umohnani8/image/deployments","created_at":"2017-07-18T20:27:23Z","updated_at":"2017-07-18T20:27:25Z","pushed_at":"2017-09-08T17:25:22Z","git_url":"git://github.com/umohnani8/image.git","ssh_url":"git@github.com:umohnani8/image.git","clone_url":"https://github.com/umohnani8/image.git","svn_url":"https://github.com/umohnani8/image","homepage":"","size":6369,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"containers:master","ref":"master","sha":"9d8632df37317401eae8cefff1f6d90198413a9f","user":{"login":"containers","id":5874934,"avatar_url":"https://avatars3.githubusercontent.com/u/5874934?v=4","gravatar_id":"","url":"https://api.github.com/users/containers","html_url":"https://github.com/containers","followers_url":"https://api.github.com/users/containers/followers","following_url":"https://api.github.com/users/containers/following{/other_user}","gists_url":"https://api.github.com/users/containers/gists{/gist_id}","starred_url":"https://api.github.com/users/containers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/containers/subscriptions","organizations_url":"https://api.github.com/users/containers/orgs","repos_url":"https://api.github.com/users/containers/repos","events_url":"https://api.github.com/users/containers/events{/privacy}","received_events_url":"https://api.github.com/users/containers/received_events","type":"Organization","site_admin":false},"repo":{"id":61739430,"name":"image","full_name":"containers/image","owner":{"login":"containers","id":5874934,"avatar_url":"https://avatars3.githubusercontent.com/u/5874934?v=4","gravatar_id":"","url":"https://api.github.com/users/containers","html_url":"https://github.com/containers","followers_url":"https://api.github.com/users/containers/followers","following_url":"https://api.github.com/users/containers/following{/other_user}","gists_url":"https://api.github.com/users/containers/gists{/gist_id}","starred_url":"https://api.github.com/users/containers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/containers/subscriptions","organizations_url":"https://api.github.com/users/containers/orgs","repos_url":"https://api.github.com/users/containers/repos","events_url":"https://api.github.com/users/containers/events{/privacy}","received_events_url":"https://api.github.com/users/containers/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/containers/image","description":"Work with containers' images","fork":false,"url":"https://api.github.com/repos/containers/image","forks_url":"https://api.github.com/repos/containers/image/forks","keys_url":"https://api.github.com/repos/containers/image/keys{/key_id}","collaborators_url":"https://api.github.com/repos/containers/image/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/containers/image/teams","hooks_url":"https://api.github.com/repos/containers/image/hooks","issue_events_url":"https://api.github.com/repos/containers/image/issues/events{/number}","events_url":"https://api.github.com/repos/containers/image/events","assignees_url":"https://api.github.com/repos/containers/image/assignees{/user}","branches_url":"https://api.github.com/repos/containers/image/branches{/branch}","tags_url":"https://api.github.com/repos/containers/image/tags","blobs_url":"https://api.github.com/repos/containers/image/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/containers/image/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/containers/image/git/refs{/sha}","trees_url":"https://api.github.com/repos/containers/image/git/trees{/sha}","statuses_url":"https://api.github.com/repos/containers/image/statuses/{sha}","languages_url":"https://api.github.com/repos/containers/image/languages","stargazers_url":"https://api.github.com/repos/containers/image/stargazers","contributors_url":"https://api.github.com/repos/containers/image/contributors","subscribers_url":"https://api.github.com/repos/containers/image/subscribers","subscription_url":"https://api.github.com/repos/containers/image/subscription","commits_url":"https://api.github.com/repos/containers/image/commits{/sha}","git_commits_url":"https://api.github.com/repos/containers/image/git/commits{/sha}","comments_url":"https://api.github.com/repos/containers/image/comments{/number}","issue_comment_url":"https://api.github.com/repos/containers/image/issues/comments{/number}","contents_url":"https://api.github.com/repos/containers/image/contents/{+path}","compare_url":"https://api.github.com/repos/containers/image/compare/{base}...{head}","merges_url":"https://api.github.com/repos/containers/image/merges","archive_url":"https://api.github.com/repos/containers/image/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/containers/image/downloads","issues_url":"https://api.github.com/repos/containers/image/issues{/number}","pulls_url":"https://api.github.com/repos/containers/image/pulls{/number}","milestones_url":"https://api.github.com/repos/containers/image/milestones{/number}","notifications_url":"https://api.github.com/repos/containers/image/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/containers/image/labels{/name}","releases_url":"https://api.github.com/repos/containers/image/releases{/id}","deployments_url":"https://api.github.com/repos/containers/image/deployments","created_at":"2016-06-22T17:46:41Z","updated_at":"2017-09-05T05:39:26Z","pushed_at":"2017-09-08T18:06:38Z","git_url":"git://github.com/containers/image.git","ssh_url":"git@github.com:containers/image.git","clone_url":"https://github.com/containers/image.git","svn_url":"https://github.com/containers/image","homepage":"","size":3450,"stargazers_count":89,"watchers_count":89,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":51,"mirror_url":null,"open_issues_count":49,"forks":51,"open_issues":49,"watchers":89,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/containers/image/pulls/333"},"html":{"href":"https://github.com/containers/image/pull/333"},"issue":{"href":"https://api.github.com/repos/containers/image/issues/333"},"comments":{"href":"https://api.github.com/repos/containers/image/issues/333/comments"},"review_comments":{"href":"https://api.github.com/repos/containers/image/pulls/333/comments"},"review_comment":{"href":"https://api.github.com/repos/containers/image/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/containers/image/pulls/333/commits"},"statuses":{"href":"https://api.github.com/repos/containers/image/statuses/c740ce29090f8656db311efc564eeb495ea3ea86"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"mtrmac"}}