tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【github】二段階認証後にprivate repositoryのgit cloneで失敗するときの対応メモ

はじめに

しばらくManagementや組織開発を行っていました。
久しぶりに開発環境を作る必要がありcloneから…と思ったらいきなりコケたのでそのメモです。

条件は以下:

  • 個人のgit設定は問題がない
  • Private Organization
  • Private Repository
  • 二段階認証済み

アジェンダ

  1. エラーの状態と原因
  2. How to

1. エラーの状態と原因

エラーはこんな感じ

$ git clone git@github.com:yyyy/xxxx.git
Cloning into 'xxxx'...
ERROR: The 'yyyy' organization has enabled or enforced SAML SSO. To access
this repository, you must use the HTTPS remote with a personal access token
or SSH with an SSH key and passphrase
that has been whitelisted for this organization. Visit
https://docs.github.com/articles/authenticating-to-a-github-organization-with-saml-single-sign-on/ for more information.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

原因は、ERRORに記載のある通りですが訳を載せておきます。

The 'yyyy' organization has enabled or enforced SAML SSO. To access this repository, you must use the HTTPS remote with a personal access token or SSH with an SSH key and passphrase that has been whitelisted for this organization.
 
'yyyy'組織はSAML SSOを有効化または適用しています。このリポジトリにアクセスするには、個人用アクセストークンでHTTPSリモートを使用する必要があります。 または、この組織のホワイトリストに登録されているSSHキーとパスフレーズを使用してSSHを実行します。

2. How to

大まかに以下の順で設定を行います。

  • tokenを設定
  • tokenを保存
  • SSO連携を行いAuthorizeする
  • https x tokenでgit cloneを行う

tokenを設定

tokenを設定するために、 githubにログインして以下の順に開きます。

  • Settings
  • 左サイドバー > Developer settings
  • 左サイドバー > Personal access tokens
  • Generate new tokenボタン

以下の画面を開き、
Generate new tokenボタンからtokenの設定を行います。 f:id:tweeeety:20200818035049p:plain

次の画面にて以下を行います。

  • Personal Tokenに対するNoteに任意の文字列を入力
  • Select scopes(チェックボックス)をすべてチェック
  • Generate tokenボタンでtoken作成

f:id:tweeeety:20200818035037p:plain

tokenを保存

tokenが生成されると、次の画面が表示されます。
緑背景の部分がtokenですが、一度しか表示されないのでコピペするなりして保存します。

f:id:tweeeety:20200818035025p:plain

SSO連携を行いAuthorizeする

Enable SSOのプルダウンを選択します。
SSOが必要なorganizationsが表示されているのでAuthorizeを行います。

f:id:tweeeety:20200818035012p:plain

https x tokenでgit cloneを行う

ターミナルに戻りhttpsでgit cloneします。

UsernameとPasswordを聞かれるので、Passwordに保存したtokenを利用して認証します。

$ git clone https://github.com/yyyy/xxxx.git
Cloning into 'xxxx'...
Username for 'https://github.com':
Password for 'https://tweeeety@github.com':
remote: Enumerating objects: 158, done.
remote: Counting objects: 100% (158/158), done.
remote: Compressing objects: 100% (136/136), done.
remote: Total 7465 (delta 75), reused 82 (delta 22), pack-reused 7307
Receiving objects: 100% (7465/7465), 1.24 MiB | 1.41 MiB/s, done.
Resolving deltas: 100% (3699/3699), done.

終わり

git cloneみたいな最初でコケると「アレ、だいぶ忘れてるな…」と思いますねー。