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

Add support for WAF packages #475

Merged

Conversation

xaf
Copy link
Contributor

@xaf xaf commented Sep 20, 2019

Depends on #474

@jacobbednarz
Copy link
Member

Please remove the dependency update from this PR, we'll do that separately.

@xaf
Copy link
Contributor Author

xaf commented Sep 23, 2019

Please remove the dependency update from this PR, we'll do that separately.

I can't as it depends on methods that are only available in the latest cloudflare-go

"sensitivity": {
Type: schema.TypeString,
Optional: true,
Default: "high",
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove the defaults here and instead provide validation that the sensitivity is one of the accepted values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't we simply do both? We need to know the default anyway to be able to act properly on the delete action, might as well use it as the default value if not provided.
I will add the Enum of valid values too, but thought it would be something that the API can directly check instead of forcing it here (doing that means any new value added for those in the API in the future will require an update to that object)

Copy link
Member

Choose a reason for hiding this comment

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

This isn't the provided default that a customer gets out of the box so we're making an assumption here to make the default high which comes with trade offs and not something we want to force on people.

If you want to leave this, it needs to be the actual default that comes for a customer.

We need to know the default anyway to be able to act properly on the delete action, might as well use it as the default value if not provided.

We don't have to have delete options if the resource doesn't support them. Once it's in Terraform, it can just be managed for Updates. If this complicates it, just noop the delete with an explanation in the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't the provided default that a customer gets out of the box so we're making an assumption here to make the default high which comes with trade offs and not something we want to force on people.

I was just following the API defaults: https://api.cloudflare.com/#waf-rule-packages-edit-firewall-package
If the defaults are different, please tell me what they are so I can change that.

We don't have to have delete options if the resource doesn't support them. Once it's in Terraform, it can just be managed for Updates. If this complicates it, just noop the delete with an explanation in the method.

This would be weak and would be a different behavior than what is used for WAF Rules, which would be very confusing (unless we prefer disabling the delete from WAF Rules too ?). I think it makes more sense to have sort of a "reset to default", but I totally agree that the "reset to default" should reset to the actual default.

"action_mode": {
Type: schema.TypeString,
Optional: true,
Default: "challenge",
Copy link
Member

Choose a reason for hiding this comment

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

Similar to https://github.com/terraform-providers/terraform-provider-cloudflare/pull/475/files#r326930670, let's move towards better validation without a default.

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from a976e2b to 95f4978 Compare September 23, 2019 14:52
@ghost ghost added size/L and removed size/XXL labels Sep 23, 2019
@xaf
Copy link
Contributor Author

xaf commented Sep 23, 2019

Updated to only include those changes, but of course it won't pass the tests for now

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from 95f4978 to b6911c8 Compare September 30, 2019 22:42
@xaf
Copy link
Contributor Author

xaf commented Sep 30, 2019

Rebased on master

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from b6911c8 to 7db5998 Compare October 1, 2019 20:42
@xaf
Copy link
Contributor Author

xaf commented Oct 1, 2019

Fixed small mistakes I made when adding the ValidateFunc

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from 7db5998 to cb183db Compare October 1, 2019 22:54
@xaf
Copy link
Contributor Author

xaf commented Oct 1, 2019

Updated the ValidateFunc to use the helper as suggested by @jacobbednarz !

@xaf
Copy link
Contributor Author

xaf commented Oct 2, 2019

@patryk @jacobbednarz is there anything I could do so we can move forward with this PR and the other one for WAF groups ?

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from cb183db to 27a53c9 Compare October 4, 2019 20:24
@ghost ghost added size/XL kind/documentation Categorizes issue or PR as related to documentation. and removed size/L labels Oct 4, 2019
@xaf
Copy link
Contributor Author

xaf commented Oct 4, 2019

Added documentation for the change

@jacobbednarz
Copy link
Member

I've just kicked off the integration tests for these and will report back shortly! Thanks for following through here @xaf!

@jacobbednarz
Copy link
Member

Looks like there is a failure in the test suite.

------- Stdout: -------
=== RUN   TestAccCloudflareWAFPackage_CreateThenUpdate
=== PAUSE TestAccCloudflareWAFPackage_CreateThenUpdate
=== CONT  TestAccCloudflareWAFPackage_CreateThenUpdate
--- FAIL: TestAccCloudflareWAFPackage_CreateThenUpdate (0.00s)
    testing.go:569: Step 0 error: Missing newline after argument: On /opt/teamcity-agent/temp/buildTmp/tf-test483636842/main.tf line 3: An argument definition must end with a newline.
FAIL

I'm able to replicate this running locally too.

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from 32fc6f6 to 3cd0b7e Compare October 8, 2019 21:27
@xaf
Copy link
Contributor Author

xaf commented Oct 8, 2019

I would add it as a part of the PreCheck step for the test.

The problem with that approach is that I need to know the packageID to prepare the config, which is evaluated at the time we're creating the test, so if we grab the actual packageID only when running the tests, we cannot act properly.

I thus went and created a new function that creates a new client specifically to grab the information needed, as you can see in the updated code. Please tell me what you think about that @jacobbednarz and if you have any better way to implement this ?

For now, the acceptance tests are passing:

=== RUN   TestAccCloudflareWAFPackage_CreateThenUpdate
=== PAUSE TestAccCloudflareWAFPackage_CreateThenUpdate
=== CONT  TestAccCloudflareWAFPackage_CreateThenUpdate
2019/10/08 17:26:36 [INFO] Cloudflare Client configured for user:
--- PASS: TestAccCloudflareWAFPackage_CreateThenUpdate (3.65s)
PASS
ok  	github.com/terraform-providers/terraform-provider-cloudflare/cloudflare	3.676s

@xaf xaf force-pushed the xaf/add_support_for_waf_packages branch from 3cd0b7e to 066cdba Compare October 8, 2019 21:43

func testAccGetWAFPackage(zoneID string) (string, error) {
config := Config{}
if apiToken, ok := os.LookupEnv("CLOUDFLARE_API_TOKEN"); ok {
Copy link
Member

Choose a reason for hiding this comment

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

I don't really like this in here as we're duplicating the logic we already have in the provider setup. If that ever changes, we now have somewhere that depends on it directly. Was there a reason you couldn't use the sharedClient? We're already using that in sweepers and it leverages the existing methods.

Copy link
Member

@jacobbednarz jacobbednarz Oct 8, 2019

Choose a reason for hiding this comment

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

If we can't use the shareClient functionality, let's just copy the function and use it here. Don't worry about the logic as we can swap it all when/if we move to API tokens for the test suite.

Another drawback to having this logic here is that testAccPreCheck doesn't check for the API token, only the older method of authenticating which means use of API tokens isn't enforced at all.

Copy link
Contributor Author

@xaf xaf Oct 8, 2019

Choose a reason for hiding this comment

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

Another drawback to having this logic here is that testAccPreCheck doesn't check for the API token, only the older method of authenticating which means use of API tokens isn't enforced at all.

Note that testAccPreCheck is still run just after, so any check it does will be done here, so that should be fine ? But true that we're listing the packages before having done that check...

I don't really like this in here as we're duplicating the logic we already have in the provider setup. If that ever changes, we now have somewhere that depends on it directly. Was there a reason you couldn't use the sharedClient? We're already using that in sweepers and it leverages the existing methods.

sharedClient currently seems to be forcing to use API Key & email instead of API token, is this expected ?

    resource_cloudflare_waf_package_test.go:18: Error while listing WAF packages: error from makeRequest: HTTP status 400: content "{\"success\":false,\"errors\":[{\"code\":6003,\"message\":\"Invalid request headers\",\"error_chain\":[{\"code\":6103,\"message\":\"Invalid format for X-Auth-Key header\"}]}],\"messages\":[],\"result\":null}"

Copy link
Member

Choose a reason for hiding this comment

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

sharedClient currently seems to be forcing to use API Key & email instead of API token, is this expected ?

Yep, we'll eventually move this over to API tokens when it's supported fully.

WRT to your error, are you setting the CLOUDFLARE_API_KEY and CLOUDFLARE_EMAIL per the client initialisation when you're running the tests? As you mentioned, the PreCheck usually handles this but because we're running before it, it could be missed.

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 did not want to go fetch an API key so I used CLOUDFLARE_API_KEY="blah" for my tests above, which is why I had that error (this is also why I corrected my message after the fact, realizing the error was related to that).

I think my last push (fixup this time, sorry again) should be good to go ? (using sharedClient)

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha, that makes total sense then 😄

@jacobbednarz
Copy link
Member

@xaf are you able to avoid rebasing until we're ready to merge? It's quite difficult to review the individual changes you're making.

We can squash it when it's ready to merge if you'd prefer that.

@xaf
Copy link
Contributor Author

xaf commented Oct 8, 2019

@xaf are you able to avoid rebasing until we're ready to merge? It's quite difficult to review the individual changes you're making.

Yes, sorry for that... I try to push fixup commits usually but forgot this time!

@jacobbednarz
Copy link
Member

Great stuff. I've just kicked off the integration suite again and we'll see how we go this time.

@xaf
Copy link
Contributor Author

xaf commented Oct 8, 2019

🤞

@jacobbednarz
Copy link
Member

Looks like the unit tests are failing to build - https://travis-ci.org/terraform-providers/terraform-provider-cloudflare/jobs/595347342#L234

Integration tests are also failing in a similar fashion.


[09:22:28]
cloudflare/resource_cloudflare_waf_package.go:8:2: cannot find package "github.com/hashicorp/terraform/helper/schema" in any of:
[09:22:28]
	/opt/teamcity-agent/work/962a4ee8807683e3/src/github.com/terraform-providers/terraform-provider-cloudflare/vendor/github.com/hashicorp/terraform/helper/schema (vendor tree)
[09:22:28]
	/opt/goenv/versions/1.12.9/src/github.com/hashicorp/terraform/helper/schema (from $GOROOT)
[09:22:28]
	/opt/teamcity-agent/work/962a4ee8807683e3/src/github.com/hashicorp/terraform/helper/schema (from $GOPATH)
[09:22:28]
cloudflare/resource_cloudflare_waf_package.go:9:2: cannot find package "github.com/hashicorp/terraform/helper/validation" in any of:
[09:22:28]
	/opt/teamcity-agent/work/962a4ee8807683e3/src/github.com/terraform-providers/terraform-provider-cloudflare/vendor/github.com/hashicorp/terraform/helper/validation (vendor tree)
[09:22:28]
	/opt/goenv/versions/1.12.9/src/github.com/hashicorp/terraform/helper/validation (from $GOROOT)
[09:22:28]
	/opt/teamcity-agent/work/962a4ee8807683e3/src/github.com/hashicorp/terraform/helper/validation (from $GOPATH)
[09:22:28]
Process exited with code 1

@xaf
Copy link
Contributor Author

xaf commented Oct 9, 2019

Probably because it's based on a previous version that did not use the same packages in the headers (the path seem to have changed).
Updated with the new paths, but I'm not able to test locally anymore unless I rebase (and thus push -f, so will let things run in here...

@jacobbednarz
Copy link
Member

Updated with the new paths, but I'm not able to test locally anymore unless I rebase (and thus push -f, so will let things run in here...

I don't understand why this is? Are you able to let me know what issues you're hitting? You should always be able to run the tests locally when developing the provider.

@xaf
Copy link
Contributor Author

xaf commented Oct 9, 2019

Oh, failed again as I forgot to update all of them.

It's just that my local branch is not on top of the current master, and to get on top of the current master I need to rebase.
The errors I get are kind of the opposite really:

==> Checking that code complies with gofmt requirements...
go install -ldflags="-X github.com/terraform-providers/terraform-provider-cloudflare/version.ProviderVersion=v2.0.0-8-g43ed656"
cloudflare/resource_cloudflare_waf_package.go:8:2: cannot find package "github.com/hashicorp/terraform-plugin-sdk/helper/schema" in any of:
/Users/xaf/src/github.com/terraform-providers/terraform-provider-cloudflare/vendor/github.com/hashicorp/terraform-plugin-sdk/helper/schema (vendor tree)
/usr/local/Cellar/go/1.12.4/libexec/src/github.com/hashicorp/terraform-plugin-sdk/helper/schema (from $GOROOT)
/Users/xaf/src/github.com/hashicorp/terraform-plugin-sdk/helper/schema (from $GOPATH)
cloudflare/resource_cloudflare_waf_package.go:9:2: cannot find package "github.com/hashicorp/terraform-plugin-sdk/helper/validation" in any of:
/Users/xaf/src/github.com/terraform-providers/terraform-provider-cloudflare/vendor/github.com/hashicorp/terraform-plugin-sdk/helper/validation (vendor tree)
/usr/local/Cellar/go/1.12.4/libexec/src/github.com/hashicorp/terraform-plugin-sdk/helper/validation (from $GOROOT)
/Users/xaf/src/github.com/hashicorp/terraform-plugin-sdk/helper/validation (from $GOPATH)

@jacobbednarz
Copy link
Member

It's just that my local branch is not on top of the current master, and to get on top of the current master I need to rebase.

You can still merge in the upstream master and it will have the same effect without needing to rebase or force push changes. We're not fussed if there is merge commits or not so that is up to you.

@xaf
Copy link
Contributor Author

xaf commented Oct 9, 2019

resource_cloudflare_waf_package_test.go:18: invalid credentials: key & email must not be empty
testing.go:462: Acceptance tests skipped unless env 'TF_ACC' set

Ok, might need to manually test for TF_ACC before trying to fetch the package!

@xaf
Copy link
Contributor Author

xaf commented Oct 9, 2019

All checks have passed

🎉

@jacobbednarz
Copy link
Member

Looks like the integration tests are happy too! I'll take another look over this and we'll get it merged.

@xaf
Copy link
Contributor Author

xaf commented Oct 9, 2019

🎉

Let me know if you want me to rebase -i --autosquash all those fixups.
Once you confirm everything is fine, I'll also push the changes to the "Add support for WAF Groups" one!


* `zone_id` - (Required) The DNS zone ID to apply to.
* `package_id` - (Required) The WAF Package ID.
* `sensitivity` - (Required) The sensitivity of the package, can be one of ["high", "medium", "low", "off"].
Copy link
Member

Choose a reason for hiding this comment

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

This documentation doesn't match the schema. Do you intend for this to be optional (to match the schema)? Same with action_mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, should read Optional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@jacobbednarz jacobbednarz merged commit cdaa76c into cloudflare:master Oct 9, 2019
@jacobbednarz
Copy link
Member

Thanks for your patience with this! If you can apply the same fixes to #476, we'll get that merged too.

@xaf xaf deleted the xaf/add_support_for_waf_packages branch October 10, 2019 01:36
boekkooi-lengoo pushed a commit to boekkooi-lengoo/terraform-provider-cloudflare that referenced this pull request Feb 28, 2022
SetWorkersSecret creates or updates a secret
DeleteWorkersSecret deletes a secret
ListWorkersSecrets lists all secrets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies kind/documentation Categorizes issue or PR as related to documentation. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants