PAT(Personal access token) vs github appsのtoken (installation access token)
課題
- PAT はセキュリティ的にリスクが大きいのでやめたい
結論
- 個人リポジトリで PAT を使っても良いが、organization で使う場合は GitHub App を使うべき
-
Personal access tokens are intended to access GitHub resources on behalf of yourself. To access resources on behalf of an organization, or for long-lived integrations, you should use a GitHub App. For more information, see "About creating GitHub Apps."
- Ref
PAT(Personal access token)
-
Personal access tokens are an alternative to using passwords for authentication to GitHub when using the GitHub API or the command line.
Personal access tokens are intended to access GitHub resources on behalf of yourself. -
Ref
-
あなたの GihHub アカウントの代わりに使うもの。言い換えると、PAT を使って何かした時にあなたのアカウントでログインした時に実施したことと同じ
github appsのtoken (installation access token)
作成の流れ
流れから書くとわりと分かりやすいため定義より流れを先に書く
- GitHub Appsを作成する
- 署名用の秘密鍵を生成する
- GitHub Appsをアカウントへインストールする
- SecretsにApp IDと秘密鍵を登録する
- GitHub Appsトークン(installation access token)の生成
説明
-
GitHub Apps のトークンの種類
-
installation access token
- GitHub Apps トークンと呼ぶことが多いようですが、今回は CI/CD で使うため installation access token を使うことになる
- installation access token が PAT の代わりに使うもの
- installation access token について(Ref1 & Ref2)
-
A GitHub App can request an installation access token by using a private key with a JSON web token format out-of-band.
An installation token identifies the app as the GitHub Apps bot, such as @jenkins-bot.
Installation access tokens expire after a predefined amount of time (currently 1 hour). -
If you want your app to take actions on behalf of itself, rather than a user, you should use an installation access token for authentication.
-
-
なぜ PAT より優位性があるか
- 突然動かなくなるリスクがない
- GitHub Appsに登録した秘密鍵が、勝手に失効することはありません。つまりFine-grained PATのように、突然動かなくなるリスクはありません。
- トークンの有効期限が短い
- installation access token は短命です。そのため万が一漏えいしても、影響は時限的です。「長命な秘密鍵」と「短命なトークン」という組み合わせは強力です。秘密鍵さえ守れば、インシデント発生時の被害はかなり小さくなります。これは長命なPATでは実現できない大きな魅力です。
- 突然動かなくなるリスクがない
installation access token の生成方法
- 一番シンプルだが、Third Partyのためセキュリティは懸念があり
- 自作もあり
-
https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
-
You can use GitHub's Octokit.js SDK to authenticate as an app installation. One advantage of using the SDK to authenticate is that you do not need to generate a JSON web token (JWT) yourself.
- GitHub's Octokit.js SDK を使ったほうが楽?
- 自前実装の参考例(Generating an installation access token)
-
-
https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
More: About GitHub Apps
補足
- github app token を作成用の 正式(1st party) gha は下記になります