Skip to content

Commit

Permalink
refactor: keto
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelfimov committed Jan 12, 2024
1 parent d04a208 commit 40f80bb
Show file tree
Hide file tree
Showing 193 changed files with 1,249 additions and 2,557 deletions.
1,496 changes: 136 additions & 1,360 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/ip-npm-1.1.5-af36318aa6-bf1e24b657.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ packageExtensions:
dependencies:
'@babel/runtime': '*'
'@ory/keto-grpc-client@*':
peerDependencies:
grpc: '*'
peerDependencies:
grpc: '*'

yarnPath: .yarn/releases/yarn-remote.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { GraphQLExecutionContext } from '@nestjs/graphql'

import { Observable } from 'rxjs'

import { GET_LOADER_CONTEXT_KEY } from '../constants'
import { NestDataLoader } from '../interfaces'
import { GET_LOADER_CONTEXT_KEY } from '../constants'
import { NestDataLoader } from '../interfaces'

@Injectable()
export class DataLoaderInterceptor implements NestInterceptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DataLoader from 'dataloader'

export interface NestDataLoader {
generateDataLoader(): DataLoader<any,any>
generateDataLoader(): DataLoader<any, any>
}
6 changes: 3 additions & 3 deletions packages/grpc-keto/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './guarded-by-keto.decorator'
export { GuardedByKetoFunction } from './guarded-by-keto.interfaces'
export * from './guarded-by-keto.decorator'
export { GuardedByKetoFunction } from './guarded-by-keto.interfaces'
export { GetGuardingRelationTuple } from './guarded-by-keto.interfaces'
export { GUARDED_BY_METADATA_KEY } from './guarded-by-keto.constants'
export { GUARDED_BY_METADATA_KEY } from './guarded-by-keto.constants'
export { getGuardingRelationTuple } from './get-guarding-relation.helper'
1 change: 1 addition & 0 deletions packages/keto/integration/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './keto-integration.module'
21 changes: 21 additions & 0 deletions packages/keto/integration/src/keto-integration.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { UseGuards } from '@nestjs/common'
import { Controller } from '@nestjs/common'
import { Get } from '@nestjs/common'

import { GuardedByKeto } from '../../src'
import { KetoGuard } from '../../src'

@Controller()
export class KetoIntegrationController {
@Get('/allowed')
async allow() {
return true
}

@Get('/protected-by-keto')
@GuardedByKeto((user) => `Group:admin#members@${user}`)
@UseGuards(KetoGuard)
async protect() {
return true
}
}
14 changes: 14 additions & 0 deletions packages/keto/integration/src/keto-integration.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common'

import { KetoModule } from '../../src'
import { KetoIntegrationController } from './keto-integration.controller'

@Module({
imports: [
KetoModule.register({
basePath: '127.0.0.1:4466',
}),
],
controllers: [KetoIntegrationController],
})
export class KetoIntegrationModule {}
95 changes: 95 additions & 0 deletions packages/keto/integration/test/client-read.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @jest-environment node
*/

import { INestApplication } from '@nestjs/common'
import { TestingModule } from '@nestjs/testing'
import { Test } from '@nestjs/testing'

import request from 'supertest'
import { log } from 'testcontainers'
import { Network } from 'testcontainers'

Check failure on line 11 in packages/keto/integration/test/client-read.test.ts

View workflow job for this annotation

GitHub Actions / Lint

(@typescript-eslint/no-unused-vars): 'log' is defined but never used.

'log' is defined but never used.
Raw output
   8 |
   9 | import request                   from 'supertest'
> 10 | import { log }                   from 'testcontainers'
     |          ^
  11 | import { Network }               from 'testcontainers'
  12 | import { Wait }                  from 'testcontainers'
  13 | import { StartedTestContainer }  from 'testcontainers'
import { Wait } from 'testcontainers'
import { StartedTestContainer } from 'testcontainers'
import { GenericContainer } from 'testcontainers'

import { KETO_MODULE_CONFIGURATION } from '../../src'
import { KetoIntegrationModule } from '../src'
import { KETO_WRITE_PORT } from './test.constants'
import { KETO_READ_PORT } from './test.constants'
import { APP_PORT } from './test.constants'
import { KETO_FILES } from './test.constants'
import { KETO_ENVIRONMENT } from './test.constants'
import { DB_PORT } from './test.constants'
import { DB_ENVIRONMENT } from './test.constants'
import { KETO_START_COMMAND } from './test.constants'
import { KETO_INIT_COMMAND } from './test.constants'
import { KETO_MIGRATE_COMMAND } from './test.constants'

jest.setTimeout(15000)

describe('Keto read client', () => {
let app: INestApplication
let url: string
let module: TestingModule

let dbContainer: StartedTestContainer
let ketoContainer: StartedTestContainer

beforeAll(async () => {
const network = await new Network().start()

dbContainer = await new GenericContainer('bitnami/postgresql')
.withWaitStrategy(Wait.forLogMessage('database system is ready to accept connections'))
.withEnvironment(DB_ENVIRONMENT)
.withNetwork(network)
.withNetworkAliases('db')
.withExposedPorts(DB_PORT)
.start()

await new GenericContainer('oryd/keto')
.withEnvironment(KETO_ENVIRONMENT)
.withCopyFilesToContainer(KETO_FILES)
.withNetwork(network)
.withCommand(KETO_MIGRATE_COMMAND)
.start()

ketoContainer = await new GenericContainer('oryd/keto')
.withEnvironment(KETO_ENVIRONMENT)
.withCopyFilesToContainer(KETO_FILES)
.withNetwork(network)
.withExposedPorts(KETO_READ_PORT, KETO_WRITE_PORT)
.withCommand(KETO_START_COMMAND)
.start()

await ketoContainer.exec(KETO_INIT_COMMAND)

module = await Test.createTestingModule({
imports: [KetoIntegrationModule],
})
.overrideProvider(KETO_MODULE_CONFIGURATION)
.useValue({
basePath: `http://localhost:${ketoContainer.getMappedPort(KETO_READ_PORT)}`,
})
.compile()

Check failure on line 75 in packages/keto/integration/test/client-read.test.ts

View workflow job for this annotation

GitHub Actions / Test:Integration

Keto read client

allowed
Raw output
Error: thrown: "Exceeded timeout of 15000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
    at /home/runner/work/nestjs/nestjs/packages/keto/integration/test/client-read.test.ts:33:5
    at _dispatchDescribe (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-circus-npm-27.5.1-d8d9d88da5-ddfd4df426.zip/node_modules/jest-circus/build/index.js:98:26)
    at describe (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-circus-npm-27.5.1-d8d9d88da5-ddfd4df426.zip/node_modules/jest-circus/build/index.js:60:5)
    at Object.<anonymous> (/home/runner/work/nestjs/nestjs/packages/keto/integration/test/client-read.test.ts:27:1)
    at Runtime._execModule (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runtime-npm-27.5.1-c106eea3ba-7d352c3a44.zip/node_modules/jest-runtime/build/index.js:1646:24)
    at Runtime._loadModule (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runtime-npm-27.5.1-c106eea3ba-7d352c3a44.zip/node_modules/jest-runtime/build/index.js:1185:12)
    at Runtime.requireModule (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runtime-npm-27.5.1-c106eea3ba-7d352c3a44.zip/node_modules/jest-runtime/build/index.js:1009:12)
    at jestAdapter (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-circus-npm-27.5.1-d8d9d88da5-ddfd4df426.zip/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at runTestInternal (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runner-npm-27.5.1-2ed2c1cda8-eb93a4b0f2.zip/node_modules/jest-runner/build/runTest.js:389:16)
app = module.createNestApplication()

Check failure on line 76 in packages/keto/integration/test/client-read.test.ts

View workflow job for this annotation

GitHub Actions / Test:Integration

Keto read client

allows if relation tuple is ok
Raw output
Error: thrown: "Exceeded timeout of 15000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
    at /home/runner/work/nestjs/nestjs/packages/keto/integration/test/client-read.test.ts:33:5
    at _dispatchDescribe (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-circus-npm-27.5.1-d8d9d88da5-ddfd4df426.zip/node_modules/jest-circus/build/index.js:98:26)
    at describe (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-circus-npm-27.5.1-d8d9d88da5-ddfd4df426.zip/node_modules/jest-circus/build/index.js:60:5)
    at Object.<anonymous> (/home/runner/work/nestjs/nestjs/packages/keto/integration/test/client-read.test.ts:27:1)
    at Runtime._execModule (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runtime-npm-27.5.1-c106eea3ba-7d352c3a44.zip/node_modules/jest-runtime/build/index.js:1646:24)
    at Runtime._loadModule (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runtime-npm-27.5.1-c106eea3ba-7d352c3a44.zip/node_modules/jest-runtime/build/index.js:1185:12)
    at Runtime.requireModule (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runtime-npm-27.5.1-c106eea3ba-7d352c3a44.zip/node_modules/jest-runtime/build/index.js:1009:12)
    at jestAdapter (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-circus-npm-27.5.1-d8d9d88da5-ddfd4df426.zip/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at runTestInternal (/home/runner/work/nestjs/nestjs/.yarn/cache/jest-runner-npm-27.5.1-2ed2c1cda8-eb93a4b0f2.zip/node_modules/jest-runner/build/runTest.js:389:16)

await app.init()
await app.listen(APP_PORT)

url = await app.getHttpServer()
})

afterAll(async () => {
await app.close()

await dbContainer.stop()
await ketoContainer.stop()
})

it('allowed', async () => request(url).get('/allowed').expect(200))

it('allows if relation tuple is ok', async () =>
request(url).get('/protected-by-keto').set('x-user', 'testUser').expect(200))
})
34 changes: 34 additions & 0 deletions packages/keto/integration/test/keto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: v0.11.1-alpha.0

dsn: memory

serve:
read:
host: 0.0.0.0
cors:
enabled: false
allow_credentials: true
debug: true
write:
host: 0.0.0.0
cors:
enabled: false
allow_credentials: true
debug: true

opl:
host: localhost
cors:
enabled: false
allow_credentials: true
debug: true

namespaces:
location: file:///home/ory/namespaces.keto.ts

log:
level: debug
format: text
leak_sensitive_values: true

$schema: file:///config/keto/schema.json
11 changes: 11 additions & 0 deletions packages/keto/integration/test/namespaces.keto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */

// @ts-ignore
class User implements Namespace {}

// @ts-ignore
class Group implements Namespace {
related: {
members: User[]
}
}
6 changes: 6 additions & 0 deletions packages/keto/integration/test/relationships.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"namespace": "Group",
"object": "admin",
"relation": "members",
"subject_id": "testUser"
}
54 changes: 54 additions & 0 deletions packages/keto/integration/test/test.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { FileToCopy } from 'testcontainers/build/types'

export const KETO_MIGRATE_COMMAND = ['migrate', 'up', '--yes']

export const KETO_START_COMMAND = ['serve']

export const KETO_INIT_COMMAND = [
'keto',
'relation-tuple',
'create',
'/home/ory/relationships',
'--insecure-disable-transport-security',
'--insecure-skip-hostname-verification',
]

export const DB_ENVIRONMENT = {
POSTGRESQL_PASSWORD: 'password',
POSTGRESQL_DATABASE: 'db',
POSTGRESQL_USER: 'postgres',
}

export const DB_PORT = 5432

export const KETO_ENVIRONMENT = {
KETO_WRITE_REMOTE: 'localhost:4467',
KETO_READ_REMOTE: 'localhost:4466',
LOG_LEVEL: 'debug',
DSN: `postgres://${DB_ENVIRONMENT.POSTGRESQL_USER}:${DB_ENVIRONMENT.POSTGRESQL_PASSWORD}@${
DB_ENVIRONMENT.POSTGRESQL_DATABASE
}:${DB_PORT.toString()}/db?sslmode=disable&max_conns=20&max_idle_conns=4`,
}

export const KETO_READ_PORT = 4466

export const KETO_WRITE_PORT = 4467

const PROJECT_PATH =
'/Users/nikoroach/Documents/Typescript/atlantis/nestjs/packages/keto/integration/test'

export const KETO_FILES: FileToCopy[] = [
{
source: `${PROJECT_PATH}/keto.yml`,
target: '/home/ory/keto.yml',
},
{
source: `${PROJECT_PATH}/namespaces.keto.ts`,
target: '/home/ory/namespaces.keto.ts',
},
{
source: `${PROJECT_PATH}/relationships.json`,
target: '/home/ory/relationships/relationships.json',
},
]
export const APP_PORT = 3000
28 changes: 16 additions & 12 deletions packages/keto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,29 @@
"postpack": "rm -rf dist"
},
"dependencies": {
"@oryd/keto-client": "^0.5.6-alpha.1"
"@ory/keto-client": "0.11.0-alpha.0"
},
"devDependencies": {
"@nestjs/common": "patch:@nestjs/common@npm%3A10.2.5#~/.yarn/patches/@nestjs-common-npm-10.2.5-816dceeade.patch",
"@nestjs/core": "10.2.5",
"@nestjs/graphql": "^7.0.0",
"@types/node": "17.0.18",
"apollo-server-core": "^3.3.0",
"graphql": "^15.5.3",
"reflect-metadata": "0.1.13",
"@nestjs/graphql": "12.0.10",
"@nestjs/testing": "10.2.5",
"@types/node": "18.19.6",
"@types/supertest": "6",
"apollo-server-core": "3.3.0",
"graphql": "16.8.1",
"reflect-metadata": "0.2.1",
"rimraf": "3.0.2",
"rxjs": "^7.5.4",
"typescript": "^3.8.3"
"rxjs": "7.8.1",
"supertest": "6.3.3",
"testcontainers": "10.4.0",
"typescript": "4.6.4"
},
"peerDependencies": {
"@nestjs/common": "^8.0.4",
"@nestjs/core": "^8.0.4",
"reflect-metadata": "^0.1.12",
"rxjs": "^7.2.0"
"@nestjs/common": "*",
"@nestjs/core": "*",
"reflect-metadata": "*",
"rxjs": "*"
},
"publishConfig": {
"main": "dist/index.js",
Expand Down
3 changes: 0 additions & 3 deletions packages/keto/src/constants.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/keto/src/decorators/access-policy.decorator.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/keto/src/decorators/get-guarding-relation.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GUARDED_BY_METADATA_KEY } from './guarded-by-keto.constants'
import { ReplaceGenerator } from './guarded-by-keto.interfaces'
import { GetGuardingRelationTuple } from './guarded-by-keto.interfaces'

export const getGuardingRelationTuple: GetGuardingRelationTuple = (reflector, handler) =>
reflector.get<string | ReplaceGenerator, typeof GUARDED_BY_METADATA_KEY>(
GUARDED_BY_METADATA_KEY,
handler
) ?? null
1 change: 1 addition & 0 deletions packages/keto/src/decorators/guarded-by-keto.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GUARDED_BY_METADATA_KEY = Symbol('GuardedByKeto')
7 changes: 7 additions & 0 deletions packages/keto/src/decorators/guarded-by-keto.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SetMetadata } from '@nestjs/common'

import { GUARDED_BY_METADATA_KEY } from './guarded-by-keto.constants'
import { GuardedByKetoFunction } from './guarded-by-keto.interfaces'

export const GuardedByKeto: GuardedByKetoFunction = (relationTuple) =>
SetMetadata(GUARDED_BY_METADATA_KEY, relationTuple)
13 changes: 13 additions & 0 deletions packages/keto/src/decorators/guarded-by-keto.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CustomDecorator } from '@nestjs/common'
import { Reflector } from '@nestjs/core'

export type GetGuardingRelationTuple = (
reflector: Reflector,
handler: Parameters<Reflector['get']>[1]
) => string | ReplaceGenerator

export type ReplaceGenerator = (value: string) => string

export type GuardedByKetoFunction = (
relationTuple: string | ReplaceGenerator
) => CustomDecorator<symbol>
6 changes: 5 additions & 1 deletion packages/keto/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './access-policy.decorator'
export * from './guarded-by-keto.decorator'
export { GuardedByKetoFunction } from './guarded-by-keto.interfaces'
export { GetGuardingRelationTuple } from './guarded-by-keto.interfaces'
export { GUARDED_BY_METADATA_KEY } from './guarded-by-keto.constants'
export { getGuardingRelationTuple } from './get-guarding-relation.helper'
4 changes: 4 additions & 0 deletions packages/keto/src/exceptions/exception-message.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum KetoExceptionMessage {
GENERAL_ERROR = 'General Keto error',
RELATION_TUPLE_INVALID = 'Provided relation tuple is invalid',
}
9 changes: 9 additions & 0 deletions packages/keto/src/exceptions/general.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AssertionError } from 'assert'

import { KetoExceptionMessage } from './exception-message.constants'

export class KetoGeneralException extends AssertionError {
constructor(message: string) {
super({ message: `${KetoExceptionMessage.GENERAL_ERROR}: ${message}` })
}
}
2 changes: 2 additions & 0 deletions packages/keto/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './general.exception'
export * from './relation-tuple-invalid.exception'
Loading

0 comments on commit 40f80bb

Please sign in to comment.