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

Added policy_document on verified access endpoint resource #34264

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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/34264.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_verifiedaccess_endpoint: Add `policy_document` argument
```
24 changes: 24 additions & 0 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -7046,6 +7046,30 @@ func FindVerifiedAccessGroupPolicyByID(ctx context.Context, conn *ec2_sdkv2.Clie
return output, nil
}

func FindVerifiedAccessEndpointPolicyByID(ctx context.Context, conn *ec2_sdkv2.Client, id string) (*ec2_sdkv2.GetVerifiedAccessEndpointPolicyOutput, error) {
input := &ec2_sdkv2.GetVerifiedAccessEndpointPolicyInput{
VerifiedAccessEndpointId: &id,
}
output, err := conn.GetVerifiedAccessEndpointPolicy(ctx, input)

if tfawserr_sdkv2.ErrCodeEquals(err, errCodeInvalidVerifiedAccessEndpointIdNotFound) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}

func FindVerifiedAccessGroup(ctx context.Context, conn *ec2_sdkv2.Client, input *ec2_sdkv2.DescribeVerifiedAccessGroupsInput) (*awstypes.VerifiedAccessGroup, error) {
output, err := FindVerifiedAccessGroups(ctx, conn, input)

Expand Down
30 changes: 30 additions & 0 deletions internal/service/ec2/verifiedaccess_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func ResourceVerifiedAccessEndpoint() *schema.Resource {
},
},
},
"policy_document": {
Type: schema.TypeString,
Optional: true,
},
"security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -206,6 +210,10 @@ func resourceVerifiedAccessEndpointCreate(ctx context.Context, d *schema.Resourc
input.NetworkInterfaceOptions = expandCreateVerifiedAccessEndpointEniOptions(v.([]interface{})[0].(map[string]interface{}))
}

if v, ok := d.GetOk("policy_document"); ok {
input.PolicyDocument = aws.String(v.(string))
}

if v, ok := d.GetOk("security_group_ids"); ok && v.(*schema.Set).Len() > 0 {
input.SecurityGroupIds = flex.ExpandStringValueSet(v.(*schema.Set))
}
Expand Down Expand Up @@ -266,6 +274,14 @@ func resourceVerifiedAccessEndpointRead(ctx context.Context, d *schema.ResourceD
d.Set("verified_access_group_id", ep.VerifiedAccessGroupId)
d.Set("verified_access_instance_id", ep.VerifiedAccessInstanceId)

output, err := FindVerifiedAccessEndpointPolicyByID(ctx, conn, d.Id())

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Verified Access Endpoint (%s) policy: %s", d.Id(), err)
}

d.Set("policy_document", output.PolicyDocument)

return diags
}

Expand Down Expand Up @@ -309,6 +325,20 @@ func resourceVerifiedAccessEndpointUpdate(ctx context.Context, d *schema.Resourc
}
}

if d.HasChange("policy_document") {
input := &ec2.ModifyVerifiedAccessEndpointPolicyInput{
PolicyDocument: aws.String(d.Get("policy_document").(string)),
PolicyEnabled: aws.Bool(true),
VerifiedAccessEndpointId: aws.String(d.Id()),
}

_, err := conn.ModifyVerifiedAccessEndpointPolicy(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Verified Access Endpoint (%s) policy: %s", d.Id(), err)
}
}

return append(diags, resourceVerifiedAccessEndpointRead(ctx, d, meta)...)
}

Expand Down
85 changes: 85 additions & 0 deletions internal/service/ec2/verifiedaccess_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestAccVerifiedAccessEndpoint_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "domain_certificate_arn"),
resource.TestCheckResourceAttr(resourceName, "endpoint_domain_prefix", "example"),
resource.TestCheckResourceAttr(resourceName, "endpoint_type", "load-balancer"),
resource.TestCheckResourceAttr(resourceName, "policy_document", ""),
resource.TestCheckResourceAttr(resourceName, "sse_specification.0.customer_managed_key_enabled", "false"),
resource.TestCheckResourceAttrSet(resourceName, "load_balancer_options.0.load_balancer_arn"),
resource.TestCheckResourceAttr(resourceName, "load_balancer_options.0.port", "443"),
Expand Down Expand Up @@ -196,6 +197,49 @@ func TestAccVerifiedAccessEndpoint_disappears(t *testing.T) {
})
}

func TestAccVerifiedAccessEndpoint_policyDocument(t *testing.T) {
ctx := acctest.Context(t)
var v types.VerifiedAccessEndpoint
resourceName := "aws_verifiedaccess_endpoint.test"
key := acctest.TLSRSAPrivateKeyPEM(t, 2048)
certificate := acctest.TLSRSAX509SelfSignedCertificatePEM(t, key, "example.com")
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
policyDoc := "permit(principal, action, resource) \nwhen {\ncontext.http_request.method == \"GET\"\n};"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheckVerifiedAccess(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.EC2),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckVerifiedAccessEndpointDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccVerifiedAccessEndpointConfig_policyBase(rName, acctest.TLSPEMEscapeNewlines(key), acctest.TLSPEMEscapeNewlines(certificate)),
Check: resource.ComposeTestCheckFunc(
testAccCheckVerifiedAccessEndpointExists(ctx, resourceName, &v),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"endpoint_domain_prefix",
},
},
{
Config: testAccVerifiedAccessEndpointConfig_policyUpdate(rName, acctest.TLSPEMEscapeNewlines(key), acctest.TLSPEMEscapeNewlines(certificate), policyDoc),
Check: resource.ComposeTestCheckFunc(
testAccCheckVerifiedAccessEndpointExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "policy_document", policyDoc),
),
},
},
})
}

func testAccCheckVerifiedAccessEndpointDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx)
Expand Down Expand Up @@ -450,3 +494,44 @@ resource "aws_verifiedaccess_endpoint" "test" {

`, rName, key, certificate, tagKey1, tagValue1, tagKey2, tagValue2))
}

func testAccVerifiedAccessEndpointConfig_policyBase(rName, key, certificate string) string {
return acctest.ConfigCompose(testAccVerifiedAccessEndpointConfig_base(rName, key, certificate), `
resource "aws_verifiedaccess_endpoint" "test" {
application_domain = "example.com"
attachment_type = "vpc"
description = "example"
domain_certificate_arn = aws_acm_certificate.test.arn
endpoint_domain_prefix = "example"
endpoint_type = "network-interface"
network_interface_options {
network_interface_id = aws_network_interface.test.id
port = 443
protocol = "https"
}
security_group_ids = [aws_security_group.test.id]
verified_access_group_id = aws_verifiedaccess_group.test.id
}
`)
}

func testAccVerifiedAccessEndpointConfig_policyUpdate(rName, key, certificate, policyDocument string) string {
return acctest.ConfigCompose(testAccVerifiedAccessEndpointConfig_base(rName, key, certificate), fmt.Sprintf(`
resource "aws_verifiedaccess_endpoint" "test" {
application_domain = "example.com"
attachment_type = "vpc"
description = "example"
domain_certificate_arn = aws_acm_certificate.test.arn
endpoint_domain_prefix = "example"
endpoint_type = "network-interface"
network_interface_options {
network_interface_id = aws_network_interface.test.id
port = 443
protocol = "https"
}
policy_document = %[4]q
security_group_ids = [aws_security_group.test.id]
verified_access_group_id = aws_verifiedaccess_group.test.id
}
`, rName, key, certificate, policyDocument))
}
1 change: 1 addition & 0 deletions website/docs/r/verifiedaccess_endpoint.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ The following arguments are optional:
* `sse_specification` - (Optional) The options in use for server side encryption.
* `load_balancer_options` - (Optional) The load balancer details. This parameter is required if the endpoint type is `load-balancer`.
* `network_interface_options` - (Optional) The network interface details. This parameter is required if the endpoint type is `network-interface`.
* `policy_docment` - (Optional) The policy document that is associated with this resource.
* `security_group_ids` - (Optional) List of the the security groups IDs to associate with the Verified Access endpoint.
* `tags` - (Optional) Key-value tags for the Verified Access Endpoint. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

Expand Down
Loading