From bc04129342f3b6363a54f107b49380a572d2f5ba Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 23 Jan 2025 15:03:23 -0500 Subject: [PATCH] feat: CLI options for adding and deleting users --- api/src/unraid-api/cli/cli.module.ts | 2 ++ .../cli/sso/add-sso-user.command.ts | 26 ++++++++++++----- .../cli/sso/list-sso-user.command.ts | 28 +++++++++++++++++++ .../cli/sso/remove-sso-user.command.ts | 19 +++++++++++++ api/src/unraid-api/cli/sso/sso.command.ts | 3 +- 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 api/src/unraid-api/cli/sso/list-sso-user.command.ts diff --git a/api/src/unraid-api/cli/cli.module.ts b/api/src/unraid-api/cli/cli.module.ts index cd2c901db..4fd6eb20c 100644 --- a/api/src/unraid-api/cli/cli.module.ts +++ b/api/src/unraid-api/cli/cli.module.ts @@ -19,6 +19,7 @@ import { SwitchEnvCommand } from '@app/unraid-api/cli/switch-env.command'; import { VersionCommand } from '@app/unraid-api/cli/version.command'; import { RemoveSSOUserCommand } from '@app/unraid-api/cli/sso/remove-sso-user.command'; import { RemoveSSOUserQuestionSet } from '@app/unraid-api/cli/sso/remove-sso-user.questions'; +import { ListSSOUserCommand } from '@app/unraid-api/cli/sso/list-sso-user.command'; @Module({ providers: [ @@ -26,6 +27,7 @@ import { RemoveSSOUserQuestionSet } from '@app/unraid-api/cli/sso/remove-sso-use AddSSOUserQuestionSet, RemoveSSOUserCommand, RemoveSSOUserQuestionSet, + ListSSOUserCommand, LogService, StartCommand, StopCommand, diff --git a/api/src/unraid-api/cli/sso/add-sso-user.command.ts b/api/src/unraid-api/cli/sso/add-sso-user.command.ts index 148e8d27f..a440f2e55 100644 --- a/api/src/unraid-api/cli/sso/add-sso-user.command.ts +++ b/api/src/unraid-api/cli/sso/add-sso-user.command.ts @@ -30,7 +30,7 @@ export class AddSSOUserCommand extends CommandRunner { async run(_input: string[], options: AddSSOUserCommandOptions): Promise { try { options = await this.inquirerService.prompt(AddSSOUserQuestionSet.name, options); - + console.log(options); if (options.disclaimer === 'y' && options.username) { await store.dispatch(loadConfigFile()); store.dispatch(addSsoUser(options.username)); @@ -47,18 +47,30 @@ export class AddSSOUserCommand extends CommandRunner { } @Option({ - flags: '--username [username]', + flags: '--username ', description: 'Cognito Username', }) - parseUsername(val: string) { - return val; + parseUsername(input: string) { + if ( + !/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(input) + ) { + throw new Error('Username must be in the format of a UUID (e.g., ${v4()}}\n'); + } + + return input; } @Option({ - flags: '--disclaimer [disclaimer]', + flags: '--disclaimer ', description: 'Disclaimer (y/n)', }) - parseDisclaimer(val: string) { - return val; + parseDisclaimer(input: string) { + if (!input || !['y', 'n'].includes(input.toLowerCase())) { + throw new Error('Please answer the diclaimer with (y/n)\n'); + } + if (input.toLowerCase() === 'n') { + process.exit(1); + } + return input; } } diff --git a/api/src/unraid-api/cli/sso/list-sso-user.command.ts b/api/src/unraid-api/cli/sso/list-sso-user.command.ts new file mode 100644 index 000000000..b9804b777 --- /dev/null +++ b/api/src/unraid-api/cli/sso/list-sso-user.command.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@nestjs/common'; + +import { CommandRunner, SubCommand } from 'nest-commander'; + +import { store } from '@app/store/index'; +import { loadConfigFile } from '@app/store/modules/config'; +import { LogService } from '@app/unraid-api/cli/log.service'; + +@Injectable() +@SubCommand({ + name: 'list-users', + aliases: ['list', 'l'], + description: 'List all users for SSO', +}) +export class ListSSOUserCommand extends CommandRunner { + constructor( + private readonly logger: LogService, + ) { + super(); + } + + async run(_input: string[]): Promise { + await store.dispatch(loadConfigFile()); + this.logger.info( + store.getState().config.remote.ssoSubIds.split(',').filter(Boolean).join('\n') + ); + } +} diff --git a/api/src/unraid-api/cli/sso/remove-sso-user.command.ts b/api/src/unraid-api/cli/sso/remove-sso-user.command.ts index 669c1e58b..39fb3adc2 100644 --- a/api/src/unraid-api/cli/sso/remove-sso-user.command.ts +++ b/api/src/unraid-api/cli/sso/remove-sso-user.command.ts @@ -36,4 +36,23 @@ export class RemoveSSOUserCommand extends CommandRunner { } writeConfigSync('flash'); } + + @Option({ + name: 'username', + flags: '--username ', + description: 'Cognito Username', + }) + parseUsername(input: string) { + if (!input) { + throw new Error('Username is required\n'); + } + + if ( + !/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(input) + ) { + throw new Error('Username must be in the format of a UUID (e.g., ${v4()}}\n'); + } + + return input; + } } diff --git a/api/src/unraid-api/cli/sso/sso.command.ts b/api/src/unraid-api/cli/sso/sso.command.ts index fdc42dc8b..bea082881 100644 --- a/api/src/unraid-api/cli/sso/sso.command.ts +++ b/api/src/unraid-api/cli/sso/sso.command.ts @@ -6,12 +6,13 @@ import { LogService } from '@app/unraid-api/cli/log.service'; import { ValidateTokenCommand } from '@app/unraid-api/cli/sso/validate-token.command'; import { AddSSOUserCommand } from '@app/unraid-api/cli/sso/add-sso-user.command'; import { RemoveSSOUserCommand } from '@app/unraid-api/cli/sso/remove-sso-user.command'; +import { ListSSOUserCommand } from '@app/unraid-api/cli/sso/list-sso-user.command'; @Injectable() @Command({ name: 'sso', description: 'Main Command to Configure / Validate SSO Tokens', - subCommands: [ValidateTokenCommand, AddSSOUserCommand, RemoveSSOUserCommand], + subCommands: [ValidateTokenCommand, AddSSOUserCommand, RemoveSSOUserCommand, ListSSOUserCommand], }) export class SSOCommand extends CommandRunner { constructor(private readonly logger: LogService) {