Skip to content

fix: BuildKit secretへのアクセス権限を修正 - #174

Merged
keito4 merged 1 commit into
mainfrom
fix/docker-plugin-secret-permission
Dec 24, 2025
Merged

fix: BuildKit secretへのアクセス権限を修正#174
keito4 merged 1 commit into
mainfrom
fix/docker-plugin-secret-permission

Conversation

@keito4

@keito4 keito4 commented Dec 24, 2025

Copy link
Copy Markdown
Owner

問題

v1.6.1のDockerイメージでもプラグインが正しくインストールされていませんでした。

ビルドログに記録されたエラー

cp: cannot open '/run/secrets/claude_credentials' for reading: Permission denied

根本原因の詳細分析

  1. ユーザー切り替えのタイミング

    USER vscode  # ← ここで vscode ユーザーに切り替え
    RUN --mount=type=secret,id=claude_credentials \  # ← vscode ユーザーで実行
        /tmp/install-claude-plugins.sh ...
  2. BuildKit secretのデフォルト動作

    • Secretは root ユーザー (uid=0, gid=0) でマウントされる
    • デフォルトのパーミッション: -r-------- (400)
    • vscode ユーザーには読み取り権限がない
  3. 結果

    • スクリプトが secret ファイルにアクセスできない
    • プラグインインストールが実行されない
    • || true で失敗が無視される

解決策

Dockerfile の修正

Before (v1.6.1):

USER vscode
RUN --mount=type=secret,id=claude_credentials \
    /tmp/install-claude-plugins.sh /home/vscode/.claude/plugins/plugins.txt || true
USER root

After (v1.6.2):

RUN --mount=type=secret,id=claude_credentials,uid=0,gid=0 \
    /tmp/install-claude-plugins.sh /home/vscode/.claude/plugins/plugins.txt || true \
    && chown -R vscode:vscode /home/vscode/.claude

変更ポイント

項目 Before After 効果
実行ユーザー vscode root Secret アクセス可能
Secret mount デフォルト uid=0,gid=0 明示 明確化
所有権変更 なし chown 追加 vscode ユーザーで使用可能

期待効果

プラグインインストール

項目 v1.6.0 v1.6.1 v1.6.2 (This PR)
マーケットプレイス追加
Secret アクセス
プラグインインストール
エラーログ表示

ビルド時間

変更なし(処理内容は同じ)

セキュリティ

  • ✅ root 実行だが、インストール後に所有権を vscode に変更
  • .credentials.json は削除される(スクリプト内で実装済み)
  • ✅ Secret は一時的にのみアクセス可能

テスト計画

フェーズ1: ローカルでの確認

  1. ✅ Dockerfile の文法チェック
  2. ✅ pre-commit フック通過

フェーズ2: CI/CD ビルド

  1. ⏳ Docker ビルド実行
  2. ⏳ ビルドログでプラグインインストールログ確認
  3. ⏳ エラーログに「Permission denied」が出ないことを確認

フェーズ3: DevContainer 起動確認

  1. ⏳ v1.6.2 イメージで DevContainer 起動
  2. /plugin コマンドでエラーが出ないか確認
  3. ⏳ 15個のプラグインすべてが正常動作するか確認

ビルドログで確認すべき項目

成功時のログ:

[INFO] Claude プラグインのインストールを開始します...
[INFO] BuildKit secret から認証情報を読み込み中...
[INFO] マーケットプレイスを初期化中...
[INFO] プラグインをインストール中...
[SUCCESS]   完了: frontend-design@claude-code-plugins
[SUCCESS]   完了: hookify@claude-code-plugins
...
[INFO] プラグイン: 15 インストール完了、0 失敗/スキップ

影響範囲

Dockerビルド

  • ✅ プラグインが正しくインストールされる
  • ✅ ビルド時間は変わらず

ローカル環境

  • ✅ 影響なし

DevContainer 環境

  • ✅ プラグインが利用可能になる
  • /plugin コマンドでエラーが出なくなる

関連PR

ロールバック手順

問題が発生した場合:

# v1.6.1 に戻す
git revert <commit-hash>

# または、Dockerfile を v1.6.1 に戻す
git checkout v1.6.1 -- .devcontainer/Dockerfile

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Updated development container configuration to improve build process reliability and file permission handling.

✏️ Tip: You can customize this high-level summary in your review settings.

## 問題

v1.6.1でもプラグインインストールが失敗していた根本原因:

```
cp: cannot open '/run/secrets/claude_credentials' for reading: Permission denied
```

### 原因
1. `USER vscode` に切り替わった状態で RUN 実行
2. BuildKit secret は root ユーザーでマウントされる
3. vscode ユーザーには読み取り権限がない

## 解決策

### Before
```dockerfile
USER vscode
RUN --mount=type=secret,id=claude_credentials \
    /tmp/install-claude-plugins.sh ...
USER root
```

### After
```dockerfile
RUN --mount=type=secret,id=claude_credentials,uid=0,gid=0 \
    /tmp/install-claude-plugins.sh ... \
    && chown -R vscode:vscode /home/vscode/.claude
```

### 変更点
1. **USER vscode 削除**: root ユーザーのまま実行
2. **uid/gid 明示**: `uid=0,gid=0` でマウント
3. **所有権変更**: インストール後に `chown -R vscode:vscode`

## 期待効果

- ✅ BuildKit secret へのアクセス成功
- ✅ プラグインが正しくインストールされる
- ✅ インストール後のファイル所有権も正しく設定

## 影響範囲

- **Dockerビルド**: プラグインインストールが成功
- **ビルド時間**: 変化なし(処理内容は同じ)
- **セキュリティ**: root実行だが、インストール後に所有権を変更

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 24, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The .devcontainer/Dockerfile is modified to adjust BuildKit secret mounting permissions and file ownership for the Claude plugins directory. The build process now runs the installation script as root to access the secret, then reassigns directory ownership to the vscode user.

Changes

Cohort / File(s) Summary
Docker dev container configuration
.devcontainer/Dockerfile
BuildKit secret mounting changed from user vscode to root (uid=0,gid=0) for credential access; directory ownership reassigned to vscode via chown -R vscode:vscode /home/vscode/.claude

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

Poem

🐰 In the root's gentle paws, secrets safely dwell,
Then passed to vscode with a chown farewell.
Permissions dance in the Dockerfile's verse,
Building containers—for better, not worse! 🏗️

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title in Japanese directly describes the main change: fixing BuildKit secret access permissions in the Dockerfile, which aligns with the core issue and solution.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/docker-plugin-secret-permission

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5cd77d1 and 63e0a74.

📒 Files selected for processing (1)
  • .devcontainer/Dockerfile
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: CR
Repo: keito4/config PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-01T03:45:17.253Z
Learning: Applies to .github/workflows/docker-image.yml : Build DevContainer images automatically with semantic versioning and multi-platform support in .github/workflows/docker-image.yml
📚 Learning: 2025-12-01T03:45:17.253Z
Learnt from: CR
Repo: keito4/config PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-01T03:45:17.253Z
Learning: Applies to .github/workflows/docker-image.yml : Build DevContainer images automatically with semantic versioning and multi-platform support in .github/workflows/docker-image.yml

Applied to files:

  • .devcontainer/Dockerfile
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: claude-review
🔇 Additional comments (2)
.devcontainer/Dockerfile (2)

84-84: Good documentation of the permission fix approach.

The comment clearly explains why the installation runs as root and the subsequent ownership change, which will help future maintainers understand this security-sensitive operation.


85-87: Permission fix is correct; the || true masks credential errors and should be documented.

The solution correctly addresses the BuildKit secret permission issue:

  • uid=0,gid=0 makes the secret accessible to root
  • chown properly returns ownership to vscode after installation

The install script is safe for root execution: it properly handles credentials (copies with 600 permissions, cleans up after use), uses the established claude CLI tool, and contains no dangerous patterns (eval, exec, arbitrary rm, etc.).

However, the || true silently masks a critical error: if credentials are missing, the script exits with code 1 (line 44 of the script), but this failure is hidden. Either remove || true if plugin installation is required, or document that this allows the build to succeed without credentials and skip plugin installation.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@claude

claude Bot commented Dec 24, 2025

Copy link
Copy Markdown
Contributor

PRレビュー結果

全体評価: ✅ 承認(条件付き)

このPRはBuildKit secretへのアクセス権限問題を正しく解決しています。変更内容は最小限で、問題の根本原因に対する適切な修正です。


1. コード品質とベストプラクティス

✅ 良い点

  1. 問題の根本原因を正確に特定

    • vscodeユーザーからrootユーザーへの実行変更は、BuildKit secretのパーミッション(400, uid=0)を考慮した正しい判断
    • .devcontainer/Dockerfile:85でのuid=0,gid=0の明示は、意図を明確にする良いプラクティス
  2. 最小権限の原則を維持

    • インストール後にchown -R vscode:vscodeでファイル所有権を適切に変更
    • スクリプト内(script/install-claude-plugins.sh:90)で認証情報を削除
  3. コメントによる文書化

    • .devcontainer/Dockerfile:84に変更理由を明記

⚠️ 改善提案

  1. エラーハンドリングの|| trueについて

    • .devcontainer/Dockerfile:86|| trueにより、プラグインインストール失敗が無視される
    • 推奨: ビルドログで失敗を確認する手順をドキュメント化
    • または: プラグインインストールを必須とする場合は|| trueを削除
  2. スクリプトのエラー出力改善

    • script/install-claude-plugins.sh:26-28でcpが失敗した場合、エラーメッセージが不十分
    • 推奨: エラーハンドリングを強化し、set -xでデバッグ出力を有効化するオプションを追加

2. 潜在的なバグと問題

✅ 問題なし

  • Docker multi-stage buildやマルチアーキテクチャビルド(linux/amd64, linux/arm64)との互換性を確認済み
  • chown -Rの実行タイミングは適切(プラグインインストール後)

⚠️ 注意事項

  1. secretが提供されない場合の動作
    • CI環境でCLAUDE_CODE_OAUTH_TOKENが設定されていない場合、ビルドは成功するがプラグインは未インストール
    • || trueにより失敗が隠蔽される可能性
    • 推奨: CI/CD環境でビルドログに「[ERROR] 認証情報が見つかりません」が出力されていないか確認

3. パフォーマンス考慮事項

✅ 問題なし

  • chown -Rの追加による影響は微小(.claudeディレクトリのみ)
  • ビルドキャッシュ戦略(cache-from/cache-to)は維持されている

4. セキュリティ懸念

✅ セキュリティ対策は適切

  1. 認証情報の取り扱い

    • BuildKit secretは一時的にのみマウントされる
    • スクリプト内で.credentials.jsonを確実に削除(script/install-claude-plugins.sh:90)
  2. rootユーザー実行の妥当性

    • プラグインインストール処理のみroot実行
    • インストール後に所有権をvscodeに変更

⚠️ 推奨事項

  1. ANTHROPIC_API_KEYの環境変数フォールバック
    • script/install-claude-plugins.sh:30-39で環境変数からの認証情報作成をサポート
    • 本番環境では環境変数にAPIキーを設定しないことを推奨
    • 推奨: 環境変数フォールバックをドキュメントで明示的に非推奨とマーク

5. テストカバレッジ

❌ 不足している項目

重要: CLAUDE.mdの要求事項に基づき、以下のテストが不足しています:

  1. Dockerビルドの統合テスト

    • プラグインインストール成功/失敗のテストケースがない
    • 推奨: test/docker-build.test.jsを追加し、以下をテスト:
      • ✅ secretあり: プラグインインストール成功
      • ✅ secretなし: ビルド成功(プラグインスキップ)
      • ✅ ファイル所有権がvscode:vscodeであることを確認
  2. install-claude-plugins.shのユニットテスト

    • スクリプトの動作検証がない
    • 推奨: Bashテストフレームワーク(bats-core)で以下をテスト:
      • secret読み込み成功パス
      • 環境変数フォールバック
      • エラーハンドリング
  3. カバレッジ要件

    • CLAUDE.mdでは70%+の行カバレッジを要求
    • 現在のテスト: test/credential-filtering.test.js, test/config-validation.test.js
    • Dockerビルド関連のコードはカバレッジ計測対象外の可能性

推奨アクション

# テスト追加例
# test/docker-plugin-install.test.js
describe('Claude Plugin Installation', () => {
  it('should install plugins with valid secret', async () => {
    // Docker build with secret
    // Verify plugin installation logs
  });
  
  it('should skip plugins without secret', async () => {
    // Docker build without secret
    // Verify build succeeds with warning
  });
});

6. リポジトリ規約との整合性

✅ 準拠している項目

  1. Conventional Commits: ✅ コミットメッセージはfix:で開始
  2. PR説明の充実度: ✅ Why/What/How/Riskが明確
  3. 関連Issueのリンク: ⚠️ 明示的なIssue番号がない(PRで完結)

⚠️ 改善提案

  1. リリースタイプ要件

    • CLAUDE.mdによると、.devcontainer/**の変更はfeat/fix/perfを使用
    • fix:を使用しているため問題なし
  2. ドキュメント更新

    • README.mdやADRへの更新が不要か確認
    • 推奨: docs/adr/にBuildKit secret権限修正のADRを追加

7. 承認条件

必須対応(マージ前)

なし(すぐにマージ可能)

推奨対応(フォローアップPR)

  1. テストカバレッジの追加 (優先度: 高)

    • Dockerビルドの統合テストを追加
    • TDD原則に基づき、70%+カバレッジ達成
  2. エラーハンドリング改善 (優先度: 中)

    • || trueの必要性を再評価
    • プラグインインストール必須の場合は削除
  3. ドキュメント追加 (優先度: 中)

    • ADRでBuildKit secret権限の設計判断を記録
    • README.mdにプラグイン仕組みを追加

8. 総合評価

項目 評価 コメント
コード品質 ✅ 良好 最小限の変更で問題解決
セキュリティ ✅ 適切 認証情報の取り扱いは安全
パフォーマンス ✅ 問題なし 影響は微小
テストカバレッジ ❌ 不足 統合テストが必要
ドキュメント ⚠️ 改善余地 ADR追加を推奨

まとめ

即座にマージ可能ですが、フォローアップPRで以下を対応することを強く推奨します:

  1. ✅ Dockerビルドの統合テスト追加(70%+カバレッジ達成)
  2. ✅ ADRでBuildKit secret権限の設計判断を文書化
  3. ✅ エラーハンドリング戦略の見直し

🤖 レビュー実施者: Claude Sonnet 4.5
📋 レビュー基準: CLAUDE.md + リポジトリCLAUDE.md

@keito4
keito4 merged commit f2c5fe9 into main Dec 24, 2025
3 of 4 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.6.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released リリース済み

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant