Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding CFT scanning support for the resources: #1149

Merged
merged 9 commits into from
Feb 11, 2022
20 changes: 20 additions & 0 deletions pkg/mapper/iac-providers/cft/cft.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/awslabs/goformation/v5/cloudformation/applicationautoscaling"
"github.com/awslabs/goformation/v5/cloudformation/appmesh"
"github.com/awslabs/goformation/v5/cloudformation/athena"
"github.com/awslabs/goformation/v5/cloudformation/autoscaling"
"github.com/awslabs/goformation/v5/cloudformation/backup"
"github.com/awslabs/goformation/v5/cloudformation/certificatemanager"
Expand All @@ -31,12 +32,15 @@ import (
"github.com/awslabs/goformation/v5/cloudformation/dms"
"github.com/awslabs/goformation/v5/cloudformation/eks"
"github.com/awslabs/goformation/v5/cloudformation/emr"
"github.com/awslabs/goformation/v5/cloudformation/globalaccelerator"
"github.com/awslabs/goformation/v5/cloudformation/lambda"
"github.com/awslabs/goformation/v5/cloudformation/msk"
"github.com/awslabs/goformation/v5/cloudformation/qldb"
"github.com/awslabs/goformation/v5/cloudformation/ram"
"github.com/awslabs/goformation/v5/cloudformation/sagemaker"
"github.com/awslabs/goformation/v5/cloudformation/sns"
"github.com/awslabs/goformation/v5/cloudformation/sqs"
"github.com/awslabs/goformation/v5/cloudformation/waf"

cf "github.com/awslabs/goformation/v5/cloudformation/cloudformation"
cnf "github.com/awslabs/goformation/v5/cloudformation/config"
Expand Down Expand Up @@ -148,6 +152,10 @@ func (m cftMapper) mapConfigForResource(r cloudformation.Resource, resourceName
return config.GetAPIGatewayStageConfig(resource)
case *apigatewayv2.Stage:
return config.GetAPIGatewayV2StageConfig(resource)
case *apigatewayv2.Api:
return config.GetAPIGatewayV2ApiConfig(resource)
case *athena.WorkGroup:
return config.GetAthenaWorkGroupConfig(resource)
case *logs.LogGroup:
return config.GetLogCloudWatchGroupConfig(resource)
case *ecs.Service:
Expand All @@ -158,6 +166,12 @@ func (m cftMapper) mapConfigForResource(r cloudformation.Resource, resourceName
return config.GetDaxClusterConfig(resource)
case *rds.DBInstance:
return config.GetDBInstanceConfig(resource)
case *rds.EventSubscription:
return config.GetDBEventSubscriptionConfig(resource)
case *qldb.Ledger:
return config.GetQldbLedgerConfig(resource)
case *ecs.Cluster:
return config.GetEcsClusterConfig(resource)
case *iam.Role:
return config.GetIamRoleConfig(resource)
case *iam.Policy:
Expand Down Expand Up @@ -198,6 +212,12 @@ func (m cftMapper) mapConfigForResource(r cloudformation.Resource, resourceName
return config.GetWorkspacesWorkspaceConfig(resource)
case *neptune.DBCluster:
return config.GetNeptuneClusterConfig(resource)
case *neptune.DBInstance:
return config.GetNeptuneClusterInstanceConfig(resource)
case *globalaccelerator.Accelerator:
return config.GetGlobalAcceleratorConfig(resource)
case *waf.SizeConstraintSet:
return config.GetWafSizeConstraintSetConfig(resource)
case *secretsmanager.Secret:
return config.GetSecretsManagerSecretConfig(resource)
case *ecr.Repository:
Expand Down
85 changes: 85 additions & 0 deletions pkg/mapper/iac-providers/cft/config/api-gatewayv2-api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright (C) 2021 Accurics, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import "github.com/awslabs/goformation/v5/cloudformation/apigatewayv2"

// CorsConfigurationBlock holds config for cors_configuration attribute
type CorsConfigurationBlock struct {
AllowCredentials bool `json:"allow_credentials,omitempty"`
MaxAge int `json:"max_age,omitempty"`
ExposeHeaders []string `json:",omitempty"`
AllowOrigins []string `json:"allow_origins,omitempty"`
AllowMethods []string `json:"allow_methods,omitempty"`
AllowHeaders []string `json:"allow_headers,omitempty"`
}

// APIGatewayV2ApiConfig holds config for aws_apigatewayv2_api resource
type APIGatewayV2ApiConfig struct {
Config
Name string `json:"name"`
ProtocolType string `json:"protocol_type"`
RouteKey string `json:"route_key,omitempty"`
Description string `json:"description,omitempty"`
CredentialsArn string `json:"credentials_arn,omitempty"`
RouteSelectionExpression string `json:"route_selection_expression,omitempty"`
Target string `json:"target,omitempty"`
Version string `json:"version,omitempty"`
APIKeySelectionExpression string `json:"api_key_selection_expression,omitempty"`
DisableExecuteAPIEndpoint bool `json:"disable_execute_api_endpoint,omitempty"`
FailOnWarnings bool `json:"fail_on_warnings,omitempty"`
CorsConfiguration []CorsConfigurationBlock `json:"cors_configuration,omitempty"`
}

// GetAPIGatewayV2ApiConfig returns config for aws_apigatewayv2_api resource
func GetAPIGatewayV2ApiConfig(a *apigatewayv2.Api) []AWSResourceConfig {
var corsConfigData []CorsConfigurationBlock

if a.CorsConfiguration != nil {
corsConfigData = make([]CorsConfigurationBlock, 1)
corsConfigData[0].AllowCredentials = a.CorsConfiguration.AllowCredentials
corsConfigData[0].AllowHeaders = a.CorsConfiguration.AllowHeaders
corsConfigData[0].AllowMethods = a.CorsConfiguration.AllowMethods
corsConfigData[0].AllowOrigins = a.CorsConfiguration.AllowOrigins
corsConfigData[0].ExposeHeaders = a.CorsConfiguration.ExposeHeaders
corsConfigData[0].MaxAge = a.CorsConfiguration.MaxAge
}

cf := APIGatewayV2ApiConfig{
Config: Config{
Name: a.Name,
Tags: a.Tags,
},
Name: a.Name,
ProtocolType: a.ProtocolType,
RouteKey: a.RouteKey,
Description: a.Description,
CredentialsArn: a.CredentialsArn,
RouteSelectionExpression: a.RouteSelectionExpression,
Target: a.Target,
Version: a.Version,
APIKeySelectionExpression: a.ApiKeySelectionExpression,
DisableExecuteAPIEndpoint: a.DisableExecuteApiEndpoint,
FailOnWarnings: a.FailOnWarnings,
CorsConfiguration: corsConfigData,
}

return []AWSResourceConfig{{
Resource: cf,
Metadata: a.AWSCloudFormationMetadata,
}}
}
105 changes: 105 additions & 0 deletions pkg/mapper/iac-providers/cft/config/athena-workgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright (C) 2021 Accurics, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"github.com/awslabs/goformation/v5/cloudformation/athena"
)

// EncryptionConfigurationBlock holds config for encryption_configuration attribute
type EncryptionConfigurationBlock struct {
EncryptionOption string `json:"encryption_option"`
KmsKeyArn string `json:"kms_key_arn"`
}

// ResultConfigurationBlock holds config for result_configuration attribute
type ResultConfigurationBlock struct {
EncryptionConfiguration []EncryptionConfigurationBlock `json:"encryption_configuration"`
OutputLocation string `json:"output_location"`
}

// EnginerVersionBlock holds config for engine_version attribute
type EnginerVersionBlock struct {
SelectedEngineVersion string `json:"selected_version"`
}

// WorkgroupConfigurationBlock holds config for configuration attribute
type WorkgroupConfigurationBlock struct {
BytesScannedCutoffPerQuery int `json:"bytes_scanned_cutoff_per_query,omitempty"`
EnforceWorkgroupConfiguration bool `json:"enforce_workgroup_configuration"`
RequesterPaysEnabled bool `json:"requester_pays_enabled"`
PublishCloudwatchMetricsEnabled bool `json:"publish_cloudwatch_metrics_enabled"`
EngineVersion []EnginerVersionBlock `json:"engine_version"`
ResultConfiguration []ResultConfigurationBlock `json:"result_configuration"`
}

// AthenaWorkGroupConfig holds config for aws_athena_workgroup resource
type AthenaWorkGroupConfig struct {
Config
Name string `json:"name"`
Configuration []WorkgroupConfigurationBlock `json:"configuration"`
}

// GetAthenaWorkGroupConfig returns config for aws_athena_workgroup resource
func GetAthenaWorkGroupConfig(w *athena.WorkGroup) []AWSResourceConfig {
var workGroupConfig []WorkgroupConfigurationBlock

if w.WorkGroupConfiguration != nil {
workGroupConfig = make([]WorkgroupConfigurationBlock, 1)

workGroupConfig[0].BytesScannedCutoffPerQuery = w.WorkGroupConfiguration.BytesScannedCutoffPerQuery
workGroupConfig[0].EnforceWorkgroupConfiguration = w.WorkGroupConfiguration.EnforceWorkGroupConfiguration
workGroupConfig[0].RequesterPaysEnabled = w.WorkGroupConfiguration.RequesterPaysEnabled
workGroupConfig[0].PublishCloudwatchMetricsEnabled = w.WorkGroupConfiguration.PublishCloudWatchMetricsEnabled

if w.WorkGroupConfiguration.EngineVersion != nil {
engineConfig := make([]EnginerVersionBlock, 1)
engineConfig[0].SelectedEngineVersion = w.WorkGroupConfiguration.EngineVersion.SelectedEngineVersion
workGroupConfig[0].EngineVersion = engineConfig
}

if w.WorkGroupConfiguration.ResultConfiguration != nil {
resultConfig := make([]ResultConfigurationBlock, 1)
resultConfig[0].OutputLocation = w.WorkGroupConfiguration.ResultConfiguration.OutputLocation

if w.WorkGroupConfiguration.ResultConfiguration.EncryptionConfiguration != nil {
encryptionCofig := make([]EncryptionConfigurationBlock, 1)
encryptionCofig[0].EncryptionOption = w.WorkGroupConfiguration.ResultConfiguration.EncryptionConfiguration.EncryptionOption
encryptionCofig[0].KmsKeyArn = w.WorkGroupConfiguration.ResultConfiguration.EncryptionConfiguration.KmsKey

resultConfig[0].EncryptionConfiguration = encryptionCofig
}

workGroupConfig[0].ResultConfiguration = resultConfig
}
}

cf := AthenaWorkGroupConfig{
Config: Config{
Name: w.Name,
Tags: w.Tags,
},
Name: w.Name,
}

cf.Configuration = workGroupConfig

return []AWSResourceConfig{{
Resource: cf,
Metadata: w.AWSCloudFormationMetadata,
}}
}
48 changes: 48 additions & 0 deletions pkg/mapper/iac-providers/cft/config/db-event-subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright (C) 2021 Accurics, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import "github.com/awslabs/goformation/v5/cloudformation/rds"

// DBEventSubscriptionConfig holds config for aws_db_event_subscription resource
type DBEventSubscriptionConfig struct {
Config
SnsTopicArn string `json:"sns_topic"`
Enabled bool `json:"enabled,omitempty"`
EventCategories []string `json:"event_categories,omitempty"`
SourceIds []string `json:"source_ids,omitempty"`
SourceType string `json:"source_type,omitempty"`
}

// GetDBEventSubscriptionConfig returns config for aws_db_event_subscription resource
func GetDBEventSubscriptionConfig(d *rds.EventSubscription) []AWSResourceConfig {

cf := DBEventSubscriptionConfig{
Config: Config{},
SnsTopicArn: d.SnsTopicArn,
Enabled: d.Enabled,
EventCategories: d.EventCategories,
SourceIds: d.SourceIds,
SourceType: d.SourceType,
}

return []AWSResourceConfig{{
Resource: cf,
Metadata: d.AWSCloudFormationMetadata,
}}

}
Loading