-
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
Added tunnel options to vpn_connection #1862
Added tunnel options to vpn_connection #1862
Conversation
FYI I compiled this locally and it Worked-For-Me™ when I needed it, thanks @a-teisseire . |
This PR would close #2220 |
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.
This is looking pretty good! I have suggested some additional validation since Amazon is very explicit about the acceptable values for these and some other minor comments.
Can you please ensure the documentation in website/docs/r/vpn_connection.html.markdown
is updated as well? Thanks!
@@ -94,6 +94,36 @@ func resourceAwsVpnConnection() *schema.Resource { | |||
ForceNew: true, | |||
}, | |||
|
|||
"tunnel1_inside_cidr": { |
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.
We can add validation for these new tunnel#_inside_cidr
attributes via:
ValidateFunc: validateVpnConnectionTunnelInsideCIDR,
Which is defined something like:
// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_VpnTunnelOptionsSpecification.html
func validateVpnConnectionTunnelInsideCIDR(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, ipnet, err := net.ParseCIDR(value)
if err != nil {
errors = append(errors, fmt.Errorf("%q must contain a valid CIDR, got error parsing: %s", k, err))
return
}
if !strings.HasSuffix(ipnet.String(), "/30") {
errors = append(errors, fmt.Errorf("%q must be /30 CIDR", k))
}
if !strings.HasPrefix(ipnet.String(), "169.254.") {
errors = append(errors, fmt.Errorf("%q must be within 169.254.0.0/16", k))
} else if ipnet.String() == "169.254.0.0/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.0.0/30", k))
} else if ipnet.String() == "169.254.1.0/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.1.0/30", k))
} else if ipnet.String() == "169.254.2.0/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.2.0/30", k))
} else if ipnet.String() == "169.254.3.0/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.3.0/30", k))
} else if ipnet.String() == "169.254.4.0/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.4.0/30", k))
} else if ipnet.String() == "169.254.5.0/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.5.0/30", k))
} else if ipnet.String() == "169.254.169.252/30" {
errors = append(errors, fmt.Errorf("%q cannot be 169.254.169.252/30", k))
}
return
}
And tested by making the tunnel1_inside_cidr
/tunnel1_preshared_key
configurable in testAccAwsVpnConnectionConfigTunnelOptions
then calling these test steps:
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "not-a-cidr"),
ExpectError: regexp.MustCompile(`must contain a valid CIDR`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.254.0/31"),
ExpectError: regexp.MustCompile(`must be /30 CIDR`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "172.16.0.0/30"),
ExpectError: regexp.MustCompile(`must be within 169.254.0.0/16`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.0.0/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.0.0/30`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.1.0/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.1.0/30`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.2.0/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.2.0/30`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.3.0/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.3.0/30`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.4.0/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.4.0/30`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.5.0/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.5.0/30`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "12345678", "169.254.169.252/30"),
ExpectError: regexp.MustCompile(`cannot be 169.254.169.252/30`),
},
aws/resource_aws_vpn_connection.go
Outdated
ForceNew: true, | ||
}, | ||
|
||
"tunnel1_preshared_key": { |
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.
We can add validation for these now configurable tunnel#_preshared_key
attributes via:
ValidateFunc: validateVpnConnectionTunnelPreSharedKey,
Which is defined something like:
// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_VpnTunnelOptionsSpecification.html
func validateVpnConnectionTunnelPreSharedKey(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if (len(value) < 8) || (len(value) > 64) {
errors = append(errors, fmt.Errorf("%q must be between 8 and 64 characters in length", k))
}
if strings.HasPrefix(value, "0") {
errors = append(errors, fmt.Errorf("%q cannot start with zero character", k))
}
if !regexp.MustCompile(`^[0-9a-zA-Z_]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q can only contain alphanumeric and underscore characters", k))
}
return
}
And tested by making the tunnel1_inside_cidr
/tunnel1_preshared_key
configurable in testAccAwsVpnConnectionConfigTunnelOptions
then calling these test steps:
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "1234567", "169.254.254.0/30"),
ExpectError: regexp.MustCompile(`must be between 8 and 64 characters in length`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, acctest.RandStringFromCharSet(65, acctest.CharSetAlpha), "169.254.254.0/30"),
ExpectError: regexp.MustCompile(`must be between 8 and 64 characters in length`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "01234567", "169.254.254.0/30"),
ExpectError: regexp.MustCompile(`cannot start with zero character`),
},
{
Config: testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn, "1234567!", "169.254.254.0/30"),
ExpectError: regexp.MustCompile(`can only contain alphanumeric and underscore characters`),
},
aws/resource_aws_vpn_connection.go
Outdated
@@ -245,8 +261,37 @@ func resourceAwsVpnConnection() *schema.Resource { | |||
func resourceAwsVpnConnectionCreate(d *schema.ResourceData, meta interface{}) error { | |||
conn := meta.(*AWSClient).ec2conn | |||
|
|||
// Get the optional tunnel options | |||
tunnel1_cidr := d.Get("tunnel1_inside_cidr").(string) |
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.
These can be simplified by using d.GetOk()
and placed where you have your other if statements 😄
e.g.
if v, ok := d.GetOk("tunnel1_preshared_key"); ok {
options[0].TunnelInsideCidr = aws.String(v.(string))
}
@@ -325,6 +359,38 @@ func testAccAwsVpnConnectionConfigUpdate(rInt, rBgpAsn int) string { | |||
`, rBgpAsn, rInt) | |||
} | |||
|
|||
func testAccAwsVpnConnectionConfigTunnelOptions(rBgpAsn int) string { | |||
return fmt.Sprintf(` | |||
resource "aws_vpn_gateway" "vpn_gateway" { |
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.
Nitpick: I don't think we have a style guide I can point to, but can you please ensure the Terraform configuration inside the testing here is fully to the left with 2 space indentation? We're trying to standardize going forward. Thanks!
Hey, Thanks a lot for this feedback, I'll try to commit an update with these details today or in the upcoming days. |
Hi @a-teisseire Do you need any help implementing the changes @bflad mentioned? |
56403c9
to
88ae76f
Compare
88ae76f
to
da3bce1
Compare
Hello @radeksimko I have implemented the changes and made the documentation. |
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.
This looks great @a-teisseire! Thanks for the work here!
make testacc TEST=./aws TESTARGS='-run=TestAccAWSVpnConnection'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSVpnConnection -timeout 120m
=== RUN TestAccAWSVpnConnection_importBasic
--- PASS: TestAccAWSVpnConnection_importBasic (1259.52s)
=== RUN TestAccAWSVpnConnectionRoute_basic
--- PASS: TestAccAWSVpnConnectionRoute_basic (565.63s)
=== RUN TestAccAWSVpnConnection_basic
--- PASS: TestAccAWSVpnConnection_basic (964.83s)
=== RUN TestAccAWSVpnConnection_tunnelOptions
--- PASS: TestAccAWSVpnConnection_tunnelOptions (375.86s)
=== RUN TestAccAWSVpnConnection_withoutStaticRoutes
--- PASS: TestAccAWSVpnConnection_withoutStaticRoutes (468.70s)
=== RUN TestAccAWSVpnConnection_disappears
--- PASS: TestAccAWSVpnConnection_disappears (435.19s)
PASS
ok github.com/terraform-providers/terraform-provider-aws/aws 4069.793s
This has been released in terraform-provider-aws version 1.8.0. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Closes #2220
Context
AWS recently allowed users to let them choose the CIDR and the PSK to use inside IPsec tunnels.
More details here : https://aws.amazon.com/about-aws/whats-new/2017/10/aws-vpn-update-custom-psk-inside-tunnel-ip-and-sdk-update/
Changes
This Pull Request changes the way tunnel1_preshared_key and tunnel2_preshared_key behave (from read-only to writeable).
The tunnel1_inside_cidr and tunnel2_inside_cidr have been introduced also to be used as parameters for the CreateVpnConnection API.
Test coverage has been added to test these features.
Tests