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

resource/api_gateway_authorizer: support authorizer type as COGNITO_USER_POOLS #3156

Conversation

loivis
Copy link
Contributor

@loivis loivis commented Jan 27, 2018

Fixes #1106
Duplicate of #2189

I missed existing issue and pr when I started. Anyway I continued with this new pr.

QUSTION: type changed to ForceNew

Because COGNITO_USER_POOLS is different with TOKEN/REQUEST. But when changing between TOKEN and REQUEST, it doesn't required new resource. To conditionally ForceNew, the only way I can think of is to have some logic in update function that will detect type change and call create function. I was wondering if terraform has already supported conditional ForceNew someway or we just go with always ForceNew.

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jan 27, 2018
@loivis
Copy link
Contributor Author

loivis commented Jan 27, 2018

⎇  make testacc TEST=./aws TESTARGS='-run=TestAccAWSAPIGatewayAuthorizer_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAPIGatewayAuthorizer_ -timeout 120m
=== RUN   TestAccAWSAPIGatewayAuthorizer_basic
--- PASS: TestAccAWSAPIGatewayAuthorizer_basic (50.11s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_cognito
--- PASS: TestAccAWSAPIGatewayAuthorizer_cognito (34.12s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_switchAuthType
--- PASS: TestAccAWSAPIGatewayAuthorizer_switchAuthType (83.78s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	168.058s

@loivis loivis force-pushed the resource-api-gateway-authorizer-support-cognito-user-pool branch from f13113d to 7db27dc Compare January 27, 2018 01:55
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jan 27, 2018
@loivis
Copy link
Contributor Author

loivis commented Jan 27, 2018

Also introduced randString in acceptance test, which makes code change more than needed for this pr. I think it's ok to include it here instead of opening another pr. No worth for that.

@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. service/apigateway Issues and PRs that pertain to the apigateway service. labels Jan 28, 2018
@loivis
Copy link
Contributor Author

loivis commented Feb 1, 2018

@bflad Do you have time to take a look? Thanks.

@bflad
Copy link
Contributor

bflad commented Feb 1, 2018

Not at the current time. Something that is an immediate red flag is this though:

I missed existing issue and pr when I started. Anyway I continued with this new pr.

We really do not like ignoring someone's contributions and the original author is asking for help in their issue. Can you help them instead of ignoring their contribution?

@loivis
Copy link
Contributor Author

loivis commented Feb 1, 2018 via email

@loivis
Copy link
Contributor Author

loivis commented Feb 1, 2018

Back to this PR again with close of #2189.

@bflad
Copy link
Contributor

bflad commented Feb 1, 2018

Thanks for the followup @loivis! Sorry if I came off a little mean earlier, that was not my intention.

@loivis
Copy link
Contributor Author

loivis commented Feb 1, 2018

No need sorry, not at all. It's good to know how we're managing issue/prs and follow the same.

@loivis
Copy link
Contributor Author

loivis commented Mar 2, 2018

@bflad do you have time to take a look? Thanks.

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @loivis! This is looking pretty good. I left you some initial feedback. Please ping me again when its ready to be looked at again or if you do not have time to implement 👍

Testing is currently passing for me:

=== RUN   TestAccAWSAPIGatewayAuthorizer_cognito
--- PASS: TestAccAWSAPIGatewayAuthorizer_cognito (14.86s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_switchAuthType
--- PASS: TestAccAWSAPIGatewayAuthorizer_switchAuthType (48.30s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_basic
--- PASS: TestAccAWSAPIGatewayAuthorizer_basic (81.04s)

@@ -55,6 +56,11 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"provider_arns": &schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: the &schema.Schema here (copy paste 😄 ) is extra

@@ -55,6 +56,11 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"provider_arns": &schema.Schema{
Type: schema.TypeList,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the ordering of the values matter for the provider_arns attribute? If not, we should probably switch this to schema.TypeSet 👍

@@ -21,7 +21,7 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
Schema: map[string]*schema.Schema{
"authorizer_uri": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm unfamiliar with this resource, but is this required for TOKEN/REQUEST? Just curious if that was an oversight before, if we should add documentation, or CustomizeDiff for continued plan-time validation

Copy link
Contributor Author

@loivis loivis Mar 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bflad Indeed CustomizeDiff should be used here. But it doesn't seem to get the argument. Did I miss something?
https://github.com/loivis/terraform-provider-aws/blob/9726a5187a1fb34a8edbc660e0d3968d8e46a3f0/aws/resource_aws_api_gateway_authorizer.go#L277-L280

Copy link
Contributor

@bflad bflad Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to perform two checks, one for existence and one for string length:

v, ok := diff.GetOk("authorizer_uri")
if ok {
  if v.(string) == "" {
    return errors.New("authorizer_uri must be non-empty when type is ...")
  }
} else {
  return errors.New("authorizer_uri is required when type is ...")
}

You'll need to do the same with len() == 0 for provider_arns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few lines to debug

	args := []string{"authorizer_uri", "name", "rest_api_id", "identity_source", "type", "identity_validation_expression", "authorizer_credentials"}
	for _, arg := range args {
		val, ok := diff.GetOk(arg)
		log.Printf("[DEBUG] %s: #%s#, #%v#", arg, val.(string), ok)
	}

It gives me logs:

2018/03/05 21:50:54 [DEBUG] authorizer_uri: ##, #false#
2018/03/05 21:50:54 [DEBUG] name: #tf-acctest-igw-authorizer-yddwa40#, #true#
2018/03/05 21:50:54 [DEBUG] rest_api_id: ##, #false#
2018/03/05 21:50:54 [DEBUG] identity_source: #method.request.header.Authorization#, #true#
2018/03/05 21:50:54 [DEBUG] type: #TOKEN#, #true#
2018/03/05 21:50:54 [DEBUG] identity_validation_expression: ##, #false#
2018/03/05 21:50:54 [DEBUG] authorizer_credentials: ##, #false#

name, identity_source, type are arguments with default value. It seems like diff.GetOk() doesn't return value for arguments without default value. This is what makes me confused.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is happening because the values are computed:

https://github.com/hashicorp/terraform/blob/ea50f455edca3acaa0a6ab372b861e6cd2d386aa/helper/schema/resource_diff.go#L339

Unfortunately, I'm not sure of a workaround at the moment. For now, we can keep that logic outside CustomizeDiff. 🙁

@@ -210,3 +228,38 @@ func resourceAwsApiGatewayAuthorizerDelete(d *schema.ResourceData, meta interfac

return nil
}

func diffProviderARNsOp(prefix string, old, new []interface{}) (ops []*apigateway.PatchOperation) {
// providerARNs can't be empty, so add first and then remove
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this comment 👍

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSAPIGatewayAuthorizer_basic(t *testing.T) {
var conf apigateway.Authorizer
rString := acctest.RandString(7)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@@ -113,3 +113,4 @@ The following arguments are supported:
For `TOKEN` type, this value should be a regular expression. The incoming token from the client is matched
against this expression, and will proceed if the token matches. If the token doesn't match,
the client receives a 401 Unauthorized response.
* `provider_arns` - (Optional, required for type `COGNITO_USER_POOLS`) A list of the Amazon Cognito user pool ARNs. Each element is of this format: `arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind taking a look at adding a CustomizeDiff function that adds plan-time validation for this situation? Maybe something similar to aws_dynamodb_table function can work here too:

https://github.com/terraform-providers/terraform-provider-aws/blob/0cd8e5a0e6572740511f54c44c483a0549ecee2e/aws/validators.go#L2236

For `TOKEN` type, this must be a well-formed Lambda function URI in the form of
`arn:aws:apigateway:{region}:lambda:path/{service_api}`. e.g. `arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:012345678912:function:my-function/invocations`
* `name` - (Required) The name of the authorizer
* `rest_api_id` - (Required) The ID of the associated REST API
* `identity_source` - (Optional) The source of the identity in an incoming request.
Defaults to `method.request.header.Authorization`. For `REQUEST` type, this may be a comma-separated list of values, including headers, query string parameters and stage variables - e.g. `"method.request.header.SomeHeaderName,method.request.querystring.SomeQueryStringName,stageVariables.SomeStageVariableName"`
* `type` - (Optional) The type of the authorizer. Possible values are `TOKEN` and `REQUEST`.
* `type` - (Optional) The type of the authorizer. Possible values are `TOKEN` for a Lambda function using a single authorization token submitted in a custom header, `REQUEST` for a Lambda function using incoming request parameters, or `COGNITO_USER_POOLS` for using an Amazon Cognito user pool.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

resource "aws_api_gateway_authorizer" "test" {
name = "tf-acc-test-authorizer"
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
func testAccAWSAPIGatewayAuthorizerConfig_lambda(apiGatewayName, authorizerName, lambdaName string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍 thank you

@@ -41,6 +41,7 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: "TOKEN",
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add plan-time validation here please?

ValidateFunc: validation.StringInSlice([]string{
  apigateway.AuthorizerTypeCognitoUserPools,
  apigateway.AuthorizerTypeRequest,
  apigateway.AuthorizerTypeToken
}, false),

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Mar 3, 2018
@bflad
Copy link
Contributor

bflad commented Mar 3, 2018

To answer your question above:

I was wondering if terraform has already supported conditional ForceNew someway or we just go with always ForceNew.

You can use a CustomizeDiff function to do that. 😄 You can check diff.HasChange("type") and if it does use o,n := diff.GetChange("type") to selectively call a diff.ForceNew("type") appropriately 🎉

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 3, 2018
@loivis loivis force-pushed the resource-api-gateway-authorizer-support-cognito-user-pool branch from a2f4a49 to f32ffbd Compare March 3, 2018 12:55
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 3, 2018
@loivis loivis force-pushed the resource-api-gateway-authorizer-support-cognito-user-pool branch from 9726a51 to dfea540 Compare March 5, 2018 20:52
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Mar 5, 2018
@@ -181,6 +199,12 @@ func resourceAwsApiGatewayAuthorizerUpdate(d *schema.ResourceData, meta interfac
Value: aws.String(d.Get("identity_validation_expression").(string)),
})
}
if d.HasChange("provider_arns") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice:+1:

@loivis
Copy link
Contributor Author

loivis commented Mar 6, 2018

⎇  make testacc TEST=./aws TESTARGS='-run=TestAccAWSAPIGatewayAuthorizer_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAPIGatewayAuthorizer_ -timeout 120m
=== RUN   TestAccAWSAPIGatewayAuthorizer_basic
--- PASS: TestAccAWSAPIGatewayAuthorizer_basic (54.47s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_cognito
--- PASS: TestAccAWSAPIGatewayAuthorizer_cognito (33.99s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_switchAuthType
--- PASS: TestAccAWSAPIGatewayAuthorizer_switchAuthType (89.65s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_authTypeValidation
--- PASS: TestAccAWSAPIGatewayAuthorizer_authTypeValidation (44.02s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	222.195s

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fantastic, thanks for the great work! 🚀

4 tests passed (all tests)
=== RUN   TestAccAWSAPIGatewayAuthorizer_authTypeValidation
--- PASS: TestAccAWSAPIGatewayAuthorizer_authTypeValidation (29.27s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_switchAuthType
--- PASS: TestAccAWSAPIGatewayAuthorizer_switchAuthType (64.32s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_basic
--- PASS: TestAccAWSAPIGatewayAuthorizer_basic (105.69s)
=== RUN   TestAccAWSAPIGatewayAuthorizer_cognito
--- PASS: TestAccAWSAPIGatewayAuthorizer_cognito (220.05s)

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Mar 7, 2018
@bflad bflad added this to the v1.11.0 milestone Mar 7, 2018
@bflad bflad merged commit 075c4a2 into hashicorp:master Mar 7, 2018
bflad added a commit that referenced this pull request Mar 7, 2018
@loivis loivis deleted the resource-api-gateway-authorizer-support-cognito-user-pool branch March 7, 2018 17:40
@bflad
Copy link
Contributor

bflad commented Mar 9, 2018

This has been released in version 1.11.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 7, 2020

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!

@ghost ghost locked and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/apigateway Issues and PRs that pertain to the apigateway service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support COGNITO_USER_POOLS authorizer
2 participants