This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Owen Rumney
authored
Dec 2, 2021
1 parent
c6f5ba7
commit fca3203
Showing
51 changed files
with
1,331 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser" | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/util" | ||
"github.com/aquasecurity/defsec/provider/aws/iam" | ||
"github.com/aquasecurity/defsec/provider/aws/sam" | ||
) | ||
|
||
func getFunctions(cfFile parser.FileContext) (functions []sam.Function) { | ||
|
||
functionResources := cfFile.GetResourceByType("AWS::Serverless::Function") | ||
for _, r := range functionResources { | ||
function := sam.Function{ | ||
Metadata: r.Metadata(), | ||
FunctionName: r.GetStringProperty("FunctionName"), | ||
Tracing: r.GetStringProperty("Tracing", sam.TracingModePassThrough), | ||
} | ||
|
||
setFunctionPolicies(r, &function) | ||
functions = append(functions, function) | ||
} | ||
|
||
return functions | ||
} | ||
|
||
func setFunctionPolicies(r *parser.Resource, function *sam.Function) { | ||
policies := r.GetProperty("Policies") | ||
if policies.IsNotNil() { | ||
if policies.IsString() { | ||
function.ManagedPolicies = append(function.ManagedPolicies, policies.AsStringValue()) | ||
} else if policies.IsList() { | ||
for _, property := range policies.AsList() { | ||
if property.IsMap() { | ||
policyDoc, err := getPolicyDocument(property, r.SourceFormat()) | ||
if err != nil { | ||
|
||
function.ManagedPolicies = append(function.ManagedPolicies, property.AsStringValue()) | ||
continue | ||
} | ||
function.Policies = append(function.Policies, *policyDoc) | ||
} else { | ||
function.ManagedPolicies = append(function.ManagedPolicies, property.AsStringValue()) | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
func getPolicyDocument(policyProp *parser.Property, sourceFormat parser.SourceFormat) (*iam.PolicyDocument, error) { | ||
policyDoc := util.GetJsonBytes(policyProp, sourceFormat, true) | ||
|
||
return iam.ParsePolicyDocument(policyDoc, policyProp.Metadata()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser" | ||
"github.com/aquasecurity/defsec/provider/aws/sam" | ||
"github.com/aquasecurity/defsec/types" | ||
) | ||
|
||
func getHttpApis(cfFile parser.FileContext) (apis []sam.HttpAPI) { | ||
|
||
apiResources := cfFile.GetResourceByType("AWS::Serverless::HttpApi") | ||
for _, r := range apiResources { | ||
api := sam.HttpAPI{ | ||
Metadata: r.Metadata(), | ||
Name: r.GetStringProperty("Name", ""), | ||
DomainConfiguration: getDomainConfiguration(r), | ||
AccessLogging: getAccessLogging(r), | ||
DefaultRouteSettings: getRouteSettings(r), | ||
} | ||
|
||
apis = append(apis, api) | ||
} | ||
|
||
return apis | ||
} | ||
|
||
func getRouteSettings(r *parser.Resource) sam.RouteSettings { | ||
|
||
route := r.GetProperty("DefaultRouteSettings") | ||
if route.IsNil() { | ||
return sam.RouteSettings{ | ||
Metadata: r.Metadata(), | ||
LoggingEnabled: types.BoolDefault(false, r.Metadata()), | ||
DataTraceEnabled: types.BoolDefault(false, r.Metadata()), | ||
DetailedMetricsEnabled: types.BoolDefault(false, r.Metadata()), | ||
} | ||
} | ||
|
||
return sam.RouteSettings{ | ||
Metadata: route.Metadata(), | ||
LoggingEnabled: route.GetBoolProperty("LoggingLevel"), | ||
DataTraceEnabled: route.GetBoolProperty("DataTraceEnabled"), | ||
DetailedMetricsEnabled: route.GetBoolProperty("DetailedMetricsEnabled"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser" | ||
"github.com/aquasecurity/defsec/provider/aws/sam" | ||
"github.com/aquasecurity/defsec/types" | ||
) | ||
|
||
func getStateMachines(cfFile parser.FileContext) (stateMachines []sam.StateMachine) { | ||
|
||
stateMachineResources := cfFile.GetResourceByType("AWS::Serverless::StateMachine") | ||
for _, r := range stateMachineResources { | ||
stateMachine := sam.StateMachine{ | ||
Metadata: r.Metadata(), | ||
Name: r.GetStringProperty("Name"), | ||
LoggingConfiguration: sam.LoggingConfiguration{}, | ||
Tracing: getTracingConfiguration(r), | ||
} | ||
|
||
setStateMachinePolicies(r, &stateMachine) | ||
stateMachines = append(stateMachines, stateMachine) | ||
} | ||
|
||
return stateMachines | ||
} | ||
|
||
func getTracingConfiguration(r *parser.Resource) sam.TracingConfiguration { | ||
tracing := r.GetProperty("Tracing") | ||
if tracing.IsNil() { | ||
return sam.TracingConfiguration{ | ||
Metadata: r.Metadata(), | ||
Enabled: types.BoolDefault(false, r.Metadata()), | ||
} | ||
} | ||
|
||
return sam.TracingConfiguration{ | ||
Metadata: tracing.Metadata(), | ||
Enabled: tracing.GetBoolProperty("Enabled"), | ||
} | ||
} | ||
|
||
func setStateMachinePolicies(r *parser.Resource, stateMachine *sam.StateMachine) { | ||
policies := r.GetProperty("Policies") | ||
if policies.IsNotNil() { | ||
if policies.IsString() { | ||
stateMachine.ManagedPolicies = append(stateMachine.ManagedPolicies, policies.AsStringValue()) | ||
} else if policies.IsList() { | ||
for _, property := range policies.AsList() { | ||
policyDoc, err := getPolicyDocument(property, r.SourceFormat()) | ||
if err != nil { | ||
|
||
stateMachine.ManagedPolicies = append(stateMachine.ManagedPolicies, property.AsStringValue()) | ||
continue | ||
} | ||
stateMachine.Policies = append(stateMachine.Policies, *policyDoc) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/parser" | ||
"github.com/aquasecurity/defsec/provider/aws/sam" | ||
"github.com/aquasecurity/defsec/types" | ||
) | ||
|
||
func getSimpleTables(cfFile parser.FileContext) (tables []sam.SimpleTable) { | ||
|
||
tableResources := cfFile.GetResourceByType("AWS::Serverless::SimpleTable") | ||
for _, r := range tableResources { | ||
table := sam.SimpleTable{ | ||
Metadata: r.Metadata(), | ||
TableName: r.GetStringProperty("TableName"), | ||
SSESpecification: getSSESpecification(r), | ||
} | ||
|
||
tables = append(tables, table) | ||
} | ||
|
||
return tables | ||
} | ||
|
||
func getSSESpecification(r *parser.Resource) sam.SSESpecification { | ||
sse := r.GetProperty("SSESpecification") | ||
if sse.IsNil() { | ||
return sam.SSESpecification{ | ||
Metadata: r.Metadata(), | ||
Enabled: types.BoolDefault(false, r.Metadata()), | ||
KMSMasterKeyID: types.StringDefault("", r.Metadata()), | ||
} | ||
} | ||
|
||
return sam.SSESpecification{ | ||
Metadata: sse.Metadata(), | ||
Enabled: sse.GetBoolProperty("SSEEnabled"), | ||
KMSMasterKeyID: sse.GetStringProperty("KMSMasterKeyID"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,6 @@ Resources: | |
`, | ||
}, | ||
|
||
Base: sam.CheckUseSecureTlsPolicy, | ||
Base: sam.CheckApiUseSecureTlsPolicy, | ||
}) | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/app/cfsec/rules/aws/sam/api_use_secure_tls_rule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/test" | ||
"github.com/aquasecurity/defsec/rules/aws/sam" | ||
|
||
"testing" | ||
) | ||
|
||
func Test_CheckApiUseSecureTlsPolicy_FailureExamples(t *testing.T) { | ||
expectedCode := sam.CheckApiUseSecureTlsPolicy.Rule().LongID() | ||
test.RunFailureExamplesTest(t, expectedCode) | ||
} | ||
|
||
func Test_CheckApiUseSecureTlsPolicy_PassedExamples(t *testing.T) { | ||
expectedCode := sam.CheckApiUseSecureTlsPolicy.Rule().LongID() | ||
test.RunPassingExamplesTest(t, expectedCode) | ||
} |
18 changes: 0 additions & 18 deletions
18
internal/app/cfsec/rules/aws/sam/enable_access_logging_rule_test.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,6 @@ Resources: | |
`, | ||
}, | ||
|
||
Base: sam.CheckEnableAccessLogging, | ||
Base: sam.CheckEnableApiAccessLogging, | ||
}) | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/app/cfsec/rules/aws/sam/enable_api_access_logging_rule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/test" | ||
"github.com/aquasecurity/defsec/rules/aws/sam" | ||
|
||
"testing" | ||
) | ||
|
||
func Test_CheckEnableApiAccessLogging_FailureExamples(t *testing.T) { | ||
expectedCode := sam.CheckEnableApiAccessLogging.Rule().LongID() | ||
test.RunFailureExamplesTest(t, expectedCode) | ||
} | ||
|
||
func Test_CheckEnableApiAccessLogging_PassedExamples(t *testing.T) { | ||
expectedCode := sam.CheckEnableApiAccessLogging.Rule().LongID() | ||
test.RunPassingExamplesTest(t, expectedCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,6 @@ Resources: | |
`, | ||
}, | ||
|
||
Base: sam.CheckEnableCacheEncryption, | ||
Base: sam.CheckEnableApiCacheEncryption, | ||
}) | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/app/cfsec/rules/aws/sam/enable_api_cache_encryption_rule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/test" | ||
"github.com/aquasecurity/defsec/rules/aws/sam" | ||
|
||
"testing" | ||
) | ||
|
||
func Test_CheckEnableApiCacheEncryption_FailureExamples(t *testing.T) { | ||
expectedCode := sam.CheckEnableApiCacheEncryption.Rule().LongID() | ||
test.RunFailureExamplesTest(t, expectedCode) | ||
} | ||
|
||
func Test_CheckEnableApiCacheEncryption_PassedExamples(t *testing.T) { | ||
expectedCode := sam.CheckEnableApiCacheEncryption.Rule().LongID() | ||
test.RunPassingExamplesTest(t, expectedCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,6 @@ Resources: | |
`, | ||
}, | ||
|
||
Base: sam.CheckEnableTracing, | ||
Base: sam.CheckEnableApiTracing, | ||
}) | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/app/cfsec/rules/aws/sam/enable_api_tracing_rule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sam | ||
|
||
import ( | ||
"github.com/aquasecurity/cfsec/internal/app/cfsec/test" | ||
"github.com/aquasecurity/defsec/rules/aws/sam" | ||
|
||
"testing" | ||
) | ||
|
||
func Test_CheckEnableApiTracingFailureExamples(t *testing.T) { | ||
expectedCode := sam.CheckEnableApiTracing.Rule().LongID() | ||
test.RunFailureExamplesTest(t, expectedCode) | ||
} | ||
|
||
func Test_CheckEnableApiTracing_PassedExamples(t *testing.T) { | ||
expectedCode := sam.CheckEnableApiTracing.Rule().LongID() | ||
test.RunPassingExamplesTest(t, expectedCode) | ||
} |
18 changes: 0 additions & 18 deletions
18
internal/app/cfsec/rules/aws/sam/enable_cache_encryption_rule_test.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.