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

feat: add keystore package #2756

Merged
merged 3 commits into from
Sep 24, 2024
Merged

feat: add keystore package #2756

merged 3 commits into from
Sep 24, 2024

Conversation

TBonnin
Copy link
Collaborator

@TBonnin TBonnin commented Sep 23, 2024

Describe your changes

A new keystore package containing logic to create, store, retrieve and delete private keys.
I decided to make it an independant package instead of adding it to server so it can be self-contained and reusable. It means the table migrations are different (which creates new migrations table) but also means the package can be used independently.
The db client must be provided though so it can use the same as the one used by the service importing keystore.
In this PR I added keystore to server. In next PR I will add the endpoint used in the new auth flow to create and check session token

Issue ticket number and link

https://linear.app/nango/issue/NAN-1758/implement-keystore-package

Checklist before requesting a review (skip if just adding/editing APIs & templates)

  • I added tests, otherwise the reason is:
  • I added observability, otherwise the reason is:
  • I added analytics, otherwise the reason is:

Copy link

linear bot commented Sep 23, 2024

CREATE TYPE entity_types AS ENUM (
'session',
'connection',
'environment'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we might eventually want to use this package to handle environment private keys.

{ displayName, entityType, entityId, ttlInMs }: Pick<PrivateKey, 'displayName' | 'entityType' | 'entityId'> & { ttlInMs?: number }
): Promise<Result<string, PrivateKeyError>> {
const now = new Date();
const prefix = entityType.slice(0, 4);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

adding a prefix to make it easier to debug

): Promise<Result<string, PrivateKeyError>> {
const now = new Date();
const prefix = entityType.slice(0, 4);
const random = crypto.randomBytes(32).toString('hex');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I picked a size of 32 bytes but I am happy to reconsider

@TBonnin TBonnin force-pushed the tbonnin/nan-1758/keystore branch from b428a28 to 2463280 Compare September 23, 2024 15:27
Copy link
Collaborator

@bodinsamuel bodinsamuel 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, a few open questions and suggestions

await trx.raw(`
CREATE TABLE IF NOT EXISTS ${PRIVATE_KEYS_TABLE} (
id SERIAL PRIMARY KEY,
display_name VARCHAR(255) NOT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing env_id and/or account_id to be able to quickly select / delete all of them. Especially if we have a put environment keys inside

packages/keystore/lib/types.ts Outdated Show resolved Hide resolved
packages/keystore/lib/models/privatekeys.ts Show resolved Hide resolved
packages/keystore/lib/models/privatekeys.ts Outdated Show resolved Hide resolved
packages/keystore/lib/models/privatekeys.ts Outdated Show resolved Hide resolved
packages/keystore/lib/models/privatekeys.ts Show resolved Hide resolved
@@ -0,0 +1,32 @@
import path from 'node:path';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand why you created a dedicated file, however, it's going to create two lock tables for each package that uses this pattern. I don't have a strong opinion because it's definitely handy to split everything, just not sure it's worth it imo

Copy link
Collaborator Author

@TBonnin TBonnin Sep 23, 2024

Choose a reason for hiding this comment

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

It is true the package could assume the tables exist and have a single place that ensure the database is in the correct state. The cost is two extra lock tables, the advantage if we ever use the package in another service is that we don't have to worry about order of deployment/migration since it is embedded in keystore package.
It also make testing easier, the package can just run the keystore related migrations without having to setup the entire db.

btw we are also creating extra migrations tables for the scheduler.

What if the migrations tables were in their own migrations schema?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, the main difference is that it's not in the same schema. Let's discuss tomorrow ☺️

@TBonnin TBonnin force-pushed the tbonnin/nan-1758/keystore branch from 969ecca to c27c146 Compare September 23, 2024 18:58
@TBonnin TBonnin requested a review from bodinsamuel September 23, 2024 19:01
@TBonnin
Copy link
Collaborator Author

TBonnin commented Sep 23, 2024

@bodinsamuel thank you very much for the review. I think I have addressed all your comments.
I'll ping you to discuss further about the migration tables

@TBonnin TBonnin force-pushed the tbonnin/nan-1758/keystore branch from c27c146 to 659eca6 Compare September 23, 2024 19:25
packages/types/lib/keystore/index.ts Outdated Show resolved Hide resolved
@TBonnin TBonnin force-pushed the tbonnin/nan-1758/keystore branch 2 times, most recently from acc49ea to 67f8eb9 Compare September 24, 2024 14:19
keystore package contains logic to create, store, retrieve and delete
private keys
@TBonnin TBonnin force-pushed the tbonnin/nan-1758/keystore branch from 67f8eb9 to 3b7ea4e Compare September 24, 2024 14:34
@TBonnin TBonnin enabled auto-merge (squash) September 24, 2024 14:34
@TBonnin TBonnin merged commit 89e68f0 into master Sep 24, 2024
22 checks passed
@TBonnin TBonnin deleted the tbonnin/nan-1758/keystore branch September 24, 2024 14:42
hassan254-prog pushed a commit that referenced this pull request Sep 25, 2024
## Describe your changes

A new keystore package containing logic to create, store, retrieve and
delete private keys.
I decided to make it an independant package instead of adding it to
`server` so it can be self-contained and reusable. It means the table
migrations are different (which creates new migrations table) but also
means the package can be used independently.
The db client must be provided though so it can use the same as the one
used by the service importing `keystore`.
In this PR I added keystore to `server`. In next PR I will add the
endpoint used in the new auth flow to create and check session token

## Issue ticket number and link
https://linear.app/nango/issue/NAN-1758/implement-keystore-package

## Checklist before requesting a review (skip if just adding/editing
APIs & templates)
- [x] I added tests, otherwise the reason is: 
- [x] I added observability, otherwise the reason is:
- [ ] I added analytics, otherwise the reason is:
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.

2 participants