Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ Set up or update your Kernel API key and Cronometer credentials. If credentials
crono login
```

**Headless / container deployments:** all credentials can be provided via environment variables instead of `crono login`. Env vars take precedence over stored credentials.

| Variable | Credential |
| --------------------------- | ------------------- |
| `KERNEL_API_KEY` | Kernel.sh API key |
| `CRONO_CRONOMETER_USERNAME` | Cronometer email |
| `CRONO_CRONOMETER_PASSWORD` | Cronometer password |

### `crono quick-add`

Add a quick macro entry to your Cronometer diary.
Expand Down
13 changes: 13 additions & 0 deletions src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ export type CredentialKey =

const SERVICE_NAME = "crono";

/**
* Environment variables that override stored credentials.
* Useful for headless/container deployments where `crono login`
* cannot be run interactively.
*/
const ENV_VARS: Record<CredentialKey, string> = {
"kernel-api-key": "KERNEL_API_KEY",
"cronometer-username": "CRONO_CRONOMETER_USERNAME",
"cronometer-password": "CRONO_CRONOMETER_PASSWORD",
};

let configDirOverride: string | null = null;

function getConfigDir(): string {
Expand Down Expand Up @@ -174,6 +185,8 @@ function fileDelete(key: string): void {
// -- Public API --

export function getCredential(key: CredentialKey): string | null {
const envValue = process.env[ENV_VARS[key]];
if (envValue) return envValue;
return getBackend() === "keyring" ? keyringGet(key) : fileGet(key);
}

Expand Down
5 changes: 4 additions & 1 deletion src/cronometer/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export async function exportData(
const password = getCredential("cronometer-password");

if (!username || !password) {
throw new Error("No Cronometer credentials found. Run: crono login");
throw new Error(
"No Cronometer credentials found. Run `crono login`, or set:\n" +
" CRONO_CRONOMETER_USERNAME and CRONO_CRONOMETER_PASSWORD"
);
}

// Resolve GWT overrides: env vars > config > defaults
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export type {
* Each operation creates a fresh browser and logs in.
*/
export async function getKernelClient(): Promise<KernelClient> {
const apiKey =
process.env["KERNEL_API_KEY"] ?? getCredential("kernel-api-key");
const apiKey = getCredential("kernel-api-key");
if (!apiKey) {
throw new Error(
"Kernel API key not found.\n" +
Expand Down
51 changes: 51 additions & 0 deletions tests/credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,54 @@ describe("credentials (encrypted file backend)", () => {
});
});
});

describe("credentials (environment variable overrides)", () => {
const ENV_KEYS = [
"KERNEL_API_KEY",
"CRONO_CRONOMETER_USERNAME",
"CRONO_CRONOMETER_PASSWORD",
];

beforeEach(() => {
mkdirSync(testDir, { recursive: true });
_resetBackend();
_setBackend("file");
_setConfigDir(testDir);
for (const key of ENV_KEYS) delete process.env[key];
});

afterEach(() => {
for (const key of ENV_KEYS) delete process.env[key];
_setConfigDir(null);
_resetBackend();
try {
rmSync(testDir, { recursive: true, force: true });
} catch {
// cleanup best-effort
}
});

it("should resolve credentials from env vars without stored values", () => {
process.env.KERNEL_API_KEY = "env-key";
process.env.CRONO_CRONOMETER_USERNAME = "env@example.com";
process.env.CRONO_CRONOMETER_PASSWORD = "env-password";

expect(getCredential("kernel-api-key")).toBe("env-key");
expect(getCredential("cronometer-username")).toBe("env@example.com");
expect(getCredential("cronometer-password")).toBe("env-password");
expect(hasCredentials()).toBe(true);
});

it("should prefer env vars over stored credentials", () => {
setCredential("cronometer-username", "stored@example.com");
process.env.CRONO_CRONOMETER_USERNAME = "env@example.com";

expect(getCredential("cronometer-username")).toBe("env@example.com");
});

it("should fall back to stored credentials when env var is unset", () => {
setCredential("cronometer-username", "stored@example.com");

expect(getCredential("cronometer-username")).toBe("stored@example.com");
});
});
Loading