- Notifications
You must be signed in to change notification settings - Fork927
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
478001a
feat: add session token to provisioner
sreya49fb3b5
add test
sreya8b52ca5
fix perms
sreyab49d18b
fix panic
sreya3ff4539
move api key generation to its own package
sreya01f7a5e
lint
sreya905cd1a
add apikey pkg test
sreya6ad169d
update provisionerd test
sreyabc6dec0
fix apikey test
sreya1d35967
pr comments
sreya4183989
rename proto field
sreya0bba543
stuff
sreyaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
118 changes: 19 additions & 99 deletionscoderd/apikey.go
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
110 changes: 110 additions & 0 deletionscoderd/apikey/apikey.go
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,110 @@ | ||
package apikey | ||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"net" | ||
"time" | ||
"github.com/google/uuid" | ||
"github.com/tabbed/pqtype" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/cryptorand" | ||
) | ||
type CreateParams struct { | ||
UserID uuid.UUID | ||
LoginType database.LoginType | ||
DeploymentValues *codersdk.DeploymentValues | ||
// Optional. | ||
ExpiresAt time.Time | ||
LifetimeSeconds int64 | ||
Scope database.APIKeyScope | ||
TokenName string | ||
RemoteAddr string | ||
} | ||
// Generate generates an API key, returning the key as a string as well as the | ||
// database representation. It is the responsibility of the caller to insert it | ||
// into the database. | ||
func Generate(params CreateParams) (database.InsertAPIKeyParams, string, error) { | ||
keyID, keySecret, err := generateKey() | ||
if err != nil { | ||
return database.InsertAPIKeyParams{}, "", xerrors.Errorf("generate API key: %w", err) | ||
} | ||
hashed := sha256.Sum256([]byte(keySecret)) | ||
// Default expires at to now+lifetime, or use the configured value if not | ||
// set. | ||
if params.ExpiresAt.IsZero() { | ||
if params.LifetimeSeconds != 0 { | ||
params.ExpiresAt = database.Now().Add(time.Duration(params.LifetimeSeconds) * time.Second) | ||
} else { | ||
params.ExpiresAt = database.Now().Add(params.DeploymentValues.SessionDuration.Value()) | ||
params.LifetimeSeconds = int64(params.DeploymentValues.SessionDuration.Value().Seconds()) | ||
} | ||
} | ||
if params.LifetimeSeconds == 0 { | ||
params.LifetimeSeconds = int64(time.Until(params.ExpiresAt).Seconds()) | ||
} | ||
ip := net.ParseIP(params.RemoteAddr) | ||
if ip == nil { | ||
ip = net.IPv4(0, 0, 0, 0) | ||
} | ||
bitlen := len(ip) * 8 | ||
scope := database.APIKeyScopeAll | ||
if params.Scope != "" { | ||
scope = params.Scope | ||
} | ||
switch scope { | ||
case database.APIKeyScopeAll, database.APIKeyScopeApplicationConnect: | ||
default: | ||
return database.InsertAPIKeyParams{}, "", xerrors.Errorf("invalid API key scope: %q", scope) | ||
} | ||
token := fmt.Sprintf("%s-%s", keyID, keySecret) | ||
return database.InsertAPIKeyParams{ | ||
ID: keyID, | ||
UserID: params.UserID, | ||
LifetimeSeconds: params.LifetimeSeconds, | ||
IPAddress: pqtype.Inet{ | ||
IPNet: net.IPNet{ | ||
IP: ip, | ||
Mask: net.CIDRMask(bitlen, bitlen), | ||
}, | ||
Valid: true, | ||
}, | ||
// Make sure in UTC time for common time zone | ||
ExpiresAt: params.ExpiresAt.UTC(), | ||
CreatedAt: database.Now(), | ||
UpdatedAt: database.Now(), | ||
HashedSecret: hashed[:], | ||
LoginType: params.LoginType, | ||
Scope: scope, | ||
TokenName: params.TokenName, | ||
}, token, nil | ||
} | ||
// generateKey a new ID and secret for an API key. | ||
func generateKey() (id string, secret string, err error) { | ||
// Length of an API Key ID. | ||
id, err = cryptorand.String(10) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
// Length of an API Key secret. | ||
secret, err = cryptorand.String(22) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return id, secret, nil | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.