-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14714 from Tensho/securityhub-standard-controls
New resource: aws_securityhub_standards_control
- Loading branch information
Showing
13 changed files
with
680 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_securityhub_standards_control | ||
``` |
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,44 @@ | ||
package securityhub | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
) | ||
|
||
const ( | ||
ARNSeparator = "/" | ||
ARNService = "securityhub" | ||
) | ||
|
||
// StandardsControlARNToStandardsSubscriptionARN converts a security standard control ARN to a subscription ARN. | ||
func StandardsControlARNToStandardsSubscriptionARN(inputARN string) (string, error) { | ||
parsedARN, err := arn.Parse(inputARN) | ||
|
||
if err != nil { | ||
return "", fmt.Errorf("error parsing ARN (%s): %w", inputARN, err) | ||
} | ||
|
||
if actual, expected := parsedARN.Service, ARNService; actual != expected { | ||
return "", fmt.Errorf("expected service %s in ARN (%s), got: %s", expected, inputARN, actual) | ||
} | ||
|
||
inputResourceParts := strings.Split(parsedARN.Resource, ARNSeparator) | ||
|
||
if actual, expected := len(inputResourceParts), 3; actual < expected { | ||
return "", fmt.Errorf("expected at least %d resource parts in ARN (%s), got: %d", expected, inputARN, actual) | ||
} | ||
|
||
outputResourceParts := append([]string{"subscription"}, inputResourceParts[1:len(inputResourceParts)-1]...) | ||
|
||
outputARN := arn.ARN{ | ||
Partition: parsedARN.Partition, | ||
Service: parsedARN.Service, | ||
Region: parsedARN.Region, | ||
AccountID: parsedARN.AccountID, | ||
Resource: strings.Join(outputResourceParts, ARNSeparator), | ||
}.String() | ||
|
||
return outputARN, nil | ||
} |
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,65 @@ | ||
package securityhub_test | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
tfsecurityhub "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub" | ||
) | ||
|
||
func TestStandardsControlARNToStandardsSubscriptionARN(t *testing.T) { | ||
testCases := []struct { | ||
TestName string | ||
InputARN string | ||
ExpectedError *regexp.Regexp | ||
ExpectedARN string | ||
}{ | ||
{ | ||
TestName: "empty ARN", | ||
InputARN: "", | ||
ExpectedError: regexp.MustCompile(`error parsing ARN`), | ||
}, | ||
{ | ||
TestName: "unparsable ARN", | ||
InputARN: "test", | ||
ExpectedError: regexp.MustCompile(`error parsing ARN`), | ||
}, | ||
{ | ||
TestName: "invalid ARN service", | ||
InputARN: "arn:aws:ec2:us-west-2:1234567890:control/cis-aws-foundations-benchmark/v/1.2.0/1.1", | ||
ExpectedError: regexp.MustCompile(`expected service securityhub`), | ||
}, | ||
{ | ||
TestName: "invalid ARN resource parts", | ||
InputARN: "arn:aws:securityhub:us-west-2:1234567890:control/cis-aws-foundations-benchmark", | ||
ExpectedError: regexp.MustCompile(`expected at least 3 resource parts`), | ||
}, | ||
{ | ||
TestName: "valid ARN", | ||
InputARN: "arn:aws:securityhub:us-west-2:1234567890:control/cis-aws-foundations-benchmark/v/1.2.0/1.1", | ||
ExpectedARN: "arn:aws:securityhub:us-west-2:1234567890:subscription/cis-aws-foundations-benchmark/v/1.2.0", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.TestName, func(t *testing.T) { | ||
got, err := tfsecurityhub.StandardsControlARNToStandardsSubscriptionARN(testCase.InputARN) | ||
|
||
if err == nil && testCase.ExpectedError != nil { | ||
t.Fatalf("expected error %s, got no error", testCase.ExpectedError.String()) | ||
} | ||
|
||
if err != nil && testCase.ExpectedError == nil { | ||
t.Fatalf("got unexpected error: %s", err) | ||
} | ||
|
||
if err != nil && !testCase.ExpectedError.MatchString(err.Error()) { | ||
t.Fatalf("expected error %s, got: %s", testCase.ExpectedError.String(), err) | ||
} | ||
|
||
if got != testCase.ExpectedARN { | ||
t.Errorf("got %s, expected %s", got, testCase.ExpectedARN) | ||
} | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.