Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
make OPA URL+path configurable (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus authored Mar 14, 2024
1 parent bb8b763 commit e98b130
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ $ npm install
## Running the app

```bash
# default env variables, corresponding to http://127.0.0.1:8181/v1/data/cats/allow
cp example.env .env

# development
$ npm run start

Expand Down
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPA_URL="http://127.0.0.1:8181"
OPA_PATH="cats/allow"
50 changes: 48 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"@nestjs/common": "^10.3.2",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.2",
"@nestjs/jwt": "^10.2.0",
"@nestjs/mapped-types": "*",
Expand Down
9 changes: 8 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CatsModule } from './cats/cats.module';
Expand All @@ -8,7 +9,13 @@ import { AuthzService } from './authz/authz.service';
import { AuthzModule } from './authz/authz.module';

@Module({
imports: [CatsModule, AuthModule, UsersModule, AuthzModule],
imports: [
CatsModule,
AuthModule,
UsersModule,
AuthzModule,
ConfigModule.forRoot(),
],
controllers: [AppController],
providers: [AppService, AuthzService],
})
Expand Down
2 changes: 2 additions & 0 deletions src/authz/authz.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { APP_GUARD } from '@nestjs/core';
import { AuthzService } from './authz.service';
import { AuthzGuard } from './authz.guard';

@Module({
imports: [ConfigModule],
providers: [
AuthzService,
{
Expand Down
18 changes: 12 additions & 6 deletions src/authz/authz.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Opa } from 'opa/sdk';
import { authorizer } from 'opa/sdk/helpers';

@Injectable()
export class AuthzService {
private readonly opa = new Opa(); // NB(sr): we'd need to get some config into this
private readonly authorizer = authorizer<Record<string, any>, boolean>(
this.opa,
'cats/allow',
);
private opa: Opa;
private authorizer: (_?: Record<string, any>) => Promise<boolean>;

async authorize(inp: any) {
constructor(private configService: ConfigService) {
this.opa = new Opa({ serverURL: this.configService.getOrThrow('OPA_URL') });
this.authorizer = authorizer<Record<string, any>, boolean>(
this.opa,
this.configService.getOrThrow('OPA_PATH'),
);
}

async authorize(inp: any): Promise<boolean> {
return await this.authorizer(inp);
}
}

0 comments on commit e98b130

Please sign in to comment.