From c291bfcf304f7961475822e7ceae99ee6ec601cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Gran=C3=A1t?= Date: Mon, 20 Jan 2025 15:51:03 +0100 Subject: [PATCH] feat: allow api key in config (#148) --- schema.json | 4 ++++ src/cli.ts | 2 +- src/schema.d.ts | 9 +++++++++ test/e2e/login.test.ts | 45 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/schema.json b/schema.json index efd54d56..30c8b9db 100644 --- a/schema.json +++ b/schema.json @@ -9,6 +9,10 @@ "description": "The url of Tolgee API.", "type": "string" }, + "apiKey": { + "description": "Api key to Tolgee Platform.\n\nWARNING: Make sure you don't leak your API key\nUse `apiKey` only if you are loading it from an environment or other secured source (supported in .js or .yml files) or your config is not public.\n\nIn most cases, it's better to use a one-time `login` command or set it via the `TOLGEE_API_KEY` environment variable.", + "type": "string" + }, "format": { "$ref": "#/$defs/format" }, diff --git a/src/cli.ts b/src/cli.ts index fec96d0c..648bce36 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -166,7 +166,7 @@ async function run() { program.addOption(VERBOSE); program.addOption(CONFIG_OPT); program.addOption(API_URL_OPT.default(config.apiUrl ?? DEFAULT_API_URL)); - program.addOption(API_KEY_OPT); + program.addOption(API_KEY_OPT.default(config.apiKey)); program.addOption(PROJECT_ID_OPT.default(config.projectId ?? -1)); program.addOption(FORMAT_OPT.default(config.format ?? 'JSON_TOLGEE')); program.addOption(EXTRACTOR.default(config.extractor)); diff --git a/src/schema.d.ts b/src/schema.d.ts index f7223a80..4499beb5 100644 --- a/src/schema.d.ts +++ b/src/schema.d.ts @@ -63,6 +63,15 @@ export interface Schema { * The url of Tolgee API. */ apiUrl?: string; + /** + * Api key to Tolgee Platform. + * + * WARNING: Make sure you don't leak your API key + * Use `apiKey` only if you are loading it from an environment or other secured source (supported in .js or .yml files) or your config is not public. + * + * In most cases, it's better to use a one-time `login` command or set it via the `TOLGEE_API_KEY` environment variable. + */ + apiKey?: string; format?: Format; /** * A path to a custom extractor to use instead of the default one. diff --git a/test/e2e/login.test.ts b/test/e2e/login.test.ts index f5e94282..da261720 100644 --- a/test/e2e/login.test.ts +++ b/test/e2e/login.test.ts @@ -1,6 +1,6 @@ import { tmpdir } from 'os'; import { join } from 'path'; -import { rm, readFile } from 'fs/promises'; +import { rm, readFile, mkdtemp, writeFile } from 'fs/promises'; import { run } from './utils/run.js'; import { TolgeeClient } from '#cli/client/TolgeeClient.js'; import { PROJECT_1 } from './utils/api/project1.js'; @@ -10,6 +10,7 @@ import { createProjectWithClient, deleteProject, } from './utils/api/common.js'; +import { Schema } from '#cli/schema.js'; const AUTH_FILE_PATH = join(tmpdir(), '.tolgee-e2e', 'authentication.json'); @@ -86,4 +87,46 @@ describe('Project 1', () => { const authFile = await readFile(AUTH_FILE_PATH, 'utf8'); expect(authFile).not.toContain(pat); }); + + it('allows to specify api key through config', async () => { + const pak = await createPak(client); + const { tempFolder, configFile } = await createTmpFolderWithConfig({ + apiKey: pak, + pull: { + path: './data', + }, + }); + const englishFile = join(tempFolder, 'data', 'en.json'); + + const out = await run(['-c', configFile, 'pull']); + + expect(out.code).toBe(0); + expect(out.stderr).toBe(''); + + const enFile = JSON.parse((await readFile(englishFile)).toString()); + expect(enFile.controller).toEqual('Controller'); + }); + + it('ignores login, when apiKey defined in config', async () => { + const pak = await createPak(client); + const pat = await createPat(client); + const loginOut = await run(['login', pat]); + expect(loginOut.code).toBe(0); + + const { configFile } = await createTmpFolderWithConfig({ + apiKey: pak, + pull: { + path: './data', + }, + }); + const out = await run(['-c', configFile, 'pull']); + expect(out.code).toBe(0); + }); + + async function createTmpFolderWithConfig(config: Schema) { + const tempFolder = await mkdtemp(join(tmpdir(), 'cli-project-')); + const configFile = join(tempFolder, '.tolgeerc.json'); + await writeFile(configFile, JSON.stringify(config, null, 2)); + return { tempFolder, configFile }; + } });