From 5eb0657a305f1464d6a8c130235341579350f6ed Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Sun, 1 Oct 2023 18:17:33 +0200 Subject: [PATCH] feat: create some routes with rpc naming (#332) * feat: create some routes with rpc naming * fix(workflow.json): change HTTP method from POST to GET for "Get Key" step * fix(workflow.json): change "params" to "json" for consistency and clarity * chore(deploy-agent.yaml): add MySQL service for testing feat(deploy-agent.yaml): add DATABASE_DSN environment variable for testing * chore(workflow.json): update step name from "Verify Key" to "Verify key again after removal" fix(workflow.json): update expected value of jsonpath "$.valid" to false * chore(workflow.json): remove unnecessary step for verifying key again after removal * chore(workflow.json): add newline at the end of the file --- .github/workflows/deploy-agent.yaml | 13 +++ apps/agent/e2e/workflow.json | 61 ++++++++++++-- apps/agent/pkg/server/key_delete.go | 11 +-- apps/agent/pkg/server/server.go | 24 +++--- apps/agent/pkg/server/v1_apis_create_test.go | 4 +- apps/agent/pkg/server/v1_apis_remove_test.go | 2 +- .../{key_create.go => v1_key_create.go} | 2 +- ...y_create_test.go => v1_key_create_test.go} | 0 apps/agent/pkg/server/v1_key_find.go | 80 +++++++++++++++++++ .../{key_get_test.go => v1_key_find_test.go} | 10 ++- apps/agent/pkg/server/v1_key_remove.go | 63 +++++++++++++++ apps/agent/pkg/server/v1_key_remove_test.go | 57 +++++++++++++ .../{key_verify.go => v1_key_verify.go} | 21 +++-- ...y_verify_test.go => v1_key_verify_test.go} | 40 +++++----- .../pkg/server/v1_workspace_create_test.go | 2 +- 15 files changed, 325 insertions(+), 65 deletions(-) rename apps/agent/pkg/server/{key_create.go => v1_key_create.go} (98%) rename apps/agent/pkg/server/{key_create_test.go => v1_key_create_test.go} (100%) create mode 100644 apps/agent/pkg/server/v1_key_find.go rename apps/agent/pkg/server/{key_get_test.go => v1_key_find_test.go} (77%) create mode 100644 apps/agent/pkg/server/v1_key_remove.go create mode 100644 apps/agent/pkg/server/v1_key_remove_test.go rename apps/agent/pkg/server/{key_verify.go => v1_key_verify.go} (95%) rename apps/agent/pkg/server/{key_verify_test.go => v1_key_verify_test.go} (93%) diff --git a/.github/workflows/deploy-agent.yaml b/.github/workflows/deploy-agent.yaml index 79757581bc..9bec0f9b66 100644 --- a/.github/workflows/deploy-agent.yaml +++ b/.github/workflows/deploy-agent.yaml @@ -8,9 +8,20 @@ on: concurrency: deploy jobs: + test: name: Test Agent runs-on: ubuntu-latest + services: + mysql: + image: mysql:latest + env: + MYSQL_ALLOW_EMPTY_PASSWORD: yes + MYSQL_DATABASE: unkey + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + steps: - uses: actions/checkout@v3 @@ -28,6 +39,8 @@ jobs: - name: Test run: go test -p 4 -v -json -shuffle=on --race ./... | gotestfmt working-directory: apps/agent + env: + DATABASE_DSN: root@tcp(localhost:3306)/unkey build: name: Build Agent diff --git a/apps/agent/e2e/workflow.json b/apps/agent/e2e/workflow.json index f65397365e..962ae7efcc 100644 --- a/apps/agent/e2e/workflow.json +++ b/apps/agent/e2e/workflow.json @@ -28,7 +28,7 @@ { "name": "Create Key", "http": { - "url": "${{env.baseUrl}}/v1/keys", + "url": "${{env.baseUrl}}/v1/keys.createKey", "method": "POST", "headers": { "Content-Type": "application/json", @@ -38,10 +38,18 @@ "apiId": "${{env.apiId}}" }, "check": { - "status": "/^20/", + "status": 200, "jsonpath": { - "$.keyId": [{ "isString": true }], - "$.key": [{ "isString": true }] + "$.keyId": [ + { + "isString": true + } + ], + "$.key": [ + { + "isString": true + } + ] } }, "captures": { @@ -57,7 +65,7 @@ { "name": "Verify Key", "http": { - "url": "${{env.baseUrl}}/v1/keys/verify", + "url": "${{env.baseUrl}}/v1/keys.verifyKey", "method": "POST", "headers": { "Content-Type": "application/json" @@ -66,7 +74,7 @@ "key": "${{captures.key}}" }, "check": { - "status": "/^20/", + "status": 200, "jsonpath": { "$.valid": true } @@ -92,6 +100,47 @@ } } } + }, + { + "name": "Get Key", + "http": { + "url": "${{env.baseUrl}}/v1/keys.findKey", + "method": "GET", + "headers": { + "Content-Type": "application/json", + "Authorization": "Bearer ${{secrets.rootKey}}" + }, + "params": { + "keyId": "${{captures.keyId}}" + }, + "check": { + "status": 200, + "jsonpath": { + "$.id": [ + { + "isString": true + } + ] + } + } + } + }, + { + "name": "Remove Key", + "http": { + "url": "${{env.baseUrl}}/v1/keys.removeKey", + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Authorization": "Bearer ${{secrets.rootKey}}" + }, + "json": { + "keyId": "${{captures.keyId}}" + }, + "check": { + "status": 200 + } + } } ] } diff --git a/apps/agent/pkg/server/key_delete.go b/apps/agent/pkg/server/key_delete.go index 7a20655050..28f7e5822d 100644 --- a/apps/agent/pkg/server/key_delete.go +++ b/apps/agent/pkg/server/key_delete.go @@ -10,17 +10,10 @@ import ( "github.com/gofiber/fiber/v2" ) -type DeleteKeyRequest struct { - KeyId string `json:"keyId" validate:"required"` -} - -type DeleteKeyResponse struct { -} - func (s *Server) deleteKey(c *fiber.Ctx) error { ctx, span := s.tracer.Start(c.UserContext(), "server.deleteKey") defer span.End() - req := DeleteKeyRequest{} + req := RemoveKeyRequestV1{} err := c.ParamsParser(&req) if err != nil { return errors.NewHttpError(c, errors.BAD_REQUEST, err.Error()) @@ -59,5 +52,5 @@ func (s *Server) deleteKey(c *fiber.Ctx) error { }, }) - return c.JSON(DeleteKeyResponse{}) + return c.JSON(RemoveKeyResponseV1{}) } diff --git a/apps/agent/pkg/server/server.go b/apps/agent/pkg/server/server.go index f3d9b11879..35e96089e9 100644 --- a/apps/agent/pkg/server/server.go +++ b/apps/agent/pkg/server/server.go @@ -206,27 +206,27 @@ func New(config Config) *Server { s.app.Post("/v1/internal.removeRootKey", s.deleteRootKey) // workspaceService - s.app.Post("/v1/workspace.createWorkspace", s.v1CreateWorkspace) + s.app.Post("/v1/workspaces.createWorkspace", s.v1CreateWorkspace) // apiService - s.app.Post("/v1/api.createApi", s.v1CreateApi) - s.app.Post("/v1/api.removeApi", s.v1RemoveApi) - s.app.Get("/v1/api.findApi", s.getApi) - s.app.Get("/v1/api.listKeys", s.listKeys) + s.app.Post("/v1/apis.createApi", s.v1CreateApi) + s.app.Post("/v1/apis.removeApi", s.v1RemoveApi) + s.app.Get("/v1/apis.findApi", s.getApi) + s.app.Get("/v1/apis.listKeys", s.listKeys) // keyService - s.app.Post("/v1/key.createKey", s.createKey) - s.app.Post("/v1/key.verifyKey", s.verifyKey) - s.app.Post("/v1/key.removeKey", s.deleteKey) - s.app.Post("/v1/key.updateKey", s.updateKey) - s.app.Get("/v1/key.findKey", s.getKey) + s.app.Post("/v1/keys.createKey", s.v1CreateKey) + s.app.Post("/v1/keys.verifyKey", s.v1VerifyKey) + s.app.Post("/v1/keys.removeKey", s.v1RemoveKey) + s.app.Post("/v1/keys.updateKey", s.updateKey) + s.app.Get("/v1/keys.findKey", s.v1FindKey) // legacy - s.app.Post("/v1/keys", s.createKey) + s.app.Post("/v1/keys", s.v1CreateKey) s.app.Get("/v1/keys/:keyId", s.getKey) s.app.Put("/v1/keys/:keyId", s.updateKey) s.app.Delete("/v1/keys/:keyId", s.deleteKey) - s.app.Post("/v1/keys/verify", s.verifyKey) + s.app.Post("/v1/keys/verify", s.v1VerifyKey) s.app.Get("/v1/apis/:apiId", s.getApi) s.app.Get("/v1/apis/:apiId/keys", s.listKeys) diff --git a/apps/agent/pkg/server/v1_apis_create_test.go b/apps/agent/pkg/server/v1_apis_create_test.go index be224cad58..148cd1c961 100644 --- a/apps/agent/pkg/server/v1_apis_create_test.go +++ b/apps/agent/pkg/server/v1_apis_create_test.go @@ -38,7 +38,7 @@ func TestV1ApisCreate(t *testing.T) { testutil.Json(t, srv.app, testutil.JsonRequest{ Debug: true, Method: "POST", - Path: "/v1/api.createApi", + Path: "/v1/apis.createApi", Bearer: resources.UserRootKey, Body: `{ "name":"simple" }`, Response: &res, @@ -80,7 +80,7 @@ func TestV1ApisCreate_RejectsUnauthorized(t *testing.T) { testutil.Json(t, srv.app, testutil.JsonRequest{ Debug: true, Method: "POST", - Path: "/v1/api.createApi", + Path: "/v1/apis.createApi", Bearer: resources.UserRootKey, Body: `{ "name":"simple" }`, Response: &res, diff --git a/apps/agent/pkg/server/v1_apis_remove_test.go b/apps/agent/pkg/server/v1_apis_remove_test.go index 7fea6e274a..61023ad69f 100644 --- a/apps/agent/pkg/server/v1_apis_remove_test.go +++ b/apps/agent/pkg/server/v1_apis_remove_test.go @@ -37,7 +37,7 @@ func TestV1ApisRemove(t *testing.T) { res := RemoveApiResponse{} testutil.Json(t, srv.app, testutil.JsonRequest{ Method: "POST", - Path: "/v1/api.removeApi", + Path: "/v1/apis.removeApi", Bearer: resources.UserRootKey, Body: fmt.Sprintf(`{ "apiId":"%s" diff --git a/apps/agent/pkg/server/key_create.go b/apps/agent/pkg/server/v1_key_create.go similarity index 98% rename from apps/agent/pkg/server/key_create.go rename to apps/agent/pkg/server/v1_key_create.go index c89af8523f..da3d2685ed 100644 --- a/apps/agent/pkg/server/key_create.go +++ b/apps/agent/pkg/server/v1_key_create.go @@ -42,7 +42,7 @@ type CreateKeyResponse struct { KeyId string `json:"keyId"` } -func (s *Server) createKey(c *fiber.Ctx) error { +func (s *Server) v1CreateKey(c *fiber.Ctx) error { ctx, span := s.tracer.Start(c.UserContext(), "server.createKey") defer span.End() diff --git a/apps/agent/pkg/server/key_create_test.go b/apps/agent/pkg/server/v1_key_create_test.go similarity index 100% rename from apps/agent/pkg/server/key_create_test.go rename to apps/agent/pkg/server/v1_key_create_test.go diff --git a/apps/agent/pkg/server/v1_key_find.go b/apps/agent/pkg/server/v1_key_find.go new file mode 100644 index 0000000000..2bf805f9c4 --- /dev/null +++ b/apps/agent/pkg/server/v1_key_find.go @@ -0,0 +1,80 @@ +package server + +import ( + "fmt" + + "github.com/gofiber/fiber/v2" + "github.com/unkeyed/unkey/apps/agent/pkg/cache" + "github.com/unkeyed/unkey/apps/agent/pkg/errors" +) + +type GetKeyRequestV1 struct { + KeyId string `validate:"required"` +} + +type GetKeyResponseV1 = keyResponse + +func (s *Server) v1FindKey(c *fiber.Ctx) error { + ctx, span := s.tracer.Start(c.UserContext(), "server.v1FindKey") + defer span.End() + req := GetKeyRequest{ + KeyId: c.Query("keyId"), + } + + err := s.validator.Struct(req) + if err != nil { + return errors.NewHttpError(c, errors.BAD_REQUEST, err.Error()) + } + + authorizedWorkspaceId, err := s.authorizeRootKey(ctx, c) + if err != nil { + return errors.NewHttpError(c, errors.UNAUTHORIZED, err.Error()) + } + + key, found, err := cache.WithCache(s.keyCache, s.db.FindKeyById)(ctx, req.KeyId) + if err != nil { + return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, err.Error()) + } + if !found { + return errors.NewHttpError(c, errors.NOT_FOUND, fmt.Sprintf("key %s not found", req.KeyId)) + } + if key.WorkspaceId != authorizedWorkspaceId { + return errors.NewHttpError(c, errors.UNAUTHORIZED, "workspace access denied") + } + api, found, err := cache.WithCache(s.apiCache, s.db.FindApiByKeyAuthId)(ctx, key.KeyAuthId) + if err != nil { + return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, fmt.Sprintf("unable to find api: %s", err.Error())) + } + if !found { + + return errors.NewHttpError(c, errors.NOT_FOUND, fmt.Sprintf("unable to find api: %s", err.Error())) + } + + res := GetKeyResponse{ + Id: key.Id, + ApiId: api.Id, + WorkspaceId: key.WorkspaceId, + Name: key.Name, + Start: key.Start, + OwnerId: key.OwnerId, + Meta: key.Meta, + CreatedAt: key.CreatedAt.UnixMilli(), + ForWorkspaceId: key.ForWorkspaceId, + } + if !key.Expires.IsZero() { + res.Expires = key.Expires.UnixMilli() + } + if key.Ratelimit != nil { + res.Ratelimit = &ratelimitSettng{ + Type: key.Ratelimit.Type, + Limit: key.Ratelimit.Limit, + RefillRate: key.Ratelimit.RefillRate, + RefillInterval: key.Ratelimit.RefillInterval, + } + } + if key.Remaining != nil { + res.Remaining = key.Remaining + } + + return c.JSON(res) +} diff --git a/apps/agent/pkg/server/key_get_test.go b/apps/agent/pkg/server/v1_key_find_test.go similarity index 77% rename from apps/agent/pkg/server/key_get_test.go rename to apps/agent/pkg/server/v1_key_find_test.go index a324533f77..2e864b05e8 100644 --- a/apps/agent/pkg/server/key_get_test.go +++ b/apps/agent/pkg/server/v1_key_find_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http/httptest" + "strings" "testing" "time" @@ -19,7 +20,7 @@ import ( "github.com/unkeyed/unkey/apps/agent/pkg/uid" ) -func KeyGetKey_Simple(t *testing.T) { +func TestKeyFindV1_Simple(t *testing.T) { t.Parallel() ctx := context.Background() resources := testutil.SetupResources(t) @@ -59,6 +60,11 @@ func KeyGetKey_Simple(t *testing.T) { err = json.Unmarshal(body, &successResponse) require.NoError(t, err) - require.Equal(t, key, successResponse) + require.Equal(t, key.Id, successResponse.Id) + require.Equal(t, resources.UserApi.Id, successResponse.ApiId) + require.Equal(t, key.WorkspaceId, successResponse.WorkspaceId) + require.Equal(t, key.Name, successResponse.Name) + require.True(t, strings.HasPrefix(key.Hash, successResponse.Start)) + require.WithinDuration(t, key.CreatedAt, time.UnixMilli(successResponse.CreatedAt), time.Second) } diff --git a/apps/agent/pkg/server/v1_key_remove.go b/apps/agent/pkg/server/v1_key_remove.go new file mode 100644 index 0000000000..8915d54b8a --- /dev/null +++ b/apps/agent/pkg/server/v1_key_remove.go @@ -0,0 +1,63 @@ +package server + +import ( + "fmt" + + "github.com/unkeyed/unkey/apps/agent/pkg/cache" + "github.com/unkeyed/unkey/apps/agent/pkg/errors" + "github.com/unkeyed/unkey/apps/agent/pkg/events" + + "github.com/gofiber/fiber/v2" +) + +type RemoveKeyRequestV1 struct { + KeyId string `json:"keyId" validate:"required"` +} + +type RemoveKeyResponseV1 struct { +} + +func (s *Server) v1RemoveKey(c *fiber.Ctx) error { + ctx, span := s.tracer.Start(c.UserContext(), "server.v1RemoveKey") + defer span.End() + req := RemoveKeyRequestV1{} + + err := c.BodyParser(&req) + if err != nil { + return errors.NewHttpError(c, errors.BAD_REQUEST, err.Error()) + } + err = s.validator.Struct(req) + if err != nil { + return errors.NewHttpError(c, errors.BAD_REQUEST, err.Error()) + } + + authorizedWorkspaceId, err := s.authorizeRootKey(ctx, c) + if err != nil { + return errors.NewHttpError(c, errors.UNAUTHORIZED, err.Error()) + } + key, found, err := cache.WithCache(s.keyCache, s.db.FindKeyById)(ctx, req.KeyId) + if err != nil { + return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, fmt.Sprintf("unable to find key: %s", err.Error())) + } + if !found { + return errors.NewHttpError(c, errors.NOT_FOUND, fmt.Sprintf("unable to find key: %s", req.KeyId)) + } + if key.WorkspaceId != authorizedWorkspaceId { + return errors.NewHttpError(c, errors.UNAUTHORIZED, "access to workspace denied") + } + + err = s.db.DeleteKey(ctx, key.Id) + if err != nil { + return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, fmt.Sprintf("unable to delete key: %s", err.Error())) + } + + s.events.EmitKeyEvent(ctx, events.KeyEvent{ + Type: events.KeyDeleted, + Key: events.Key{ + Id: key.Id, + Hash: key.Hash, + }, + }) + + return c.JSON(RemoveKeyResponseV1{}) +} diff --git a/apps/agent/pkg/server/v1_key_remove_test.go b/apps/agent/pkg/server/v1_key_remove_test.go new file mode 100644 index 0000000000..d131cdc796 --- /dev/null +++ b/apps/agent/pkg/server/v1_key_remove_test.go @@ -0,0 +1,57 @@ +package server + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/unkeyed/unkey/apps/agent/pkg/cache" + "github.com/unkeyed/unkey/apps/agent/pkg/entities" + "github.com/unkeyed/unkey/apps/agent/pkg/hash" + "github.com/unkeyed/unkey/apps/agent/pkg/logging" + "github.com/unkeyed/unkey/apps/agent/pkg/testutil" + "github.com/unkeyed/unkey/apps/agent/pkg/tracing" + + "github.com/stretchr/testify/require" + "github.com/unkeyed/unkey/apps/agent/pkg/uid" +) + +func TestV1RemoveKey(t *testing.T) { + t.Parallel() + ctx := context.Background() + + resources := testutil.SetupResources(t) + + key := entities.Key{ + Id: uid.Key(), + KeyAuthId: resources.UserKeyAuth.Id, + WorkspaceId: resources.UserWorkspace.Id, + Hash: hash.Sha256(uid.New(16, "test")), + CreatedAt: time.Now(), + } + err := resources.Database.InsertKey(ctx, key) + require.NoError(t, err) + + srv := New(Config{ + Logger: logging.NewNoopLogger(), + KeyCache: cache.NewNoopCache[entities.Key](), + ApiCache: cache.NewNoopCache[entities.Api](), + Database: resources.Database, + Tracer: tracing.NewNoop(), + }) + + res := RemoveKeyResponseV1{} + testutil.Json(t, srv.app, testutil.JsonRequest{ + Method: "POST", + Path: "/v1/keys.removeKey", + Body: fmt.Sprintf(`{"keyId": "%s"}`, key.Id), + Bearer: resources.UserRootKey, + Response: &res, + StatusCode: 200, + }) + + _, found, err := resources.Database.FindKeyById(ctx, key.Id) + require.NoError(t, err) + require.False(t, found) +} diff --git a/apps/agent/pkg/server/key_verify.go b/apps/agent/pkg/server/v1_key_verify.go similarity index 95% rename from apps/agent/pkg/server/key_verify.go rename to apps/agent/pkg/server/v1_key_verify.go index da29d32238..0cbc6f6d48 100644 --- a/apps/agent/pkg/server/key_verify.go +++ b/apps/agent/pkg/server/v1_key_verify.go @@ -14,7 +14,7 @@ import ( "go.opentelemetry.io/otel/trace" ) -type VerifyKeyRequest struct { +type VerifyKeyRequestV1 struct { Key string `json:"key" validate:"required"` X struct { Resource string `json:"resource,omitempty"` @@ -28,7 +28,7 @@ type ratelimitResponse struct { Reset int64 `json:"reset"` } -type VerifyKeyResponse struct { +type VerifyKeyResponseV1 struct { Valid bool `json:"valid"` OwnerId string `json:"ownerId,omitempty"` Meta map[string]any `json:"meta,omitempty"` @@ -39,11 +39,11 @@ type VerifyKeyResponse struct { Error string `json:"error,omitempty"` } -func (s *Server) verifyKey(c *fiber.Ctx) error { +func (s *Server) v1VerifyKey(c *fiber.Ctx) error { ctx, span := s.tracer.Start(c.UserContext(), "server.verifyKey") defer span.End() - req := VerifyKeyRequest{} + req := VerifyKeyRequestV1{} err := c.BodyParser(&req) if err != nil { return errors.NewHttpError(c, errors.BAD_REQUEST, err.Error()) @@ -62,14 +62,14 @@ func (s *Server) verifyKey(c *fiber.Ctx) error { hash, err := getKeyHash(req.Key) hashSpan.End() if err != nil { - return c.JSON(VerifyKeyResponse{ + return c.JSON(VerifyKeyResponseV1{ Valid: false, Code: errors.NOT_FOUND, }) } key, hit := s.keyCache.Get(ctx, hash) if hit == cache.Null { - return c.JSON(VerifyKeyResponse{ + return c.JSON(VerifyKeyResponseV1{ Valid: false, Code: errors.NOT_FOUND, }) @@ -83,7 +83,7 @@ func (s *Server) verifyKey(c *fiber.Ctx) error { } if !found { s.keyCache.SetNull(ctx, hash) - return c.JSON(VerifyKeyResponse{ + return c.JSON(VerifyKeyResponseV1{ Valid: false, Code: errors.NOT_FOUND, }) @@ -98,7 +98,7 @@ func (s *Server) verifyKey(c *fiber.Ctx) error { return errors.NewHttpError(c, errors.INTERNAL_SERVER_ERROR, err.Error()) } - return c.JSON(VerifyKeyResponse{ + return c.JSON(VerifyKeyResponseV1{ Valid: false, Code: errors.NOT_FOUND, }) @@ -128,7 +128,7 @@ func (s *Server) verifyKey(c *fiber.Ctx) error { ipSpan.End() if !whitelist.Ip(sourceIp, api.IpWhitelist) { s.logger.Info().Str("workspaceId", api.WorkspaceId).Str("apiId", api.Id).Str("keyId", key.Id).Str("sourceIp", sourceIp).Strs("whitelist", api.IpWhitelist).Msg("ip denied") - return c.JSON(VerifyKeyResponse{ + return c.JSON(VerifyKeyResponseV1{ Valid: false, Code: errors.FORBIDDEN, }) @@ -151,7 +151,7 @@ func (s *Server) verifyKey(c *fiber.Ctx) error { sp.End() } - res := VerifyKeyResponse{ + res := VerifyKeyResponseV1{ Valid: true, OwnerId: key.OwnerId, Meta: key.Meta, @@ -255,4 +255,3 @@ func (s *Server) verifyKey(c *fiber.Ctx) error { return c.JSON(res) } - diff --git a/apps/agent/pkg/server/key_verify_test.go b/apps/agent/pkg/server/v1_key_verify_test.go similarity index 93% rename from apps/agent/pkg/server/key_verify_test.go rename to apps/agent/pkg/server/v1_key_verify_test.go index d35cf82b8e..91a4611a80 100644 --- a/apps/agent/pkg/server/key_verify_test.go +++ b/apps/agent/pkg/server/v1_key_verify_test.go @@ -55,7 +55,7 @@ func TestVerifyKey_Simple(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") res, err := srv.app.Test(req) @@ -67,7 +67,7 @@ func TestVerifyKey_Simple(t *testing.T) { require.Equal(t, 200, res.StatusCode) - successResponse := VerifyKeyResponse{} + successResponse := VerifyKeyResponseV1{} err = json.Unmarshal(body, &successResponse) require.NoError(t, err) @@ -103,7 +103,7 @@ func TestVerifyKey_ReturnErrorForBadRequest(t *testing.T) { "somethingelse":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") res, err := srv.app.Test(req) @@ -152,7 +152,7 @@ func TestVerifyKey_WithTemporaryKey(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") res, err := srv.app.Test(req) @@ -163,7 +163,7 @@ func TestVerifyKey_WithTemporaryKey(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res.StatusCode) - successResponse := VerifyKeyResponse{} + successResponse := VerifyKeyResponseV1{} err = json.Unmarshal(body, &successResponse) require.NoError(t, err) @@ -180,7 +180,7 @@ func TestVerifyKey_WithTemporaryKey(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, errorRes.StatusCode) - verifyKeyResponse := VerifyKeyResponse{} + verifyKeyResponse := VerifyKeyResponseV1{} err = json.Unmarshal(errorBody, &verifyKeyResponse) require.NoError(t, err) @@ -222,7 +222,7 @@ func TestVerifyKey_WithRatelimit(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") // first request @@ -235,7 +235,7 @@ func TestVerifyKey_WithRatelimit(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res1.StatusCode) - verifyRes1 := VerifyKeyResponse{} + verifyRes1 := VerifyKeyResponseV1{} err = json.Unmarshal(body1, &verifyRes1) require.NoError(t, err) @@ -255,7 +255,7 @@ func TestVerifyKey_WithRatelimit(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res2.StatusCode) - verifyRes2 := VerifyKeyResponse{} + verifyRes2 := VerifyKeyResponseV1{} err = json.Unmarshal(body2, &verifyRes2) require.NoError(t, err) @@ -275,7 +275,7 @@ func TestVerifyKey_WithRatelimit(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res3.StatusCode) - verifyRes3 := VerifyKeyResponse{} + verifyRes3 := VerifyKeyResponseV1{} err = json.Unmarshal(body3, &verifyRes3) require.NoError(t, err) @@ -296,7 +296,7 @@ func TestVerifyKey_WithRatelimit(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res4.StatusCode) - verifyRes4 := VerifyKeyResponse{} + verifyRes4 := VerifyKeyResponseV1{} err = json.Unmarshal(body4, &verifyRes4) require.NoError(t, err) @@ -353,7 +353,7 @@ func TestVerifyKey_WithIpWhitelist_Pass(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") req.Header.Set("Fly-Client-IP", "100.100.100.100") @@ -365,7 +365,7 @@ func TestVerifyKey_WithIpWhitelist_Pass(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res.StatusCode) - successResponse := VerifyKeyResponse{} + successResponse := VerifyKeyResponseV1{} err = json.Unmarshal(body, &successResponse) require.NoError(t, err) @@ -418,7 +418,7 @@ func TestVerifyKey_WithIpWhitelist_Blocked(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") req.Header.Set("Fly-Client-IP", "1.2.3.4") @@ -430,7 +430,7 @@ func TestVerifyKey_WithIpWhitelist_Blocked(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res.StatusCode) - verifyKeyResponse := VerifyKeyResponse{} + verifyKeyResponse := VerifyKeyResponseV1{} err = json.Unmarshal(body, &verifyKeyResponse) require.NoError(t, err) @@ -469,7 +469,7 @@ func TestVerifyKey_WithRemaining(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") // Use up 10 requests @@ -483,7 +483,7 @@ func TestVerifyKey_WithRemaining(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res.StatusCode) - vr := VerifyKeyResponse{} + vr := VerifyKeyResponseV1{} err = json.Unmarshal(body1, &vr) require.NoError(t, err) @@ -502,7 +502,7 @@ func TestVerifyKey_WithRemaining(t *testing.T) { require.NoError(t, err) require.Equal(t, 200, res2.StatusCode) - verifyRes2 := VerifyKeyResponse{} + verifyRes2 := VerifyKeyResponseV1{} err = json.Unmarshal(body2, &verifyRes2) require.NoError(t, err) @@ -553,7 +553,7 @@ func TestVerifyKey_ShouldReportUsageWhenUsageExceeded(t *testing.T) { "key":"%s" }`, key)) - req := httptest.NewRequest("POST", "/v1/keys/verify", buf) + req := httptest.NewRequest("POST", "/v1/keys.verifyKey", buf) req.Header.Set("Content-Type", "application/json") res, err := srv.app.Test(req) @@ -565,7 +565,7 @@ func TestVerifyKey_ShouldReportUsageWhenUsageExceeded(t *testing.T) { require.Equal(t, 200, res.StatusCode) - successResponse := VerifyKeyResponse{} + successResponse := VerifyKeyResponseV1{} err = json.Unmarshal(body, &successResponse) require.NoError(t, err) diff --git a/apps/agent/pkg/server/v1_workspace_create_test.go b/apps/agent/pkg/server/v1_workspace_create_test.go index 58d04c8f79..bf5f4dd083 100644 --- a/apps/agent/pkg/server/v1_workspace_create_test.go +++ b/apps/agent/pkg/server/v1_workspace_create_test.go @@ -38,7 +38,7 @@ func TestCreateWorkspace_Simple(t *testing.T) { testutil.Json(t, srv.app, testutil.JsonRequest{ Debug: true, Method: "POST", - Path: "/v1/workspace.createWorkspace", + Path: "/v1/workspaces.createWorkspace", Body: fmt.Sprintf(`{ "name":"simple", "tenantId": "%s"