Skip to content

chore(cli)!: change default max-token-duration to unlimited #6467

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

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,11 @@ func newConfig() *codersdk.DeploymentConfig {
Default: flag.Lookup("test.v") == nil && !buildinfo.IsDev(),
},
MaxTokenLifetime: &codersdk.DeploymentConfigField[time.Duration]{
Name: "Max Token Lifetime",
Usage: "The maximum lifetime duration users can specify when creating an API token.",
Flag: "max-token-lifetime",
Default: 24 * 30 * time.Hour,
Name: "Max Token Lifetime",
Usage: "The maximum lifetime duration users can specify when creating an API token.",
Flag: "max-token-lifetime",
// max time.Duration is 290 years
Default: 290 * 365 * 24 * time.Hour,
Copy link
Member Author

@Kira-Pilot Kira-Pilot Mar 6, 2023

Choose a reason for hiding this comment

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

According to the docs, the max time.Duration one can set is 290 years. If there's a cleaner way to represent this max, let me know.

Copy link
Member

Choose a reason for hiding this comment

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

A cleaner way to do this is time.Duration(math.MaxInt64)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I'll follow up in my next PR!

Copy link
Member

Choose a reason for hiding this comment

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

},
Swagger: &codersdk.SwaggerConfig{
Enable: &codersdk.DeploymentConfigField[bool]{
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Flags:
can specify when creating an API
token.
Consumes $CODER_MAX_TOKEN_LIFETIME
(default 720h0m0s)
(default 2540400h0m0s)
--oauth2-github-allow-everyone Allow all logins, setting this
option means allowed orgs and teams
must be empty.
Expand Down
45 changes: 43 additions & 2 deletions coderd/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestTokenScoped(t *testing.T) {
require.Equal(t, keys[0].Scope, codersdk.APIKeyScopeApplicationConnect)
}

func TestTokenDuration(t *testing.T) {
func TestUserSetTokenDuration(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
Expand All @@ -89,7 +89,23 @@ func TestTokenDuration(t *testing.T) {
require.Less(t, keys[0].ExpiresAt, time.Now().Add(time.Hour*8*24))
}

func TestTokenMaxLifetime(t *testing.T) {
func TestDefaultTokenDuration(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
client := coderdtest.New(t, nil)
_ = coderdtest.CreateFirstUser(t, client)

_, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{})
require.NoError(t, err)
keys, err := client.Tokens(ctx, codersdk.Me, codersdk.TokensFilter{})
require.NoError(t, err)
require.Greater(t, keys[0].ExpiresAt, time.Now().Add(time.Hour*29*24))
require.Less(t, keys[0].ExpiresAt, time.Now().Add(time.Hour*31*24))
}

func TestTokenUserSetMaxLifetime(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
Expand All @@ -114,6 +130,31 @@ func TestTokenMaxLifetime(t *testing.T) {
require.ErrorContains(t, err, "lifetime must be less")
}

func TestTokenDefaultMaxLifetime(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
dc := coderdtest.DeploymentConfig(t)
client := coderdtest.New(t, &coderdtest.Options{
DeploymentConfig: dc,
})
_ = coderdtest.CreateFirstUser(t, client)

// success
_, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{
Lifetime: time.Hour * 24 * 365,
})
require.NoError(t, err)

// fail - default --max-token-lifetime is the maximum value of time.Duration
// which is 24 * 365 * 290.
_, err = client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{
Lifetime: time.Hour * 24 * 366 * 290,
})
require.ErrorContains(t, err, "lifetime must be less")
}

func TestSessionExpiry(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion docs/cli/coder_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ The maximum lifetime duration users can specify when creating an API token.
| | |
| --- | --- |
| Consumes | <code>$CODER_MAX_TOKEN_LIFETIME</code> |
| Default | <code>720h0m0s</code> |
| Default | <code>2540400h0m0s</code> |

### --oauth2-github-allow-everyone

Expand Down
4 changes: 3 additions & 1 deletion docs/templates/change-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ curl -L https://coder.com/install.sh | sh
# curl -L https://coder.com/install.sh | sh -s -- --version=0.x

# To create API tokens, use `coder tokens create`.
# If no `--lifetime` flag is passed during creation, the default token lifetime
# will be 30 days.
# These variables are consumed by Coder
export CODER_URL=https://coder.example.com
export CODER_SESSION_TOKEN=*****
Expand All @@ -26,4 +28,4 @@ coder templates push --yes $CODER_TEMPLATE_NAME \
> Looking for an example? See how we push our development image
> and template [via GitHub actions](https://github.com/coder/coder/blob/main/.github/workflows/dogfood.yaml).

> To create tokens with over a 30 day lifetime, [configure Coder server to set a longer max token lifetime](../cli/coder_server#--max-token-lifetime)
> To cap token lifetime on creation, [configure Coder server to set a shorter max token lifetime](../cli/coder_server#--max-token-lifetime)