- Notifications
You must be signed in to change notification settings - Fork906
feat: add separate max token lifetime for administrators#18267
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -144,6 +144,88 @@ func TestTokenUserSetMaxLifetime(t *testing.T) { | ||
require.ErrorContains(t, err, "lifetime must be less") | ||
} | ||
func TestTokenAdminSetMaxLifetime(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
dc := coderdtest.DeploymentValues(t) | ||
dc.Sessions.MaximumTokenDuration = serpent.Duration(time.Hour * 24 * 7) | ||
dc.Sessions.MaximumAdminTokenDuration = serpent.Duration(time.Hour * 24 * 14) | ||
client := coderdtest.New(t, &coderdtest.Options{ | ||
DeploymentValues: dc, | ||
}) | ||
adminUser := coderdtest.CreateFirstUser(t, client) | ||
nonAdminClient, _ := coderdtest.CreateAnotherUser(t, client, adminUser.OrganizationID) | ||
// Admin should be able to create a token with a lifetime longer than the non-admin max. | ||
_, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 10, | ||
}) | ||
require.NoError(t, err) | ||
// Admin should NOT be able to create a token with a lifetime longer than the admin max. | ||
_, err = client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 15, | ||
}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "lifetime must be less") | ||
// Non-admin should NOT be able to create a token with a lifetime longer than the non-admin max. | ||
_, err = nonAdminClient.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 8, | ||
}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "lifetime must be less") | ||
// Non-admin should be able to create a token with a lifetime shorter than the non-admin max. | ||
_, err = nonAdminClient.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 6, | ||
}) | ||
require.NoError(t, err) | ||
} | ||
func TestTokenAdminSetMaxLifetimeShorter(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
dc := coderdtest.DeploymentValues(t) | ||
dc.Sessions.MaximumTokenDuration = serpent.Duration(time.Hour * 24 * 14) | ||
dc.Sessions.MaximumAdminTokenDuration = serpent.Duration(time.Hour * 24 * 7) | ||
client := coderdtest.New(t, &coderdtest.Options{ | ||
DeploymentValues: dc, | ||
}) | ||
adminUser := coderdtest.CreateFirstUser(t, client) | ||
nonAdminClient, _ := coderdtest.CreateAnotherUser(t, client, adminUser.OrganizationID) | ||
// Admin should NOT be able to create a token with a lifetime longer than the admin max. | ||
_, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 8, | ||
}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "lifetime must be less") | ||
ThomasK33 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// Admin should be able to create a token with a lifetime shorter than the admin max. | ||
_, err = client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 6, | ||
}) | ||
require.NoError(t, err) | ||
// Non-admin should be able to create a token with a lifetime longer than the admin max. | ||
_, err = nonAdminClient.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 10, | ||
}) | ||
require.NoError(t, err) | ||
// Non-admin should NOT be able to create a token with a lifetime longer than the non-admin max. | ||
_, err = nonAdminClient.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{ | ||
Lifetime: time.Hour * 24 * 15, | ||
}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "lifetime must be less") | ||
} | ||
func TestTokenCustomDefaultLifetime(t *testing.T) { | ||
t.Parallel() | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -468,6 +468,8 @@ type SessionLifetime struct { | ||
DefaultTokenDuration serpent.Duration `json:"default_token_lifetime,omitempty" typescript:",notnull"` | ||
MaximumTokenDuration serpent.Duration `json:"max_token_lifetime,omitempty" typescript:",notnull"` | ||
MaximumAdminTokenDuration serpent.Duration `json:"max_admin_token_lifetime,omitempty" typescript:",notnull"` | ||
} | ||
type DERP struct { | ||
@@ -2340,6 +2342,17 @@ func (c *DeploymentValues) Options() serpent.OptionSet { | ||
YAML: "maxTokenLifetime", | ||
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"), | ||
}, | ||
{ | ||
Name: "Maximum Admin Token Lifetime", | ||
Description: "The maximum lifetime duration administrators can specify when creating an API token.", | ||
Flag: "max-admin-token-lifetime", | ||
Env: "CODER_MAX_ADMIN_TOKEN_LIFETIME", | ||
Default: (7 * 24 * time.Hour).String(), | ||
Value: &c.Sessions.MaximumAdminTokenDuration, | ||
Group: &deploymentGroupNetworkingHTTP, | ||
YAML: "maxAdminTokenLifetime", | ||
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"), | ||
}, | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
{ | ||
Name: "Default Token Lifetime", | ||
Description: "The default lifetime duration for API tokens. This value is used when creating a token without specifying a duration, such as when authenticating the CLI or an IDE plugin.", | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.