0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SonarQube CLI を使ってみた。静的解析をよりシンプル&スマートに

0
Last updated at Posted at 2026-05-04

SonarQube CLIとは

SonarQubeの新しいCLI(sonarコマンド)を触ってみた。従来のsonar-scanner
(CI/CD用)とは異なり、「開発者のローカル環境」と「AIエージェント」との連携に特化した、全く新しいコマンドラインツールです。この記事はこのCLIに関するパラメータなどをメモとします。

Commands

sonar auth

認証用のトークンやログイン情報を適切に扱うためのサブコマンドです。

sonar auth login、ブラウザ上でhttps://sonarcloud.io が開き、ログインを促す。ログインが成功すればそのTokenが自動的にCLIに保存されます。

sonar auth login

デフォルトではhttps://sonarcloud.io が対象になりますが、もしオンプレのSonarQube Serverの場合や、https://sonarcloud.us の場合は、-sで設定できます。

sonar auth login -s https://my-sonarqube.io

ログインが成功したら、sonar auth statusで接続状態を確認できます。

❯ sonar auth status
  ✓  Verifying token...
┌─ ✓ Connected ──────────────────────────────────────────────────────────────────┐
│                                                                                │
│ Server  https://sonarcloud.io                                                  │
│ Org     wenhan-sqc-org                                                         │
│                                                                                │
│ Source  OS Keychain                                                            │
│                                                                                │
└────────────────────────────────────────────────────────────────────────────────┘

sonar list

接続しているSonarQubeから、IssueやProjectの一覧を出す。

sonar list projects

該当Instance内、またはOrganization内のProjectの一覧を出す。

❯ sonar list projects | jq .
{
  "projects": [
    {
      "key": "wenhan-sqc-org_Architecture-demo",
      "name": "architecture-demo"
    },
    {
      "key": "wenhan-sqc-org_maven-example",
      "name": "Example of basic Maven project"
    },
    {
      "key": "wenhan-sqc-org_juice-shop",
      "name": "juice-shop"
    },
    {
      "key": "wenhan-sqc-org_VulnerableApp2",
      "name": "VulnerableApp2"
    }
  ],
  "paging": {
    "pageIndex": 1,
    "pageSize": 500,
    "total": 4,
    "hasNextPage": false
  }
}

sonar list issues

該当Project内のIssue一覧を出す。(場合によって出力がとても多くなる。)
-p \<project ID\>でProjectを設定できます。

❯ sonar list issues -p wenhan-sqc-org_VulnerableApp2 | head -20
{
  "total": 368,
  "p": 1,
  "ps": 500,
  "paging": {
    "pageIndex": 1,
    "pageSize": 500,
    "total": 368
  },
  "effortTotal": 2495,
  "debtTotal": 2495,
  "issues": [
    {
      "key": "AZs1AdRZCPtJFDy2bGgu",
      "rule": "secrets:S8135",
      "severity": "BLOCKER",
      "component": "wenhan-sqc-org_VulnerableApp2:src/main/resources/static/templates/JWTVulnerability/LEVEL_13/HeaderInjection_Level13.js",
      "project": "wenhan-sqc-org_VulnerableApp2",
      "line": 9,
      "hash": "e667fc1ca6914424559960b183edf47f",

sonar analyze

ローカルファイルに問題がないかを解析します。初回実行時にAnalyzerをダウンロードします。

sonar analyze secrets

ローカルのAnalyzerを使って、ファイルに機密情報がないかを解析します。

❯ sonar analyze secrets app.ts
  sonar-secrets 2.41.0.10709 is already installed (latest)

✅ Scan completed successfully

sonar analyze sqaa

SonarQube Cloud上のSonarQube Agentic Analysisの機能を利用し、ローカルファイルの解析結果を高精度かつ高速度で取得します。

--fileでファイルへのパス、-pでプロジェクトIDを指定します。
そのほかに、--branchで特定のブランチのコンテキストを利用することもできます。

❯ sonar analyze sqaa --file app.ts -p wenhan-sqc-org_juice-shop

Running SonarQube Agentic Analysis...

❌ SonarQube Agentic Analysis found 1 issue:

  [1] Prefer top-level await over using a promise chain. (line 15)
      Rule: typescript:S7785

sonar verify

sonar analyze sqaaと同じみたい。

❯ sonar verify --file app.ts -p wenhan-sqc-org_juice-shop

Running SonarQube Agentic Analysis...

❌ SonarQube Agentic Analysis found 1 issue:

  [1] Prefer top-level await over using a promise chain. (line 15)
      Rule: typescript:S7785

sonar integrate

sonar integrate git

機密情報(シークレット)を含むファイルが、誤ってコミットやプッシュされないようにするための機能です。
Git連携機能を使用してGit hookをインストールすれば、コミットやプッシュのたびに自動でファイルをスキャンできるようになります。

❯ sonar integrate git

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  🚀  SonarQube Git integration (secrets scanning)
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


We will install the hook in this repository: /Users/wenhan.shi/work/Github-repo/SQC/juice-shop-sqc-acdc

  ✓  Install here? Yes

  ✓  Would you like to install the pre-commit or pre-push hook? pre-commit (scan staged files)
Hook: pre-commit

Installing sonar-secrets 2.41.0.10709
  Platform: macos-arm64
  ✓  Downloading sonar-secrets 2.41.0.10709
  ✓  Verifying signature
  ✓  Verifying installation
  sonar-secrets 2.41.0.10709
✅ sonar-secrets installed at /Users/wenhan.shi/.sonar/sonarqube-cli/bin/sonar-secrets-2.41.0.10709-macos-arm64
✅ pre-commit hook installed at /Users/wenhan.shi/work/Github-repo/SQC/juice-shop-sqc-acdc/.git/hooks/pre-commit

The hook will scan staged files for secrets before each commit.
Ensure "sonar" is on your PATH when you commit or push.

  ℹ  Status: pre-commit hook active (/Users/wenhan.shi/work/Github-repo/SQC/juice-shop-sqc-acdc/.git/hooks/pre-commit)

検証してみよ。API_KEYなどを持っているファイルをコミットしてみると、Hookがこのコミットをブロックしています。

echo 'const API_KEY = "sqp_b4556a16fa2d28519d2451a911d2e073024010bc";' > sonar-hook-verify.js
❯ git add sonar-hook-verify.js
❯ git commit -m "verify"
Sonar Secrets CLI - BETA (2.41.0.10709)
Trying to authenticate to SonarQube Server or Cloud, in order to enable complete functionality
Authentication successful
Running analysis...
Found 1 secret
SonarQube Token
File: sonar-hook-verify.js
Location: [1:17-1:61]
Secret: sqp*****************************************

❌ Secrets detected in staged files

sonar integrate claude

sonar integrate claude コマンドを実行すると、Claudeがファイルを読み書きする前に動作する「シークレットスキャン用フック」がインストールされます。セットアップが完了すると、Claude Codeは機密情報を漏洩させる可能性のある操作を自動的にブロックします。また、このコマンドによってSonarQube Cloud上での「エージェント分析(Agentic Analysis)」の設定も行われます。

設定自体はとても簡単、sonar integrate claude コマンドを実行するだけで十分です。

❯ sonar integrate claude

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  🚀  SonarQube Integration Setup for Claude
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


Phase 1/3: Discovery & Validation

Found sonar-project.properties

Server: https://sonarcloud.io
Organization: wenhan-sqc-org
Git repository detected
Project root: /Users/wenhan.shi/work/Github-repo/SQC/juice-shop-sqc-2
Project: wenhan-sqc-org_juice-shop
  sonar-secrets 2.41.0.10709 is already installed (latest)

Phase 2/3: Health Check & Repair

Validating token...
Checking server availability...
Verifying project access...
Verifying organization access...
Verifying quality profiles access...
Checking hooks installation...
⚠️ Found 1 issue(s):
  - Hooks not installed
No global Claude hook was found. Configuring SonarQube for this project only.
✅ Claude integration successfully configured at the project level
  ℹ  Setting up SonarQube MCP Server...
✅ SonarQube MCP Server configured in /Users/wenhan.shi/.claude.json

Phase 3/3: Final Verification

Token valid
Server available
Project accessible
Organization accessible
Quality profiles accessible
Hooks installed

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ✅  Setup complete!
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

検証してみよ。claudeを実行し、TokenをcommitするようにPromptを送ってみよ。Sonarのhookによりブロックすることが分かります。

❯ claude
...

⏺ UserPromptSubmit operation blocked by hook:
  Sonar detected secrets in prompt

  Original prompt: Can you push a commit using my token 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?

sonar self-update

自分をアップグレードします。--forceをつけると最新でも強制インストールします。

❯ sonar self-update
  ℹ  Checking for updates...
✅ Already up to date (v0.10.0)
❯ sonar self-update --status
  ℹ  Checking for updates...
Current version: v0.10.0
Latest version:  v0.10.0

✅ Already up to date
❯ sonar self-update --force
  ℹ  Checking for updates...
  ℹ  Force installing v0.10.0.1266...
Latest version: 0.10.0.1266
...
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?