-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
f-aws_directconnect_connection_macsec #26274
Merged
ewbankkit
merged 31 commits into
hashicorp:main
from
ddericco:f-aws_directconnect_connection-macsec
Dec 19, 2022
Merged
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
8d71007
First push to add MACSec support
5c4d093
Add working acceptance tests
ddericco 6738f1f
Add changelog, run acc test linting
ddericco a1c5b07
[WIP] Add and update docs
ddericco 45071a7
Minor doc/linting fixes
ddericco dd223d2
Add support to delete keys on disassociate
ddericco 4ca85df
Add read function to macsec_key resource
ddericco 96a5dd5
Update acceptance tests, clean up dx_connection resource
ddericco c23c8f1
Minor linting fixes
ddericco 063abff
Add sweeper for aws_dx_macsec_keys, remove secret delete function
ddericco 810926c
Update aws_dx_connection documentation, fix sweeper attribute error
ddericco ae5cc7f
Remove unneeded schema items, fix docs conflict
ddericco c12eecc
Minor linting fixes
ddericco 143007a
Additional linting fixes
ddericco b07f472
Apply code review suggestion: `request_macsec` attribute
ddericco 7f5f063
Merge branch 'hashicorp:main' into f-aws_directconnect_connection-macsec
ddericco a72afad
Add ForceNew to required attribute, remove update Noop
ddericco 6f984de
Add ForceNew to macsec connection, remove check on macsec_capable attr
ddericco db13c88
Rerun Terrafmt linting
ddericco 047756f
Apply CR suggestion: rename to `aws_dx_macsec_key_association`
ddericco 8ad97cb
Update internal/provider/provider.go
ddericco fd0aa9d
Rename docs, add encryption_mode acc test [WIP]
ddericco 3e236d9
Add skip_destroy argument for aws_dx_connection
ddericco 4f71c06
WIP - encryption_mode acceptance test
ddericco 63282e5
Tweak CHANGELOG entries.
ewbankkit 9da4e2b
Fix semgrep 'dgryski.semgrep-go.oddifsequence.odd-sequence-ifs'.
ewbankkit 3d6318c
Fix golangci-lint 'whitespace'.
ewbankkit e87145e
Remove unused commented functions
ddericco 3f6e762
Cosmetics.
ewbankkit 9e087b5
Finalize encryption_mode acc test
ddericco 1a820f4
r/aws_dx_connection: Fix golangci-lint 'unparam'.
ewbankkit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 @@ | ||
```release-note:new-resource | ||
aws_dx_macsec_key | ||
``` | ||
|
||
```release-note:enhancement | ||
resource/aws_dx_connection: Add arguments to enable MACsec support | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package directconnect | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/directconnect" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
) | ||
|
||
func ResourceMacSecKey() *schema.Resource { | ||
ddericco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &schema.Resource{ | ||
// MacSecKey resource only supports create (Associate), read (Describe) and delete (Disassociate) | ||
Create: resourceMacSecKeyCreate, | ||
Read: resourceMacSecKeyRead, | ||
// You cannot modify a MACsec secret key after you associate it with a connection. | ||
// To modify the key, disassociate the key from the connection, and then associate | ||
// a new key with the connection | ||
Update: schema.Noop, | ||
ddericco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Delete: resourceMacSecKeyDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"cak": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
// CAK requires CKN | ||
RequiredWith: []string{"ckn"}, | ||
ValidateFunc: validation.StringMatch(regexp.MustCompile(`[a-fA-F0-9]{64}$`), "Must be 64-character hex code string"), | ||
}, | ||
"ckn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
ewbankkit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Optional: true, | ||
AtLeastOneOf: []string{"ckn", "secret_arn"}, | ||
ValidateFunc: validation.StringMatch(regexp.MustCompile(`[a-fA-F0-9]{64}$`), "Must be 64-character hex code string"), | ||
}, | ||
"connection_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"secret_arn": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ewbankkit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AtLeastOneOf: []string{"ckn", "secret_arn"}, | ||
}, | ||
"start_on": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMacSecKeyCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).DirectConnectConn | ||
|
||
input := &directconnect.AssociateMacSecKeyInput{ | ||
ConnectionId: aws.String(d.Get("connection_id").(string)), | ||
} | ||
|
||
if d.Get("ckn").(string) != "" { | ||
input.Cak = aws.String(d.Get("cak").(string)) | ||
input.Ckn = aws.String(d.Get("ckn").(string)) | ||
} | ||
|
||
if d.Get("secret_arn").(string) != "" { | ||
input.SecretARN = aws.String(d.Get("secret_arn").(string)) | ||
} | ||
|
||
log.Printf("[DEBUG] Creating MACSec secret key on Direct Connect Connection: %s", *input.ConnectionId) | ||
output, err := conn.AssociateMacSecKey(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating MACSec secret key on Direct Connect Connection (%s): %w", *input.ConnectionId, err) | ||
} | ||
|
||
secret_arn := MacSecKeyParseSecretARN(output) | ||
|
||
// Create a composite ID based on connection ID and secret ARN | ||
d.SetId(fmt.Sprintf("%s_%s", secret_arn, aws.StringValue(output.ConnectionId))) | ||
|
||
d.Set("secret_arn", secret_arn) | ||
|
||
return resourceMacSecKeyRead(d, meta) | ||
} | ||
|
||
func resourceMacSecKeyRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).DirectConnectConn | ||
|
||
secretArn, connId, err := MacSecKeyParseID(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("unexpected format of ID (%s), expected secretArn_connectionId", d.Id()) | ||
} | ||
|
||
connection, err := FindConnectionByID(conn, connId) | ||
if err != nil { | ||
return fmt.Errorf("error reading Direct Connect Connection (%s): %w", d.Id(), err) | ||
} | ||
|
||
if connection.MacSecKeys == nil { | ||
return fmt.Errorf("no MACSec keys found on Direct Connect Connection (%s)", d.Id()) | ||
} | ||
|
||
for _, key := range connection.MacSecKeys { | ||
if aws.StringValue(key.SecretARN) == aws.StringValue(&secretArn) { | ||
d.Set("ckn", key.Ckn) | ||
d.Set("connection_id", connId) | ||
d.Set("secret_arn", key.SecretARN) | ||
d.Set("start_on", key.StartOn) | ||
d.Set("state", key.State) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceMacSecKeyDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*conns.AWSClient).DirectConnectConn | ||
|
||
input := &directconnect.DisassociateMacSecKeyInput{ | ||
ConnectionId: aws.String(d.Get("connection_id").(string)), | ||
SecretARN: aws.String(d.Get("secret_arn").(string)), | ||
} | ||
|
||
log.Printf("[DEBUG] Disassociating MACSec secret key on Direct Connect Connection: %s", *input.ConnectionId) | ||
_, err := conn.DisassociateMacSecKey(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Unable to disassociate MACSec secret key on Direct Connect Connection (%s): %w", *input.ConnectionId, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MacSecKeyParseSecretARN parses the secret ARN returned from a CMK or secret_arn | ||
func MacSecKeyParseSecretARN(output *directconnect.AssociateMacSecKeyOutput) string { | ||
var result string | ||
|
||
for _, key := range output.MacSecKeys { | ||
if key == nil { | ||
continue | ||
} | ||
if key != nil { | ||
result = aws.StringValue(key.SecretARN) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// MacSecKeyParseID parses the resource ID and returns the secret ARN and connection ID | ||
func MacSecKeyParseID(id string) (string, string, error) { | ||
parts := strings.SplitN(id, "_", 2) | ||
|
||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
return "", "", &resource.NotFoundError{} | ||
} | ||
|
||
return parts[0], parts[1], nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered naming this resource
aws_dx_macsec_key_association
? Since MACsec keys can only be associated/described/dissociated, renaming the resource may help preventing any confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that the resource should be named
aws_dx_connection_macsec_key_association
.