diff --git a/CHANGELOG.md b/CHANGELOG.md index b87ad22..3a0b268 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ バージョンごとの変更。タグはバージョンと対応する。 +## v0.5.2 + +- auth の introspection (`/introspect`) に共有の秘密を付けられるようにした。`AUTH_INTROSPECT_SECRET` (auth の `INTROSPECT_SECRET` と同じ値) があれば `X-Toro-Token` で送る。無ければ従来どおり付けない + ## v0.5.1 - Go を 1.27.4 へ更新した (標準ライブラリ net/http の脆弱性 GO-2026-6859 の修正)。コードの変更は無い diff --git a/compose.example.yaml b/compose.example.yaml index 34de25f..d891c7c 100644 --- a/compose.example.yaml +++ b/compose.example.yaml @@ -62,7 +62,7 @@ # observe → dry-run → review (人や Muse が見て決める) → live (自動でロールバックまで) の順に上げる - CRIMEBUSTER_MODE=observe # 操作のためのコネクタ (:8090、内部のネットワークだけ。ports は付けない)。TORO-MCP が、利用者の OAuth のトークンをそのまま渡す。 - # コネクタが auth の /introspect に尋ね、いまの Discord のロールが Admin 以上 (ADMIN_ROLE_IDS) の人だけを通す。共有の秘密は無い + # コネクタが auth の /introspect に尋ね、いまの Discord のロールが Admin 以上 (ADMIN_ROLE_IDS) の人だけを通す。auth が INTROSPECT_SECRET を求めるときは、AUTH_INTROSPECT_SECRET に同じ値を渡す (秘密は env ファイルへ) - AUTH_INTERNAL_URL=http://auth.internal.example:8081 - ADMIN_ROLE_IDS= - DB_HOST=db.internal.example diff --git a/docs/configuration.md b/docs/configuration.md index 3581fff..4dd35e9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -20,6 +20,7 @@ | --- | --- | --- | | `CONNECTOR_ADDR` | `:8090` | コネクタの待ち受けアドレス。空ならコネクタを開かない | | `AUTH_INTERNAL_URL` | (無し。環境変数で指定) | 認証サービスの introspection の接続先 (内部のネットワークだけ)。コネクタを開くとき (`CONNECTOR_ADDR` が空でないとき) は必須 | +| `AUTH_INTROSPECT_SECRET` | (空。秘密) | 認証サービス (auth) の共有の秘密と同じ値。空でなければ、introspection の呼び出しに `X-Toro-Token` として付ける (認証サービスが共有の秘密を求めるとき)。env ファイルに置き、リポジトリには書かない | | `ADMIN_ROLE_IDS` | (無し。環境変数で指定) | コネクタを使える Discord のロールの ID (コンマ区切り)。指定が無ければ、どのロールでも通さない。操作を受けるモードでは必須 | | `CONNECTOR_REQUIRE_LIVE_ROLES` | `true` | 認可で、いまの Discord のロール (roles_source が live のもの) だけを信じるか。 | | `CONNECTOR_DISCORD_ALLOW` | `^discord_[a-z0-9_]+$` (全てのツール) | コネクタから呼んでよい Discord のツール名の正規表現 | diff --git a/internal/config/config.go b/internal/config/config.go index 87d0eb8..81aec9c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -102,11 +102,12 @@ type ShotConfig struct { // 認証は OAuth 2.0 のトークン: auth の introspection (/introspect) に尋ね、いまの Discord のロールが AdminRoles のどれかであること。 // Addr が空ならコネクタは開かない。 type ConnectorConfig struct { - Addr string - AuthInternalURL string // auth の introspection の接続先 (内部のネットワークだけ) - AdminRoles []string // コネクタを使える Discord のロールの ID (Owner と Admin) - RequireLiveRoles bool // true なら、auth がいまの Discord で確かめたロール (roles_source=live) だけを信じる - DiscordAllow string // コネクタから呼んでよい Discord のツール名の正規表現 + Addr string + AuthInternalURL string // auth の introspection の接続先 (内部のネットワークだけ) + AuthIntrospectSecret string // auth の INTROSPECT_SECRET と同じ値。空でなければ introspection の呼び出しに X-Toro-Token として付ける + AdminRoles []string // コネクタを使える Discord のロールの ID (Owner と Admin) + RequireLiveRoles bool // true なら、auth がいまの Discord で確かめたロール (roles_source=live) だけを信じる + DiscordAllow string // コネクタから呼んでよい Discord のツール名の正規表現 } // StorageConfig はファイルの保存先。 @@ -220,11 +221,12 @@ func LoadConfig() (*Config, error) { ShotHeight: envInt("SHOT_HEIGHT", 720), }, ConnectorConfig: ConnectorConfig{ - Addr: envStr("CONNECTOR_ADDR", ":8090"), - AuthInternalURL: strings.TrimRight(envStr("AUTH_INTERNAL_URL", ""), "/"), - AdminRoles: splitList(envStr("ADMIN_ROLE_IDS", "")), - RequireLiveRoles: envBool("CONNECTOR_REQUIRE_LIVE_ROLES", true), - DiscordAllow: envStr("CONNECTOR_DISCORD_ALLOW", DefaultDiscordAllow), + Addr: envStr("CONNECTOR_ADDR", ":8090"), + AuthInternalURL: strings.TrimRight(envStr("AUTH_INTERNAL_URL", ""), "/"), + AuthIntrospectSecret: envStr("AUTH_INTROSPECT_SECRET", ""), + AdminRoles: splitList(envStr("ADMIN_ROLE_IDS", "")), + RequireLiveRoles: envBool("CONNECTOR_REQUIRE_LIVE_ROLES", true), + DiscordAllow: envStr("CONNECTOR_DISCORD_ALLOW", DefaultDiscordAllow), }, StorageConfig: StorageConfig{ WarpsDir: envStr("WARPS_DIR", ""), diff --git a/internal/connector/authz.go b/internal/connector/authz.go index 4c8af23..68d40d6 100644 --- a/internal/connector/authz.go +++ b/internal/connector/authz.go @@ -44,6 +44,7 @@ func principalOf(r *http.Request) *Principal { } type Authz struct { + secret string // auth の INTROSPECT_SECRET (空なら付けない) url string admins map[string]bool requireLive bool @@ -55,7 +56,7 @@ func NewAuthz(cfg *config.Config) *Authz { for _, r := range cfg.ConnectorConfig.AdminRoles { admins[r] = true } - return &Authz{url: cfg.ConnectorConfig.AuthInternalURL, admins: admins, requireLive: cfg.ConnectorConfig.RequireLiveRoles, http: &http.Client{Timeout: introspectTimeout}} + return &Authz{secret: cfg.ConnectorConfig.AuthIntrospectSecret, url: cfg.ConnectorConfig.AuthInternalURL, admins: admins, requireLive: cfg.ConnectorConfig.RequireLiveRoles, http: &http.Client{Timeout: introspectTimeout}} } type authResult int @@ -78,6 +79,10 @@ func (a *Authz) Check(ctx context.Context, token string) (*Principal, authResult return nil, authUnavailable, "auth への依頼を作れません" } req.Header.Set("Content-Type", "application/json") + if a.secret != "" { + // auth が共有秘密を求めるとき (INTROSPECT_SECRET) の呼び出し側の証明 + req.Header.Set("X-Toro-Token", a.secret) + } res, err := a.http.Do(req) if err != nil { return nil, authUnavailable, "auth に尋ねられません" diff --git a/internal/connector/connector_test.go b/internal/connector/connector_test.go index 656a856..92fe5ba 100644 --- a/internal/connector/connector_test.go +++ b/internal/connector/connector_test.go @@ -790,3 +790,22 @@ func TestConnectorScreenshotValidation(t *testing.T) { t.Errorf("unknown job: %d, want 404", code) } } + +// auth の共有秘密 (INTROSPECT_SECRET) は、設定したときだけ X-Toro-Token として付く。 +func TestAuthzSendsTheSharedSecretOnlyWhenConfigured(t *testing.T) { + var got []string + auth := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + got = append(got, r.Header.Get("X-Toro-Token")) + _, _ = w.Write([]byte(`{"active":true,"claims":{"id":"1","roles":["` + adminRole + `"],"roles_source":"live"}}`)) + })) + defer auth.Close() + for _, secret := range []string{"", "shared-secret-value"} { + a := NewAuthz(&config.Config{ConnectorConfig: config.ConnectorConfig{AuthInternalURL: auth.URL, AuthIntrospectSecret: secret, AdminRoles: []string{adminRole}, RequireLiveRoles: true}}) + if p, res, _ := a.Check(context.Background(), "tok"); p == nil || res != authOK { + t.Fatalf("%v %v", p, res) + } + } + if len(got) != 2 || got[0] != "" || got[1] != "shared-secret-value" { + t.Fatalf("got %q", got) + } +}