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

Extract metadata from enumeration pkg #1572

Merged
merged 9 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 10 additions & 3 deletions enumeration/remote/aws/cloudformation_stack_enumerator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package aws

import (
"fmt"
"strconv"

"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/hashicorp/terraform/flatmap"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
"github.com/snyk/driftctl/enumeration/resource"
Expand Down Expand Up @@ -35,7 +39,10 @@ func (e *CloudformationStackEnumerator) Enumerate() ([]*resource.Resource, error
for _, stack := range stacks {
attrs := map[string]interface{}{}
if stack.Parameters != nil && len(stack.Parameters) > 0 {
attrs["parameters"] = flattenParameters(stack.Parameters)
attrs["parameters.%"] = strconv.FormatInt(int64(len(stack.Parameters)), 10)
for k, v := range flattenParameters(stack.Parameters) {
attrs[fmt.Sprintf("parameters.%s", k)] = v
}
}

results = append(
Expand All @@ -51,10 +58,10 @@ func (e *CloudformationStackEnumerator) Enumerate() ([]*resource.Resource, error
return results, err
}

func flattenParameters(parameters []*cloudformation.Parameter) interface{} {
func flattenParameters(parameters []*cloudformation.Parameter) flatmap.Map {
params := make(map[string]interface{}, len(parameters))
for _, p := range parameters {
params[*p.ParameterKey] = *p.ParameterValue
}
return params
return flatmap.Flatten(params)
}
4 changes: 3 additions & 1 deletion enumeration/remote/aws/dynamodb_table_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (e *DynamoDBTableEnumerator) Enumerate() ([]*resource.Resource, error) {
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*table,
map[string]interface{}{},
map[string]interface{}{
"table_name": *table,
},
),
)
}
Expand Down
8 changes: 4 additions & 4 deletions enumeration/remote/aws/ec2_network_acl_rule_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (e *EC2NetworkACLRuleEnumerator) Enumerate() ([]*resource.Resource, error)
attrs := map[string]interface{}{
"egress": *entry.Egress,
"network_acl_id": *res.NetworkAclId,
"rule_action": *entry.RuleAction, // Used in default middleware
"rule_number": float64(*entry.RuleNumber), // Used in default middleware
"protocol": *entry.Protocol, // Used in default middleware
"rule_action": *entry.RuleAction, // Used in default middleware
"rule_number": *entry.RuleNumber, // Used in default middleware
"protocol": *entry.Protocol, // Used in default middleware
}

if entry.CidrBlock != nil {
Expand All @@ -56,7 +56,7 @@ func (e *EC2NetworkACLRuleEnumerator) Enumerate() ([]*resource.Resource, error)
string(e.SupportedType()),
aws.CreateNetworkACLRuleID(
*res.NetworkAclId,
int(float64(*entry.RuleNumber)),
*entry.RuleNumber,
*entry.Egress,
*entry.Protocol,
),
Expand Down
14 changes: 1 addition & 13 deletions enumeration/remote/aws/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ import (
* Required to use Scanner
*/

func Init(version string, alerter *alerter.Alerter,
providerLibrary *terraform.ProviderLibrary,
remoteLibrary *common.RemoteLibrary,
progress enumeration.ProgressCounter,
resourceSchemaRepository *resource.SchemaRepository,
factory resource.ResourceFactory,
configDir string) error {
func Init(version string, alerter *alerter.Alerter, providerLibrary *terraform.ProviderLibrary, remoteLibrary *common.RemoteLibrary, progress enumeration.ProgressCounter, factory resource.ResourceFactory, configDir string) error {

provider, err := NewAWSTerraformProvider(version, progress, configDir)
if err != nil {
Expand Down Expand Up @@ -248,11 +242,5 @@ func Init(version string, alerter *alerter.Alerter,

remoteLibrary.AddEnumerator(NewElastiCacheClusterEnumerator(elasticacheRepository, factory))

err = resourceSchemaRepository.Init(terraform.AWS, provider.Version(), provider.Schema())
if err != nil {
return err
}
aws.InitResourcesMetadata(resourceSchemaRepository)

return nil
}
4 changes: 3 additions & 1 deletion enumeration/remote/aws/lambda_function_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (e *LambdaFunctionEnumerator) Enumerate() ([]*resource.Resource, error) {
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*function.FunctionName,
map[string]interface{}{},
map[string]interface{}{
"function_name": *function.FunctionName,
},
),
)
}
Expand Down
3 changes: 2 additions & 1 deletion enumeration/remote/aws/s3_bucket_analytic_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"

"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (e *S3BucketAnalyticEnumerator) Enumerate() ([]*resource.Resource, error) {
string(e.SupportedType()),
id,
map[string]interface{}{
"region": region,
"alias": region,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no sure I understand this change Λ†Λ†

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you read the previous func that defined read attributes for those s3 related resources, we were retrieviing the regions attribute and setting it to the "alias" field. Since we do not have this method anymore, I needed to change it there directly.

image

Does it answer your question ?

},
),
)
Expand Down
2 changes: 1 addition & 1 deletion enumeration/remote/aws/s3_bucket_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (e *S3BucketEnumerator) Enumerate() ([]*resource.Resource, error) {
string(e.SupportedType()),
*bucket.Name,
map[string]interface{}{
"region": region,
"alias": region,
},
),
)
Expand Down
3 changes: 2 additions & 1 deletion enumeration/remote/aws/s3_bucket_inventory_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"

"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (e *S3BucketInventoryEnumerator) Enumerate() ([]*resource.Resource, error)
string(e.SupportedType()),
id,
map[string]interface{}{
"region": region,
"alias": region,
},
),
)
Expand Down
3 changes: 2 additions & 1 deletion enumeration/remote/aws/s3_bucket_metrics_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"

"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (e *S3BucketMetricsEnumerator) Enumerate() ([]*resource.Resource, error) {
string(e.SupportedType()),
id,
map[string]interface{}{
"region": region,
"alias": region,
},
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (e *S3BucketNotificationEnumerator) Enumerate() ([]*resource.Resource, erro
string(e.SupportedType()),
*bucket.Name,
map[string]interface{}{
"region": region,
"alias": region,
},
),
)
Expand Down
2 changes: 1 addition & 1 deletion enumeration/remote/aws/s3_bucket_policy_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (e *S3BucketPolicyEnumerator) Enumerate() ([]*resource.Resource, error) {
string(e.SupportedType()),
*bucket.Name,
map[string]interface{}{
"region": region,
"alias": region,
},
),
)
Expand Down
4 changes: 3 additions & 1 deletion enumeration/remote/aws/sns_topic_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (e *SNSTopicEnumerator) Enumerate() ([]*resource.Resource, error) {
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*topic.TopicArn,
map[string]interface{}{},
map[string]interface{}{
"topic_arn": *topic.TopicArn,
},
),
)
}
Expand Down
4 changes: 3 additions & 1 deletion enumeration/remote/aws/sns_topic_policy_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (e *SNSTopicPolicyEnumerator) Enumerate() ([]*resource.Resource, error) {
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*topic.TopicArn,
map[string]interface{}{},
map[string]interface{}{
"topic_arn": *topic.TopicArn,
},
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"

"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
Expand Down Expand Up @@ -75,7 +76,9 @@ func (e *SNSTopicSubscriptionEnumerator) Enumerate() ([]*resource.Resource, erro
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*subscription.SubscriptionArn,
map[string]interface{}{},
map[string]interface{}{
"SubscriptionId": *subscription.SubscriptionArn,
},
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func (s *securityGroupRule) getAttrs() resource.Attributes {
"type": s.Type,
"security_group_id": s.SecurityGroupId,
"protocol": s.Protocol,
"from_port": s.FromPort,
"to_port": s.ToPort,
"from_port": int(s.FromPort),
"to_port": int(s.ToPort),
"self": s.Self,
"source_security_group_id": s.SourceSecurityGroupId,
"cidr_blocks": toInterfaceSlice(s.CidrBlocks),
Expand Down
Loading