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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 の修正)。コードの変更は無い
Expand Down
2 changes: 1 addition & 1 deletion compose.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 のツール名の正規表現 |
Expand Down
22 changes: 12 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 はファイルの保存先。
Expand Down Expand Up @@ -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", ""),
Expand Down
7 changes: 6 additions & 1 deletion internal/connector/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 に尋ねられません"
Expand Down
19 changes: 19 additions & 0 deletions internal/connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}