-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(permissions-manager): integrate nest-commander and create first …
…command
- Loading branch information
Showing
12 changed files
with
334 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { OryPermissionsModule } from '@ticketing/microservices/ory-client'; | ||
import { validate } from '@ticketing/microservices/shared/env'; | ||
|
||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { CreateRelationCommand } from './create-relation.command'; | ||
import { EnvironmentVariables } from './env'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
imports: [ | ||
OryPermissionsModule.forRootAsync({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
validate: validate(EnvironmentVariables), | ||
}), | ||
], | ||
inject: [ConfigService], | ||
useFactory: ( | ||
configService: ConfigService<EnvironmentVariables, true>, | ||
) => ({ | ||
ketoAccessToken: configService.get('ORY_KETO_API_KEY'), | ||
ketoPublicApiPath: configService.get('ORY_KETO_PUBLIC_URL'), | ||
ketoAdminApiPath: configService.get('ORY_KETO_ADMIN_URL'), | ||
}), | ||
}), | ||
], | ||
providers: [CreateRelationCommand], | ||
}) | ||
export class AppModule {} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
apps/permissions-manager/src/app/create-relation.command.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { RelationTuple } from '@ticketing/microservices/shared/relation-tuple-parser'; | ||
import { MockOryPermissionService } from '@ticketing/microservices/shared/testing'; | ||
import { CommandTestFactory } from 'nest-commander-testing'; | ||
|
||
import { CreateRelationCommand } from './create-relation.command'; | ||
|
||
describe('CreateRelationCommand', () => { | ||
let service: CreateRelationCommand; | ||
|
||
beforeAll(async () => { | ||
const app = await CommandTestFactory.createTestingCommand({ | ||
imports: [], | ||
providers: [CreateRelationCommand, MockOryPermissionService], | ||
}).compile(); | ||
|
||
service = app.get<CreateRelationCommand>(CreateRelationCommand); | ||
}); | ||
|
||
describe('run', () => { | ||
it('should process tuple and create relationship', async () => { | ||
const expectedTuple: RelationTuple = { | ||
namespace: 'Group', | ||
object: 'admin', | ||
relation: 'members', | ||
subjectIdOrSet: { | ||
namespace: 'User', | ||
object: '1', | ||
}, | ||
}; | ||
|
||
await expect( | ||
service.run(['--tuple', 'Group:admin#members@User:1']), | ||
).resolves.toBeUndefined(); | ||
expect(service['oryPermissionsService'].createRelation).toBeCalledWith( | ||
expectedTuple, | ||
); | ||
}); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
apps/permissions-manager/src/app/create-relation.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { OryPermissionsService } from '@ticketing/microservices/ory-client'; | ||
import { | ||
type RelationTuple, | ||
parseRelationTuple, | ||
} from '@ticketing/microservices/shared/relation-tuple-parser'; | ||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
|
||
interface CommandOptions { | ||
tuple?: RelationTuple; | ||
} | ||
|
||
@Command({ name: 'create', description: 'Create relationship on Ory Keto' }) | ||
export class CreateRelationCommand extends CommandRunner { | ||
readonly logger = new Logger(CreateRelationCommand.name); | ||
|
||
constructor(private readonly oryPermissionsService: OryPermissionsService) { | ||
super(); | ||
} | ||
async run(passedParams: string[], options?: CommandOptions): Promise<void> { | ||
const { tuple } = options; | ||
this.logger.debug('Creating relation', passedParams); | ||
const isCreated = await this.oryPermissionsService.createRelation(tuple); | ||
if (!isCreated) { | ||
throw new Error('Failed to create relation'); | ||
} | ||
this.logger.debug('Created relation', tuple); | ||
} | ||
|
||
@Option({ | ||
flags: '-t, --tuple [string]', | ||
description: 'Relationship tuple to create, using Zanzibar notation', | ||
required: true, | ||
}) | ||
parseRelationTuple(val: string): RelationTuple { | ||
const res = parseRelationTuple(val); | ||
if (res.hasError()) { | ||
throw res.error; | ||
} | ||
return res.unwrapOrThrow(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { OryKetoEnvironmentVariables } from '@ticketing/microservices/shared/env'; | ||
import { Exclude } from 'class-transformer'; | ||
import { readFileSync } from 'node:fs'; | ||
import { dirname, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { Mixin } from 'ts-mixer'; | ||
|
||
export type AppConfigService = ConfigService<EnvironmentVariables, true>; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const pkgPath = join(__dirname, '..', '..', '..', '..', '..', 'package.json'); | ||
|
||
export class EnvironmentVariables extends Mixin(OryKetoEnvironmentVariables) { | ||
@Exclude() | ||
private pkg: { [key: string]: unknown; name?: string; version?: string } = | ||
JSON.parse(readFileSync(pkgPath, 'utf8')); | ||
|
||
APP_NAME?: string = 'payments'; | ||
|
||
APP_VERSION?: string = this.pkg?.version || '0.0.1'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
/** | ||
* This is not a production server yet! | ||
* This is only a minimal backend to get started. | ||
*/ | ||
|
||
import { Logger } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { CommandFactory } from 'nest-commander'; | ||
|
||
import { AppModule } from './app/app.module'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
const globalPrefix = 'api'; | ||
app.setGlobalPrefix(globalPrefix); | ||
const port = process.env.PORT || 3000; | ||
await app.listen(port); | ||
Logger.log( | ||
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`, | ||
); | ||
async function bootstrap(): Promise<void> { | ||
Logger.log('Starting permissions-manager', process.argv); | ||
await CommandFactory.run(AppModule, { | ||
logger: ['log', 'error', 'warn', 'debug', 'verbose'], | ||
enablePositionalOptions: true, | ||
enablePassThroughOptions: true, | ||
cliName: 'permissions-manager', | ||
version: '0.0.1', | ||
usePlugins: true, | ||
}); | ||
} | ||
|
||
bootstrap(); | ||
bootstrap().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.