-
Notifications
You must be signed in to change notification settings - Fork 806
Feat: User-level secrets store #217
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
Open
AshishKumar4
wants to merge
4
commits into
nightly
Choose a base branch
from
feat/do-backed-secrets
base: nightly
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
12d2132
feat: add user secrets management with durable objects
AshishKumar4 6ae57ef
feat: updated llm.md and claude.md
AshishKumar4 b26bbb6
feat: added user secrets store tests
AshishKumar4 078fbee
feat: enhance security of secrets management system
AshishKumar4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| bun test | ||
| bun run test:bun |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| /** | ||
| * User Secrets Controller - RPC wrapper for UserSecretsStore DO | ||
| */ | ||
|
|
||
| import { BaseController } from '../baseController'; | ||
| import { ApiResponse, ControllerResponse } from '../types'; | ||
| import { RouteContext } from '../../types/route-context'; | ||
| import { createLogger } from '../../../logger'; | ||
| import type { SecretMetadata, StoreSecretRequest, UpdateSecretRequest } from '../../../services/secrets/types'; | ||
|
|
||
| type UserSecretsListData = { secrets: SecretMetadata[] }; | ||
| type UserSecretStoreData = { secret: SecretMetadata; message: string }; | ||
| type UserSecretValueData = { value: string; metadata: SecretMetadata }; | ||
| type UserSecretUpdateData = { secret: SecretMetadata; message: string }; | ||
| type UserSecretDeleteData = { message: string }; | ||
|
|
||
| export class UserSecretsController extends BaseController { | ||
| static logger = createLogger('UserSecretsController'); | ||
|
|
||
| /** | ||
| * Get Durable Object stub for user | ||
| */ | ||
| private static getUserSecretsStub(env: Env, userId: string) { | ||
| const id = env.UserSecretsStore.idFromName(userId); | ||
| return env.UserSecretsStore.get(id); | ||
| } | ||
|
|
||
| /** | ||
| * List all secrets (metadata only) | ||
| * GET /api/user-secrets | ||
| */ | ||
| static async listSecrets( | ||
| _request: Request, | ||
| env: Env, | ||
| _ctx: ExecutionContext, | ||
| context: RouteContext | ||
| ): Promise<ControllerResponse<ApiResponse<UserSecretsListData>>> { | ||
| try { | ||
| const user = context.user!; | ||
| const stub = this.getUserSecretsStub(env, user.id); | ||
|
|
||
| const secrets = await stub.listSecrets(); | ||
|
|
||
| return UserSecretsController.createSuccessResponse({ secrets }); | ||
| } catch (error) { | ||
| this.logger.error('Error listing secrets:', error); | ||
| return UserSecretsController.createErrorResponse<UserSecretsListData>( | ||
| 'Failed to list secrets', | ||
| 500 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Store a new secret | ||
| * POST /api/user-secrets | ||
| */ | ||
| static async storeSecret( | ||
| request: Request, | ||
| env: Env, | ||
| _ctx: ExecutionContext, | ||
| context: RouteContext | ||
| ): Promise<ControllerResponse<ApiResponse<UserSecretStoreData>>> { | ||
| try { | ||
| const user = context.user!; | ||
| const stub = this.getUserSecretsStub(env, user.id); | ||
|
|
||
| const bodyResult = await UserSecretsController.parseJsonBody<StoreSecretRequest>(request); | ||
|
|
||
| if (!bodyResult.success) { | ||
| return bodyResult.response! as ControllerResponse<ApiResponse<UserSecretStoreData>>; | ||
| } | ||
|
|
||
| const secret = await stub.storeSecret(bodyResult.data!); | ||
|
|
||
| if (!secret) { | ||
| return UserSecretsController.createErrorResponse<UserSecretStoreData>( | ||
| 'Validation failed: Invalid secret data', | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| return UserSecretsController.createSuccessResponse({ | ||
| secret, | ||
| message: 'Secret stored successfully' | ||
| }); | ||
| } catch (error) { | ||
| this.logger.error('Error storing secret:', error); | ||
| return UserSecretsController.createErrorResponse<UserSecretStoreData>( | ||
| error instanceof Error ? error.message : 'Failed to store secret', | ||
| 500 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get decrypted secret value | ||
| * GET /api/user-secrets/:secretId/value | ||
| */ | ||
| static async getSecretValue( | ||
| _request: Request, | ||
| env: Env, | ||
| _ctx: ExecutionContext, | ||
| context: RouteContext | ||
| ): Promise<ControllerResponse<ApiResponse<UserSecretValueData>>> { | ||
| try { | ||
| const user = context.user!; | ||
| const secretId = context.pathParams.secretId; | ||
|
|
||
| if (!secretId) { | ||
| return UserSecretsController.createErrorResponse<UserSecretValueData>( | ||
| 'Secret ID is required', | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| const stub = this.getUserSecretsStub(env, user.id); | ||
|
|
||
| const result = await stub.getSecretValue(secretId); | ||
|
|
||
| if (!result) { | ||
| return UserSecretsController.createErrorResponse<UserSecretValueData>( | ||
| 'Secret not found or has expired', | ||
| 404 | ||
| ); | ||
| } | ||
|
|
||
| return UserSecretsController.createSuccessResponse(result); | ||
| } catch (error) { | ||
| this.logger.error('Error getting secret value:', error); | ||
| return UserSecretsController.createErrorResponse<UserSecretValueData>( | ||
| 'Failed to get secret value', | ||
| 500 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update secret | ||
| * PATCH /api/user-secrets/:secretId | ||
| */ | ||
| static async updateSecret( | ||
| request: Request, | ||
| env: Env, | ||
| _ctx: ExecutionContext, | ||
| context: RouteContext | ||
| ): Promise<ControllerResponse<ApiResponse<UserSecretUpdateData>>> { | ||
| try { | ||
| const user = context.user!; | ||
| const secretId = context.pathParams.secretId; | ||
|
|
||
| if (!secretId) { | ||
| return UserSecretsController.createErrorResponse<UserSecretUpdateData>( | ||
| 'Secret ID is required', | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| const bodyResult = await UserSecretsController.parseJsonBody<UpdateSecretRequest>(request); | ||
|
|
||
| if (!bodyResult.success) { | ||
| return bodyResult.response! as ControllerResponse<ApiResponse<UserSecretUpdateData>>; | ||
| } | ||
|
|
||
| const stub = this.getUserSecretsStub(env, user.id); | ||
|
|
||
| const secret = await stub.updateSecret(secretId, bodyResult.data!); | ||
|
|
||
| if (!secret) { | ||
| return UserSecretsController.createErrorResponse<UserSecretUpdateData>( | ||
| 'Secret not found or validation failed', | ||
| 404 | ||
| ); | ||
| } | ||
|
|
||
| return UserSecretsController.createSuccessResponse({ | ||
| secret, | ||
| message: 'Secret updated successfully' | ||
| }); | ||
| } catch (error) { | ||
| this.logger.error('Error updating secret:', error); | ||
| return UserSecretsController.createErrorResponse<UserSecretUpdateData>( | ||
| 'Failed to update secret', | ||
| 500 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delete a secret (soft delete) | ||
| * DELETE /api/user-secrets/:secretId | ||
| */ | ||
| static async deleteSecret( | ||
| _request: Request, | ||
| env: Env, | ||
| _ctx: ExecutionContext, | ||
| context: RouteContext | ||
| ): Promise<ControllerResponse<ApiResponse<UserSecretDeleteData>>> { | ||
| try { | ||
| const user = context.user!; | ||
| const secretId = context.pathParams.secretId; | ||
|
|
||
| if (!secretId) { | ||
| return UserSecretsController.createErrorResponse<UserSecretDeleteData>( | ||
| 'Secret ID is required', | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| const stub = this.getUserSecretsStub(env, user.id); | ||
|
|
||
| const deleted = await stub.deleteSecret(secretId); | ||
|
|
||
| if (!deleted) { | ||
| return UserSecretsController.createErrorResponse<UserSecretDeleteData>( | ||
| 'Secret not found', | ||
| 404 | ||
| ); | ||
| } | ||
|
|
||
| return UserSecretsController.createSuccessResponse({ | ||
| message: 'Secret deleted successfully' | ||
| }); | ||
| } catch (error) { | ||
| this.logger.error('Error deleting secret:', error); | ||
| return UserSecretsController.createErrorResponse<UserSecretDeleteData>( | ||
| 'Failed to delete secret', | ||
| 500 | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * User Secrets Controller Types | ||
| */ | ||
|
|
||
| export interface UserSecretsListData { | ||
| secrets: unknown[]; | ||
| } | ||
|
|
||
| export interface UserSecretStoreData { | ||
| secret: unknown; | ||
| message: string; | ||
| } | ||
|
|
||
| export interface UserSecretValueData { | ||
| value: string; | ||
| metadata: unknown; | ||
| } | ||
|
|
||
| export interface UserSecretUpdateData { | ||
| secret: unknown; | ||
| message: string; | ||
| } | ||
|
|
||
| export interface UserSecretDeleteData { | ||
| message: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.