Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions cmd/koroku/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,31 @@ func TestCheckReportsABrokenConfig(t *testing.T) {
}

func TestInstallationAccountFilter(t *testing.T) {
allowed := installationAccountFilter(config.GitHub{AllowedUserID: 42, InstallationAccounts: []string{" My-Org "}})
both := config.GitHub{AllowedUserID: 42, InstallationAccountIDs: []int64{2002}, InstallationAccounts: []string{" My-Org "}}
idsOnly := config.GitHub{AllowedUserID: 42, InstallationAccountIDs: []int64{2002}}
loginsOnly := config.GitHub{AllowedUserID: 42, InstallationAccounts: []string{" My-Org "}}
for _, tc := range []struct {
name string
cfg config.GitHub
id int64
login string
want bool
}{
{42, "octo", true}, // the operator's own account, whatever its login
{7, "my-org", true}, // listed, case-insensitively
{7, "MY-ORG", true},
{99, "stranger", false},
{"the operator's own account, whatever its login", idsOnly, 42, "octo", true},
{"a listed ID", idsOnly, 2002, "my-org", true},
{"a listed ID after the account was renamed", idsOnly, 2002, "renamed-org", true},
{"a listed login", loginsOnly, 2002, "my-org", true},
{"a listed login, case-insensitively", loginsOnly, 2002, "MY-ORG", true},
{"either list is enough: by ID", both, 2002, "renamed-org", true},
{"either list is enough: by login", both, 3003, "my-org", true},
{"a stranger", both, 9009, "stranger", false},
// Why IDs are preferred: after my-org is renamed its old name is
// free, and the login list accepts whoever registers it.
{"another account that took a listed login over is accepted by the login list", loginsOnly, 9009, "my-org", true},
{"another account that took the login over is refused when only IDs are listed", idsOnly, 9009, "my-org", false},
} {
if got := allowed(tc.id, tc.login); got != tc.want {
t.Errorf("allowed(%d, %q) = %v, want %v", tc.id, tc.login, got, tc.want)
if got := installationAccountFilter(tc.cfg)(tc.id, tc.login); got != tc.want {
t.Errorf("%s: allowed(%d, %q) = %v, want %v", tc.name, tc.id, tc.login, got, tc.want)
}
}
}
14 changes: 7 additions & 7 deletions cmd/koroku/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -119,14 +118,11 @@ func newGitHubClient(cfg config.GitHub) (*github.Client, error) {
}

// installationAccountFilter allows the installations on the operator's own
// account and on the accounts listed in installation_accounts.
// account and on the accounts listed in installation_account_ids (by ID) or
// installation_accounts (by login).
func installationAccountFilter(cfg config.GitHub) func(int64, string) bool {
logins := make(map[string]bool, len(cfg.InstallationAccounts))
for _, login := range cfg.InstallationAccounts {
logins[strings.ToLower(strings.TrimSpace(login))] = true
}
return func(id int64, login string) bool {
return id == cfg.AllowedUserID || logins[strings.ToLower(login)]
return cfg.InstallationAccess(id, login) != config.InstallationIgnored
}
}

Expand Down Expand Up @@ -367,6 +363,10 @@ func serve(ctx context.Context, args []string, stderr io.Writer, ready chan<- st
})
deployOpts.Status = ghDeployments
go ghDeployments.Run(ctx)
if len(cfg.GitHub.InstallationAccounts) > 0 {
logger.Warn("github.installation_accounts matches accounts by login, and a login given up in a rename can be taken by another account: " +
"list the account IDs in github.installation_account_ids instead and remove installation_accounts (koroku check prints the IDs)")
}
if cfg.GitHub.ObsoleteRequireTwoFactor != nil {
logger.Warn("github.require_two_factor is no longer used and can be removed from config.toml: " +
"GitHub does not report the two-factor status to a GitHub App, so GitHub enforces it at sign-in instead")
Expand Down
11 changes: 8 additions & 3 deletions deploy/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ access_log_dir = "/var/log/caddy/apps"
# (see https://api.github.com/users/<login>).
# allowed_user_id = 0
# Other accounts (organizations you manage) whose installations of the App
# koroku uses, by login. The App must be installable on any account for this;
# installations on accounts not listed here (or the allowed user's own) are
# ignored.
# koroku uses, by numeric account ID. The App must be installable on any
# account for this; installations on accounts not listed here (or the allowed
# user's own) are ignored. `koroku check` and the console's system screen
# print the ID of every account the App is installed on.
# installation_account_ids = [2002]
# The same list by login is accepted too (an account in either list is used),
# but a login is not a stable identity: when an organization is renamed its
# old name is free for another account to take. List the IDs instead.
# installation_accounts = ["my-org"]
# Secret of the App's webhook, for automatic deployment on push (an app
# setting, off by default). Set the same value in the App's settings with
Expand Down
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ ARCHITECTURE 9.6「定期実行」。登録タスクに予定を書くと、koro
| `GET /api/repos` | `{"repos": [{"id": 1, "full_name": "octo/blog", "private": true, "default_branch": "main"}]}`(GitHub App をインストールしたリポジトリ) |
| `GET /api/repos/{owner}/{repo}/branches` | `{"branches": [{"name": "main", "sha": "9fceb02..."}]}` |
| `GET /api/repos/{owner}/{repo}/commits?ref=main` | `{"commits": [{"sha": "9fceb02...", "message": "Fix login", "author": "octo", "date": "2026-11-18T03:00:00Z"}]}`(新しい順、30 件) |
| `GET /api/system/github` | GitHub App とそのインストール先。`{"configured": true, "app": {"slug": "koroku-octo", "html_url": "https://github.com/apps/koroku-octo", "settings_url": "https://github.com/settings/apps/koroku-octo", "install_url": "https://github.com/apps/koroku-octo/installations/new"}, "installations": [{"account": "octo", "repository_selection": "selected", "used": true}], "installation_accounts": []}`。`used` は koroku がそのインストール先のリポジトリを使うか(ARCHITECTURE 5 章)。App が未設定なら `configured: false`、`app: null` |
| `GET /api/system/github` | GitHub App とそのインストール先。`{"configured": true, "app": {"slug": "koroku-octo", "html_url": "https://github.com/apps/koroku-octo", "settings_url": "https://github.com/settings/apps/koroku-octo", "install_url": "https://github.com/apps/koroku-octo/installations/new"}, "installations": [{"account": "octo", "account_id": 1001, "repository_selection": "selected", "used": true, "login_only": false}], "installation_account_ids": [], "installation_accounts": []}`。`account_id` はアカウントの数値 ID(`[github] installation_account_ids` に書く値)。`used` は koroku がそのインストール先のリポジトリを使うか、`login_only` は login(`installation_accounts`)だけで一致して使っているか(ARCHITECTURE 5 章)。`installation_account_ids` と `installation_accounts` は `config.toml` の値。App が未設定なら `configured: false`、`app: null` |
| `GET /api/repos/{owner}/{repo}/compose?ref=main&path=` | Compose ファイルの検出。App で読めない public リポジトリは認証なしで読む(App が未設定でも使える)。`{"files": ["compose.yaml"], "path": "compose.yaml", "services": [{"name": "web", "ports": [3000]}, {"name": "db", "ports": [5432]}]}`。`files` はリポジトリ直下で見つかった候補、`path` は読んだファイル(`path` 指定がなければ最初の候補)、`services` はそこから読み取れたサービス名とコンテナ側ポート(読み取れなければ空配列。UI は手入力にフォールバックする) |

GitHub 側の失敗は `404 repo_not_found`(リポジトリ・ref・ファイルがない、または GitHub App が未インストール)、`503 github_rate_limited`、`502 github_error`、GitHub App が未設定なら `503 not_configured`。
Expand Down
6 changes: 4 additions & 2 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ koroku は、3〜5 個の Docker Compose アプリを、VPS 1 台の上で GitHu
| App をインストールした、自分が管理する別の org のリポジトリ(private を含む) | 同上 | installation token |
| App をインストールしていない public リポジトリ(OSS など) | 作成画面で URL を入れる(`repo_url`) | 認証なし |

- 別の org に App をインストールするには、App の「Where can this GitHub App be installed?」を「Any account」にする。そうすると誰でも App を自分のアカウントにインストールできるので、koroku は使うインストール先を、オーナー自身のアカウント(`allowed_user_id` と一致するアカウント ID)と、`[github] installation_accounts` に書いたアカウント(login、大文字小文字を区別しない)に限る。それ以外のインストール先のリポジトリは一覧に出さず、token も発行しない。そのリポジトリを指定しても App が入っていないものとして扱う(public なら認証なしで読む)。`koroku check` は、使わないインストール先があれば警告する。システム画面の「GitHub App」は、インストール先ごとに使うかどうかを示し、別の org を加える手順(App の設定画面とインストール画面へのリンク、`config.toml` に書く `installation_accounts` の行)を出す。作成画面のリポジトリ欄からも、そこへ移れる。
- 別の org に App をインストールするには、App の「Where can this GitHub App be installed?」を「Any account」にする。そうすると誰でも App を自分のアカウントにインストールできるので、koroku は使うインストール先を、オーナー自身のアカウント(`allowed_user_id` と一致するアカウント ID)と、`[github] installation_account_ids` に書いたアカウント(数値のアカウント ID)に限る。それ以外のインストール先のリポジトリは一覧に出さず、token も発行しない。そのリポジトリを指定しても App が入っていないものとして扱う(public なら認証なしで読む)。
- アカウントを ID で指定するのは、login ではアカウントを特定できないため。org の名前を変えると元の名前は空き、その名前を取った別のアカウントが App をインストールできる(メンバーを ID で照合するのと同じ理由。10.7)。`[github] installation_accounts`(login の一覧、大文字小文字を区別しない)でも指定でき、どちらかの一覧にあるアカウントを使う(両方を書くと、ID の一覧と login の一覧の和になる)。login での指定は、その名前を取った別のアカウントのインストールも使ってしまう弱い形で、login で指定した既存のホストがインストール先を失わないために受け付ける。書いてあれば `koroku serve` は起動時に、ID を `installation_account_ids` に書くよう警告を出す。
- `koroku check` は、インストール先ごとにアカウントの login と ID を表示し、使わないインストール先があれば警告する。login だけで一致しているアカウントがあれば、代わりに書く `installation_account_ids` の行を示す(10.4)。システム画面の「GitHub App」は、インストール先ごとにアカウントの ID(コピーできる)と使うかどうかを示し、login だけで一致しているインストール先にはその旨を出す。別の org を加える手順(App の設定画面とインストール画面へのリンク、`config.toml` に書く `installation_account_ids` の行)も出す。作成画面のリポジトリ欄からも、そこへ移れる。
- 作成画面の Compose ファイルの検出(`GET /api/repos/{owner}/{repo}/compose`)は、App で読めないリポジトリ(未インストール、または使わないインストール先)を認証なしで読み直す。github.com 以外の URL は読めないので、サービスとポートを手で入れる。

| 用途 | 方式 |
Expand Down Expand Up @@ -1963,7 +1965,7 @@ Heroku の Review Apps に当たるもの。staging アプリ(8.2)を親に
- DNS: `console_host` と、登録していない名前(`koroku-check-<乱数>.<base_domain>`)が `public_ip` だけに解決されること。後者が解決されなければワイルドカードがないとして警告する(アプリごとの登録でも運用できるため)。判定は独自ドメインの検証(8 章)と同じ関数。
- Caddy の admin API が応答すること。`console_url` に HTTPS で接続でき、証明書が信頼できること。`acme_ca` が Let's Encrypt の staging なら、信頼できない証明書は警告にとどめ、本番への切り替えを促す。
- `master.key` があり 0600 であること(ないときは警告。初回起動で作られる)。
- GitHub App: App の JWT で `GET /app` が通り、`app_id`・`client_id` が一致し、権限が Contents と Metadata の read(足りなければ失敗)と Deployments の write(なければ GitHub への反映が止まる旨の警告)で、それ以外がないこと(余分なら警告。Actions の read と Pull requests の read は余分に数えない)。`GET /app/installations` で 1 つ以上インストールされていること。`allowed_user_id` が実在するアカウントであること(`GET /user/{id}`、認証なし)。callback URL は API で読めないので、正しい値を表示するだけにする。
- GitHub App: App の JWT で `GET /app` が通り、`app_id`・`client_id` が一致し、権限が Contents と Metadata の read(足りなければ失敗)と Deployments の write(なければ GitHub への反映が止まる旨の警告)で、それ以外がないこと(余分なら警告。Actions の read と Pull requests の read は余分に数えない)。`GET /app/installations` で 1 つ以上インストールされていること。インストール先は、アカウントの login と ID を並べて表示する(`my-org (ID 2002, all repositories)`)。koroku が使わないアカウントへのインストールがあれば警告する。`installation_accounts`(login での指定。5 章)が書いてあれば警告し、login だけで一致しているアカウントがあるときは、代わりに書く行(`installation_account_ids = [2002]`。設定済みの ID も含める)を示す。`allowed_user_id` が実在するアカウントであること(`GET /user/{id}`、認証なし)。callback URL は API で読めないので、正しい値を表示するだけにする。
- 自動デプロイ(5.2): 有効なアプリがあるのに `webhook_secret` がない、App が push のイベントを購読していない(`GET /app` の `events`)、App の Webhook の URL が `<console_url>/webhooks/github` でない(`GET /app/hook/config`)、のいずれかなら警告。有効なアプリは DB を読み取り専用で開いて数える。Webhook が Active かどうかは API で読めないので、表示するだけにする。
- ワークフローを待つ自動デプロイ(5.2): 待つワークフローを設定した、自動デプロイが有効なアプリがあるのに、App に Actions の read がない、または workflow_run のイベントを購読していない(`GET /app` の `permissions` と `events`)なら警告。
- レビューアプリ(9.9): App に Pull requests の read がなければ警告(レビューアプリを作れない旨)。
Expand Down
3 changes: 2 additions & 1 deletion docs/OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ sudo ./deploy/provision.sh ./koroku
| `[github] app_id`、`client_id`、`client_secret` | GitHub App の値。権限は Contents と Metadata の Read-only と Deployments の Read and write、callback は `https://<console_host>/auth/github/callback` |
| `[github] private_key_file` | `/etc/koroku/github-app.pem` |
| `[github] allowed_user_id` | オーナーの GitHub ユーザーの数値 ID(1 件)。オーナーはすべての操作ができ、メンバーとロールを管理画面で管理する。メンバーは DB にあり、ここには書かない。この値を変えると、前のオーナーのセッションと API トークンは使えなくなる(ARCHITECTURE 10.7) |
| `[github] installation_accounts` | 任意。App をインストールした別の org など、koroku が使うインストール先の login の一覧。オーナー自身のアカウントは書かなくても使う(SETUP 4.5) |
| `[github] installation_account_ids` | 任意。App をインストールした別の org など、koroku が使うインストール先のアカウント ID(数値)の一覧。オーナー自身のアカウントは書かなくても使う。ID は `koroku check` の `github.install` と管理画面の「GitHub App」に出る(SETUP 4.5) |
| `[github] installation_accounts` | 任意。同じ一覧を login で書く形。どちらかの一覧にあるアカウントを使う。login は org の名前を変えると空き、別のアカウントが取れるので、ID で書く(`installation_account_ids`)。書いてあれば起動時に警告が出て、`koroku check` が代わりに書く行を示す(SETUP 4.5) |
| `[github] webhook_secret` | 任意。push での自動デプロイと、ワークフローの成功を待つ自動デプロイ(ARCHITECTURE 5.2)に使う GitHub App の Webhook の secret(20 文字以上。`openssl rand -hex 32` で作る)。App 側の Webhook secret と同じ値にする。未設定なら `/webhooks/github` はすべての配送を断る(SETUP 4.6) |
| `[github] require_two_factor` | 使われない(古い版の設定)。書いてあれば起動時に警告が出るので、消してよい。2 段階認証は GitHub アカウント側で有効にする(ARCHITECTURE 10.3) |
| `[storage] endpoint`、`region`、`bucket`、`access_key_id`、`secret_access_key` | S3 互換のオブジェクトストレージ。endpoint と region は事業者の S3 互換 API のもの(region の概念がなければ `auto`)。パス形式(`https://<endpoint>/<bucket>/<key>`)で接続する。バケットは先に作っておく(`koroku` も restic もバケットは作らない)。このセクションがないと、ログは VPS 内にだけ残り、バックアップは無効になる |
Expand Down
Loading