😸

GitHubのSSO認証を通す

に公開

SAML SSO認証が必須のリポジトリを触る場合、Personal Access Tokenで認証通すことが多いと思うのですが、組織のリポジトリに対するPATを作成しても組織から有効にしてもらわないと使えなかったりと面倒です。SSH認証にしたら特に組織からのアクションなくても動かせたのでメモ。

以下はMacでの内容なので、windowsの人はgithubのdocsを参照して読み替えてください。

https://docs.github.com/ja/enterprise-cloud@latest/authentication/authenticating-with-single-sign-on/authorizing-an-ssh-key-for-use-with-single-sign-on

SSHキーを作成

https://docs.github.com/ja/enterprise-cloud@latest/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

ssh-keygen -t ed25519 -C "your_email@example.com"

Users/[username]/.sshにid_ed25519, id_ed25519.pubが生成されます。

emailはgithubが作成してくれるnoreply用の123456789+[username]@users.noreply.github.comみたいなemailを使うと良いです。
https://github.com/settings/emails
設定画面の上部に記載があります。

SSHプライベートKeyを設定

Users/[username]/.ssh/configファイルに以下を追記。configファイルがなければ作成してください。

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

パスフレーズを利用しない場合にはAAddKeysToAgent, UseKeychainはなくても大丈夫です。

Host github.com
  IdentityFile ~/.ssh/id_ed25519

またHost名に別名を与えることでリポジトリごとにSSHkeyを変えたい場合は

Host github.com-hoge
   HostName github.com
   IdentityFile ~/.ssh/id_ed25519

のように別名を与えることで使い分けることができます。

git clone git@github.com-hoge:user/repo.git

.git/configをSSH認証に変更

https認証を行なっている場合は下記のようにssh認証用の書き方に変更します。

[remote "origin"]
	# url = https://github.com/user/repo.git
	url = git@github.com:user/repo.git

SSH公開KeyをGitHubに設定

keyの右端にconfigure SSOのプルダウンがあるので、そこからSSO認証を行なってください。

Discussion