{"action":"created","comment":{"url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/comments/128851399","pull_request_review_id":51567986,"id":128851399,"diff_hunk":"@@ -0,0 +1,169 @@\n+package io.jenkins.blueocean.credential;\n+\n+import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;\n+import com.cloudbees.plugins.credentials.Credentials;\n+import com.cloudbees.plugins.credentials.CredentialsProvider;\n+import com.cloudbees.plugins.credentials.CredentialsScope;\n+import com.cloudbees.plugins.credentials.CredentialsStore;\n+import com.cloudbees.plugins.credentials.domains.Domain;\n+import com.google.common.base.Preconditions;\n+import hudson.model.User;\n+import hudson.remoting.Base64;\n+import io.jenkins.blueocean.commons.ServiceException;\n+import java.io.IOException;\n+import java.security.KeyFactory;\n+import java.security.KeyPair;\n+import java.security.NoSuchAlgorithmException;\n+import java.security.interfaces.RSAPrivateCrtKey;\n+import java.security.interfaces.RSAPrivateKey;\n+import java.security.interfaces.RSAPublicKey;\n+import java.security.spec.InvalidKeySpecException;\n+import java.security.spec.PKCS8EncodedKeySpec;\n+import java.security.spec.RSAPublicKeySpec;\n+import java.util.List;\n+import javax.annotation.CheckForNull;\n+import javax.annotation.Nonnull;\n+import jenkins.model.Jenkins;\n+import org.kohsuke.accmod.Restricted;\n+import org.kohsuke.accmod.restrictions.NoExternalUse;\n+\n+/**\n+ * Helper to deal with managing a Jenkins-created per-user SSH key\n+ * @author kzantow\n+ */\n+@Restricted(NoExternalUse.class)\n+public class UserSSHKeyManager {\n+    private static final int KEY_SIZE = 2048;\n+    private static final String BLUEOCEAN_GENERATED_SSH_KEY_ID = \"jenkins-generated-ssh-key\";\n+    private static final String BLUEOCEAN_DOMAIN_NAME = \"blueocean-git-domain\";\n+    private static final Domain BLUEOCEAN_DOMAIN = new Domain(BLUEOCEAN_DOMAIN_NAME, null, null);\n+    \n+    /**\n+     * Gets the existing generated SSH key for the user or creates one and\n+     * returns it in the user's credential store\n+     * @param user owner of the key\n+     * @return the user's personal private key\n+     */\n+    public static @Nonnull BasicSSHUserPrivateKey getOrCreate(@Nonnull User user) {\n+        Preconditions.checkNotNull(user);\n+        \n+        CredentialsStore store = getUserStore(user);\n+        if(store == null){\n+            throw new ServiceException.ForbiddenException(String.format(\"Logged in user: %s doesn't have writable credentials store\", user.getId()));\n+        }\n+        // try to find the right key\n+        for (Credentials cred : store.getCredentials(BLUEOCEAN_DOMAIN)) {\n+            if (cred instanceof BasicSSHUserPrivateKey) {\n+                BasicSSHUserPrivateKey sshKey = (BasicSSHUserPrivateKey)cred;\n+                if (BLUEOCEAN_GENERATED_SSH_KEY_ID.equals(sshKey.getId())) {\n+                    return sshKey;\n+                }\n+            }\n+        }\n+        // if none found, create one\n+        BasicSSHUserPrivateKey key;\n+        try {\n+            // create one!\n+            KeyPair keyPair = SSHKeyUtils.generateRSAKey(KEY_SIZE);\n+            RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();\n+            String id_rsa = Base64.encode(privateKey.getEncoded());\n+            BasicSSHUserPrivateKey.DirectEntryPrivateKeySource keySource = new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(id_rsa);\n+            key = new BasicSSHUserPrivateKey(CredentialsScope.USER, BLUEOCEAN_GENERATED_SSH_KEY_ID, user.getId(), keySource, null, BLUEOCEAN_GENERATED_SSH_KEY_ID);\n+            store.addCredentials(BLUEOCEAN_DOMAIN, key);","path":"blueocean-pipeline-scm-api/src/main/java/io/jenkins/blueocean/credential/UserSSHKeyManager.java","position":72,"original_position":72,"commit_id":"5f0fdd72cf11478e47c24a73e70cd5a7e4405e35","original_commit_id":"5f0fdd72cf11478e47c24a73e70cd5a7e4405e35","user":{"login":"vivek","id":38139,"avatar_url":"https://avatars1.githubusercontent.com/u/38139?v=4","gravatar_id":"","url":"https://api.github.com/users/vivek","html_url":"https://github.com/vivek","followers_url":"https://api.github.com/users/vivek/followers","following_url":"https://api.github.com/users/vivek/following{/other_user}","gists_url":"https://api.github.com/users/vivek/gists{/gist_id}","starred_url":"https://api.github.com/users/vivek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vivek/subscriptions","organizations_url":"https://api.github.com/users/vivek/orgs","repos_url":"https://api.github.com/users/vivek/repos","events_url":"https://api.github.com/users/vivek/events{/privacy}","received_events_url":"https://api.github.com/users/vivek/received_events","type":"User","site_admin":false},"body":"Where is domain BLUEOCEAN_DOMAIN getting created? Look at [CredentialApi.create()](https://github.com/jenkinsci/blueocean-plugin/blob/a986f24023c4192f94ce6fc5b28b2f4b1e7b1626/blueocean-pipeline-api-impl/src/main/java/io/jenkins/blueocean/rest/impl/pipeline/credential/CredentialApi.java#L55). but we should not create these APIs when we already have one.","created_at":"2017-07-21T20:11:30Z","updated_at":"2017-07-21T20:14:55Z","html_url":"https://github.com/jenkinsci/blueocean-plugin/pull/1262#discussion_r128851399","pull_request_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262","_links":{"self":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/comments/128851399"},"html":{"href":"https://github.com/jenkinsci/blueocean-plugin/pull/1262#discussion_r128851399"},"pull_request":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262"}}},"pull_request":{"url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262","id":131421516,"html_url":"https://github.com/jenkinsci/blueocean-plugin/pull/1262","diff_url":"https://github.com/jenkinsci/blueocean-plugin/pull/1262.diff","patch_url":"https://github.com/jenkinsci/blueocean-plugin/pull/1262.patch","issue_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues/1262","number":1262,"state":"open","locked":false,"title":"[FIX JENKINS-43148] Add API for creating/reading a user's private SSH key","user":{"login":"kzantow","id":3009477,"avatar_url":"https://avatars2.githubusercontent.com/u/3009477?v=4","gravatar_id":"","url":"https://api.github.com/users/kzantow","html_url":"https://github.com/kzantow","followers_url":"https://api.github.com/users/kzantow/followers","following_url":"https://api.github.com/users/kzantow/following{/other_user}","gists_url":"https://api.github.com/users/kzantow/gists{/gist_id}","starred_url":"https://api.github.com/users/kzantow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kzantow/subscriptions","organizations_url":"https://api.github.com/users/kzantow/orgs","repos_url":"https://api.github.com/users/kzantow/repos","events_url":"https://api.github.com/users/kzantow/events{/privacy}","received_events_url":"https://api.github.com/users/kzantow/received_events","type":"User","site_admin":false},"body":"# Description\r\n\r\nAdds a get-or-create API for a user's Jenkins-managed generated public key. Can test this by logging in and accessing (a similar URL):\r\n\r\nhttp://localhost:8080/jenkins/blue/rest/organizations/jenkins/credentials/user/personalSSHKey\r\n\r\nSee [JENKINS-43148](https://issues.jenkins-ci.org/browse/JENKINS-43148).\r\n\r\n# Submitter checklist\r\n- [x] Link to JIRA ticket in description, if appropriate.\r\n- [x] Change is code complete and matches issue description\r\n- [x] Appropriate unit or acceptance tests or explanation to why this change has no tests\r\n- [x] Reviewer's manual test instructions provided in PR description. See Reviewer's first task below.\r\n\r\n# Reviewer checklist\r\n- [ ] Run the changes and verified the change matches the issue description\r\n- [ ] Reviewed the code\r\n- [ ] Verified that the appropriate tests have been written or valid explanation given\r\n\r\n","created_at":"2017-07-19T21:14:47Z","updated_at":"2017-07-21T20:14:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"aee9579509e132dd369e6702aa8e06e3462ac906","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262/commits","review_comments_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262/comments","review_comment_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues/1262/comments","statuses_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/statuses/5f0fdd72cf11478e47c24a73e70cd5a7e4405e35","head":{"label":"kzantow:git-ssh-editor","ref":"git-ssh-editor","sha":"5f0fdd72cf11478e47c24a73e70cd5a7e4405e35","user":{"login":"kzantow","id":3009477,"avatar_url":"https://avatars2.githubusercontent.com/u/3009477?v=4","gravatar_id":"","url":"https://api.github.com/users/kzantow","html_url":"https://github.com/kzantow","followers_url":"https://api.github.com/users/kzantow/followers","following_url":"https://api.github.com/users/kzantow/following{/other_user}","gists_url":"https://api.github.com/users/kzantow/gists{/gist_id}","starred_url":"https://api.github.com/users/kzantow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kzantow/subscriptions","organizations_url":"https://api.github.com/users/kzantow/orgs","repos_url":"https://api.github.com/users/kzantow/repos","events_url":"https://api.github.com/users/kzantow/events{/privacy}","received_events_url":"https://api.github.com/users/kzantow/received_events","type":"User","site_admin":false},"repo":{"id":60725922,"name":"blueocean-plugin","full_name":"kzantow/blueocean-plugin","owner":{"login":"kzantow","id":3009477,"avatar_url":"https://avatars2.githubusercontent.com/u/3009477?v=4","gravatar_id":"","url":"https://api.github.com/users/kzantow","html_url":"https://github.com/kzantow","followers_url":"https://api.github.com/users/kzantow/followers","following_url":"https://api.github.com/users/kzantow/following{/other_user}","gists_url":"https://api.github.com/users/kzantow/gists{/gist_id}","starred_url":"https://api.github.com/users/kzantow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kzantow/subscriptions","organizations_url":"https://api.github.com/users/kzantow/orgs","repos_url":"https://api.github.com/users/kzantow/repos","events_url":"https://api.github.com/users/kzantow/events{/privacy}","received_events_url":"https://api.github.com/users/kzantow/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kzantow/blueocean-plugin","description":"Blue Ocean UI app","fork":true,"url":"https://api.github.com/repos/kzantow/blueocean-plugin","forks_url":"https://api.github.com/repos/kzantow/blueocean-plugin/forks","keys_url":"https://api.github.com/repos/kzantow/blueocean-plugin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kzantow/blueocean-plugin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kzantow/blueocean-plugin/teams","hooks_url":"https://api.github.com/repos/kzantow/blueocean-plugin/hooks","issue_events_url":"https://api.github.com/repos/kzantow/blueocean-plugin/issues/events{/number}","events_url":"https://api.github.com/repos/kzantow/blueocean-plugin/events","assignees_url":"https://api.github.com/repos/kzantow/blueocean-plugin/assignees{/user}","branches_url":"https://api.github.com/repos/kzantow/blueocean-plugin/branches{/branch}","tags_url":"https://api.github.com/repos/kzantow/blueocean-plugin/tags","blobs_url":"https://api.github.com/repos/kzantow/blueocean-plugin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kzantow/blueocean-plugin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kzantow/blueocean-plugin/git/refs{/sha}","trees_url":"https://api.github.com/repos/kzantow/blueocean-plugin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kzantow/blueocean-plugin/statuses/{sha}","languages_url":"https://api.github.com/repos/kzantow/blueocean-plugin/languages","stargazers_url":"https://api.github.com/repos/kzantow/blueocean-plugin/stargazers","contributors_url":"https://api.github.com/repos/kzantow/blueocean-plugin/contributors","subscribers_url":"https://api.github.com/repos/kzantow/blueocean-plugin/subscribers","subscription_url":"https://api.github.com/repos/kzantow/blueocean-plugin/subscription","commits_url":"https://api.github.com/repos/kzantow/blueocean-plugin/commits{/sha}","git_commits_url":"https://api.github.com/repos/kzantow/blueocean-plugin/git/commits{/sha}","comments_url":"https://api.github.com/repos/kzantow/blueocean-plugin/comments{/number}","issue_comment_url":"https://api.github.com/repos/kzantow/blueocean-plugin/issues/comments{/number}","contents_url":"https://api.github.com/repos/kzantow/blueocean-plugin/contents/{+path}","compare_url":"https://api.github.com/repos/kzantow/blueocean-plugin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kzantow/blueocean-plugin/merges","archive_url":"https://api.github.com/repos/kzantow/blueocean-plugin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kzantow/blueocean-plugin/downloads","issues_url":"https://api.github.com/repos/kzantow/blueocean-plugin/issues{/number}","pulls_url":"https://api.github.com/repos/kzantow/blueocean-plugin/pulls{/number}","milestones_url":"https://api.github.com/repos/kzantow/blueocean-plugin/milestones{/number}","notifications_url":"https://api.github.com/repos/kzantow/blueocean-plugin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kzantow/blueocean-plugin/labels{/name}","releases_url":"https://api.github.com/repos/kzantow/blueocean-plugin/releases{/id}","deployments_url":"https://api.github.com/repos/kzantow/blueocean-plugin/deployments","created_at":"2016-06-08T19:46:31Z","updated_at":"2017-05-12T19:16:33Z","pushed_at":"2017-07-21T17:01:37Z","git_url":"git://github.com/kzantow/blueocean-plugin.git","ssh_url":"git@github.com:kzantow/blueocean-plugin.git","clone_url":"https://github.com/kzantow/blueocean-plugin.git","svn_url":"https://github.com/kzantow/blueocean-plugin","homepage":null,"size":15581,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"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":"jenkinsci:master","ref":"master","sha":"b8c93fb1a830cace5c156aa23d924f40d237f540","user":{"login":"jenkinsci","id":107424,"avatar_url":"https://avatars0.githubusercontent.com/u/107424?v=4","gravatar_id":"","url":"https://api.github.com/users/jenkinsci","html_url":"https://github.com/jenkinsci","followers_url":"https://api.github.com/users/jenkinsci/followers","following_url":"https://api.github.com/users/jenkinsci/following{/other_user}","gists_url":"https://api.github.com/users/jenkinsci/gists{/gist_id}","starred_url":"https://api.github.com/users/jenkinsci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenkinsci/subscriptions","organizations_url":"https://api.github.com/users/jenkinsci/orgs","repos_url":"https://api.github.com/users/jenkinsci/repos","events_url":"https://api.github.com/users/jenkinsci/events{/privacy}","received_events_url":"https://api.github.com/users/jenkinsci/received_events","type":"Organization","site_admin":false},"repo":{"id":50250915,"name":"blueocean-plugin","full_name":"jenkinsci/blueocean-plugin","owner":{"login":"jenkinsci","id":107424,"avatar_url":"https://avatars0.githubusercontent.com/u/107424?v=4","gravatar_id":"","url":"https://api.github.com/users/jenkinsci","html_url":"https://github.com/jenkinsci","followers_url":"https://api.github.com/users/jenkinsci/followers","following_url":"https://api.github.com/users/jenkinsci/following{/other_user}","gists_url":"https://api.github.com/users/jenkinsci/gists{/gist_id}","starred_url":"https://api.github.com/users/jenkinsci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenkinsci/subscriptions","organizations_url":"https://api.github.com/users/jenkinsci/orgs","repos_url":"https://api.github.com/users/jenkinsci/repos","events_url":"https://api.github.com/users/jenkinsci/events{/privacy}","received_events_url":"https://api.github.com/users/jenkinsci/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/jenkinsci/blueocean-plugin","description":"Blue Ocean is a reboot of the Jenkins CI/CD User Experience","fork":false,"url":"https://api.github.com/repos/jenkinsci/blueocean-plugin","forks_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/forks","keys_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/teams","hooks_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/hooks","issue_events_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues/events{/number}","events_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/events","assignees_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/assignees{/user}","branches_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/branches{/branch}","tags_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/tags","blobs_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/git/refs{/sha}","trees_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/statuses/{sha}","languages_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/languages","stargazers_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/stargazers","contributors_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/contributors","subscribers_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/subscribers","subscription_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/subscription","commits_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/commits{/sha}","git_commits_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/git/commits{/sha}","comments_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/comments{/number}","issue_comment_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues/comments{/number}","contents_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/contents/{+path}","compare_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/merges","archive_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/downloads","issues_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues{/number}","pulls_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls{/number}","milestones_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/milestones{/number}","notifications_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/labels{/name}","releases_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/releases{/id}","deployments_url":"https://api.github.com/repos/jenkinsci/blueocean-plugin/deployments","created_at":"2016-01-23T18:02:45Z","updated_at":"2017-07-21T09:24:38Z","pushed_at":"2017-07-21T19:21:42Z","git_url":"git://github.com/jenkinsci/blueocean-plugin.git","ssh_url":"git@github.com:jenkinsci/blueocean-plugin.git","clone_url":"https://github.com/jenkinsci/blueocean-plugin.git","svn_url":"https://github.com/jenkinsci/blueocean-plugin","homepage":"https://jenkins.io/projects/blueocean/","size":16087,"stargazers_count":1937,"watchers_count":1937,"language":"JavaScript","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":237,"mirror_url":null,"open_issues_count":25,"forks":237,"open_issues":25,"watchers":1937,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262"},"html":{"href":"https://github.com/jenkinsci/blueocean-plugin/pull/1262"},"issue":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues/1262"},"comments":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/issues/1262/comments"},"review_comments":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262/comments"},"review_comment":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/pulls/1262/commits"},"statuses":{"href":"https://api.github.com/repos/jenkinsci/blueocean-plugin/statuses/5f0fdd72cf11478e47c24a73e70cd5a7e4405e35"}}}}
{"actor":{"display_login":"vivek"}}