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

Enable specifying api key in config #18

Merged
merged 6 commits into from
Aug 22, 2023
Merged

Enable specifying api key in config #18

merged 6 commits into from
Aug 22, 2023

Conversation

joewagner
Copy link
Contributor

@joewagner joewagner commented Aug 21, 2023

This enables specifying an api key that bypasses the Validator's rate limiting.
You can pass an apiKey parameter to the Validator and Database classes, which is converted to the correct header and send with read requests.

Example use:

new Validator({ apiKey, ... });
new Database({ apiKey, ... });

@@ -45,7 +45,7 @@
"scripts": {
"postinstall": "cd registry && npm install",
"test": "mocha",
"test:ci": "npm run build && mocha --exit",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but lerna and nx handle the need to build (or not) before running the test. Having this written into the test command was causing issues.

@@ -59,7 +59,8 @@
},
"scripts": {
"prepublishOnly": "npm run build",
"test": "npm run test:browser && mocha --exit",
"test": "npm run test:browser && npm run test:mocha",
"test:mocha": "mocha --exit",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this command to make it easier for devs to just run the mocha tests since the browser tests take so long.

import { type ChainName, getBaseUrl } from "./chains.js";
import { type Signer, type ExternalProvider, getSigner } from "./ethers.js";

export interface ReadConfig {
baseUrl: string;
aliases?: AliasesNameMap;
apiKey?: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage is now:

new Validator({ apiKey, ... });
new Database({ apiKey, ... });

if (config.apiKey) {
conf.init = {
headers: {
"Api-Key": config.apiKey,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bulk of the logic. Take the apiKey param and set the Api-Key request header with the given value.

@@ -1,4 +1,4 @@
import { type Signal } from "../helpers/await.js";
import { type Signal } from "../helpers/index.js";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated cleanup

@@ -3,7 +3,8 @@
"HTTP": {
"Port": "8081",
"RateLimInterval": "1s",
"MaxRequestPerInterval": 10
"MaxRequestPerInterval": 10,
"APIKey": "foo"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the tests simple for the time being. The api key is always going to be "foo"

As an aside in regard to the api key implementation in the validator... Since the key is a fixed config value we should probably ensure the value is only ever used on the server side of the Studio.
Otherwise anyone who logs into the Studio will be able to get the api key and use it as much as they like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vercel makes it hard to expose secrets to the client. By default all env vars are server side only. You have to opt in to exposing them to the client by prefixing the env var name with NEXT_PUBLIC_.

@joewagner joewagner requested a review from asutula August 22, 2023 18:38
@joewagner joewagner marked this pull request as ready for review August 22, 2023 18:38
@carsonfarmer
Copy link
Member

Is this still WIP @joewagner?

@joewagner
Copy link
Contributor Author

Is this still WIP @joewagner?

It's ready for review, but it's aiming at the cleanup branch, which I'll merge and then point this at main.

Base automatically changed from joe/pack-cleanup to main August 22, 2023 19:32
Copy link
Contributor

@asutula asutula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Nice work. I'm fine with this as is, but just want to surface an idea... What about making the config option called extraHeaders and it's type be { [key: string]: string }. I'm just thinking this is, at this point at least, kind of a bandaid fix for Studio and not a real feature we want to advertise. If we leave the config option as kinda generic like that, we don't have to explain much about it in the docs.

@joewagner joewagner merged commit d32e0f2 into main Aug 22, 2023
4 checks passed
@joewagner joewagner deleted the joe/apikey branch August 22, 2023 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants