-
Notifications
You must be signed in to change notification settings - Fork 899
feat: add session token injection to provisioner #7461
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
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
478001a
feat: add session token to provisioner
sreya 49fb3b5
add test
sreya 8b52ca5
fix perms
sreya b49d18b
fix panic
sreya 3ff4539
move api key generation to its own package
sreya 01f7a5e
lint
sreya 905cd1a
add apikey pkg test
sreya 6ad169d
update provisionerd test
sreya bc6dec0
fix apikey test
sreya 1d35967
pr comments
sreya 4183989
rename proto field
sreya 0bba543
stuff
sreya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add apikey pkg test
- Loading branch information
commit 905cd1afff7fac01161353c47b16cb057ca96770
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package apikey_test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clibase" | ||
"github.com/coder/coder/coderd/apikey" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func TestGenerate(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testcase struct { | ||
name string | ||
params apikey.CreateParams | ||
fail bool | ||
} | ||
|
||
cases := []testcase{ | ||
{ | ||
name: "OK", | ||
params: apikey.CreateParams{ | ||
UserID: uuid.New(), | ||
LoginType: database.LoginTypeOIDC, | ||
DeploymentValues: &codersdk.DeploymentValues{}, | ||
ExpiresAt: time.Now().Add(time.Hour), | ||
LifetimeSeconds: int64(time.Hour.Seconds()), | ||
TokenName: "hello", | ||
RemoteAddr: "1.2.3.4", | ||
Scope: database.APIKeyScopeApplicationConnect, | ||
}, | ||
}, | ||
{ | ||
name: "InvalidScope", | ||
params: apikey.CreateParams{ | ||
UserID: uuid.New(), | ||
LoginType: database.LoginTypeOIDC, | ||
DeploymentValues: &codersdk.DeploymentValues{}, | ||
ExpiresAt: time.Now().Add(time.Hour), | ||
LifetimeSeconds: int64(time.Hour.Seconds()), | ||
TokenName: "hello", | ||
RemoteAddr: "1.2.3.4", | ||
Scope: database.APIKeyScope("test"), | ||
}, | ||
fail: true, | ||
}, | ||
{ | ||
name: "DeploymentSessionDuration", | ||
params: apikey.CreateParams{ | ||
UserID: uuid.New(), | ||
LoginType: database.LoginTypeOIDC, | ||
DeploymentValues: &codersdk.DeploymentValues{ | ||
SessionDuration: clibase.Duration(time.Hour), | ||
}, | ||
LifetimeSeconds: 0, | ||
ExpiresAt: time.Time{}, | ||
TokenName: "hello", | ||
RemoteAddr: "1.2.3.4", | ||
Scope: database.APIKeyScopeApplicationConnect, | ||
}, | ||
}, | ||
{ | ||
name: "DefaultIP", | ||
params: apikey.CreateParams{ | ||
UserID: uuid.New(), | ||
LoginType: database.LoginTypeOIDC, | ||
DeploymentValues: &codersdk.DeploymentValues{}, | ||
ExpiresAt: time.Now().Add(time.Hour), | ||
LifetimeSeconds: int64(time.Hour.Seconds()), | ||
TokenName: "hello", | ||
RemoteAddr: "", | ||
Scope: database.APIKeyScopeApplicationConnect, | ||
}, | ||
}, | ||
{ | ||
name: "DefaultScope", | ||
params: apikey.CreateParams{ | ||
UserID: uuid.New(), | ||
LoginType: database.LoginTypeOIDC, | ||
DeploymentValues: &codersdk.DeploymentValues{}, | ||
ExpiresAt: time.Now().Add(time.Hour), | ||
LifetimeSeconds: int64(time.Hour.Seconds()), | ||
TokenName: "hello", | ||
RemoteAddr: "1.2.3.4", | ||
Scope: database.APIKeyScopeApplicationConnect, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
keystr, key, err := apikey.Generate(tc.params) | ||
if tc.fail { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.NotEmpty(t, keystr) | ||
require.NotEmpty(t, key.ID) | ||
require.NotEmpty(t, key.HashedSecret) | ||
|
||
// Assert the string secret is formatted correctly | ||
keytokens := strings.Split(keystr, "-") | ||
require.Len(t, keytokens, 2) | ||
require.Equal(t, key.ID, keytokens[0]) | ||
|
||
// Assert that the hashed secret is correct. | ||
hashed := sha256.Sum256([]byte(keytokens[1])) | ||
require.ElementsMatch(t, hashed, key.HashedSecret[:]) | ||
|
||
require.Equal(t, tc.params.UserID, key.UserID) | ||
require.WithinDuration(t, database.Now(), key.CreatedAt, time.Second*5) | ||
require.WithinDuration(t, database.Now(), key.UpdatedAt, time.Second*5) | ||
|
||
if tc.params.LifetimeSeconds > 0 { | ||
require.Equal(t, tc.params.LifetimeSeconds, key.LifetimeSeconds) | ||
} else if !tc.params.ExpiresAt.IsZero() { | ||
// Should not be a delta greater than 5 seconds. | ||
require.InDelta(t, time.Until(tc.params.ExpiresAt).Seconds(), key.LifetimeSeconds, 5) | ||
} else { | ||
require.Equal(t, int64(tc.params.DeploymentValues.SessionDuration.Value().Seconds()), key.LifetimeSeconds) | ||
} | ||
|
||
if !tc.params.ExpiresAt.IsZero() { | ||
require.Equal(t, tc.params.ExpiresAt.UTC(), key.ExpiresAt) | ||
} else if tc.params.LifetimeSeconds > 0 { | ||
require.WithinDuration(t, database.Now().Add(time.Duration(tc.params.LifetimeSeconds)), key.ExpiresAt, time.Second*5) | ||
} else { | ||
require.WithinDuration(t, database.Now().Add(tc.params.DeploymentValues.SessionDuration.Value()), key.ExpiresAt, time.Second*5) | ||
} | ||
|
||
if tc.params.RemoteAddr != "" { | ||
require.Equal(t, tc.params.RemoteAddr, key.IPAddress.IPNet.IP.String()) | ||
} else { | ||
require.Equal(t, "0.0.0.0", key.IPAddress.IPNet.IP.String()) | ||
} | ||
|
||
if tc.params.Scope != "" { | ||
require.Equal(t, tc.params.Scope, key.Scope) | ||
} else { | ||
require.Equal(t, database.APIKeyScopeAll, key.Scope) | ||
} | ||
|
||
if tc.params.TokenName != "" { | ||
require.Equal(t, tc.params.TokenName, key.TokenName) | ||
} | ||
if tc.params.LoginType != "" { | ||
require.Equal(t, tc.params.LoginType, key.LoginType) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.