-
Notifications
You must be signed in to change notification settings - Fork 11.7k
feat(policy): implement project-level policy support #18682
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
+1,896
−187
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
be43c86
feat(policy): implement project-level policy support
Abhijit-2592 ccae4a1
docs(policy): document project-level policy support
Abhijit-2592 d84a33f
feat(policy): change priority hierarchy to Admin > User > Project > D…
Abhijit-2592 3ef8cfc
fix: Update test expectations to match createPolicyEngineConfig signa…
Abhijit-2592 7166748
feat(policy): implement project policy integrity verification
Abhijit-2592 3a2fd2a
fix(policy): refactor policy dialog to remove process.exit and fix in…
Abhijit-2592 b9245da
test(cli): improve project policy config test coverage
Abhijit-2592 a4143be
refactor(policy): rename "Project" policies to "Workspace" policies
Abhijit-2592 6206862
feat(policy): implement hot-reloading for workspace policies
Abhijit-2592 e0c02a9
refactor(policy): consolidate workspacePoliciesDir into PolicySettings
Abhijit-2592 bbb97fc
refactor(cli): abstract workspace policy resolution logic
Abhijit-2592 5a37934
fix(cli): remove unused PolicyIntegrityManager import in AppContainer
Abhijit-2592 51c9deb
test(cli): update createPolicyEngineConfig mock expectations
Abhijit-2592 ed6a20d
feat(policy): address review feedback and improve project-level polic…
Abhijit-2592 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import * as os from 'node:os'; | ||
| import { resolveWorkspacePolicyState } from './policy.js'; | ||
| import { writeToStderr } from '@google/gemini-cli-core'; | ||
|
|
||
| // Mock debugLogger to avoid noise in test output | ||
| vi.mock('@google/gemini-cli-core', async (importOriginal) => { | ||
| const actual = | ||
| await importOriginal<typeof import('@google/gemini-cli-core')>(); | ||
| return { | ||
| ...actual, | ||
| debugLogger: { | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| debug: vi.fn(), | ||
| }, | ||
| writeToStderr: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('resolveWorkspacePolicyState', () => { | ||
| let tempDir: string; | ||
| let workspaceDir: string; | ||
| let policiesDir: string; | ||
|
|
||
| beforeEach(() => { | ||
| // Create a temporary directory for the test | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gemini-cli-test-')); | ||
| // Redirect GEMINI_CLI_HOME to the temp directory to isolate integrity storage | ||
| vi.stubEnv('GEMINI_CLI_HOME', tempDir); | ||
|
|
||
| workspaceDir = path.join(tempDir, 'workspace'); | ||
| fs.mkdirSync(workspaceDir); | ||
| policiesDir = path.join(workspaceDir, '.gemini', 'policies'); | ||
|
|
||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Clean up temporary directory | ||
| fs.rmSync(tempDir, { recursive: true, force: true }); | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it('should return empty state if folder is not trusted', async () => { | ||
| const result = await resolveWorkspacePolicyState({ | ||
| cwd: workspaceDir, | ||
| trustedFolder: false, | ||
| interactive: true, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ | ||
| workspacePoliciesDir: undefined, | ||
| policyUpdateConfirmationRequest: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it('should return policy directory if integrity matches', async () => { | ||
| // Set up policies directory with a file | ||
| fs.mkdirSync(policiesDir, { recursive: true }); | ||
| fs.writeFileSync(path.join(policiesDir, 'policy.toml'), 'rules = []'); | ||
|
|
||
| // First call to establish integrity (interactive accept) | ||
| const firstResult = await resolveWorkspacePolicyState({ | ||
| cwd: workspaceDir, | ||
| trustedFolder: true, | ||
| interactive: true, | ||
| }); | ||
| expect(firstResult.policyUpdateConfirmationRequest).toBeDefined(); | ||
|
|
||
| // Establish integrity manually as if accepted | ||
| const { PolicyIntegrityManager } = await import('@google/gemini-cli-core'); | ||
| const integrityManager = new PolicyIntegrityManager(); | ||
| await integrityManager.acceptIntegrity( | ||
| 'workspace', | ||
| workspaceDir, | ||
| firstResult.policyUpdateConfirmationRequest!.newHash, | ||
| ); | ||
|
|
||
| // Second call should match | ||
| const result = await resolveWorkspacePolicyState({ | ||
| cwd: workspaceDir, | ||
| trustedFolder: true, | ||
| interactive: true, | ||
| }); | ||
|
|
||
| expect(result.workspacePoliciesDir).toBe(policiesDir); | ||
| expect(result.policyUpdateConfirmationRequest).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined if integrity is NEW but fileCount is 0', async () => { | ||
| const result = await resolveWorkspacePolicyState({ | ||
| cwd: workspaceDir, | ||
| trustedFolder: true, | ||
| interactive: true, | ||
| }); | ||
|
|
||
| expect(result.workspacePoliciesDir).toBeUndefined(); | ||
| expect(result.policyUpdateConfirmationRequest).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return confirmation request if changed in interactive mode', async () => { | ||
| fs.mkdirSync(policiesDir, { recursive: true }); | ||
| fs.writeFileSync(path.join(policiesDir, 'policy.toml'), 'rules = []'); | ||
|
|
||
| const result = await resolveWorkspacePolicyState({ | ||
| cwd: workspaceDir, | ||
| trustedFolder: true, | ||
| interactive: true, | ||
| }); | ||
|
|
||
| expect(result.workspacePoliciesDir).toBeUndefined(); | ||
| expect(result.policyUpdateConfirmationRequest).toEqual({ | ||
| scope: 'workspace', | ||
| identifier: workspaceDir, | ||
| policyDir: policiesDir, | ||
| newHash: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| it('should warn and auto-accept if changed in non-interactive mode', async () => { | ||
| fs.mkdirSync(policiesDir, { recursive: true }); | ||
| fs.writeFileSync(path.join(policiesDir, 'policy.toml'), 'rules = []'); | ||
|
|
||
| const result = await resolveWorkspacePolicyState({ | ||
| cwd: workspaceDir, | ||
| trustedFolder: true, | ||
| interactive: false, | ||
| }); | ||
|
|
||
| expect(result.workspacePoliciesDir).toBe(policiesDir); | ||
| expect(result.policyUpdateConfirmationRequest).toBeUndefined(); | ||
| expect(writeToStderr).toHaveBeenCalledWith( | ||
| expect.stringContaining('Automatically accepting and loading'), | ||
| ); | ||
| }); | ||
| }); |
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.