5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

terrascan にも無視してもらいたい

Last updated at Posted at 2021-07-05

はじめに

terrascan は素晴らしいツールです

Terraform で書いたインフラの脆弱性を指摘してくれます

AWS などのクラウドインフラを構築する際、推奨の構成を知らないままだと、必要な設定が漏れていたり、よりセキュアな値になっていなかったりします

terrascan は私たちに最適でセキュアなインフラを教えてくれるのです

私は GitHub Actions の Super-Linter でいつもお世話になっています

しかし、やはり、どうしても無視してほしいときがあるのです

どうしても無視してほしい

AWS で API Gwateway を使って REST API を構築しています

resource "aws_api_gateway_rest_api" "hello" {
  name        = "Hello API"
  description = "Hello!!"
  body        = data.template_file.hello.rendered

  endpoint_configuration {
    types = ["EDGE"]
  }

  tags = local.tags.stage
}

これに対して、 terrascan は怒るのです

	Description    :	API Gateway Private Endpoints
	File           :	apigateway-stage.tf
	Line           :	1
	Severity       :	MEDIUM

endpoint_configurationtypesEDGE を指定しています

types はどのように API にアクセスするかを指定します

  • EDGE

    CloudFront を経由してアクセスします

    世界各地からアクセスされる場合に最適です

  • REGIONAL

    直接アクセスします

    同じ地域からのみアクセスする場合に最適です

  • PRIVATE

    VPC 内からのみアクセス可能にします

    システム内部の API の場合に最適です

当然、 PRIVATE が最もセキュアですが、今回作っているのは公開 API なので、そのような指摘はお節介なのです

なぜか無視できない

当然、無視したいときのためにコメントで指定できるようになっています

"ts:skip=<RULENAME><SKIP_REASON>" を無視したいブロックの中に書きます

resource "aws_db_instance" "aaa" {
  "ts:skip=AWS.RDS.DataSecurity.High.0414 need to skip this rule

早速、エラーメッセージに出ていた API Gateway Private Endpoints terrascan で検索します

すると、エラー一覧のドキュメントが出てきました

当該エラーの Reference IDAWS.APIGateway.Network Security.Medium.0570 になっていました

というわけで、無視のためのコメントを加えます

resource "aws_api_gateway_rest_api" "hello" {
  #ts:skip=AWS.APIGateway.Network Security.Medium.0570 This is public API
  name        = "Hello API"
  description = "Hello!!"
  body        = data.template_file.hello.rendered

  endpoint_configuration {
    types = ["EDGE"]
  }

  tags = local.tags.stage
}

が、なぜかエラーは変わらず

どういうわけだ?

詳細結果を見る

GitHub Actions の結果ではエラーの ID が分からないので、ローカルで実行して詳細結果を表示します

terrascan -t aws -v

多くのツールで -vverbose 詳細を意味します

これで実行すると、以下のようにエラーが表示されました

        Description    :        API Gateway Private Endpoints
        File           :        apigateway-stage.tf
        Module Name    :        root
        Plan Root      :        ./
        Line           :        1
        Severity       :        MEDIUM
        Rule Name      :        apiGatewayEndpointConfig
        Rule ID        :        AWS.APIGateway.NetworkSecurity.Medium.0570
        Resource Name  :        sodai-vision-api-stage
        Resource Type  :        aws_api_gateway_rest_api
        Category       :        Infrastructure Security

Rule IDAWS.APIGateway.NetworkSecurity.Medium.0570 です

つまり、スペースを省くのが正しかったのです

よく考えれば当然ですね

というわけで、以下のように修正します

resource "aws_api_gateway_rest_api" "hello" {
  #ts:skip=AWS.APIGateway.NetworkSecurity.Medium.0570 This is public API.
  name        = "Hello API"
  description = "Hello!!"
  body        = data.template_file.hello.rendered

  endpoint_configuration {
    types = ["EDGE"]
  }

  tags = local.tags.stage
}

これでエラーにならず、 Skipped Violations として表示されるようになりました

最後に

公式ドキュメントを見ても分からないこともあります

エラー時にはより詳細なログを見てみましょう

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?