Repository navigation
feat: add AI Gateway coderd key CRUD endpoints #25565
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af0cc8d
feat: add AI Gateway coderd key CRUD endpoints
pawbana ef7d4e7
agentic review fixes
pawbana 1b73464
agentic review 2
pawbana f4042fe
ai_gateway_coderd_key -> ai_gateway_keys table rename
pawbana 8840cef
agentic review 3 + make gen diff
pawbana 159e425
TestEnterpriseEndpointsDocumented fix
pawbana 2f6ac7f
fix flaky TestAIGatewayKeysTableConstraints
pawbana 4050bc2
human edit mostly nits
pawbana 9f39eed
human review 1
pawbana 5169922
human review 2
pawbana 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
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,43 @@ | ||
| package keys | ||
|
|
||
| import ( | ||
| "github.com/google/uuid" | ||
| "golang.org/x/xerrors" | ||
|
|
||
| "github.com/coder/coder/v2/coderd/apikey" | ||
| "github.com/coder/coder/v2/coderd/database" | ||
| ) | ||
|
|
||
| const ( | ||
| privateSuffixLength = 32 | ||
|
|
||
| // KeyPrefixLength is the total length of the visible key prefix. | ||
| KeyPrefixLength = 11 | ||
|
|
||
| // KeyLength is the total length of the plaintext key returned to | ||
| // the user on Create. | ||
| KeyLength = KeyPrefixLength + privateSuffixLength | ||
| ) | ||
|
|
||
| // New generates an AI Gateway key used for authenticating standalone replicas. | ||
| // Returns InsertParams ready for the database query. | ||
| func New(name string) (database.InsertAIGatewayKeyParams, string, error) { | ||
| secret, hashed, err := apikey.GenerateSecret(KeyLength) | ||
| if err != nil { | ||
| return database.InsertAIGatewayKeyParams{}, "", xerrors.Errorf("generate secret: %w", err) | ||
| } | ||
| if len(secret) != KeyLength { | ||
| return database.InsertAIGatewayKeyParams{}, "", xerrors.Errorf("generated secret has unexpected length: got %d, want %d", len(secret), KeyLength) | ||
| } | ||
| if KeyLength < KeyPrefixLength { | ||
| return database.InsertAIGatewayKeyParams{}, "", xerrors.Errorf("KeyLength (%d) must be >= KeyPrefixLength (%d)", KeyLength, KeyPrefixLength) | ||
| } | ||
| visiblePrefix := secret[:KeyPrefixLength] | ||
|
|
||
| return database.InsertAIGatewayKeyParams{ | ||
| ID: uuid.New(), | ||
| Name: name, | ||
| SecretPrefix: visiblePrefix, | ||
|
dannykopping marked this conversation as resolved.
Resolved
|
||
| HashedSecret: hashed, | ||
| }, secret, nil | ||
| } | ||
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,22 @@ | ||
| package keys_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/coder/coder/v2/coderd/aibridge/keys" | ||
| "github.com/coder/coder/v2/coderd/apikey" | ||
| ) | ||
|
|
||
| func TestNew(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| params, key, err := keys.New("test-key") | ||
| require.NoError(t, err) | ||
| require.Len(t, key, keys.KeyLength) | ||
| require.Len(t, params.SecretPrefix, keys.KeyPrefixLength) | ||
| require.Equal(t, key[:keys.KeyPrefixLength], params.SecretPrefix) | ||
| require.True(t, apikey.ValidateHash(params.HashedSecret, key)) | ||
| require.False(t, apikey.ValidateHash(params.HashedSecret, key[keys.KeyPrefixLength:])) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.