-
Notifications
You must be signed in to change notification settings - Fork 604
feat: v2/keys.getKey #3421
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
feat: v2/keys.getKey #3421
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c94772e
inital changes for getKey
Flo4604 0ad5828
update db and add auto_apply
Flo4604 f04a6d6
do some openapi spec merging
Flo4604 2e626fe
Merge branch 'main' into feat/v2/keys.getKey
Flo4604 2df229f
actually find key
Flo4604 d0066aa
cleanup openapi spec some more and add bulk insert method
Flo4604 7a181f4
add identity ratelimits to list keys and show correct interval
Flo4604 d0a1e42
fix tests
Flo4604 ea54f8d
allow createKey to encrypt key
Flo4604 41a2710
Merge branch 'main' into feat/v2/keys.getKey
Flo4604 7a789c5
cleanup auditlog to use TX wrapper and add some bulk inserts
Flo4604 d0c2517
add 412 tests
Flo4604 42c0b9b
Update openapi spec for credit req/res and createKey for allowing to …
Flo4604 286d77f
make tests pass
Flo4604 1670c1f
make tests pass 2
Flo4604 ffdc205
Merge branch 'main' into feat/v2/keys.getKey
Flo4604 a23c2f8
fix rabbit comments
Flo4604 7f9a473
fix comment and unlimited credits
Flo4604 375f412
fix flaky test?
Flo4604 c49d0ef
adjust for comments
Flo4604 4139c77
adjust for comments
Flo4604 498e044
adjust for comments
Flo4604 9a072c0
make linter happy
Flo4604 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,31 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/unkeyed/unkey/go/apps/api/openapi" | ||
| "github.com/unkeyed/unkey/go/pkg/otel/logging" | ||
| "github.com/unkeyed/unkey/go/pkg/zen" | ||
| ) | ||
|
|
||
| // Handler implements zen.Route interface for the API reference endpoint | ||
| type Handler struct { | ||
| // Services as public fields (even though not used in this handler, showing the pattern) | ||
| Logger logging.Logger | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Method returns the HTTP method this route responds to | ||
| func (h *Handler) Method() string { | ||
| return "GET" | ||
| } | ||
|
|
||
| // Path returns the URL path pattern this route matches | ||
| func (h *Handler) Path() string { | ||
| return "/openapi.yaml" | ||
| } | ||
|
|
||
| // Handle processes the HTTP request | ||
| func (h *Handler) Handle(ctx context.Context, s *zen.Session) error { | ||
| s.AddHeader("Content-Type", "text/html") | ||
| return s.Send(200, openapi.Spec) | ||
| } | ||
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
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
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
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
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
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
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
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,83 @@ | ||
| package handler_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/unkeyed/unkey/go/apps/api/openapi" | ||
| handler "github.com/unkeyed/unkey/go/apps/api/routes/v2_apis_list_keys" | ||
| "github.com/unkeyed/unkey/go/pkg/db" | ||
| "github.com/unkeyed/unkey/go/pkg/ptr" | ||
| "github.com/unkeyed/unkey/go/pkg/testutil" | ||
| "github.com/unkeyed/unkey/go/pkg/uid" | ||
| ) | ||
|
|
||
| func TestPreconditionError(t *testing.T) { | ||
| ctx := context.Background() | ||
| h := testutil.NewHarness(t) | ||
|
|
||
| route := &handler.Handler{ | ||
| Logger: h.Logger, | ||
| DB: h.DB, | ||
| Keys: h.Keys, | ||
| Permissions: h.Permissions, | ||
| Vault: h.Vault, | ||
| } | ||
|
|
||
| h.Register(route) | ||
|
|
||
| // Create API manually | ||
| keyAuthID := uid.New(uid.KeyAuthPrefix) | ||
| err := db.Query.InsertKeyring(ctx, h.DB.RW(), db.InsertKeyringParams{ | ||
| ID: keyAuthID, | ||
| WorkspaceID: h.Resources().UserWorkspace.ID, | ||
| CreatedAtM: time.Now().UnixMilli(), | ||
| DefaultPrefix: sql.NullString{Valid: false, String: ""}, | ||
| DefaultBytes: sql.NullInt32{Valid: false, Int32: 0}, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| apiID := uid.New(uid.APIPrefix) | ||
| err = db.Query.InsertApi(ctx, h.DB.RW(), db.InsertApiParams{ | ||
| ID: apiID, | ||
| Name: "test-api", | ||
| WorkspaceID: h.Resources().UserWorkspace.ID, | ||
| AuthType: db.NullApisAuthType{Valid: true, ApisAuthType: db.ApisAuthTypeKey}, | ||
| KeyAuthID: sql.NullString{Valid: true, String: keyAuthID}, | ||
| CreatedAtM: time.Now().UnixMilli(), | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create a root key with appropriate permissions | ||
| rootKey := h.CreateRootKey(h.Resources().UserWorkspace.ID, "api.*.read_key", "api.*.read_api", "api.*.decrypt_key") | ||
|
|
||
| // Set up request headers | ||
| headers := http.Header{ | ||
| "Content-Type": {"application/json"}, | ||
| "Authorization": {fmt.Sprintf("Bearer %s", rootKey)}, | ||
| } | ||
|
|
||
| // Test case for API ID with special characters | ||
| t.Run("Try decrypting key without opt-in", func(t *testing.T) { | ||
| req := handler.Request{ | ||
| ApiId: apiID, | ||
| Decrypt: ptr.P(true), | ||
| } | ||
|
|
||
| res := testutil.CallRoute[handler.Request, openapi.PreconditionFailedErrorResponse]( | ||
| h, | ||
| route, | ||
| headers, | ||
| req, | ||
| ) | ||
|
|
||
| require.Equal(t, 412, res.Status) | ||
| require.NotNil(t, res.Body) | ||
| require.NotNil(t, res.Body.Error) | ||
| }) | ||
| } |
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.