Skip to content

Commit

Permalink
fix(cli): auth file should be chmod 600 (#6925)
Browse files Browse the repository at this point in the history
* wip new tests

* test for auth file mode

* check perms internally

* chore: lint
  • Loading branch information
etnoy committed Feb 5, 2024
1 parent 6ed33da commit ce6dc3b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
32 changes: 15 additions & 17 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions cli/src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export abstract class BaseCommand {
protected user!: UserResponseDto;
protected serverVersion!: ServerVersionResponseDto;

constructor(options: { config?: string }) {
if (!options.config) {
constructor(options: { configDirectory?: string }) {
if (!options.configDirectory) {
throw new Error('Config directory is required');
}
this.sessionService = new SessionService(options.config);
this.sessionService = new SessionService(options.configDirectory);
}

public async connect(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/services/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class SessionService {
}
}

await writeFile(this.authPath, yaml.stringify({ instanceUrl, apiKey }));
await writeFile(this.authPath, yaml.stringify({ instanceUrl, apiKey }), { mode: 0o600 });

console.log('Wrote auth info to ' + this.authPath);

Expand Down
2 changes: 1 addition & 1 deletion cli/test/cli-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const TEST_AUTH_FILE = path.join(TEST_CONFIG_DIR, 'auth.yml');
export const TEST_IMMICH_INSTANCE_URL = 'https://test/api';
export const TEST_IMMICH_API_KEY = 'pNussssKSYo5WasdgalvKJ1n9kdvaasdfbluPg';

export const CLI_BASE_OPTIONS = { config: TEST_CONFIG_DIR };
export const CLI_BASE_OPTIONS = { configDirectory: TEST_CONFIG_DIR };

export const setup = async () => {
const api = new ImmichApi(process.env.IMMICH_INSTANCE_URL as string, '');
Expand Down
26 changes: 25 additions & 1 deletion cli/test/e2e/login-key.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { restoreTempFolder, testApp } from '@test-utils';
import { CLI_BASE_OPTIONS, setup, spyOnConsole } from 'test/cli-test-utils';
import { CLI_BASE_OPTIONS, TEST_AUTH_FILE, deleteAuthFile, setup, spyOnConsole } from 'test/cli-test-utils';
import { readFile, stat } from 'node:fs/promises';
import { LoginCommand } from '../../src/commands/login';
import yaml from 'yaml';

describe(`login-key (e2e)`, () => {
let apiKey: string;
Expand All @@ -20,6 +22,7 @@ describe(`login-key (e2e)`, () => {
afterAll(async () => {
await testApp.teardown();
await restoreTempFolder();
deleteAuthFile();
});

beforeEach(async () => {
Expand All @@ -28,6 +31,8 @@ describe(`login-key (e2e)`, () => {

const api = await setup();
apiKey = api.apiKey;

deleteAuthFile();
});

it('should error when providing an invalid API key', async () => {
Expand All @@ -39,4 +44,23 @@ describe(`login-key (e2e)`, () => {
it('should log in when providing the correct API key', async () => {
await new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, apiKey);
});

it('should create an auth file when logging in', async () => {
await new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, apiKey);

const data: string = await readFile(TEST_AUTH_FILE, 'utf8');
const parsedConfig = yaml.parse(data);

expect(parsedConfig).toEqual(expect.objectContaining({ instanceUrl, apiKey }));
});

it('should create an auth file with chmod 600', async () => {
await new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, apiKey);

const stats = await stat(TEST_AUTH_FILE);

const mode = (stats.mode & 0o777).toString(8);

expect(mode).toEqual('600');
});
});

0 comments on commit ce6dc3b

Please sign in to comment.