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 Optional Expiry to Auth Tokens #844

Merged
merged 16 commits into from
Jun 5, 2023

Conversation

juliannatetreault
Copy link
Contributor

@juliannatetreault juliannatetreault commented Apr 10, 2023

Description

Allow an optional expired_at to be set on authentication API tokens via the provider.

Testing plan

  • Ensure that organization and team API tokens that omit the optional expired_at field still work as expected.
  • Ensure that an expired_at can be set on organization and team API tokens and that once set, the expiration date displays in the UI (/app/org-name/authentication-tokens) with the expiration date that was provided.
  • Ensure that an invalid expired_at cannot be set on organization and team API tokens and that if attempted to be set, an error is returned.

External links

Output from acceptance tests

resource_tfe_organization_token_test.go results:

=== RUN   TestAccTFEOrganizationToken_basic
--- PASS: TestAccTFEOrganizationToken_basic (8.99s)
PASS

Process finished with the exit code 0
...
=== RUN   TestAccTFEOrganizationToken_existsWithoutExpiry
--- PASS: TestAccTFEOrganizationToken_existsWithoutExpiry (18.02s)
PASS

Process finished with the exit code 0
...

=== RUN   TestAccTFEOrganizationToken_existsWithExpiry
--- PASS: TestAccTFEOrganizationToken_existsWithExpiry (9.48s)
PASS

Process finished with the exit code 0

resource_tfe_team_token_test.go results:

=== RUN   TestAccTFETeamToken_basic
--- PASS: TestAccTFETeamToken_basic (10.22s)
PASS

Process finished with the exit code 0
...
=== RUN   TestAccTFETeamToken_existsWithoutExpiry
--- PASS: TestAccTFETeamToken_existsWithoutExpiry (10.95s)
PASS

Process finished with the exit code 0
...
=== RUN   TestAccTFETeamToken_existsWithExpiry
--- PASS: TestAccTFETeamToken_existsWithExpiry (10.51s)
PASS

Process finished with the exit code 0

tfe/resource_tfe_organization_token.go Outdated Show resolved Hide resolved
tfe/resource_tfe_organization_token.go Outdated Show resolved Hide resolved
website/docs/r/organization_token.html.markdown Outdated Show resolved Hide resolved
tfe/resource_tfe_organization_token_test.go Outdated Show resolved Hide resolved
tfe/resource_tfe_organization_token_test.go Show resolved Hide resolved
@juliannatetreault juliannatetreault force-pushed the juliannatetreault/auth-token-ttls branch 2 times, most recently from b1ae8dc to edcd608 Compare April 21, 2023 14:09
CHANGELOG.md Show resolved Hide resolved
tfe/resource_tfe_organization_token_test.go Outdated Show resolved Hide resolved
@JarrettSpiker
Copy link
Contributor

JarrettSpiker commented Apr 21, 2023

If you would like the make the compile errors go away, and allow tests to run or allow other developers to clone + build and test this, you can run + commit

go get github.com/hashicorp/go-tfe@10f40dcbde314dbc81c1c4e35a0aa89823ca0ff0

This will tell go to use your draft version of go-tfe from that commit.

It will need to be updated to an actual released go-tfe version before this can be merged....but it will need to be updated regardless when the actual version of go-tfe is released, so 🤷 no extra work needed by pointing to an unreleased version of go-tfe in the mean time

@juliannatetreault juliannatetreault force-pushed the juliannatetreault/auth-token-ttls branch 2 times, most recently from 59008e1 to 42b96e5 Compare May 11, 2023 20:17
@juliannatetreault juliannatetreault force-pushed the juliannatetreault/auth-token-ttls branch from 42b96e5 to a88c677 Compare May 23, 2023 16:09
@juliannatetreault juliannatetreault marked this pull request as ready for review May 23, 2023 16:19
@juliannatetreault juliannatetreault requested a review from a team as a code owner May 23, 2023 16:19
Copy link
Contributor

@JarrettSpiker JarrettSpiker left a comment

Choose a reason for hiding this comment

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

One easy fix in the docs, and a few suggestions on cleaning up the code a bit.

If the requested tests are a bigger lift than I am thinking, let me know!

website/docs/r/team_token.html.markdown Show resolved Hide resolved
tfe/resource_tfe_organization_token.go Outdated Show resolved Hide resolved
tfe/resource_tfe_organization_token.go Show resolved Hide resolved
tfe/resource_tfe_organization_token_test.go Show resolved Hide resolved
tfe/resource_tfe_organization_token_test.go Show resolved Hide resolved
@juliannatetreault juliannatetreault force-pushed the juliannatetreault/auth-token-ttls branch from a88c677 to 52c7db1 Compare May 24, 2023 18:43
Copy link
Contributor

@JarrettSpiker JarrettSpiker left a comment

Choose a reason for hiding this comment

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

looking good to me, just one more small error in the docs

website/docs/r/team_token.html.markdown Show resolved Hide resolved
Copy link
Contributor

@JarrettSpiker JarrettSpiker left a comment

Choose a reason for hiding this comment

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

This LGTM!

Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

I wonder if we should suggest such a dramatically long expiration period in the docs (I think it's 28 years) ... and more generally I wonder if there is more guidance we can give for generating this format in particular.. Example: You can generate this string using the date tool in unix systems.. unfortunately this tool has different flags in linux and darwin and other systems:

darwin:
date -Iseconds -v"+90d"

linux:
date -Iseconds -d"+90 days"

Or you can use a terraform function that doesn't make it easy to work with long durations:

$ terraform console
> timeadd(timestamp(), "2160h")
"2023-08-28T23:04:47Z"

To open up a slightly broader discussion... I don't believe that this format is perfectly suitable for this use case because it must be static to be useful and doesn't lend itself to human readability. What I mean by static is that it would not make sense to invoke a terraform function like timestamp() to define this value because it would force a re-create on the resource every time it was evaluated. Nor is there an easy set of terraform functions to format a more human readable date... We expect users to statically define an expiration date in iso8601 format, which is hard to read and hard to write. So what if we chose a more human readable format that was also easy to produce? Like RFC 5322 (email format)

$ date -v"+90d" -R
Mon, 28 Aug 2023 17:10:48 -0600

This can be reliably parsed with go, looks nice in config, and we could even document the terraform format string for the expected format (it's EEE, DD MMM YYYY hh:mm:ss ZZZZ)

$ terraform console
> formatdate("EEE, DD MMM YYYY hh:mm:ss ZZZZ", timeadd(timestamp(), "2160h"))

Tell me what you think.
/cc @JarrettSpiker

@JarrettSpiker
Copy link
Contributor

@brandonc: @juliannatetreault and I discussed this, and definitely agree that we can improve the documentation with some more realistic examples, and provide the user with some commands they can use to generate an appropriate timestamp.

To open up a slightly broader discussion... I don't believe that this format is perfectly suitable for this use case because it must be static to be useful and doesn't lend itself to human readability.

I don't know that I totally agree here though. Maybe it is personal preference but I don't find 2023-08-28T23:04:47Z harder to read than Mon, 28 Aug 2023 17:10:48 -0600... in both the date is apparent, and I am going to need to plug the time into some sort of time zone conversion tool if it is relevant.

And I think I would actually prefer to write the 2023-08-28T23:04:47Z format, since I find it intuitively less ambiguous (e.g. no needing to guess what the correct format for the month name is).

I would be open to changing the format if there are strong feelings about it. The current format being alright could be just personal preference though...and with all things being equal I lean towards using the format that the API will be using.

On a related note, just googling around I found the time provider...do you think that that would be something worth recommending as a way to generate this timestamp? Something like:

resource "time_static" "example" {}

resource "tfe_organization_token" "token" {
    organization = data.tfe_organization.org.name
    expired_at = timeadd(time_static.example.rfc3339, "2160h")
}

@brandonc
Copy link
Collaborator

brandonc commented Jun 1, 2023

@JarrettSpiker @juliannatetreault I am super into recommending the time provider in the documentation! I wouldn't mind keeping iso8601 if it makes the examples this simple! Plus, it has the added benefit of being the same standard format that timestamp(), etc. uses.

resource "time_rotating" "example" {
  rotation_days = 90
}

resource "tfe_organization_token" "token" {
    organization = data.tfe_organization.org.name
    expired_at = time_rotating.example.id
}

@juliannatetreault juliannatetreault force-pushed the juliannatetreault/auth-token-ttls branch from ddb7c52 to d7202b9 Compare June 1, 2023 16:37
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

👇

website/docs/r/organization_token.html.markdown Outdated Show resolved Hide resolved
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

I made a mistake!

website/docs/r/organization_token.html.markdown Outdated Show resolved Hide resolved
website/docs/r/organization_token.html.markdown Outdated Show resolved Hide resolved
website/docs/r/team_token.html.markdown Outdated Show resolved Hide resolved
website/docs/r/team_token.html.markdown Outdated Show resolved Hide resolved
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

Very close 🎉

website/docs/r/organization_token.html.markdown Outdated Show resolved Hide resolved
website/docs/r/organization_token.html.markdown Outdated Show resolved Hide resolved
Copy link
Collaborator

@brandonc brandonc left a comment

Choose a reason for hiding this comment

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

WTG

@juliannatetreault juliannatetreault merged commit a83a3c5 into main Jun 5, 2023
8 checks passed
@juliannatetreault juliannatetreault deleted the juliannatetreault/auth-token-ttls branch June 5, 2023 17:42
github-merge-queue bot pushed a commit to panda-den/colorful-pandas that referenced this pull request Jul 4, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [tfe](https://registry.terraform.io/providers/hashicorp/tfe)
([source](https://togithub.com/hashicorp/terraform-provider-tfe)) |
required_provider | minor | `0.45.0` -> `0.46.0` |

---

### ⚠ Dependency Lookup Warnings ⚠

Warnings were logged while processing this repo. Please check the
Dependency Dashboard for more information.

---

### Release Notes

<details>
<summary>hashicorp/terraform-provider-tfe (tfe)</summary>

###
[`v0.46.0`](https://togithub.com/hashicorp/terraform-provider-tfe/blob/HEAD/CHANGELOG.md#v0460-July-3-2023)

[Compare
Source](https://togithub.com/hashicorp/terraform-provider-tfe/compare/v0.45.0...v0.46.0)

FEATURES:

- **New Resource**: `r/tfe_agent_pool_allowed_workspaces` restricts the
use of an agent pool to particular workspaces, by
[@&#8203;hs26gill](https://togithub.com/hs26gill)
[870](https://togithub.com/hashicorp/terraform-provider-tfe/pull/870)
- `r/tfe_organization_token`: Add optional `expired_at` field to
organization tokens, by
[@&#8203;juliannatetreault](https://togithub.com/juliannatetreault)
(#&#8203[hashicorp/terraform-provider-tfe#844))
- `r/tfe_team_token`: Add optional `expired_at` field to team tokens, by
[@&#8203;juliannatetreault](https://togithub.com/juliannatetreault)
(#&#8203[hashicorp/terraform-provider-tfe#844))
- `r/tfe_agent_pool`: Add attribute `organization_scoped` to set the
scope of an agent pool, by
[@&#8203;hs26gill](https://togithub.com/hs26gill)
[870](https://togithub.com/hashicorp/terraform-provider-tfe/pull/870)
- `d/tfe_agent_pool`: Add attribute `organization_scoped` and
`allowed_workspace_ids` to retrieve agent pool scope and associated
allowed workspace ids, by
[@&#8203;hs26gill](https://togithub.com/hs26gill)
[870](https://togithub.com/hashicorp/terraform-provider-tfe/pull/870)

BUG FIXES:

- `r/tfe_workspace_run`: Ensure `wait_for_run` correctly results in a
fire-and-forget run when set to `false`, by
[@&#8203;lucymhdavies](https://togithub.com/lucymhdavies)
(#&#8203[hashicorp/terraform-provider-tfe#910))
- `r/tfe_workspace_run`: Fix rare random run failures; adjust lists of
expected run statuses to ensure that a plan is completely processed
before attempting to apply it, by
[@&#8203;uk1288](https://togithub.com/uk1288)
(#&#8203[hashicorp/terraform-provider-tfe#921))
- `r/tfe_notification_configuration`: Add support for missing "Check
failed" Health Event notifications, by
[@&#8203;lucymhdavies](https://togithub.com/lucymhdavies)
(#&#8203[hashicorp/terraform-provider-tfe#927))
- `r/tfe_registry_module`: Fix a bug that prevented users from being
able to create a registry module using a github app, by
[@&#8203;dsa0x](https://togithub.com/dsa0x)
(#&#8203[hashicorp/terraform-provider-tfe#935))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNTkuNCIsInVwZGF0ZWRJblZlciI6IjM1LjE1OS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: astronaut-panda[bot] <137164246+astronaut-panda[bot]@users.noreply.github.com>
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.

None yet

3 participants