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

api: add base admin API key support #3274

Merged
merged 19 commits into from
Sep 20, 2023
Merged

api: add base admin API key support #3274

merged 19 commits into from
Sep 20, 2023

Conversation

mastercactapus
Copy link
Member

@mastercactapus mastercactapus commented Sep 12, 2023

Description:
This PR introduces the ability to create, delete, and use admin-only API keys for the GraphQL API.

This functionality is behind the experimental flag gql-api-keys.

make start EXPERIMENTAL=gql-api-keys

Which issue(s) this PR fixes:
Part of #3007

Out of Scope:

  • Policy caching (perf. optimization) will be done in a separate PR
  • Update and listing will be done in a separate PR, this is just base functionality
  • UI for managing and viewing usage will be done in a separate PR

Describe any introduced API changes:

  • createGQLAPIKey and deleteGQLAPIKey mutations were added

Additional Context
Since this is a large PR, here's a walkthrough of the changes:

  • There are context helpers for storing/retrieving the API key policy from a request
  • The tokens themselves are JWT, with expiration and an embedded hash of the policy (prevents policy changes after token generation)
  • last-used data is kept for each key and includes the user agent and IP address
  • the policy for a key includes all allowed GraphQL fields, as well as the role (Admin or User)
  • policy info is stored in the DB, the hash must match the embedded hash of the token
  • sqlc was used for all DB queries
  • this PR supports Delete, Authorize, and Create
  • Keys are only valid for /api/graphql and the GQLAPIKey experimental flag must be enabled
  • Field enforcement is done as AroundFields moddleware
  • schema has the 2 mutations added (create/delete), creating a key is the only way to retrieve it's token
  • The key information is stored in a gql_api_keys table, with soft-deletes
  • The usage information is a separate gql_api_key_usage table, since it will receive frequent updates

@github-actions github-actions bot added size/l and removed size/xl labels Sep 13, 2023
Copy link
Contributor

@andrewbenington andrewbenington left a comment

Choose a reason for hiding this comment

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

looks good and seems to work well locally

Copy link
Contributor

@andrewbenington andrewbenington left a comment

Choose a reason for hiding this comment

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

looks like expflag.d.ts and schema.d.ts are out of date, running go generate ./web/src updated them for me

@github-actions github-actions bot added size/xl and removed size/l labels Sep 13, 2023
@mastercactapus mastercactapus merged commit 2740875 into master Sep 20, 2023
@mastercactapus mastercactapus deleted the gql-api-key-base branch September 20, 2023 16:50
allending313 pushed a commit that referenced this pull request Sep 27, 2023
* add api key tables

* update schema for create and delete

* manage keys

* enforce api key fields

* add new key type to auth

* add usage query

* regen

* regen

* cleanup field validation

* set role

* add role to API

* remove cache for initial PR

* add experimental flag

* move migration

* regen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants