Skip to content

Peng-7929:TerraformSDK_apikey_expiry#374

Merged
soniafrancisNS1 merged 14 commits into
masterfrom
PENG-7929/terraform-apikey-expiry
Jun 11, 2026
Merged

Peng-7929:TerraformSDK_apikey_expiry#374
soniafrancisNS1 merged 14 commits into
masterfrom
PENG-7929/terraform-apikey-expiry

Conversation

@soniafrancisNS1

@soniafrancisNS1 soniafrancisNS1 commented May 26, 2026

Copy link
Copy Markdown
Contributor

Add support for API key expiry and automatic secret rotation in Terraform provider.

Changes

  • Add expiry_duration field (10d/30d/90d)
  • Add computed secrets field to view secret metadata
  • Update ns1-go SDK dependency to v2.18.0
  • Add comprehensive tests for creation, update , delete, renew
  • Update documentation with examples

Dependencies

  • Requires ns1-go SDK v2.18.0 (PENG-7928)

@hhellyer hhellyer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the fixed expiry durations probably need to be removed but otherwise this looks good.
I think this makes sense provided the api key is in use elsewhere in terraform config but not if it's used by scripts running outside terraform. (Which is not a problem with this PR.)
This doesn't expose the /renew functionality of the api endpoints which means the operations probably need to be carried out with a key with mange_api_keys permission. It might be worth us checking if there's way to expose that function that is meaningful inside terraform.

Comment thread ns1/resource_apikey.go Outdated
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(string)
if v != "" && v != "10d" && v != "30d" && v != "90d" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we are enforcing the limited set of durations via the REST api just the portal so we should probably not enforce it here. The durations will need to fix the patten of "<number>d" though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the validation to use a regex pattern ^\d+d$ instead of hardcoded 10d/30d/90d. Now accepts any duration format while still enforcing the <number>d pattern. Tests added for custom durations

),
},
{
Config: testAccAPIKeyWithExpiryDuration(name, "30d"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is this testing? I don't think we can change the expiry duration on an api key once it's created.
(Edit - I realised that since terraform will recreate the object this probably does work, I'm not sure it's much use because of the key value changing. I think this is probably fine as it's just the way terraform works, customers should probably avoid doing it but we already have this for normal keys above.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since Terraform recreates the resource when expiry_duration changes, technically the update works, but the API key/secrets will change as part of the recreation. I added a note in the docs to make that behaviour clearer

Comment thread ns1/examples/apikey.tf Outdated
resource "ns1_apikey" "rotating_key" {
name = "rotating-api-key"

# Enable automatic secret rotation every 30 days

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this comment right? I think we are enforcing secret expiry every 30 days not enabling it to happen automatically. (And I wonder if we should use "renew" or "rotate" but I think it's used interchangeably everywhere else.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated the wording in the examples/docs to “secret expiration” instead of “automatic rotation” since terraform itself isn’t performing the renew/rotate operation.

@hhellyer hhellyer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - This should be able to be merged and published now.

@soniafrancisNS1 soniafrancisNS1 merged commit e0ce7aa into master Jun 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants