-
Notifications
You must be signed in to change notification settings - Fork 0
TA-4530: Implement secure secret storage #323
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
Laberion Ajvazi (LaberionAjvazi)
wants to merge
10
commits into
master
Choose a base branch
from
LaberionAjvazi/ta-4530
base: master
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
10 commits
Select commit
Hold shift + click to select a range
dc8f086
TA-4530: Implement secure secret storage
LaberionAjvazi b73a2d7
TA-4530: Lazy load keytar
LaberionAjvazi e0e4142
TA-4530: Remove unnecessary try/catch
LaberionAjvazi 9fb969d
TA-4530: Remove async executor
LaberionAjvazi 93c42a2
TA-4530: Add tests for secret storage service
LaberionAjvazi faa55a4
TA-4530: Prevent mutating profile in context
LaberionAjvazi 52a7225
TA-4530: Fix tests
LaberionAjvazi 5aff7cd
TA-4530: Add lock file, inject service
LaberionAjvazi 210ec4b
TA-4530: Replace atom/node-keytar with @github/keytar and add profile…
LaberionAjvazi 53a77d6
TA-4530: Add try-catch around findCredentials in getSecrets
LaberionAjvazi 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
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,105 @@ | ||
| import { Profile, ProfileSecrets } from "./profile.interface"; | ||
| import { logger } from "../utils/logger"; | ||
|
|
||
| enum PROFILE_SECRET_TYPE { | ||
| API_TOKEN = "apiToken", | ||
| CLIENT_SECRET = "clientSecret", | ||
| REFRESH_TOKEN = "refreshToken", | ||
| } | ||
|
|
||
| const CONTENT_CLI_SERVICE_NAME = "celonis-content-cli"; | ||
|
|
||
| // Lazy load keytar to handle cases where native dependencies are not available | ||
| let keytar: any = null; | ||
| let keytarLoadError: Error | null = null; | ||
|
|
||
| function getKeytar(): any { | ||
| if (keytar !== null) { | ||
| return keytar; | ||
| } | ||
| if (keytarLoadError !== null) { | ||
| return null; | ||
| } | ||
| try { | ||
| keytar = require("@github/keytar"); | ||
| return keytar; | ||
| } catch (error) { | ||
| keytarLoadError = error as Error; | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export class SecureSecretStorageService { | ||
|
|
||
| public async storeSecrets(profile: Profile): Promise<boolean> { | ||
| let secretsStoredInKeychain = true; | ||
| const secretEntries = [ | ||
| { type: PROFILE_SECRET_TYPE.API_TOKEN, value: profile.apiToken }, | ||
| { type: PROFILE_SECRET_TYPE.CLIENT_SECRET, value: profile.clientSecret }, | ||
| { type: PROFILE_SECRET_TYPE.REFRESH_TOKEN, value: profile.refreshToken }, | ||
| ]; | ||
|
|
||
| for (const secretEntry of secretEntries) { | ||
| if (secretEntry.value) { | ||
| const stored = await this.storeSecret( | ||
| this.getSecretServiceName(profile.name), | ||
| secretEntry.type, | ||
| secretEntry.value | ||
| ); | ||
| if (!stored) { | ||
| secretsStoredInKeychain = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!secretsStoredInKeychain) { | ||
| logger.warn("⚠️ Failed to store secrets securely. They will be stored in plain text file."); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public async getSecrets(profileName: string): Promise<ProfileSecrets | undefined> { | ||
| const keytarModule = getKeytar(); | ||
| if (!keytarModule) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| const secrets = await keytarModule.findCredentials(this.getSecretServiceName(profileName)); | ||
|
|
||
| if (!secrets.length) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const profileSecrets = {}; | ||
|
|
||
| for (const secret of secrets) { | ||
| profileSecrets[secret.account] = secret.password | ||
| } | ||
|
|
||
| return profileSecrets as ProfileSecrets; | ||
| } catch (err) { | ||
| return undefined; | ||
| } | ||
|
Check warning on line 85 in src/core/profile/secret-storage.service.ts
|
||
| } | ||
|
|
||
| private getSecretServiceName(profileName: string): string { | ||
| return `${CONTENT_CLI_SERVICE_NAME}:${profileName}`; | ||
| } | ||
|
|
||
| private async storeSecret(service: string, account: string, secret: string): Promise<boolean> { | ||
| const keytarModule = getKeytar(); | ||
| if (!keytarModule) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| await keytarModule.setPassword(service, account, secret); | ||
| return true; | ||
| } catch (err) { | ||
| return false; | ||
| } | ||
|
Check warning on line 103 in src/core/profile/secret-storage.service.ts
|
||
| } | ||
| } | ||
Oops, something went wrong.
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.