OAuthを利用していないCodeBuildソースクレデンシャルの修正手順

このブログシリーズ 「クラウドセキュリティ 実践集」 では、一般的なセキュリティ課題を取り上げ、「なぜ危険なのか?」 というリスクの解説から、「どうやって直すのか?」 という具体的な修復手順(コンソール、AWS CLI、Terraformなど)まで、分かりやすく解説します。

この記事では、OAuthを用いずに接続されたCodeBuildソースクレデンシャルについて、そのリスクと対策を解説します。

画像に alt 属性が指定されていません。ファイル名: 13e11608c3ab504725ce4500088eb55e-1024x341.webp

ポリシーの説明

[CodeBuild.1] CodeBuild Bitbucket ソースリポジトリの URL には機密認証情報を含めないでください

CodeBuild の Security Hub コントロール – AWS Security Hub

このコントロールは、 AWS CodeBuild プロジェクトの Bitbucket ソースリポジトリ URL に個人用アクセストークンが含まれているか、ユーザー名とパスワードが含まれているかをチェックします。コントロールは、Bitbucket のソースレポジトリの URL に、個人用のアクセストークンまたはユーザー名とパスワードが含まれているかどうかをチェックします。

リスクとしては、AWS CodeBuild プロジェクトの Bitbucket ソースリポジトリ URL に個人用アクセストークンやユーザー名・パスワードなどの機密認証情報が含まれている場合、これらの情報が意図しないデータ漏えいや不正アクセスにさらされる可能性があります。

このリスクを回避するために、CodeBuild プロジェクトのソースリポジトリ URL から個人用アクセストークンやユーザー名・パスワードを削除し、代わりに OAuth を使用して Bitbucket リポジトリに接続することが望ましいです。

修復方法

  1. CodeBuildの管理コンソールから対象のビルドプロジェクトを選択します。
  2. 「アクション」→「編集」の順番に選択します。

3. 「ソースプロバイダ」でGitHubを選択後、「Use override credentials for this project only」にチェックいれます。

  1. GitHub アプリを選択します。 表示されていなければ「CodeConnections経由で新しいGitHub」のリンクをクリックし、接続アプリを作成してください。
  2. 設定完了後、画面下の「ソースの更新」を押下して更新は完了です。

Terraformでの修復手順

# CodeBuild source credentials for GitHub using OAuth
resource "aws_codebuild_source_credential" "github" {
  auth_type   = "OAUTH"
  server_type = "GITHUB"
  token       = var.github_oauth_token
}

# S3 bucket for CodeBuild artifacts
resource "aws_s3_bucket" "codebuild_artifacts" {
  bucket = "secure-codebuild-artifacts-${data.aws_caller_identity.current.account_id}"
}

# S3 bucket versioning
resource "aws_s3_bucket_versioning" "codebuild_artifacts" {
  bucket = aws_s3_bucket.codebuild_artifacts.id
  versioning_configuration {
    status = "Enabled"
  }
}

# S3 bucket encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "codebuild_artifacts" {
  bucket = aws_s3_bucket.codebuild_artifacts.id

  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "aws:kms"
      kms_master_key_id = aws_kms_key.codebuild.arn
    }
  }
}

# KMS key for artifact encryption
resource "aws_kms_key" "codebuild" {
  description             = "KMS key for CodeBuild artifacts encryption"
  deletion_window_in_days = 7
  enable_key_rotation     = true
}

# CodeBuild service role
resource "aws_iam_role" "codebuild_service_role" {
  name = "codebuild-service-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Principal = {
          Service = "codebuild.amazonaws.com"
        }
      }
    ]
  })
}

# CodeBuild service role policy
resource "aws_iam_role_policy" "codebuild_service_role_policy" {
  role = aws_iam_role.codebuild_service_role.name

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Resource = [
          aws_s3_bucket.codebuild_artifacts.arn,
          "${aws_s3_bucket.codebuild_artifacts.arn}/*"
        ]
        Action = [
          "s3:GetObject",
          "s3:GetObjectVersion",
          "s3:PutObject",
          "s3:ListBucket"
        ]
      },
      {
        Effect = "Allow"
        Resource = [
          aws_kms_key.codebuild.arn
        ]
        Action = [
          "kms:Decrypt",
          "kms:Encrypt",
          "kms:GenerateDataKey"
        ]
      },
      {
        Effect = "Allow"
        Resource = ["*"]
        Action = [
          "logs:CreateLogGroup",
          "logs:CreateLogStream",
          "logs:PutLogEvents"
        ]
      }
    ]
  })
}

# Example GitHub project
resource "aws_codebuild_project" "github_project" {
  name           = "secure-github-project"
  description    = "Secure CodeBuild project using GitHub OAuth"
  build_timeout  = "60"
  service_role   = aws_iam_role.codebuild_service_role.arn
  encryption_key = aws_kms_key.codebuild.arn

  artifacts {
    type = "S3"
    location = aws_s3_bucket.codebuild_artifacts.id
  }

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                       = "aws/codebuild/amazonlinux2-x86_64-standard:4.0"
    type                       = "LINUX_CONTAINER"
    image_pull_credentials_type = "CODEBUILD"
    privileged_mode            = false

    environment_variable {
      name  = "ENVIRONMENT"
      value = "production"
    }
  }

  logs_config {
    cloudwatch_logs {
      status = "ENABLED"
      group_name = "/aws/codebuild/secure-github-project"
    }
  }

  source {
    type            = "GITHUB"
    location        = "<https://github.com/organization/repository.git>"
    git_clone_depth = 1
    buildspec       = "buildspec.yml"
    
    git_submodules_config {
      fetch_submodules = false
    }
  }

  source_version = "main"

  vpc_config {
    vpc_id = var.vpc_id
    subnets = var.private_subnet_ids
    security_group_ids = [aws_security_group.codebuild.id]
  }

  tags = {
    Environment = "Production"
    Secure     = "true"
  }

  depends_on = [aws_codebuild_source_credential.github]
}

# Security group for CodeBuild
resource "aws_security_group" "codebuild" {
  name        = "codebuild-security-group"
  description = "Security group for CodeBuild projects"
  vpc_id      = var.vpc_id

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "codebuild-sg"
  }
}

# Variables
variable "github_oauth_token" {
  description = "GitHub OAuth token"
  type        = string
  sensitive   = true
}

variable "vpc_id" {
  description = "VPC ID for CodeBuild projects"
  type        = string
}

variable "private_subnet_ids" {
  description = "Private subnet IDs for CodeBuild projects"
  type        = list(string)
}

# Data sources
data "aws_caller_identity" "current" {}

最後に

今回は、CodeBuild プロジェクトの Bitbucket ソースリポジトリ URL に機密認証情報を含めることのリスクとその対策についてご紹介しました。ソースリポジトリ URL に機密認証情報を含めると、意図しないデータ漏えいや不正アクセスのリスクが高まります。設定を確認し、該当する場合は本記事を参考に修正してみてください。

この問題の検出は弊社が提供するSecurifyのCSPM機能で簡単に検出及び管理する事が可能です。

運用が非常に楽に出来る製品になっていますので、ぜひ興味がある方はお問い合わせお待ちしております

最後までお読みいただきありがとうございました。この記事が皆さんの役に立てば幸いです

この記事をシェアする

クラウドセキュリティ対策実践集一覧へ戻る

貴社の利用状況に合わせた見積もりを作成します。

料金プランを詳しく見る