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 vault_quota_rate_limit interval and block_interval #1084

Merged
merged 3 commits into from
Jun 6, 2022

Conversation

mdgreenfield
Copy link
Contributor

Allows users to specify either in terms of duration or number of
seconds utilizing Vault's parseutil.ParseDurationSecond logic. See
https://github.com/hashicorp/vault/blob/ed33ed1a0a5ecb9d41ab22b4899aa318239ac031/sdk/helper/parseutil/parseutil.go#L96-L147

Closes #1049

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" comments, they generate extra noise for pull request followers and do not help prioritize the request

Release note for CHANGELOG:

Add support for `vault_quota_rate_limit` `interval` and `block_interval`

Output from acceptance testing:

$ ➜  terraform-provider-vault git:(quota-rate-limit-changes) make testacc TESTARGS='-run=TestQuotaRateLimit'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run=TestQuotaRateLimit -timeout 120m
?       github.com/hashicorp/terraform-provider-vault   [no test files]
?       github.com/hashicorp/terraform-provider-vault/cmd/coverage      [no test files]
?       github.com/hashicorp/terraform-provider-vault/cmd/generate      [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/codegen   (cached) [no tests to run]
?       github.com/hashicorp/terraform-provider-vault/generated [no test files]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/datasources/transform/decode    0.622s [no tests to run]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/datasources/transform/encode    0.846s [no tests to run]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/alphabet    1.799s [no tests to run]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/role        0.477s [no tests to run]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/template    1.562s [no tests to run]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/transformation      1.101s [no tests to run]
?       github.com/hashicorp/terraform-provider-vault/schema    [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/util      (cached) [no tests to run]
WARNING: Package "github.com/golang/protobuf/protoc-gen-go/generator" is deprecated.
        A future release of golang/protobuf will delete this package,
        which has long been excluded from the compatibility promise.

=== RUN   TestQuotaRateLimit
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 0
  interval = 1
  name = tf-test-5534064214137632519
  path =
  rate = 157
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 0
  interval = 1
  name = tf-test-5534064214137632519
  path =
  rate = 157
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 0
  interval = 1
  name = tf-test-5534064214137632519
  path =
  rate = 157
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 0
  interval = 1
  name = tf-test-5534064214137632519
  path =
  rate = 157
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 0
  interval = 1
  name = tf-test-5534064214137632519
  path =
  rate = 157
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 120
  interval = 60
  name = tf-test-5534064214137632519
  path =
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 120
  interval = 60
  name = tf-test-5534064214137632519
  path =
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 120
  interval = 60
  name = tf-test-5534064214137632519
  path =
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 120
  interval = 60
  name = tf-test-5534064214137632519
  path =
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 120
  interval = 60
  name = tf-test-5534064214137632519
  path =
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 60
  interval = 120
  name = tf-test-5534064214137632519
  path = sys/
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 60
  interval = 120
  name = tf-test-5534064214137632519
  path = sys/
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 60
  interval = 120
  name = tf-test-5534064214137632519
  path = sys/
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 60
  interval = 120
  name = tf-test-5534064214137632519
  path = sys/
  rate = 525.3
vault_quota_rate_limit.foobar:
  ID = tf-test-5534064214137632519
  provider = provider.vault
  block_interval = 60
  interval = 120
  name = tf-test-5534064214137632519
  path = sys/
  rate = 525.3
--- PASS: TestQuotaRateLimit (0.60s)
PASS
ok      github.com/hashicorp/terraform-provider-vault/vault     1.018s

util/util.go Outdated Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Description: "If set, when a client reaches a rate limit threshold, the client will be prohibited from any further requests until after the 'block_interval' has elapsed.",
StateFunc: durationSecond,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because read requests return these values in seconds and not a duration the configuration value must be computed in terms of seconds to prevent a perpetual diff.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since vault converts the duration strings to seconds (int), we prefer to only support schema.TypeInt for these sorts of Vault fields. This should greatly simplify your PR.

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@benashz benashz left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution. We can scale down this PR by making interval and block_interval schema.TypeInt.

Type: schema.TypeString,
Optional: true,
Description: "If set, when a client reaches a rate limit threshold, the client will be prohibited from any further requests until after the 'block_interval' has elapsed.",
StateFunc: durationSecond,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since vault converts the duration strings to seconds (int), we prefer to only support schema.TypeInt for these sorts of Vault fields. This should greatly simplify your PR.

@benashz
Copy link
Contributor

benashz commented May 30, 2022

@mdgreenfield please let us know if you have spare cycles to work on this PR. If not we would be happy to take it over.

Thanks,

Ben

@benashz benashz self-assigned this May 30, 2022
@benashz benashz added this to the 3.7.0 milestone May 30, 2022
@mdgreenfield
Copy link
Contributor Author

Hi @benashz I can address the comments in the next day or two.

@github-actions github-actions bot added size/M and removed size/L labels Jun 1, 2022
@mdgreenfield mdgreenfield force-pushed the quota-rate-limit-changes branch 4 times, most recently from 3875b7b to d2c5ba3 Compare June 1, 2022 21:27
@mdgreenfield
Copy link
Contributor Author

@benashz I've updated the PR switching to schema.TypeInt.

I did notice that some fields, e.g. the TTL fields on the auth mount resource, use schema.TypeString and make use of validateDuration in the vault package. That may predate the move towards using schema.TypeInt and I suspect we can't really just go back and update it to schema.TypeInt without being backwards incompatible.

@mdgreenfield
Copy link
Contributor Author

The updated acceptance testing output:

$ make testacc TESTARGS='-run=TestQuotaRateLimit'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test -v -run=TestQuotaRateLimit -timeout 30m ./...
?       github.com/hashicorp/terraform-provider-vault   [no test files]
?       github.com/hashicorp/terraform-provider-vault/cmd/coverage      [no test files]
?       github.com/hashicorp/terraform-provider-vault/cmd/generate      [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/codegen   0.415s [no tests to run]
?       github.com/hashicorp/terraform-provider-vault/generated [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/datasources/transform/decode    1.823s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/datasources/transform/encode    1.803s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/alphabet    1.801s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/role        0.941s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/template    1.073s [no tests to run]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/generated/resources/transform/transformation      2.053s [no tests to run]
?       github.com/hashicorp/terraform-provider-vault/helper    [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/internal/identity/entity  1.038s [no tests to run]
?       github.com/hashicorp/terraform-provider-vault/schema    [no test files]
?       github.com/hashicorp/terraform-provider-vault/testutil  [no test files]
testing: warning: no tests to run
PASS
ok      github.com/hashicorp/terraform-provider-vault/util      0.946s [no tests to run]
=== RUN   TestQuotaRateLimit
--- PASS: TestQuotaRateLimit (81.98s)
PASS
ok      github.com/hashicorp/terraform-provider-vault/vault     83.321s

Copy link
Contributor

@benashz benashz left a comment

Choose a reason for hiding this comment

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

Looks good! I added a few minor suggestions that should be addressed.

rateLimt := fmt.Sprintf("%.1f", whole+decimal)
// Vault retuns floats with trailing zeros trimmed
return strings.TrimRight(strings.TrimRight(rateLimt, "0"), ".")
rateLimit := fmt.Sprintf("%.1f", whole+decimal)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

vault/resource_quota_rate_limit.go Outdated Show resolved Hide resolved
vault/resource_quota_rate_limit.go Outdated Show resolved Hide resolved
vault/resource_quota_rate_limit.go Outdated Show resolved Hide resolved
vault/resource_quota_rate_limit.go Outdated Show resolved Hide resolved
website/docs/r/quota_rate_limit.md Outdated Show resolved Hide resolved
vault/resource_quota_rate_limit.go Outdated Show resolved Hide resolved
@mdgreenfield
Copy link
Contributor Author

Thanks @benashz for the review. I learned a few things about writing Terraform providers from your comments.

Changes have been added to the PR.

Copy link
Contributor

@benashz benashz left a comment

Choose a reason for hiding this comment

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

LGTM!

Thank you for you contribution to HashiCorp!

@benashz benashz merged commit 19ea794 into hashicorp:main Jun 6, 2022
marcboudreau pushed a commit to marcboudreau/terraform-provider-vault that referenced this pull request Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for vault_quota_rate_limit interval and block_interval
3 participants