|
| 1 | +package s3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aquasecurity/trivy/internal/testutil" |
| 8 | + "github.com/aquasecurity/trivy/pkg/iac/providers/aws/s3" |
| 9 | + "github.com/aquasecurity/trivy/pkg/iac/scanners/cloudformation/parser" |
| 10 | + "github.com/aquasecurity/trivy/pkg/iac/types" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAdapt(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + source string |
| 18 | + expected s3.S3 |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "complete s3 bucket", |
| 22 | + source: `AWSTemplateFormatVersion: 2010-09-09 |
| 23 | +Resources: |
| 24 | + Key: |
| 25 | + Type: "AWS::KMS::Key" |
| 26 | + LoggingBucket: |
| 27 | + Type: AWS::S3::Bucket |
| 28 | + Properties: |
| 29 | + BucketName: logging-bucket |
| 30 | + Bucket: |
| 31 | + Type: AWS::S3::Bucket |
| 32 | + Properties: |
| 33 | + BucketName: test-bucket |
| 34 | + BucketEncryption: |
| 35 | + ServerSideEncryptionConfiguration: |
| 36 | + - ServerSideEncryptionByDefault: |
| 37 | + KMSMasterKeyID: |
| 38 | + Fn::GetAtt: |
| 39 | + - Key |
| 40 | + - Arn |
| 41 | + SSEAlgorithm: aws:kms |
| 42 | + AccessControl: AwsExecRead |
| 43 | + PublicAccessBlockConfiguration: |
| 44 | + BlockPublicAcls: true |
| 45 | + BlockPublicPolicy: true |
| 46 | + IgnorePublicAcls: true |
| 47 | + RestrictPublicBuckets: true |
| 48 | + LoggingConfiguration: |
| 49 | + DestinationBucketName: !Ref LoggingBucket |
| 50 | + LogFilePrefix: testing-logs |
| 51 | + LifecycleConfiguration: |
| 52 | + Rules: |
| 53 | + - Id: GlacierRule |
| 54 | + Prefix: glacier |
| 55 | + Status: Enabled |
| 56 | + ExpirationInDays: 365 |
| 57 | + AccelerateConfiguration: |
| 58 | + AccelerationStatus: Enabled |
| 59 | +`, |
| 60 | + expected: s3.S3{ |
| 61 | + Buckets: []s3.Bucket{ |
| 62 | + { |
| 63 | + Name: types.String("logging-bucket", types.NewTestMetadata()), |
| 64 | + }, |
| 65 | + { |
| 66 | + Name: types.String("test-bucket", types.NewTestMetadata()), |
| 67 | + Encryption: s3.Encryption{ |
| 68 | + Enabled: types.Bool(true, types.NewTestMetadata()), |
| 69 | + Algorithm: types.String("aws:kms", types.NewTestMetadata()), |
| 70 | + KMSKeyId: types.String("Key", types.NewTestMetadata()), |
| 71 | + }, |
| 72 | + ACL: types.String("aws-exec-read", types.NewTestMetadata()), |
| 73 | + PublicAccessBlock: &s3.PublicAccessBlock{ |
| 74 | + BlockPublicACLs: types.Bool(true, types.NewTestMetadata()), |
| 75 | + BlockPublicPolicy: types.Bool(true, types.NewTestMetadata()), |
| 76 | + IgnorePublicACLs: types.Bool(true, types.NewTestMetadata()), |
| 77 | + RestrictPublicBuckets: types.Bool(true, types.NewTestMetadata()), |
| 78 | + }, |
| 79 | + Logging: s3.Logging{ |
| 80 | + TargetBucket: types.String("LoggingBucket", types.NewTestMetadata()), |
| 81 | + Enabled: types.Bool(true, types.NewTestMetadata()), |
| 82 | + }, |
| 83 | + LifecycleConfiguration: []s3.Rules{ |
| 84 | + { |
| 85 | + Status: types.String("Enabled", types.NewTestMetadata()), |
| 86 | + }, |
| 87 | + }, |
| 88 | + AccelerateConfigurationStatus: types.String("Enabled", types.NewTestMetadata()), |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "empty s3 bucket", |
| 95 | + source: `AWSTemplateFormatVersion: 2010-09-09 |
| 96 | +Resources: |
| 97 | + Bucket: |
| 98 | + Type: AWS::S3::Bucket |
| 99 | + Properties: |
| 100 | + BucketName: test-bucket`, |
| 101 | + expected: s3.S3{ |
| 102 | + Buckets: []s3.Bucket{ |
| 103 | + { |
| 104 | + Name: types.String("test-bucket", types.NewTestMetadata()), |
| 105 | + Encryption: s3.Encryption{ |
| 106 | + Enabled: types.BoolDefault(false, types.NewTestMetadata()), |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "incorrect SSE algorithm", |
| 114 | + source: `AWSTemplateFormatVersion: 2010-09-09 |
| 115 | +Resources: |
| 116 | + Bucket: |
| 117 | + Type: AWS::S3::Bucket |
| 118 | + Properties: |
| 119 | + BucketName: test-bucket |
| 120 | + BucketEncryption: |
| 121 | + ServerSideEncryptionConfiguration: |
| 122 | + - ServerSideEncryptionByDefault: |
| 123 | + KMSMasterKeyID: alias/my-key |
| 124 | + SSEAlgorithm: aes256 |
| 125 | +`, |
| 126 | + expected: s3.S3{ |
| 127 | + Buckets: []s3.Bucket{ |
| 128 | + { |
| 129 | + Name: types.String("test-bucket", types.NewTestMetadata()), |
| 130 | + Encryption: s3.Encryption{ |
| 131 | + Enabled: types.BoolDefault(false, types.NewTestMetadata()), |
| 132 | + KMSKeyId: types.String("alias/my-key", types.NewTestMetadata()), |
| 133 | + Algorithm: types.String("aes256", types.NewTestMetadata()), |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + |
| 144 | + fsys := testutil.CreateFS(t, map[string]string{ |
| 145 | + "main.yaml": tt.source, |
| 146 | + }) |
| 147 | + |
| 148 | + fctx, err := parser.New().ParseFile(context.TODO(), fsys, "main.yaml") |
| 149 | + require.NoError(t, err) |
| 150 | + |
| 151 | + adapted := Adapt(*fctx) |
| 152 | + testutil.AssertDefsecEqual(t, tt.expected, adapted) |
| 153 | + }) |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments