LoginSignup
20
12

More than 1 year has passed since last update.

Bitbucketでアカウントパスワード認証ができなくなった時の対処方法

Last updated at Posted at 2022-03-15

概要

Bitbucketでソース管理しているアプリケーションで
git fetchをしようとした時に以下のような文言が出てソース取得ができなくなっていた。

[sample-app]# git fetch
Username for 'https://bitbucket.org': HogeTarou
Password for 'https://HogeTarou@bitbucket.org':
remote: Bitbucket Cloud recently stopped supporting account passwords for Git authentication.
remote: See our community post for more details: https://atlassian.community/t5/x/x/ba-p/1948231
remote: App passwords are recommended for most use cases and can be created in your Personal settings:
remote: https://bitbucket.org/account/settings/app-passwords/
fatal: Authentication failed for 'https://bitbucket.org/ps_sample/sample-app.git/'

原因

Bitbucket側で2022年3月1日以降、BitbucketユーザーがHTTPSおよびRESTAPIの基本認証を使用するときにアカウントパスワードを使用できなくなるように仕様変更があった事が原因です。

対応方法

いづれかになります。
① アプリパスワードを利用する
② SSHキーを利用する(SSH認証に切り替える)

今回は①アプリパスワードを利用するで対応する方法について説明します。

アプリパスワード設定方法

Bitbucketにログインします。
画面左下にある自分のプロフィールをクリックして、Personal settingsをクリックします。

image.png

サイドメニューからアプリパスワードをクリックして、アプリパスワードの作成をクリックします。

パスワードの設定画面にて、以下を入力していきます。
作成ボタンを押すとパスワードが自動生成されます。

label : 任意の名前をつけます
権限: このパスワードを使って操作できる範囲にチェックをします
(私の場合はリポジトリの操作だけが目的なのでリポジトリのみにしています)

新しいパスワードが生成されたら、表示されているパスワードを忘れないようにメモして下さい。(後から確認はできません)

これで準備完了です。

検証

再度git fetchをやってみるとできるようになっていました。

[sanmple-app]# git fetch
Username for 'https://bitbucket.org': HogeTarou
Password for 'https://HogeTarou@bitbucket.org' : さっき取得したアプリパスワードを入力
[sanmple-app]# 

成功!

おまけ:「②SSHキーを利用する」に切り替えたい場合

1.公開鍵の設定

サーバー上で公開鍵を作成済みかどうか確認

$ cat ~/.ssh/id_rsa.pub

# もしない場合は以下のような文言が出るはず
[sample-app]# cat ~/.ssh/id_rsa.pub
cat: /root/.ssh/id_rsa.pub: No such file or directory

2.鍵の作成

もし無い場合は、サーバで秘密鍵/公開鍵作成(パスフレーズはなしでもいいので、コマンド実行後エンター2回押す)

ssh-keygen -t rsa -f ~/.ssh/鍵の名前_rsa

# 作成例
[sample-app]# ssh-keygen -t rsa -f ~/.ssh/sample_rsa

# 作成されたか確認
[sample-app]# cat ~/.ssh/sample_rsa.pub
ssh-rsa AAAAAAAA3NzaC1yc2EAAAA・・・・・・・・WBSvwJWaPW3tKVDpC+JCCCCCCCCC hoge-user@ip-10-0-1-100
[sample-app]# 

3.Bitbucket側で鍵の登録

リポジトリの画面を開き、再度メニューから[Repository settings]をクリック
(admin権限がないと表示されてない場合があります)

アクセスキーをクリックして、add keyをクリックします

labelとkeyを入力して、「Add SSH key」を作成すると登録完了です
label : アプリがわかるような任意の名前をつけます
key  : 先程作成した「sample_rsa.pub」の公開鍵の部分をコピーして貼り付けます

4.ソースが配置されているサーバー側の設定

サーバにて下記設定追加

[sample-app]# vi ~/.ssh/config
##### 下記を追加 #####
Host bitbucket.org
    HostName bitbucket.org
    identityFile ~/.ssh/sample_rsa


※もし新規作成だった場合は権限の変更が必要なので注意
[sample-app]# chmod 700 ~/.ssh/config  

アプリのソース取得方法をhttpsからSSHへ変更する必要があるので
アプリがあるディレクトリ(git pullやgit fetchする場所)まで移動して、下記変更を加える

# アプリのあるディレクトリまで移動
[sample-app]# cd /var/www/smaple-app/ 

# 下記のように変更します(httpsからSSHで取得するように変更しています)
[sample-app]# vi .git/config

[remote "origin"]
        url = https://bitbucket.org/sample/sample-app.git

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

[remote "origin"]
        url = git@bitbucket.org:sample/sample-app.git

これで作業完了です。
SSHになればパスワードなどは入力する必要がないので、楽になります。

20
12
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
20
12