LoginSignup
4
0

More than 1 year has passed since last update.

git submodule update --init で認証エラーが出てしまう

Last updated at Posted at 2023-02-03

概要

GitHubで管理している開発レポジトリにgit submoduleを導入しclone済みのレポジトリでsubmoduleを取得しようとしたところ、Username / Passwordの入力を求められ、入力すると以下のようなエラーが発生した。

$ git submodule update --init 
:
: Username / Passwordを入力
:
Cloning into '/home/chart/chart-server-web/chart-server-shared'...
Username for 'https://github.com': XXXXX
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
:

ちなみに、submoduleを含んだcloneをしようとしても同様のエラーとなる

git clone git@github.com:XXXXX/XXXXX.git --recursive

背景と原因

GitHubのレポジトリ接続には2種類の方法がある。

形式 概要
SSH形式 https://github.com/settings/keys にSSHの公開鍵を登録しておくことで、接続時に認証される git@github.com:aws/aws-cli.git
HTTPS形式 https://github.com/settings/tokens にてPersonal Tokenを発行し、 それをアクセス時に渡すことで認証される https://github.com/aws/aws-cli.git

今回、開発レポジトリをAWS CodeBuild上で取得してbuildするためにsubmoduleはHTTPS形式で取得するようにセットしていた。
(CodeBuildはHTTP形式でしかGitソースの取得ができなかったため)

.gitsubmodule
[submodule "submodule-desu"]
	path = submodule-desu
	url = https://github.com/XXXXX/XXXXX.git

しかし、ローカルの開発環境ではSSH形式で開発レポジトリを取得しており、 git submodule update --init でsubmoduleを取得しようとしたところ、HTTPS形式の認証はしていないためエラーが発生した。

解決策

GitHub CLI(ghコマンド)を用いて事前にHTTPS形式の認証・認可をおこなっておく

1. ghコマンドをインストール

Macの場合

brew install gh

RedHat系(Amazon Linuxなど)の場合

sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
sudo yum install gh

2. GitHubにログインしHTTPS方式の認証をおこなう

$ gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser

3. GitHubのデバイス認可画面にアクセスし、2.で表示されたone-time codeを入力する

! First copy your one-time code: XXXX-XXXX

3. 認可状態を確認(configured to use https protocol)

$ gh auth status
github.com
  ✓ Logged in to github.com as XXXXXXX (/home/XXXXXXX)
  ✓ Git operations for github.com configured to use https protocol.
  ✓ Token: gho_************************************
  ✓ Token scopes: gist, read:org, repo, workflow

4. 改めてサブモジュールを読み込む

git submodule update --init

エラーが出なければ成功 :ok_hand:

念のため、submoduleの中身が見えることを読めることを確認する。

ls -ltr ${sumoduleのフォルダ}

:thumbsup: :sparkles:

これにより、submoduleを含んだcloneもできるようになっている。

git clone git@github.com:XXXXX/XXXXX.git --recursive

補足

ghコマンドでなく、Personal Tokenを発行して引き回すのはだめなのか?

最初のエラーに以下のような案内がある。

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.

案内されたページを参照すると、
Passwordの入力を求められるがパスワードによる認証は2021/10/13に廃止されており、現在はPersonal Tokenを入力する仕様であることがわかる。

なので、Personal Tokenを発行して入力することでも認証されエラーは解消される。
CIなどでは、秘匿情報としてTokenを保持した上で必然的にこの方式を取ることになるはず。

ただ、以下のPersonal Tokenに関するページを見ると

警告: アクセス トークンは、パスワードと同様の扱いとしてください。

コマンド ラインから GitHub にアクセスするには、personal access tokenを作成する代わりに GitHub CLI または Git Credential Manager を使用することを検討してください。

とあり、推奨はGitHub CLI または Git Credential Manager であることがわかる。

GitHubを利用していることもあり、今回はGitHub CLIによる解決方法を選択した。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0