Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Pushing up to Liam
Browse files Browse the repository at this point in the history
  • Loading branch information
owenrumney committed Oct 7, 2021
1 parent 2d2777d commit 4bac308
Show file tree
Hide file tree
Showing 67 changed files with 1,588 additions and 72 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/cfsec.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/aquasecurity/cfsec
go 1.16

require (
github.com/aquasecurity/defsec v0.0.3-0.20210924154209-d91df1dcd1f4
github.com/aquasecurity/defsec v0.0.3-0.20210927095822-3b46670e9b41
github.com/google/go-cmp v0.5.5 // indirect
github.com/liamg/clinch v1.5.6
github.com/liamg/jfather v0.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/aquasecurity/defsec v0.0.3-0.20210924123050-47f3bbb1359a h1:KlIDX0v8m
github.com/aquasecurity/defsec v0.0.3-0.20210924123050-47f3bbb1359a/go.mod h1:E53TX/xJkcgpJyF5GPSat3Z+cZiLyvSNBdJAyfdl3fc=
github.com/aquasecurity/defsec v0.0.3-0.20210924154209-d91df1dcd1f4 h1:/VVDhCTxISNLMtIdN4TFgzNKIVzq7pbRxp1t5fujR8U=
github.com/aquasecurity/defsec v0.0.3-0.20210924154209-d91df1dcd1f4/go.mod h1:E53TX/xJkcgpJyF5GPSat3Z+cZiLyvSNBdJAyfdl3fc=
github.com/aquasecurity/defsec v0.0.3-0.20210927095822-3b46670e9b41 h1:Djg0qtBwF1yBNx0Pv17KXKgZMcoAiN95p6xLl6Xofwo=
github.com/aquasecurity/defsec v0.0.3-0.20210927095822-3b46670e9b41/go.mod h1:E53TX/xJkcgpJyF5GPSat3Z+cZiLyvSNBdJAyfdl3fc=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down
6 changes: 6 additions & 0 deletions internal/app/cfsec/adapter/aws/adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/cloudwatch"
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/codebuild"
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/config"
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/documentdb"
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/dynamodb"
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/ebs"
"github.com/aquasecurity/cfsec/internal/app/cfsec/adapter/aws/s3"
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser"
"github.com/aquasecurity/defsec/provider/aws"
Expand All @@ -25,6 +28,9 @@ func Adapt(cfFile parser.FileContext) aws.AWS {
CloudWatch: cloudwatch.Adapt(cfFile),
CodeBuild: codebuild.Adapt(cfFile),
Config: config.Adapt(cfFile),
DocumentDB: documentdb.Adapt(cfFile),
DynamoDB: dynamodb.Adapt(cfFile),
EBS: ebs.Adapt(cfFile),
S3: s3.Adapt(cfFile),
}
}
Expand Down
66 changes: 66 additions & 0 deletions internal/app/cfsec/adapter/aws/documentdb/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,78 @@ package documentdb
import (
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser"
"github.com/aquasecurity/defsec/provider/aws/documentdb"
"github.com/aquasecurity/defsec/types"
)

func getClusters(ctx parser.FileContext) (clusters []documentdb.Cluster) {

clusterResources := ctx.GetResourceByType("AWS::DocDB::DBCluster")

for _, r := range clusterResources {
cluster := documentdb.Cluster{
Metadata: r.Metadata(),
Identifier: getIdentifier(r),
EnabledLogExports: getLogExports(r),
StorageEncrypted: isStorageEncrypted(r),
KMSKeyID: getKmsKeyId(r),
}

updateInstancesOnCluster(&cluster, ctx)

clusters = append(clusters, cluster)
}
return clusters
}

func updateInstancesOnCluster(cluster *documentdb.Cluster, ctx parser.FileContext) {

instanceResources := ctx.GetResourceByType("AWS::DocDB::DBInstance")

for _, r := range instanceResources {
clusterIdentifier := getIdentifier(r)
if clusterIdentifier == cluster.Identifier {
cluster.Instances = append(cluster.Instances, documentdb.Instance{
Metadata: r.Metadata(),
KMSKeyID: cluster.KMSKeyID,
})
}
}
}

func getKmsKeyId(r *parser.Resource) types.StringValue {
kmsIdProp := r.GetProperty("KmsKeyId")
if kmsIdProp.IsNil() || kmsIdProp.IsNotString() {
return types.StringDefault("", r.Metadata())
}
return kmsIdProp.AsStringValue()
}

func getLogExports(r *parser.Resource) (logExports []types.StringValue) {

exportsList := r.GetProperty("EnableCloudwatchLogsExports")

if exportsList.IsNil() || exportsList.IsNotList() {
return logExports
}

for _, export := range exportsList.AsList() {
logExports = append(logExports, export.AsStringValue())
}
return logExports
}

func isStorageEncrypted(r *parser.Resource) types.BoolValue {
encryptedProp := r.GetProperty("StorageEncrypted")
if encryptedProp.IsNil() || encryptedProp.IsNotBool() {
return types.BoolDefault(false, r.Metadata())
}
return encryptedProp.AsBoolValue()
}

func getIdentifier(r *parser.Resource) types.StringValue {
identifierProp := r.GetProperty("DBClusterIdentifier")
if identifierProp.IsNil() {
return types.StringDefault("", r.Metadata())
}
return identifierProp.AsStringValue()
}
35 changes: 35 additions & 0 deletions internal/app/cfsec/adapter/aws/dynamodb/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dynamodb

import (
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser"
"github.com/aquasecurity/defsec/provider/aws/dynamodb"
"github.com/aquasecurity/defsec/types"
)

func getClusters(file parser.FileContext) (clusters []dynamodb.DAXCluster) {

clusterResources := file.GetResourceByType("AWS::DAX::Cluster")

for _, r := range clusterResources {
cluster := dynamodb.DAXCluster{
Metadata: r.Metadata(),
ServerSideEncryption: dynamodb.ServerSideEncryption{
Enabled: isEnabled(r),
},
PointInTimeRecovery: nil,
}

clusters = append(clusters, cluster)
}

return clusters
}

func isEnabled(r *parser.Resource) types.BoolValue {

sseEnabled := r.GetProperty("SSESpecification.SSEEnabled")
if sseEnabled.IsNil() || sseEnabled.IsNotBool() {
return types.BoolDefault(false, r.Metadata())
}
return sseEnabled.AsBoolValue()
}
14 changes: 14 additions & 0 deletions internal/app/cfsec/adapter/aws/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dynamodb

import (
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser"
"github.com/aquasecurity/defsec/provider/aws/dynamodb"
)

func Adapt(cfFile parser.FileContext) dynamodb.DynamoDB {

return dynamodb.DynamoDB{
getClusters(cfFile),
}
}

14 changes: 14 additions & 0 deletions internal/app/cfsec/adapter/aws/ebs/ebs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ebs

import (
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser"
"github.com/aquasecurity/defsec/provider/aws/ebs"
)

func Adapt(cfFile parser.FileContext) ebs.EBS {
return ebs.EBS{
Volumes: getVolumes(cfFile),
}

}

41 changes: 41 additions & 0 deletions internal/app/cfsec/adapter/aws/ebs/volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ebs

import (
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser"
"github.com/aquasecurity/defsec/provider/aws/ebs"
"github.com/aquasecurity/defsec/types"
)

func getVolumes(ctx parser.FileContext) (volumes []ebs.Volume) {

volumeResources := ctx.GetResourceByType("AWS::EC2::Volume")
for _, r := range volumeResources {

volume := ebs.Volume{
Metadata: r.Metadata(),
Encryption: ebs.Encryption{
Enabled: isEncryptionEnabled(r),
KMSKeyID: getKmsKeyId(r),
},
}

volumes = append(volumes, volume)
}
return volumes
}

func getKmsKeyId(r *parser.Resource) types.StringValue {
kmsIdProp := r.GetProperty("KmsKeyId")
if kmsIdProp.IsNil() || kmsIdProp.IsNotString() {
return types.StringDefault("", r.Metadata())
}
return kmsIdProp.AsStringValue()
}

func isEncryptionEnabled(r *parser.Resource) types.BoolValue {
encryptedProp := r.GetProperty("Encrypted")
if encryptedProp.IsNil() || encryptedProp.IsNotBool() {
return types.BoolDefault(false, r.Metadata())
}
return encryptedProp.AsBoolValue()
}
19 changes: 18 additions & 1 deletion internal/app/cfsec/parser/property.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package parser

import (
"strings"

"github.com/aquasecurity/cfsec/internal/app/cfsec/cftypes"
"github.com/aquasecurity/defsec/types"
"github.com/liamg/jfather"
"gopkg.in/yaml.v3"
"strings"
)

type Property struct {
Expand Down Expand Up @@ -136,27 +137,43 @@ func (p *Property) IsString() bool {
return p.Inner.Type == cftypes.String
}

func (p *Property) IsNotString() bool {
return !p.IsString()
}

func (p *Property) IsMap() bool {
if p.IsNil() {
return false
}
return p.Inner.Type == cftypes.Map
}

func (p *Property) IsNotMap() bool {
return !p.IsMap()
}

func (p *Property) IsList() bool {
if p.IsNil() {
return false
}
return p.Inner.Type == cftypes.List
}

func (p *Property) IsNotList() bool {
return !p.IsList()
}

func (p *Property) IsBool() bool {
if p.IsNil() {
return false
}
return p.Inner.Type == cftypes.Bool
}

func (p *Property) IsNotBool() bool {
return !p.IsBool()
}

func (p *Property) AsString() string {
return p.Inner.Value.(string)
}
Expand Down
20 changes: 0 additions & 20 deletions internal/app/cfsec/rules/aws/documentdb/documentdb.yaml

This file was deleted.

29 changes: 24 additions & 5 deletions internal/app/cfsec/rules/aws/documentdb/enable_log_export_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ func init() {

scanner.RegisterCheckRule(rule.Rule{
BadExample: []string{`---
Resources:
Resources:
BadExample:
Type: "AWS::DocDB::DBCluster"
Properties:
BackupRetentionPeriod : 8
DBClusterIdentifier : "sample-cluster"
DBClusterParameterGroupName : "default.docdb3.6"
BackupRetentionPeriod: 8
DBClusterIdentifier: sample-cluster
DBClusterParameterGroupName: default.docdb3.6
BadInstanceExample:
Type: "AWS::DocDB::DBInstance"
Properties:
AutoMinorVersionUpgrade: true
AvailabilityZone: us-east-1c
DBClusterIdentifier: sample-cluster
DBInstanceClass: db.r5.large
DBInstanceIdentifier: sample-cluster-instance-0
PreferredMaintenanceWindow: 'sat:06:54-sat:07:24'
`},
GoodExample: []string{`---
Resources:
Expand All @@ -26,9 +35,19 @@ Resources:
BackupRetentionPeriod : 8
DBClusterIdentifier : "sample-cluster"
DBClusterParameterGroupName : "default.docdb3.6"
KmsKeyId : "your-kms-key-id"
EnableCloudwatchLogsExports:
- audit
- audit
- profiler
InstanceInstanceExample:
Type: "AWS::DocDB::DBInstance"
Properties:
AutoMinorVersionUpgrade: true
AvailabilityZone: "us-east-1c"
DBClusterIdentifier: "sample-cluster"
DBInstanceClass: "db.r5.large"
DBInstanceIdentifier: "sample-cluster-instance-0"
PreferredMaintenanceWindow: "sat:06:54-sat:07:24"
`},
Base: documentdb.CheckEnableLogExport,
})
Expand Down
Loading

0 comments on commit 4bac308

Please sign in to comment.