diff options
| author | Bryan Morgan <[email protected]> | 2025-06-29 16:35:20 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-29 20:35:20 +0000 |
| commit | cdb803b9a431128a851a4da82edaa82494ac6215 (patch) | |
| tree | bb281f50f41ff68cde421f78de622e220eeefb6e /packages/core/src/utils | |
| parent | dbe63e7234b0ea2577f84aab774ef011d300745f (diff) | |
Added obfuscated google account ID to clearcut log messages (#2593)
Diffstat (limited to 'packages/core/src/utils')
| -rw-r--r-- | packages/core/src/utils/user_id.test.ts | 48 | ||||
| -rw-r--r-- | packages/core/src/utils/user_id.ts | 55 |
2 files changed, 87 insertions, 16 deletions
diff --git a/packages/core/src/utils/user_id.test.ts b/packages/core/src/utils/user_id.test.ts new file mode 100644 index 00000000..81b99ef4 --- /dev/null +++ b/packages/core/src/utils/user_id.test.ts @@ -0,0 +1,48 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect } from 'vitest'; +import { getInstallationId, getObfuscatedGoogleAccountId } from './user_id.js'; + +describe('user_id', () => { + describe('getInstallationId', () => { + it('should return a valid UUID format string', () => { + const installationId = getInstallationId(); + + expect(installationId).toBeDefined(); + expect(typeof installationId).toBe('string'); + expect(installationId.length).toBeGreaterThan(0); + + // Should return the same ID on subsequent calls (consistent) + const secondCall = getInstallationId(); + expect(secondCall).toBe(installationId); + }); + }); + + describe('getObfuscatedGoogleAccountId', () => { + it('should return a non-empty string', () => { + const result = getObfuscatedGoogleAccountId(); + + expect(result).toBeDefined(); + expect(typeof result).toBe('string'); + expect(result.length).toBeGreaterThan(0); + + // Should be consistent on subsequent calls + const secondCall = getObfuscatedGoogleAccountId(); + expect(secondCall).toBe(result); + }); + + it('should return the same as installation ID when no Google Account ID is cached', () => { + // In a clean test environment, there should be no cached Google Account ID + // so getObfuscatedGoogleAccountId should fall back to installation ID + const googleAccountIdResult = getObfuscatedGoogleAccountId(); + const installationIdResult = getInstallationId(); + + // They should be the same when no Google Account ID is cached + expect(googleAccountIdResult).toBe(installationIdResult); + }); + }); +}); diff --git a/packages/core/src/utils/user_id.ts b/packages/core/src/utils/user_id.ts index 5db080a4..4f17bf96 100644 --- a/packages/core/src/utils/user_id.ts +++ b/packages/core/src/utils/user_id.ts @@ -12,7 +12,7 @@ import { GEMINI_DIR } from './paths.js'; const homeDir = os.homedir() ?? ''; const geminiDir = path.join(homeDir, GEMINI_DIR); -const userIdFile = path.join(geminiDir, 'user_id'); +const installationIdFile = path.join(geminiDir, 'installation_id'); function ensureGeminiDirExists() { if (!fs.existsSync(geminiDir)) { @@ -20,39 +20,62 @@ function ensureGeminiDirExists() { } } -function readUserIdFromFile(): string | null { - if (fs.existsSync(userIdFile)) { - const userId = fs.readFileSync(userIdFile, 'utf-8').trim(); - return userId || null; +function readInstallationIdFromFile(): string | null { + if (fs.existsSync(installationIdFile)) { + const installationid = fs.readFileSync(installationIdFile, 'utf-8').trim(); + return installationid || null; } return null; } -function writeUserIdToFile(userId: string) { - fs.writeFileSync(userIdFile, userId, 'utf-8'); +function writeInstallationIdToFile(installationId: string) { + fs.writeFileSync(installationIdFile, installationId, 'utf-8'); } /** - * Retrieves the persistent user ID from a file, creating it if it doesn't exist. - * This ID is used for unique user tracking. + * Retrieves the installation ID from a file, creating it if it doesn't exist. + * This ID is used for unique user installation tracking. * @returns A UUID string for the user. */ -export function getPersistentUserId(): string { +export function getInstallationId(): string { try { ensureGeminiDirExists(); - let userId = readUserIdFromFile(); + let installationId = readInstallationIdFromFile(); - if (!userId) { - userId = randomUUID(); - writeUserIdToFile(userId); + if (!installationId) { + installationId = randomUUID(); + writeInstallationIdToFile(installationId); } - return userId; + return installationId; } catch (error) { console.error( - 'Error accessing persistent user ID file, generating ephemeral ID:', + 'Error accessing installation ID file, generating ephemeral ID:', error, ); return '123456789'; } } + +/** + * Retrieves the obfuscated Google Account ID for the currently authenticated user. + * When OAuth is available, returns the user's cached Google Account ID. Otherwise, returns the installation ID. + * @returns A string ID for the user (Google Account ID if available, otherwise installation ID). + */ +export function getObfuscatedGoogleAccountId(): string { + // Try to get cached Google Account ID first + try { + // Dynamically import to avoid circular dependencies + // eslint-disable-next-line @typescript-eslint/no-require-imports, no-restricted-syntax + const { getCachedGoogleAccountId } = require('../code_assist/oauth2.js'); + const googleAccountId = getCachedGoogleAccountId(); + if (googleAccountId) { + return googleAccountId; + } + } catch (_error) { + // If there's any error accessing Google Account ID, fall back to installation ID + } + + // Fall back to installation ID when no Google Account ID is cached or on error + return getInstallationId(); +} |
