Skip to content

Commit

Permalink
feat(checks): improve S3 server logging access detection for AVD-AWS-…
Browse files Browse the repository at this point in the history
…0089

Signed-off-by: Nikita Pivkin <[email protected]>
  • Loading branch information
nikpivkin committed Aug 6, 2024
1 parent 9bbb577 commit d8a5624
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 100 deletions.
29 changes: 28 additions & 1 deletion checks/cloud/aws/s3/enable_logging.rego
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,36 @@
# good_examples: "checks/cloud/aws/s3/enable_bucket_logging.cf.go"
package builtin.aws.s3.aws0089

import future.keywords.if
import future.keywords.in

deny[res] {
bucket := input.aws.s3.buckets[_]
not bucket.acl.value == "log-delivery-write"
not bucket_has_server_logging_access(bucket)
not bucket.logging.enabled.value
res := result.new("Bucket has logging disabled", bucket.logging.enabled)
}

bucket_has_server_logging_access(bucket) if {
bucket.acl.value == "log-delivery-write"
}

bucket_has_server_logging_access(bucket) if {
grant := bucket.grants[_]
has_write_acl_permission(grant.permissions)
}

has_write_acl_permission(permissions) if {
permission := permissions[_]
permission.value in {"WRITE", "FULL_CONTROL"}
}

bucket_has_server_logging_access(bucket) if {
policy := bucket.bucketpolicies[_]
doc := json.unmarshal(policy.document.value)
print(doc)
statement := doc.Statement[_]
statement.Effect == "Allow"
"s3:PutObject" in statement.Action
"logging.s3.amazonaws.com" in statement.Principal.Service
}
52 changes: 48 additions & 4 deletions checks/cloud/aws/s3/enable_logging_test.rego
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
package builtin.aws.s3.aws0089

test_detects_when_disabled {
test_deny_logging_disabled {
r := deny with input as {"aws": {"s3": {"buckets": [{"logging": {"enabled": {"value": false}}}]}}}
count(r) == 1
}

test_when_enabled {
test_allow_logging_enabled {
r := deny with input as {"aws": {"s3": {"buckets": [{"logging": {"enabled": {"value": true}}}]}}}
count(r) == 0
}

test_detects_when_disabled_but_acl_is_log_write {
test_allow_logging_disabled_but_bucket_has_server_logging_access_acl {
r := deny with input as {"aws": {"s3": {"buckets": [{
"logging": {"enabled": {"value": false}},
"acl": {"value": "log-delivery-write"},
}]}}}
count(r) == 0
}

test_detects_when_enabled_and_acl_is_not_log_write {
test_deny_logging_disabled_and_bucket_does_not_have_server_access_logging {
r := deny with input as {"aws": {"s3": {"buckets": [{
"logging": {"enabled": {"value": false}},
"acl": {"value": "private"},
}]}}}
count(r) == 1
}

test_allow_logging_disabled_but_bucket_has_server_logging_access_grant {
r := deny with input as {"aws": {"s3": {"buckets": [{
"logging": {"enabled": {"value": false}},
"acl": {"value": "log-delivery-write"},
"grants": [
{
"grantee": {
"id": {"value": "111122223333"},
"type": {"value": "CanonicalUser"},
},
"permissions": [{"value": "FULL_CONTROL"}],
},
{
"grantee": {
"uri": {"value": "http://acs.amazonaws.com/groups/s3/LogDelivery"},
"type": {"value": "GROUP"},
},
"permissions": [
{"value": "READ_ACP"},
{"value": "WRITE"},
],
},
],
}]}}}
count(r) == 0
}

test_allow_logging_disabled_but_bucket_has_server_logging_access_policy {
r := deny with input as {"aws": {"s3": {"buckets": [{
"logging": {"enabled": {"value": false}},
"bucketpolicies": [{"document": {"value": json.marshal({
"Version": "2012-10-17",
"Statement": [{
"Sid": "S3ServerAccessLogsPolicy",
"Effect": "Allow",
"Principal": {"Service": ["logging.s3.amazonaws.com"]},
"Action": ["s3:PutObject"],
"Resource": "arn:aws:s3:::DOC-EXAMPLE-DESTINATION-BUCKET-logs/*",
}],
})}}],
}]}}}
count(r) == 0
}
61 changes: 33 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ go 1.22.0

toolchain go1.22.2

replace github.com/aquasecurity/trivy => github.com/nikpivkin/trivy v0.0.0-20240801055608-7140471b5059

require (
github.com/aquasecurity/trivy v0.52.1-0.20240619054236-36b3b772df21
github.com/docker/docker v26.1.3+incompatible
github.com/docker/docker v27.1.1+incompatible
github.com/liamg/iamgo v0.0.9
github.com/liamg/memoryfs v1.6.0
github.com/open-policy-agent/opa v0.65.0
github.com/owenrumney/squealer v1.2.2
github.com/open-policy-agent/opa v0.66.0
github.com/owenrumney/squealer v1.2.3
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.31.0
github.com/testcontainers/testcontainers-go v0.32.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
mvdan.cc/sh/v3 v3.8.0
Expand All @@ -36,9 +38,9 @@ require (
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aquasecurity/go-version v0.0.0-20240603093900-cf8a8d29271d // indirect
github.com/aws/aws-sdk-go v1.53.0 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.55.1 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/aws/aws-sdk-go v1.54.6 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
Expand All @@ -47,8 +49,10 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/containerd v1.7.17 // indirect
github.com/containerd/containerd v1.7.20 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
Expand Down Expand Up @@ -83,19 +87,19 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.4 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.20.1 // indirect
github.com/hashicorp/hcl v1.0.1-vault-5 // indirect
github.com/hashicorp/hcl/v2 v2.21.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/liamg/jfather v0.0.7 // indirect
github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -106,7 +110,7 @@ require (
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/buildkit v0.13.2 // indirect
github.com/moby/buildkit v0.15.1 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
Expand All @@ -125,13 +129,13 @@ require (
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/common v0.51.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/samber/lo v1.46.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/shirou/gopsutil/v3 v3.24.2 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
Expand All @@ -140,50 +144,51 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/tonistiigi/go-csvvalue v0.0.0-20240710180619-ddb21b71c0b4 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yashtewari/glob-intersection v0.2.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
github.com/zclconf/go-cty-yaml v1.0.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.172.0 // indirect
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
Expand Down
Loading

0 comments on commit d8a5624

Please sign in to comment.