{"id":1652,"date":"2021-05-27T10:59:03","date_gmt":"2021-05-27T17:59:03","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=1652"},"modified":"2021-05-27T10:59:10","modified_gmt":"2021-05-27T17:59:10","slug":"github-sso-using-password-protected-ssh-keys","status":"publish","type":"post","link":"https:\/\/nillsf.com\/index.php\/2021\/05\/27\/github-sso-using-password-protected-ssh-keys\/","title":{"rendered":"GitHub SSO using password-protected SSH keys"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are two ways to pull\/push from\/to GitHub, when connecting from a remote system: either you use HTTPS or you use SSH. When connecting to a GitHub organization, the organization might have special requirements for the connection. For example, if I want to push updates to the github.com\/azure organization, I need to use a password-protected SSH key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The goal of this post is to explain three things:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>How to use an SSH key with GitHub<\/li><li>How to use multiple SSH keys with GitHub<\/li><li>How to enable an SSH key for SSO on GitHub<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To explain everything, I&#8217;m using a new virtual machine, with nothing but git installed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start with the first topic:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use an SSH key with GitHub<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To use an SSH key with GitHub, you need to do three things:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a new SSH key<\/li><li>Share the public key with GitHub<\/li><li>Clone the repo with the SSH URL<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To create a new SSH-key, you can use the command-line tool <code>ssh-keygen<\/code>, as shown in the screenshot below. For the first key, I&#8217;ll use all the default values and won&#8217;t use a password:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1019\" height=\"627\" src=\"\/wp-content\/uploads\/2021\/05\/image-27.png\" alt=\"\" class=\"wp-image-1653\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-27.png 1019w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-27-300x185.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-27-768x473.png 768w\" sizes=\"auto, (max-width: 1019px) 100vw, 1019px\" \/><figcaption>Using ssh-keygen to generate an SSH key<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The second step is to share the public key with GitHub. To do this, you first need the public key. To get is, you can use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat ~\/.ssh\/id_rsa.pub<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now copy the output and head over to github.com. In the GitHub menu, select settings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"305\" height=\"873\" src=\"\/wp-content\/uploads\/2021\/05\/image-28.png\" alt=\"\" class=\"wp-image-1654\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-28.png 305w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-28-105x300.png 105w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><figcaption>Navigating to GitHub settings<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In setting, head over to &#8220;SSH and GPG keys&#8221; in the left-hand menu, and click on &#8220;New SSH key&#8221;. GitHub allows you to use multiple SSH keys for a single account:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"755\" src=\"\/wp-content\/uploads\/2021\/05\/image-29-1024x755.png\" alt=\"\" class=\"wp-image-1655\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-29-1024x755.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-29-300x221.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-29-768x567.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-29.png 1506w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Adding a new SSH key to your GitHub account<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here you can enter the new SSH key and give it a descriptive title. As you save the key, GitHub might ask you to input your password for additional security. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"599\" src=\"\/wp-content\/uploads\/2021\/05\/image-30-1024x599.png\" alt=\"\" class=\"wp-image-1656\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-30-1024x599.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-30-300x176.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-30-768x450.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-30.png 1160w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Adding a new key with a descriptive title<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can now use this key to clone git repos. To clone a repo using SSH, you need to select that option when you clone a repo as shown in the screenshot below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"616\" src=\"\/wp-content\/uploads\/2021\/05\/image-31.png\" alt=\"\" class=\"wp-image-1657\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-31.png 586w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-31-285x300.png 285w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the command line, you can now enter the <code>git clone<\/code> command and watch the repo get cloned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone git@github.com:Azure\/bellhop.git<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If this is the first time cloning from GitHub on that machine, you might get a question asking to confirm the authenticity of the remote host, which you can do by typing &#8220;yes&#8221; to that question. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"277\" src=\"\/wp-content\/uploads\/2021\/05\/image-32-1024x277.png\" alt=\"\" class=\"wp-image-1658\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-32-1024x277.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-32-300x81.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-32-768x208.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-32.png 1254w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Cloning a git repo using SSH, and confirming the authenticity of the remote host.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">The problem with a non-password protected SSH-key<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This setup works fine to pull a repo, but it doesn&#8217;t allow pushing to that repo. This is for two reasons:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The key isn&#8217;t authorized for SSO<\/li><li>The key isn&#8217;t password protected.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To show this to you, I made a quick change in the cloned repo and tried pushing that change. That fails due to the key not being password protected and not being authorized for SSO. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout dockerfile-updates\necho \"test\" > test.txt\ngit add .\ngit commit -m \"test\"\ngit push<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"243\" src=\"\/wp-content\/uploads\/2021\/05\/image-33-1024x243.png\" alt=\"\" class=\"wp-image-1659\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-33-1024x243.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-33-300x71.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-33-768x183.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-33.png 1439w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Git push failing due to key not being SSO authorized<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s solve this issue! We&#8217;ll do two things here:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a new SSH-key, and configure this git repo to use that new key<\/li><li>Authorize the new SSH for SSO in GitHub.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">How to use multiple SSH keys with GitHub<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start by creating a new key, now password-protected, using the same <code>ssh-keygen<\/code> command:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"401\" src=\"\/wp-content\/uploads\/2021\/05\/image-35-1024x401.png\" alt=\"\" class=\"wp-image-1661\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-35-1024x401.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-35-300x117.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-35-768x300.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-35.png 1516w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Generating a new password-protected SSH key<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I called the key <code>protected_key<\/code>, and gave it a password (which you don&#8217;t see in the screenshot). Make sure to save the key in the <em><code>.ssh<\/code> <\/em>folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we&#8217;re going to configure SSH to use different keys based on the hostname. You could also use the protected key as the only key if you so please, but in my case, I want to keep using both keys. To do so, create a new file in the <code>~\/.ssh<\/code> folder called <code>config<\/code>. Input the following &#8211; or similar &#8211; to configure SSH to use different keys based on host:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Personal repositories\nHost github.com\nHostName github.com\n User git\n IdentityFile ~\/.ssh\/id_rsa\n\n# SSO repository\nHost github.com-SSO\n HostName github.com\n User git\n IdentityFile ~\/.ssh\/protected_key<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, in the repo you cloned you&#8217;ll have to replace the remote hostname. You can do that using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git remote set-url origin git@github.com-SSO:Azure\/bellhop.git<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice how you replaced <code>github.com<\/code> by <code>github.com-sso<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One final step remains: authorizing this key for SSO:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to enable an SSH key for SSO on GitHub<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to authorize this key for SSO on GitHub. Follow the same steps as earlier to add the key to GitHub, meaning getting the public key and entering it in Github:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"566\" src=\"\/wp-content\/uploads\/2021\/05\/image-36-1024x566.png\" alt=\"\" class=\"wp-image-1662\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-36-1024x566.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-36-300x166.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-36-768x424.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-36.png 1166w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Adding a new key to GitHub<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After you saved the key, you&#8217;ll see the option next to it to enable SSO for that key. In my case, I&#8217;ll enable SSO for the Azure org:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"448\" src=\"\/wp-content\/uploads\/2021\/05\/image-37-1024x448.png\" alt=\"\" class=\"wp-image-1663\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-37-1024x448.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-37-300x131.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-37-768x336.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-37-1140x500.png 1140w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-37.png 1143w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Enabling SSO for the key<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now that the key is password-protected and it&#8217;s enabled for SSO, we should be able to push to the repo:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"243\" src=\"\/wp-content\/uploads\/2021\/05\/image-38-1024x243.png\" alt=\"\" class=\"wp-image-1665\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-38-1024x243.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-38-300x71.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-38-768x183.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-38.png 1456w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Being able to push to the remote repo using the new key<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, this prompted for the password of the SSH key and then successfully allowed the push to complete.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you learned how to use multiple SSH keys with github.com. You also learned how to use a password-protected key to push to an organization with SSO enabled. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two ways to pull\/push from\/to GitHub, when connecting from a remote system: either you use HTTPS or you use SSH. When connecting to a GitHub organization, the organization might have special requirements for the connection. For example, if I want to push updates to the github.com\/azure organization, I need to use a password-protected [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1666,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,5],"tags":[52,184,53],"class_list":["post-1652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-open-source","tag-devops","tag-git","tag-github"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/2021-05-27-10_56_58-PowerPoint-Slide-Show-Customize-core-dumps-in-Azure-Kubernetes.pptx.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":2,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"predecessor-version":[{"id":1667,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1652\/revisions\/1667"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/1666"}],"wp:attachment":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}