Skip to content

Commit 6866fbf

Browse files
authored
Fix athena (#15)
* allow create_athena_query to be false
1 parent c464997 commit 6866fbf

File tree

12 files changed

+64
-102
lines changed

12 files changed

+64
-102
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,34 @@
1+
# excluding a few checks because it isn't coping well with the hacky way we do the rds monitoring lambda
12
exclude: ".terraform"
23
repos:
34
- repo: https://github.com/antonbabenko/pre-commit-terraform
4-
rev: v1.77.1
5+
rev: v1.88.0
56
hooks:
67
- id: terraform_docs
78
always_run: true
89
- id: terraform_fmt
10+
- id: terraform_validate
11+
args:
12+
- --hook-config=--retry-once-with-cleanup=true
913
- id: terraform_tflint
1014
alias: terraform_tflint_nocreds
1115
name: terraform_tflint_nocreds
12-
- id: terraform_tfsec
13-
- repo: local
14-
hooks:
15-
- id: terraform_validate
16-
name: terraform_validate
17-
entry: |
18-
bash -c '
19-
AWS_DEFAULT_REGION=us-east-1
20-
declare -a DIRS
21-
for FILE in "$@"
22-
do
23-
DIRS+=($(dirname "$FILE"))
24-
done
25-
for DIR in $(printf "%s\n" "${DIRS[@]}" | sort -u)
26-
do
27-
cd $(dirname "$FILE")
28-
terraform init --backend=false
29-
terraform validate .
30-
cd ..
31-
done
32-
'
33-
language: system
34-
verbose: true
35-
files: \.tf(vars)?$
36-
exclude: examples
37-
- id: tflock
38-
name: provider_locks
39-
entry: |
40-
bash -c '
41-
AWS_DEFAULT_REGION=us-east-1
42-
declare -a DIRS
43-
for FILE in "$@"
44-
do
45-
DIRS+=($(dirname "$FILE"))
46-
done
47-
for DIR in $(printf "%s\n" "${DIRS[@]}" | sort -u)
48-
do
49-
cd $(dirname "$FILE")
50-
terraform providers lock -platform=windows_amd64 -platform=darwin_amd64 -platform=linux_amd64
51-
cd ..
52-
done
53-
'
54-
language: system
55-
verbose: true
56-
files: \.tf(vars)?$
57-
exclude: examples
16+
- id: terraform_trivy
17+
args:
18+
- --args=--skip-dirs="**/.terraform,examples/*"
19+
- id: terraform_providers_lock
5820
- repo: https://github.com/pre-commit/pre-commit-hooks
59-
rev: v4.4.0
21+
rev: v4.5.0
6022
hooks:
23+
- id: check-added-large-files
6124
- id: check-case-conflict
6225
- id: check-json
6326
- id: check-merge-conflict
6427
- id: check-symlinks
6528
- id: check-yaml
6629
args:
6730
- --unsafe
31+
- id: detect-private-key
6832
- id: end-of-file-fixer
6933
- id: mixed-line-ending
7034
args:

.terraform-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
latest:^1.1
1+
latest:^1.6

.terraform.lock.hcl

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.tflint.hcl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ config {
22
module = true
33
}
44

5+
plugin "aws" {
6+
enabled = true
7+
version = "0.12.0"
8+
source = "github.com/terraform-linters/tflint-ruleset-aws"
9+
}
10+
511
rule "terraform_deprecated_interpolation" {
612
enabled = true
713
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Rhythmic Technologies, Inc.
3+
Copyright (c) 2024 Rhythmic Technologies, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ No modules.
5353
| [aws_iam_policy.athena](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
5454
| [aws_s3_bucket.athena_results](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
5555
| [aws_s3_bucket.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
56-
| [aws_s3_bucket_acl.athena_results](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
57-
| [aws_s3_bucket_ownership_controls.athena_results](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
56+
| [aws_s3_bucket_logging.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
5857
| [aws_s3_bucket_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
5958
| [aws_s3_bucket_public_access_block.athena_results](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
6059
| [aws_s3_bucket_public_access_block.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
6160
| [aws_s3_bucket_server_side_encryption_configuration.athena_results](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
61+
| [aws_s3_bucket_server_side_encryption_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
6262
| [aws_s3_bucket_versioning.athena_results](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
63+
| [aws_s3_bucket_versioning.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
6364
| [null_resource.create_table](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6465
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
6566
| [aws_elb_service_account.principal](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_service_account) | data source |
@@ -74,7 +75,7 @@ No modules.
7475
|------|-------------|------|---------|:--------:|
7576
| <a name="input_bucket_name"></a> [bucket\_name](#input\_bucket\_name) | Name to apply to bucket (use `bucket_name` or `bucket_suffix`) | `string` | `null` | no |
7677
| <a name="input_bucket_suffix"></a> [bucket\_suffix](#input\_bucket\_suffix) | Suffix to apply to the bucket (use `bucket_name` or `bucket_suffix`). When using `bucket_suffix`, the bucket name will be `[ACCOUNT_ID]-[REGION]-s3logging-[BUCKET_SUFFIX].` | `string` | `"elblogging"` | no |
77-
| <a name="input_create_athena_query"></a> [create\_athena\_query](#input\_create\_athena\_query) | Create an Athena table for querying ALB logs. Uses the aws cli | `bool` | `true` | no |
78+
| <a name="input_create_athena_query"></a> [create\_athena\_query](#input\_create\_athena\_query) | Create an Athena table for querying ALB logs. Uses the aws cli | `bool` | `false` | no |
7879
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | KMS key to encrypt bucket with. | `string` | `null` | no |
7980
| <a name="input_lifecycle_rules"></a> [lifecycle\_rules](#input\_lifecycle\_rules) | lifecycle rules to apply to the bucket | <pre>list(object(<br> {<br> id = string<br> enabled = bool<br> prefix = string<br> expiration = number<br> noncurrent_version_expiration = number<br> }))</pre> | `[]` | no |
8081
| <a name="input_s3_access_logging_bucket"></a> [s3\_access\_logging\_bucket](#input\_s3\_access\_logging\_bucket) | Optional target for S3 access logging | `string` | `null` | no |

athena_bucket.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# stores results from athena queries
44
##########################################
55

6-
#tfsec:ignore:aws-s3-enable-bucket-logging
6+
#trivy:ignore:avd-aws-0089
77
resource "aws_s3_bucket" "athena_results" {
88
count = var.create_athena_query ? 1 : 0
99

@@ -25,16 +25,6 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "athena_results" {
2525
}
2626
}
2727

28-
resource "aws_s3_bucket_ownership_controls" "athena_results" {
29-
count = var.create_athena_query ? 1 : 0
30-
31-
bucket = aws_s3_bucket.athena_results[0].id
32-
33-
rule {
34-
object_ownership = "BucketOwnerEnforced"
35-
}
36-
}
37-
3828
resource "aws_s3_bucket_public_access_block" "athena_results" {
3929
count = var.create_athena_query ? 1 : 0
4030

athena_iam.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# minimal IAM policy to allow querying of elb logs with athena
44
##########################################
55

6+
locals {
7+
athena_bucket_arn = try(aws_s3_bucket.athena_results[0].arn, "arn:${local.partition}:s3:::bucket")
8+
athena_workgroup_arn = try(aws_athena_workgroup.this[0].arn, "arn:${local.partition}:athena:${local.region}:${local.account_id}:workgroup/workgroup")
9+
}
610
#tfsec:ignore:aws-iam-no-policy-wildcards
711
data "aws_iam_policy_document" "athena" {
812
statement {
@@ -24,7 +28,7 @@ data "aws_iam_policy_document" "athena" {
2428

2529
statement {
2630
sid = "AllowRunWorkgroup"
27-
resources = [aws_athena_workgroup.this[0].arn]
31+
resources = [local.athena_workgroup_arn]
2832
actions = [
2933
"athena:UpdatePreparedStatement",
3034
"athena:StopQueryExecution",
@@ -55,7 +59,7 @@ data "aws_iam_policy_document" "athena" {
5559
"s3:ListBucket"
5660
]
5761
resources = [
58-
aws_s3_bucket.athena_results[0].arn,
62+
local.athena_bucket_arn,
5963
aws_s3_bucket.this.arn,
6064
]
6165
condition {
@@ -82,7 +86,7 @@ data "aws_iam_policy_document" "athena" {
8286

8387
statement {
8488
sid = "AllowWriteResults"
85-
resources = ["${aws_s3_bucket.athena_results[0].arn}/*"]
89+
resources = ["${local.athena_bucket_arn}/*"]
8690
actions = [
8791
"s3:GetObject",
8892
"s3:PutObject"
@@ -98,7 +102,7 @@ data "aws_iam_policy_document" "athena" {
98102
resource "aws_iam_policy" "athena" {
99103
count = var.create_athena_query ? 1 : 0
100104

101-
name = "athena_query_elb_logs"
105+
name_prefix = "athena_query_elb_logs"
102106
path = "/"
103107
description = "Allows the user to query ELB logs with Athena"
104108
policy = data.aws_iam_policy_document.athena.json

bin/install-macos.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
echo 'installing brew packages'
44
brew update
55
brew tap liamg/tfsec
6-
brew install tfenv tflint terraform-docs pre-commit liamg/tfsec/tfsec coreutils
7-
brew upgrade tfenv tflint terraform-docs pre-commit liamg/tfsec/tfsec coreutils
6+
brew install tfenv tflint terraform-docs trivy pre-commit liamg/tfsec/tfsec coreutils
7+
brew upgrade tfenv tflint terraform-docs trivy pre-commit liamg/tfsec/tfsec coreutils
88

99
echo 'installing pre-commit hooks'
1010
pre-commit install

bin/install-ubuntu.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ pre-commit init-templatedir ~/.git-template
3030

3131
echo 'installing terraform with tfenv'
3232
tfenv install
33+
34+
wget https://github.com/aquasecurity/trivy/releases/download/v0.49.1/trivy_0.49.1_Linux-64bit.deb
35+
sudo dpkg -i trivy_0.49.1_Linux-64bit.deb

0 commit comments

Comments
 (0)