Skip to content

Commit

Permalink
🐛 bugfixes for aws
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey committed Oct 3, 2023
1 parent 70ca08e commit 8f23fd7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion providers/aws/resources/aws.lr.go

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

2 changes: 1 addition & 1 deletion providers/aws/resources/aws_cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func initAwsCloudwatchMetric(runtime *plugin.Runtime, args map[string]*llx.RawDa
return args, nil, err
}
if len(metrics.Metrics) == 0 {
return nil, nil, nil
return nil, nil, errors.New("no metrics found")
}
if len(metrics.Metrics) > 1 {
return nil, nil, errors.New("more than one metric found for " + namespace + " " + name + " in region " + region)
Expand Down
30 changes: 15 additions & 15 deletions providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,11 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
if instance.ImageId != nil {
mqlImage, err := NewResource(a.MqlRuntime, "aws.ec2.image",
map[string]*llx.RawData{"arn": llx.StringData(fmt.Sprintf(imageArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.ImageId)))})
if err != nil {
return nil, err
}
if mqlImage != nil {
if err == nil {
args["image"] = llx.ResourceData(mqlImage, mqlImage.MqlName())
} else {
log.Error().Err(err).Msg("cannot find image")
args["image"] = llx.NilData
}
} else {
args["image"] = llx.NilData
Expand All @@ -749,11 +749,11 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
map[string]*llx.RawData{
"arn": llx.StringData(fmt.Sprintf(vpcArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.VpcId))),
})
if err != nil {
return nil, err
}
if mqlVpcResource != nil {
if err == nil {
args["vpc"] = llx.ResourceData(mqlVpcResource, mqlVpcResource.MqlName())
} else {
log.Error().Err(err).Msg("cannot find vpc")
args["vpc"] = llx.NilData
}
} else {
args["vpc"] = llx.NilData
Expand All @@ -766,11 +766,11 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
"region": llx.StringData(regionVal),
"name": llx.StringData(convert.ToString(instance.KeyName)),
})
if err != nil {
return nil, err
}
if mqlKeyPair != nil {
if err == nil {
args["keypair"] = llx.ResourceData(mqlKeyPair, mqlKeyPair.MqlName())
} else {
log.Error().Err(err).Msg("cannot find keypair")
args["keypair"] = llx.NilData
}
} else {
args["keypair"] = llx.NilData
Expand Down Expand Up @@ -802,7 +802,7 @@ func initAwsEc2Image(runtime *plugin.Runtime, args map[string]*llx.RawData) (map
arnVal := args["arn"].Value.(string)
arn, err := arn.Parse(arnVal)
if err != nil {
return nil, nil, nil
return nil, nil, err
}
resource := strings.Split(arn.Resource, "/")
conn := runtime.Connection.(*connection.AwsConnection)
Expand All @@ -824,14 +824,14 @@ func initAwsEc2Image(runtime *plugin.Runtime, args map[string]*llx.RawData) (map
return args, nil, nil
}

return args, nil, nil
return nil, nil, errors.New("image not found")
}

func (a *mqlAwsEc2Securitygroup) id() (string, error) {
return a.Arn.Data, nil
}

func initAwsEc2SecurityGroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
func initAwsEc2Securitygroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
if len(args) > 2 {
return args, nil, nil
}
Expand Down
3 changes: 3 additions & 0 deletions providers/aws/resources/aws_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,9 @@ func (a *mqlAwsIamUser) attachedPolicies() ([]interface{}, error) {
}

func (a *mqlAwsIamPolicy) id() (string, error) {
if a == nil {
return "", nil
}
return a.Arn.Data, nil
}

Expand Down
29 changes: 22 additions & 7 deletions providers/aws/resources/aws_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,23 @@ func (a *mqlAwsS3Bucket) policy() (*mqlAwsS3BucketPolicy, error) {
})
if err != nil {
if isNotFoundForS3(err) {
return &mqlAwsS3BucketPolicy{}, nil
a.Policy.State = plugin.StateIsNull | plugin.StateIsSet
return nil, nil
}
return &mqlAwsS3BucketPolicy{}, err
return nil, err
}

if policy != nil && policy.Policy != nil {
// create the policy resource
pol, err := parsePolicyDocument(convert.ToString(policy.Policy))
if err != nil {
return nil, err
}
mqlS3BucketPolicy, err := CreateResource(a.MqlRuntime, "aws.s3.bucket.policy",
map[string]*llx.RawData{
"name": llx.StringData(bucketname),
"document": llx.StringData(convert.ToString(policy.Policy)),
"id": llx.StringData(pol.Id),
})
if err != nil {
return nil, err
Expand All @@ -198,7 +204,13 @@ func (a *mqlAwsS3Bucket) policy() (*mqlAwsS3BucketPolicy, error) {
}

// no bucket policy found, return nil for the policy
return &mqlAwsS3BucketPolicy{}, nil
a.Policy.State = plugin.StateIsNull | plugin.StateIsSet

return &mqlAwsS3BucketPolicy{
Id: plugin.TValue[string]{State: plugin.StateIsSet | plugin.StateIsNull, Data: ""},
Name: plugin.TValue[string]{State: plugin.StateIsSet | plugin.StateIsNull, Data: ""},
Document: plugin.TValue[string]{State: plugin.StateIsSet | plugin.StateIsNull, Data: ""},
}, nil
}

func (a *mqlAwsS3Bucket) tags() (map[string]interface{}, error) {
Expand Down Expand Up @@ -601,16 +613,19 @@ func (a *mqlAwsS3BucketCorsrule) id() (string, error) {
}

func (a *mqlAwsS3BucketPolicy) id() (string, error) {
policy, err := a.parsePolicyDocument()
if err != nil || policy == nil {
return "none", err
if a == nil {
return "", nil
}
return policy.Id, nil
return a.Id.Data, nil
}

func (a *mqlAwsS3BucketPolicy) parsePolicyDocument() (*awspolicy.S3BucketPolicy, error) {
data := a.Document.Data

return parsePolicyDocument(data)
}

func parsePolicyDocument(data string) (*awspolicy.S3BucketPolicy, error) {
var policy awspolicy.S3BucketPolicy
err := json.Unmarshal([]byte(data), &policy)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion providers/aws/resources/aws_sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a *mqlAwsSnsTopic) init(runtime *plugin.Runtime, args map[string]*llx.RawD
arnVal := args["arn"].Value.(string)
arn, err := arn.Parse(arnVal)
if err != nil {
return nil, nil, nil
return nil, nil, err
}

args["arn"] = llx.StringData(arnVal)
Expand Down

0 comments on commit 8f23fd7

Please sign in to comment.