GitHub ActionsとYelp detect-secretsでアクセスキー、秘密鍵等の認証情報がリポジトリに入ってないか検査する


Yelp detect-secretsとは?

Yelp detect-secrets はクラウドのアクセスキー、ユーザーID、パスワードなどの機密情報、SSHの秘密鍵等が指定したディレクトリやGitリポジトリに入ってないか検査するツールです

類似製品としては以下の様なものがあります

検査できる秘匿情報の種類

Yelp detect-secrets には以下の様な秘匿情報の検出プラグインが利用できます

ArtifactoryDetector
AWSKeyDetector
AzureStorageKeyDetector
BasicAuthDetector
CloudantDetector
Base64HighEntropyString
HexHighEntropyString
IbmCloudIamDetector
IbmCosHmacDetector
JwtTokenDetector
KeywordDetector
MailchimpDetector
NpmDetector
PrivateKeyDetector
SlackDetector
SoftlayerDetector
StripeDetector
TwilioKeyDetector

簡単な使い方

インストール


pip install detect-secrets

実行方法

Gitリポジトリのディレクトリで以下のコマンドを実行します

detect-secrets scan > results.json

検査結果がresults.jsonに格納されます。

GitHub ActionsでYelp detect-secretsを利用する

以下の様な処理を書く事で秘匿情報が混入していないか検査できます

検査結果をresults.jsonへ格納した後、検査結果の文字数をカウントしています。検査がパスすると検査結果には文字列が入りません。検査が失敗すると失敗した箇所とどのような秘匿情報が混入しているかの結果が入ります。

results.json
{
  "version": "1.0.3",
  "plugins_used": [
    {
      "name": "ArtifactoryDetector"
    },
    {
      "name": "AWSKeyDetector"
    },
    {
      "name": "AzureStorageKeyDetector"
    },
    {
      "name": "Base64HighEntropyString",
      "limit": 4.5
    },
    {
      "name": "BasicAuthDetector"
    },
    {
      "name": "CloudantDetector"
    },
    {
      "name": "HexHighEntropyString",
      "limit": 3.0
    },
    {
      "name": "IbmCloudIamDetector"
    },
    {
      "name": "IbmCosHmacDetector"
    },
    {
      "name": "JwtTokenDetector"
    },
    {
      "name": "KeywordDetector",
      "keyword_exclude": ""
    },
    {
      "name": "MailchimpDetector"
    },
    {
      "name": "NpmDetector"
    },
    {
      "name": "PrivateKeyDetector"
    },
    {
      "name": "SlackDetector"
    },
    {
      "name": "SoftlayerDetector"
    },
    {
      "name": "SquareOAuthDetector"
    },
    {
      "name": "StripeDetector"
    },
    {
      "name": "TwilioKeyDetector"
    }
  ],
  "filters_used": [
    {
      "path": "detect_secrets.filters.allowlist.is_line_allowlisted"
    },
    {
      "path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
      "min_level": 2
    },
    {
      "path": "detect_secrets.filters.heuristic.is_indirect_reference"
    },
    {
      "path": "detect_secrets.filters.heuristic.is_likely_id_string"
    },
    {
      "path": "detect_secrets.filters.heuristic.is_potential_uuid"
    },
    {
      "path": "detect_secrets.filters.heuristic.is_prefixed_with_dollar_sign"
    },
    {
      "path": "detect_secrets.filters.heuristic.is_sequential_string"
    },
    {
      "path": "detect_secrets.filters.heuristic.is_templated_secret"
    }
  ],
  "results": {
    ".github/workflows/build.yml": [
      {
        "type": "Secret Keyword",
        "filename": ".github/workflows/build.yml",
        "hashed_secret": "93a1fe0b6019fc2c461b2ae605dbe6ae2d579ea7",
        "is_verified": false,
        "line_number": 87
      }
    ]
  },
  "generated_at": "2021-03-08T00:35:39Z"
}

GitHub Actionsで導入する際は検査結果をjqでパースしJSONファイルに記述された文字数をカウントします。文字数が2文字より多ければ機密情報がリポジトリに混入されていると判断しエラーメッセージを出力し異常終了します。

.github/workflows/build.yml
---
name: build
on: [push]

jobs:
  detect-secrets:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'

      - name: Install Yelp detect-secrets
        run: |
          pip install detect-secrets

      - name: Execulte detect-secrets(disabled KeywordDetector plugin)
        run: |
          detect-secrets scan --disable-plugin KeywordDetector \
          | jq .results > results.json

      - name: Check whether detect-secrets found vulnerability
        run: |
          line=$(cat results.json | wc -w)
          if [ $line -gt 2 ]; then
            echo "vulnerability was found"
            detect-secrets scan | jq .results -C
            exit 1;
          fi

その他

秘匿情報の混入検査はgit commit前に済ませておくべきです。実際の開発ではAWS Lab's git-secrets等が導入されpre-commit前に検査が終了している状態が理想です。

参考サイト