-
Notifications
You must be signed in to change notification settings - Fork 52
[DB-29] added IPC methods for secretsManager #277
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
nafees87n
wants to merge
13
commits into
secrets/storage-listener
Choose a base branch
from
ipc-layer
base: secrets/storage-listener
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
95b60b6
added ipc methods
nafees87n 98627e5
added listProviders
nafees87n 16a4a14
fix: initialization
nafees87n ecd380a
exposed getSecrets
nafees87n 9898462
fix: event name
nafees87n 6c67bcc
only return metadata for listing
nafees87n 8237627
added providers onCHange listener
nafees87n bc4baca
Merge branch 'secrets/storage-listener' into ipc-layer
nafees87n 27ef8b3
Merge branch 'secrets/storage-listener' into ipc-layer
nafees87n 44bdcac
[DB-28] error handling in secretsManager (#278)
nafees87n 1f1c66a
Merge branch 'secrets/storage-listener' into ipc-layer
nafees87n fe6aea2
Merge branch 'secrets/storage-listener' into ipc-layer
nafees87n 63ff56a
remove examples.ts
nafees87n 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
File renamed without changes.
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,54 @@ | ||
| import { SecretReference } from "./types"; | ||
|
|
||
| export enum SecretsErrorCode { | ||
| SAFE_STORAGE_ENCRYPTION_NOT_AVAILABLE = "safe_storage_encryption_not_available", | ||
|
|
||
| PROVIDER_NOT_FOUND = "provider_not_found", | ||
|
|
||
| AUTH_FAILED = "auth_failed", | ||
| PERMISSION_DENIED = "permission_denied", | ||
|
|
||
| SECRET_NOT_FOUND = "secret_not_found", | ||
| SECRET_FETCH_FAILED = "secret_fetch_failed", | ||
|
|
||
| STORAGE_READ_FAILED = "storage_read_failed", | ||
| STORAGE_WRITE_FAILED = "storage_write_failed", | ||
|
|
||
| UNKNOWN = "unknown", | ||
| } | ||
|
|
||
| export interface SecretsError { | ||
| code: SecretsErrorCode; | ||
| message: string; | ||
| providerId?: string; | ||
| secretRef?: SecretReference; | ||
| cause?: Error; // Original error | ||
| } | ||
|
|
||
| export type SecretsManagerError = { | ||
| type: "error"; | ||
| error: SecretsError; | ||
| }; | ||
|
|
||
| export type SecretsSuccess<T> = T extends void | ||
| ? { type: "success" } | ||
| : { type: "success"; data: T }; | ||
|
|
||
| export type SecretsResult<T> = SecretsSuccess<T> | SecretsManagerError; | ||
|
|
||
| export type SecretsResultPromise<T> = Promise<SecretsResult<T>>; | ||
|
|
||
| export function createSecretsError( | ||
| code: SecretsErrorCode, | ||
| message: string, | ||
| context?: Omit<SecretsError, "code" | "message"> | ||
| ): SecretsManagerError { | ||
| return { | ||
| type: "error", | ||
| error: { | ||
| code, | ||
| message, | ||
| ...context, | ||
| }, | ||
| }; | ||
| } |
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,112 @@ | ||
| import { SecretsManagerEncryptedStorage } from "./encryptedStorage/SecretsManagerEncryptedStorage"; | ||
| import { FileBasedProviderRegistry } from "./providerRegistry/FileBasedProviderRegistry"; | ||
| import { ProviderChangeCallback } from "./providerRegistry/AbstractProviderRegistry"; | ||
| import { SecretsManager } from "./secretsManager"; | ||
| import { | ||
| SecretProviderConfig, | ||
| SecretProviderMetadata, | ||
| SecretReference, | ||
| SecretValue, | ||
| } from "./types"; | ||
| import { | ||
| createSecretsError, | ||
| SecretsErrorCode, | ||
| SecretsResultPromise, | ||
| } from "./errors"; | ||
|
|
||
| const getSecretsManager = (): SecretsManager => { | ||
| if (!SecretsManager.isInitialized()) { | ||
| return null as any; | ||
| } | ||
| return SecretsManager.getInstance(); | ||
| }; | ||
|
|
||
| const PROVIDERS_DIRECTORY = "providers"; | ||
|
|
||
| export const initSecretsManager = async (): SecretsResultPromise<void> => { | ||
| try { | ||
| const secretsStorage = new SecretsManagerEncryptedStorage( | ||
| PROVIDERS_DIRECTORY | ||
| ); | ||
| const registry = new FileBasedProviderRegistry(secretsStorage); | ||
|
|
||
| await SecretsManager.initialize(registry); | ||
|
|
||
| return { | ||
| type: "success", | ||
| }; | ||
| } catch (error) { | ||
| if ((error as Error).name === "SafeStorageEncryptionNotAvailable") { | ||
| return createSecretsError( | ||
| SecretsErrorCode.SAFE_STORAGE_ENCRYPTION_NOT_AVAILABLE, | ||
| "Safe storage encryption is not available.", // UI to show OS specific message here | ||
| { | ||
| cause: error as Error, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| return createSecretsError( | ||
| SecretsErrorCode.UNKNOWN, | ||
| "Failed to initialize SecretsManager.", | ||
| { | ||
| cause: error as Error, | ||
| } | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| export const subscribeToProvidersChange = ( | ||
| callback: ProviderChangeCallback | ||
| ): (() => void) => { | ||
| return getSecretsManager().onProvidersChange(callback); | ||
| }; | ||
|
|
||
| export const setSecretProviderConfig = async ( | ||
| config: SecretProviderConfig | ||
| ): SecretsResultPromise<void> => { | ||
| return getSecretsManager().setProviderConfig(config); | ||
| }; | ||
|
|
||
| export const removeSecretProviderConfig = async ( | ||
| providerId: string | ||
| ): SecretsResultPromise<void> => { | ||
| return getSecretsManager().removeProviderConfig(providerId); | ||
| }; | ||
|
|
||
| export const getSecretProviderConfig = async ( | ||
| providerId: string | ||
| ): SecretsResultPromise<SecretProviderConfig | null> => { | ||
| return getSecretsManager().getProviderConfig(providerId); | ||
| }; | ||
|
|
||
| export const testSecretProviderConnection = async ( | ||
| providerId: string | ||
| ): SecretsResultPromise<boolean> => { | ||
| return getSecretsManager().testProviderConnection(providerId); | ||
| }; | ||
|
|
||
| export const getSecretValue = async ( | ||
| providerId: string, | ||
| ref: SecretReference | ||
| ): SecretsResultPromise<SecretValue | null> => { | ||
| return getSecretsManager().getSecret(providerId, ref); | ||
| }; | ||
|
|
||
| export const getSecretValues = async ( | ||
| secrets: Array<{ providerId: string; ref: SecretReference }> | ||
| ): SecretsResultPromise<SecretValue[]> => { | ||
| return getSecretsManager().getSecrets(secrets); | ||
| }; | ||
|
|
||
| export const refreshSecrets = async ( | ||
| providerId: string | ||
| ): SecretsResultPromise<(SecretValue | null)[]> => { | ||
| return getSecretsManager().refreshSecrets(providerId); | ||
| }; | ||
|
|
||
| export const listSecretProviders = async (): SecretsResultPromise< | ||
| SecretProviderMetadata[] | ||
| > => { | ||
| return getSecretsManager().listProviders(); | ||
| }; | ||
8 changes: 6 additions & 2 deletions
8
src/lib/secretsManager/providerRegistry/AbstractProviderRegistry.ts
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning
null as anycauses silent runtime crashes.When
SecretsManager.isInitialized()returns false, this function returnsnullcast toSecretsManager. All exported functions then call methods on this null value (e.g.,getSecretsManager().onProvidersChange(callback)), causing NPEs at runtime with confusing error messages.Consider throwing an explicit error to fail fast with a clear message:
🐛 Proposed fix
const getSecretsManager = (): SecretsManager => { if (!SecretsManager.isInitialized()) { - return null as any; + throw new Error("SecretsManager is not initialized. Call initSecretsManager() first."); } return SecretsManager.getInstance(); };📝 Committable suggestion
🤖 Prompt for AI Agents