-
Notifications
You must be signed in to change notification settings - Fork 0
TA-4530: Implement secure secret storage #301
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
LaberionAjvazi
wants to merge
8
commits into
master
Choose a base branch
from
TA-4530-secure-secrets-storage
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
8 commits
Select commit
Hold shift + click to select a range
04f9677
TA-4530: Implement secure secret storage
LaberionAjvazi 1515972
TA-4530: Lazy load keytar
LaberionAjvazi baf40df
TA-4530: Remove unnecessary try/catch
LaberionAjvazi 5541bf3
TA-4530: Remove async executor
LaberionAjvazi 16402e1
TA-4530: Add tests for secret storage service
LaberionAjvazi 0ff7751
TA-4530: Prevent mutating profile in context
LaberionAjvazi 0b9a93a
TA-4530: Fix tests
LaberionAjvazi 9c60ea8
TA-4530: Add lock file, inject service
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| 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("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; | ||
LaberionAjvazi marked this conversation as resolved.
Show resolved
Hide resolved
ZgjimHaziri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return true; | ||
| } | ||
LaberionAjvazi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public async getSecrets(profileName: string): Promise<ProfileSecrets | undefined> { | ||
| const keytarModule = getKeytar(); | ||
| if (!keytarModule) { | ||
| return undefined; | ||
| } | ||
|
|
||
| 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; | ||
LaberionAjvazi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
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.
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.
I see you mentioned a migration solution being intentionally skipped in the PR description, but I think it would be very straightforward to enable users on it without re-create. Something like
content-cli profile secure <profile>and re-use logic the already implemented logic from this PR.