Repository navigation
feat(secrets): repository secrets and variables for agent shells and terminals #432
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b431c12
test(e2e): start the E2E backend with only the variables the suite de…
e0331b4
feat(secrets): repository secrets and variables injected into agent s…
2884e68
feat(secrets): secrets and variables UI and env mapping in trust review
e46cf51
test(e2e): cover repository secret injection, masking and the Secrets…
a0d1d91
fix(secrets): target the scope index in the secrets upsert
chriswritescode-dev 3e51280
fix(secrets): forward only planned variables into the sandbox and cac…
chriswritescode-dev e3557f8
fix(secrets): separate global from assistant-repo scope and require a…
chriswritescode-dev 21c60ed
fix(secrets): collapse legacy scope duplicates on rollback and verify…
chriswritescode-dev 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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { Migration } from '../migration-runner' | ||
|
|
||
| const migration: Migration = { | ||
| id: '202610181400-secrets', | ||
| up(db) { | ||
| db.run(` | ||
| CREATE TABLE IF NOT EXISTS secrets ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| kind TEXT NOT NULL CHECK (kind IN ('secret','variable')), | ||
| repo_id INTEGER REFERENCES repos(id) ON DELETE CASCADE, | ||
| environment TEXT NOT NULL DEFAULT '', | ||
| name TEXT NOT NULL, | ||
| value TEXT NOT NULL, | ||
| created_at INTEGER NOT NULL, | ||
| updated_at INTEGER NOT NULL | ||
| ) | ||
| `) | ||
|
|
||
| db.run('CREATE UNIQUE INDEX IF NOT EXISTS idx_secrets_scope ON secrets(kind, COALESCE(repo_id, 0), environment, name)') | ||
| }, | ||
| down(db) { | ||
| db.run('DROP TABLE IF EXISTS secrets') | ||
| }, | ||
| } | ||
|
|
||
| export default migration |
57 changes: 57 additions & 0 deletions
57
backend/src/db/migrations/202610181410-secrets-global-scope-index.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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import type { Database } from 'bun:sqlite' | ||
| import type { Migration } from '../migration-runner' | ||
| import { GLOBAL_SECRETS_SCOPE_ID } from '../secrets-scope' | ||
|
|
||
| const LEGACY_GLOBAL_SCOPE_ID = 0 | ||
|
|
||
| function rebuildScopeIndex(db: Database, globalScopeId: number): void { | ||
| db.run('DROP INDEX IF EXISTS idx_secrets_scope') | ||
| db.run( | ||
| `CREATE UNIQUE INDEX idx_secrets_scope ON secrets(kind, COALESCE(repo_id, ${globalScopeId}), environment, name)`, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the rows the legacy scope key cannot hold. | ||
| * | ||
| * The legacy index merged global rows (`repo_id IS NULL`) with the Assistant repository's scope | ||
| * (`repo_id = 0`), and the old upsert let the last write win, so the newest row of each merged | ||
| * group is the state legacy code would hold. Ordering by `updated_at`, then `id`, keeps that row | ||
| * and deletes the rest, which lets the legacy unique index be recreated. | ||
| */ | ||
| function collapseLegacyScopeDuplicates(db: Database): void { | ||
| db.run(` | ||
| DELETE FROM secrets | ||
| WHERE (repo_id IS NULL OR repo_id = ${LEGACY_GLOBAL_SCOPE_ID}) | ||
| AND EXISTS ( | ||
| SELECT 1 | ||
| FROM secrets AS newer | ||
| WHERE (newer.repo_id IS NULL OR newer.repo_id = ${LEGACY_GLOBAL_SCOPE_ID}) | ||
| AND newer.kind = secrets.kind | ||
| AND newer.environment = secrets.environment | ||
| AND newer.name = secrets.name | ||
| AND ( | ||
| newer.updated_at > secrets.updated_at | ||
| OR (newer.updated_at = secrets.updated_at AND newer.id > secrets.id) | ||
| ) | ||
| ) | ||
| `) | ||
| } | ||
|
|
||
| export const secretsGlobalScopeIndexMigration: Migration = { | ||
| id: '202610181410-secrets-global-scope-index', | ||
| up(db) { | ||
| rebuildScopeIndex(db, GLOBAL_SECRETS_SCOPE_ID) | ||
| }, | ||
| down(db) { | ||
| db.run('BEGIN TRANSACTION') | ||
| try { | ||
| collapseLegacyScopeDuplicates(db) | ||
| rebuildScopeIndex(db, LEGACY_GLOBAL_SCOPE_ID) | ||
| db.run('COMMIT') | ||
| } catch (error) { | ||
| db.run('ROLLBACK') | ||
| throw error | ||
| } | ||
| }, | ||
| } |
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,7 @@ | ||
| /** | ||
| * Repo scope stored in place of a NULL `repo_id` for global secrets, so the unique scope index | ||
| * and its conflict target treat every global entry as one scope while repository scopes keep | ||
| * their own. Real repo ids are `0` (the Assistant repository) or positive, so a negative | ||
| * sentinel can never collide with one. | ||
| */ | ||
| export const GLOBAL_SECRETS_SCOPE_ID = -1 | ||
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,22 @@ | ||
| import { Hono } from 'hono' | ||
| import type { Database } from 'bun:sqlite' | ||
| import { CredentialProvider } from '../../services/credential-provider' | ||
| import type { RepoEnvService } from '../../services/repo-env' | ||
|
|
||
| export function createInternalShellEnvRoutes(db: Database, repoEnvService: RepoEnvService) { | ||
| const app = new Hono() | ||
|
|
||
| app.get('/', async (c) => { | ||
| const provider = new CredentialProvider(db) | ||
| const cwd = c.req.query('cwd') | ||
| const ghEnv = provider.getGhCliEnv({ cwd }) | ||
| if (!cwd || c.req.query('sandboxed') === '1') { | ||
| return c.json({ env: ghEnv, mask: [] }) | ||
| } | ||
|
|
||
| const repoEnv = await repoEnvService.resolveForDirectory(cwd) | ||
| return c.json({ env: { ...ghEnv, ...repoEnv.env }, mask: repoEnv.maskValues }) | ||
| }) | ||
|
|
||
| return app | ||
| } |
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.
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.
Remove the code comment.
The constant and its uses must document the scope rule without a comment. As per coding guidelines, “Any code comments (code must be self-documenting)” must be flagged.
Source: Coding guidelines