Skip to content
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

(DAL) Update key validation #1748

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion node/pkg/dal/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,32 @@ func TestShouldFailWithoutApiKey(t *testing.T) {
testItems.Controller.Start(ctx)
go testItems.App.Listen(":8090")
resp, err := request.RequestRaw(request.WithEndpoint("http://localhost:8090/api/v1"))
if err != nil {
t.Fatalf("error getting latest data: %v", err)
}

assert.Equal(t, 200, resp.StatusCode)

sampleSubmissionData, err := generateSampleSubmissionData(
testItems.TmpConfig.ID,
int64(15),
time.Now(),
1,
"test-aggregate",
)
if err != nil {
t.Fatalf("error generating sample submission data: %v", err)
}

aggregator.SetLatestGlobalAggregateAndProof(ctx, testItems.TmpConfig.ID, sampleSubmissionData.GlobalAggregate, sampleSubmissionData.Proof)

result, err := request.RequestRaw(request.WithEndpoint("http://localhost:8090/api/v1/dal/latest-data-feeds/test-aggregate"))

if err != nil {
t.Fatalf("error getting latest data: %v", err)
}

assert.Equal(t, 401, resp.StatusCode)
assert.Equal(t, 401, result.StatusCode)
}

func TestApiGetLatest(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions node/pkg/dal/tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func setup(ctx context.Context) (func() error, *TestItems, error) {
testItems.Collector = api.ApiController.Collector

v1 := app.Group("/api/v1")
v1.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Orakl Node DAL API")
})
api.Routes(v1)

return cleanup(ctx, testItems), testItems, nil
Expand Down
7 changes: 7 additions & 0 deletions node/pkg/dal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func validator(ctx *fiber.Ctx, s string) (bool, error) {
return false, fmt.Errorf("invalid api key")
}

func authFilter(c *fiber.Ctx) bool {
originalURL := strings.ToLower(c.OriginalURL())
return originalURL == "/api/v1"
}

func Setup(ctx context.Context) (*fiber.App, error) {
_, err := db.GetPool(ctx)
if err != nil {
Expand All @@ -52,13 +57,15 @@ func Setup(ctx context.Context) (*fiber.App, error) {

app.Use(recover.New(
recover.Config{

EnableStackTrace: true,
StackTraceHandler: CustomStackTraceHandler,
},
))

app.Use(cors.New())
app.Use(keyauth.New(keyauth.Config{
Next: authFilter,
KeyLookup: "header:X-API-Key",
Validator: validator,
}))
Expand Down
Loading