forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: use API as secrets storage #1771
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
Merged
Merged
Changes from all commits
Commits
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
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
180 changes: 85 additions & 95 deletions
180
src/components/shared/SecretsManagement/secretsStorage.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,110 @@ | ||
| import { | ||
| createSecretApiSecretsPost, | ||
| deleteSecretApiSecretsSecretNameDelete, | ||
| listSecretsApiSecretsGet, | ||
| updateSecretApiSecretsSecretNamePut, | ||
| } from "@/api/sdk.gen"; | ||
|
|
||
| import type { Secret } from "./types"; | ||
|
|
||
| /** | ||
| * In-memory mocked storage for secrets. | ||
| * This will be replaced with an async API storage layer later. | ||
| * Parses a date string, assuming UTC if no timezone is specified. | ||
| * Handles cases where the server may or may not include timezone info. | ||
| */ | ||
| function parseAsUtc(dateString: string): Date { | ||
| // Already has UTC indicator | ||
| if (dateString.endsWith("Z")) { | ||
| return new Date(dateString); | ||
| } | ||
|
|
||
| // In-memory storage | ||
| const secretsStore = new Map<string, Secret>(); | ||
|
|
||
| // Subscribers for reactive updates | ||
| type Subscriber = () => void; | ||
| const subscribers = new Set<Subscriber>(); | ||
|
|
||
| function notifySubscribers() { | ||
| subscribers.forEach((callback) => callback()); | ||
| } | ||
| // Has positive timezone offset (e.g., +05:00) | ||
| if (dateString.includes("+")) { | ||
| return new Date(dateString); | ||
| } | ||
|
|
||
| /** | ||
| * Generates a unique ID for a new secret. | ||
| */ | ||
| function generateId(): string { | ||
| return `secret_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`; | ||
| } | ||
| // Check for negative timezone offset by looking for - after the date portion | ||
| // ISO date format: YYYY-MM-DD (positions 0-9), so any - after index 9 is timezone | ||
| if (dateString.lastIndexOf("-") > 9) { | ||
| return new Date(dateString); | ||
| } | ||
|
|
||
| /** | ||
| * Gets all secrets from the store. | ||
| * Returns a promise to simulate async API behavior. | ||
| */ | ||
| export async function getSecrets(): Promise<Secret[]> { | ||
| // Simulate network delay | ||
| await new Promise((resolve) => setTimeout(resolve, 50)); | ||
| return Array.from(secretsStore.values()).sort( | ||
| (a, b) => b.createdAt.getTime() - a.createdAt.getTime(), | ||
| ); | ||
| // No timezone info detected, assume UTC | ||
| return new Date(dateString + "Z"); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a new secret to the store. | ||
| */ | ||
| export async function addSecret(name: string, value: string): Promise<Secret> { | ||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
| export async function fetchSecretsList() { | ||
| const response = await listSecretsApiSecretsGet(); | ||
|
|
||
| // Check for duplicate names | ||
| const existing = Array.from(secretsStore.values()).find( | ||
| (s) => s.name === name, | ||
| ); | ||
| if (existing) { | ||
| throw new Error(`A secret with name "${name}" already exists`); | ||
| if (response.response.status !== 200) { | ||
| throw new Error(`Failed to fetch secrets: ${response.response.body}`); | ||
| } | ||
|
|
||
| const secret: Secret = { | ||
| id: generateId(), | ||
| name, | ||
| value, | ||
| createdAt: new Date(), | ||
| }; | ||
|
|
||
| secretsStore.set(secret.id, secret); | ||
| notifySubscribers(); | ||
|
|
||
| return secret; | ||
| return ( | ||
| response.data?.secrets.map( | ||
| (secret) => | ||
| ({ | ||
| id: secret.secret_name, | ||
| name: secret.secret_name, | ||
| createdAt: parseAsUtc(secret.created_at), | ||
| updatedAt: parseAsUtc(secret.updated_at), | ||
| expiresAt: secret.expires_at | ||
| ? parseAsUtc(secret.expires_at) | ||
| : undefined, | ||
| description: secret.description ?? undefined, | ||
| }) satisfies Secret, | ||
| ) ?? [] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Updates an existing secret. | ||
| */ | ||
| export async function updateSecret( | ||
| id: string, | ||
| updates: Partial<Pick<Secret, "name" | "value">>, | ||
| ): Promise<Secret> { | ||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
|
|
||
| const existing = secretsStore.get(id); | ||
| if (!existing) { | ||
| throw new Error(`Secret with id "${id}" not found`); | ||
| } | ||
|
|
||
| // Check for name conflicts if name is being updated | ||
| if (updates.name && updates.name !== existing.name) { | ||
| const nameConflict = Array.from(secretsStore.values()).find( | ||
| (s) => s.name === updates.name && s.id !== id, | ||
| ); | ||
| if (nameConflict) { | ||
| throw new Error(`A secret with name "${updates.name}" already exists`); | ||
| } | ||
| secretId: string, | ||
| secret: Partial<Secret> & Pick<Secret, "value">, | ||
| ) { | ||
| const response = await updateSecretApiSecretsSecretNamePut({ | ||
| path: { | ||
| secret_name: secretId, | ||
| }, | ||
| body: { | ||
| secret_value: secret.value ?? "", | ||
| }, | ||
| }); | ||
|
|
||
| if (response.response.status !== 200) { | ||
| throw new Error(`Failed to update secret: ${response.response.body}`); | ||
| } | ||
|
|
||
| const updated: Secret = { | ||
| ...existing, | ||
| ...updates, | ||
| }; | ||
| return true; | ||
| } | ||
|
|
||
| secretsStore.set(id, updated); | ||
| notifySubscribers(); | ||
| export async function addSecret( | ||
| secret: Partial<Secret> & Pick<Secret, "name" | "value">, | ||
| ) { | ||
| const response = await createSecretApiSecretsPost({ | ||
| query: { | ||
| secret_name: secret.name ?? "", | ||
| }, | ||
| body: { | ||
| secret_value: secret.value ?? "", | ||
| }, | ||
| }); | ||
|
|
||
| if (response.response.status !== 200) { | ||
| throw new Error(`Failed to add secret: ${response.response.body}`); | ||
| } | ||
|
|
||
| return updated; | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Removes a secret from the store. | ||
| */ | ||
| export async function removeSecret(id: string): Promise<void> { | ||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
| export async function removeSecret(secretId: string) { | ||
| const response = await deleteSecretApiSecretsSecretNameDelete({ | ||
| path: { | ||
| secret_name: secretId, | ||
| }, | ||
| }); | ||
|
|
||
| if (!secretsStore.has(id)) { | ||
| throw new Error(`Secret with id "${id}" not found`); | ||
| if (response.response.status !== 200) { | ||
| throw new Error(`Failed to remove secret: ${response.response.body}`); | ||
| } | ||
|
|
||
| secretsStore.delete(id); | ||
| notifySubscribers(); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Query keys for React Query. | ||
| */ | ||
| export const SecretsQueryKeys = { | ||
| All: () => ["secrets"] as const, | ||
| Id: (id: string) => ["secrets", id] as const, | ||
| } as const; |
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.