-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keto-cli): create CLI to interact with Keto API
- Loading branch information
Showing
15 changed files
with
562 additions
and
80 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,38 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": [ | ||
"error", | ||
{ | ||
"buildTargets": ["build"], | ||
"checkMissingDependencies": true, | ||
"checkObsoleteDependencies": true, | ||
"checkVersionMismatches": true, | ||
"includeTransitiveDependencies": false, | ||
"ignoredDependencies": [ | ||
"@getlarge/base-client-wrapper", | ||
"@nestjs/testing" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
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,11 @@ | ||
# keto-cli | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build keto-cli` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test keto-cli` to execute the unit tests via [Jest](https://jestjs.io). |
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,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'keto-cli', | ||
preset: '../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/packages/keto-cli', | ||
}; |
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,10 @@ | ||
{ | ||
"name": "@getlarge/keto-cli", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"tslib": "^2.3.0" | ||
}, | ||
"type": "commonjs", | ||
"main": "./src/index.js", | ||
"typings": "./src/index.d.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,35 @@ | ||
{ | ||
"name": "keto-cli", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/keto-cli/src", | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/js:tsc", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/packages/keto-cli", | ||
"tsConfig": "packages/keto-cli/tsconfig.lib.json", | ||
"packageJson": "packages/keto-cli/package.json", | ||
"main": "packages/keto-cli/src/index.ts", | ||
"assets": ["packages/keto-cli/*.md"] | ||
} | ||
}, | ||
"publish": { | ||
"command": "node tools/scripts/publish.mjs keto-cli {args.ver} {args.tag}", | ||
"dependsOn": ["build"] | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"] | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "packages/keto-cli/jest.config.ts" | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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,32 @@ | ||
import { | ||
OryPermissionsModule, | ||
OryRelationshipsModule, | ||
} from '@getlarge/keto-client-wrapper'; | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
|
||
import { CheckPermissionCommand } from './check-permission.command'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({}), | ||
OryPermissionsModule.forRootAsync({ | ||
inject: [ConfigService], | ||
useFactory: (configService: ConfigService) => ({ | ||
basePath: configService.get( | ||
'ORY_KETO_PUBLIC_URL', | ||
'http://localhost:4466' | ||
), | ||
}), | ||
}), | ||
OryRelationshipsModule.forRootAsync({ | ||
inject: [ConfigService], | ||
useFactory: (configService: ConfigService) => ({ | ||
accessToken: configService.get('ORY_KETO_API_KEY', ''), | ||
basePath: configService.get('ORY_KETO_ADMIN_URL', ''), | ||
}), | ||
}), | ||
], | ||
providers: [CheckPermissionCommand], | ||
}) | ||
export class AppModule {} |
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,60 @@ | ||
import { OryPermissionsService } from '@getlarge/keto-client-wrapper'; | ||
import { | ||
createPermissionCheckQuery, | ||
parseRelationTuple, | ||
} from '@getlarge/keto-relations-parser'; | ||
import { Logger } from '@nestjs/common'; | ||
import { | ||
Configuration, | ||
type PermissionApiCheckPermissionRequest, | ||
} from '@ory/client'; | ||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
|
||
interface CommandOptions | ||
extends Pick<Configuration, 'basePath' | 'accessToken'> { | ||
tuple: PermissionApiCheckPermissionRequest; | ||
} | ||
|
||
@Command({ name: 'check', description: 'Check permission on Ory Keto' }) | ||
export class CheckPermissionCommand extends CommandRunner { | ||
readonly logger = new Logger(CheckPermissionCommand.name); | ||
|
||
constructor(private readonly oryPermissionsService: OryPermissionsService) { | ||
super(); | ||
} | ||
|
||
async run(passedParams: string[], options: CommandOptions): Promise<void> { | ||
const { tuple } = options; | ||
if (options.accessToken || options.basePath) { | ||
this.oryPermissionsService.config = new Configuration({ | ||
...this.oryPermissionsService.config, | ||
...options, | ||
}); | ||
} | ||
const isAllowed = await this.oryPermissionsService.checkPermission(tuple); | ||
this.logger.log(`Permission ${isAllowed ? 'granted' : 'denied'}`); | ||
} | ||
|
||
@Option({ | ||
flags: '-t, --tuple [string]', | ||
description: | ||
'Relationship tuple to check permission from, using Zanzibar notation', | ||
required: true, | ||
}) | ||
parseRelationTuple(val: string): PermissionApiCheckPermissionRequest { | ||
const res = parseRelationTuple(val); | ||
if (res.hasError()) { | ||
throw res.error; | ||
} | ||
return createPermissionCheckQuery(res.value).unwrapOrThrow(); | ||
} | ||
|
||
@Option({ | ||
flags: '-b, --base-path [string]', | ||
description: 'Ory Keto Admin URL', | ||
required: false, | ||
}) | ||
parseBasePath(val: string): string { | ||
return val; | ||
} | ||
} |
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,19 @@ | ||
import { CommandFactory } from 'nest-commander'; | ||
|
||
import { AppModule } from './app/app.module'; | ||
|
||
async function bootstrap(): Promise<void> { | ||
await CommandFactory.run(AppModule, { | ||
logger: ['log', 'error', 'warn', 'debug', 'verbose'], | ||
enablePositionalOptions: true, | ||
enablePassThroughOptions: true, | ||
cliName: 'keto-cli', | ||
version: '0.0.1', | ||
usePlugins: true, | ||
}); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
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,16 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"declaration": true, | ||
"types": ["node"], | ||
"target": "es2021", | ||
"strictNullChecks": true, | ||
"noImplicitAny": true, | ||
"strictBindCallApply": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.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,14 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"jest.config.ts", | ||
"src/**/*.test.ts", | ||
"src/**/*.spec.ts", | ||
"src/**/*.d.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
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