@illustrisinteractive/sentinel 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +4 -0
- package/README.md +98 -0
- package/eslint.config.mjs +35 -0
- package/libs/sentinel/src/helpers/hasDuplicates.ts +13 -0
- package/libs/sentinel/src/index.ts +2 -0
- package/libs/sentinel/src/models/PermissionKey.ts +18 -0
- package/libs/sentinel/src/models/Role.ts +12 -0
- package/libs/sentinel/src/models/RoleHasPermissions.ts +25 -0
- package/libs/sentinel/src/models/SecuredResource.ts +21 -0
- package/libs/sentinel/src/models/SentinelModuleOptions.ts +9 -0
- package/libs/sentinel/src/sentinel.module-definition.ts +5 -0
- package/libs/sentinel/src/sentinel.module.ts +34 -0
- package/libs/sentinel/src/sentinel.service.spec.ts +18 -0
- package/libs/sentinel/src/sentinel.service.ts +59 -0
- package/libs/sentinel/src/sentinel.service.ts.old +88 -0
- package/libs/sentinel/tsconfig.lib.json +9 -0
- package/nest-cli.json +10 -0
- package/package.json +85 -0
- package/src/app.controller.spec.ts +22 -0
- package/src/app.controller.ts +12 -0
- package/src/app.module.ts +17 -0
- package/src/app.service.ts +8 -0
- package/src/main.ts +8 -0
- package/src/models/sentinel/TestResource.ts +22 -0
- package/src/models/sentinel/TransactionResource.ts +22 -0
- package/test/app.e2e-spec.ts +25 -0
- package/test/jest-e2e.json +16 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +33 -0
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
|
|
6
|
+
[circleci-url]: https://circleci.com/gh/nestjs/nest
|
|
7
|
+
|
|
8
|
+
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
|
|
11
|
+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
|
|
12
|
+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
|
|
13
|
+
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
|
|
14
|
+
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
|
|
15
|
+
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
|
|
16
|
+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
|
|
17
|
+
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
|
|
18
|
+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
|
|
19
|
+
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
|
|
20
|
+
</p>
|
|
21
|
+
<!--[](https://opencollective.com/nest#backer)
|
|
22
|
+
[](https://opencollective.com/nest#sponsor)-->
|
|
23
|
+
|
|
24
|
+
## Description
|
|
25
|
+
|
|
26
|
+
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
|
|
27
|
+
|
|
28
|
+
## Project setup
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
$ npm install
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Compile and run the project
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# development
|
|
38
|
+
$ npm run start
|
|
39
|
+
|
|
40
|
+
# watch mode
|
|
41
|
+
$ npm run start:dev
|
|
42
|
+
|
|
43
|
+
# production mode
|
|
44
|
+
$ npm run start:prod
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Run tests
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# unit tests
|
|
51
|
+
$ npm run test
|
|
52
|
+
|
|
53
|
+
# e2e tests
|
|
54
|
+
$ npm run test:e2e
|
|
55
|
+
|
|
56
|
+
# test coverage
|
|
57
|
+
$ npm run test:cov
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Deployment
|
|
61
|
+
|
|
62
|
+
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.
|
|
63
|
+
|
|
64
|
+
If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
$ npm install -g @nestjs/mau
|
|
68
|
+
$ mau deploy
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
|
|
72
|
+
|
|
73
|
+
## Resources
|
|
74
|
+
|
|
75
|
+
Check out a few resources that may come in handy when working with NestJS:
|
|
76
|
+
|
|
77
|
+
- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
|
|
78
|
+
- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
|
|
79
|
+
- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
|
|
80
|
+
- Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
|
|
81
|
+
- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
|
|
82
|
+
- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
|
|
83
|
+
- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
|
|
84
|
+
- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
|
|
85
|
+
|
|
86
|
+
## Support
|
|
87
|
+
|
|
88
|
+
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
|
89
|
+
|
|
90
|
+
## Stay in touch
|
|
91
|
+
|
|
92
|
+
- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
|
|
93
|
+
- Website - [https://nestjs.com](https://nestjs.com/)
|
|
94
|
+
- Twitter - [@nestframework](https://twitter.com/nestframework)
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import eslint from '@eslint/js';
|
|
3
|
+
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
|
|
4
|
+
import globals from 'globals';
|
|
5
|
+
import tseslint from 'typescript-eslint';
|
|
6
|
+
|
|
7
|
+
export default tseslint.config(
|
|
8
|
+
{
|
|
9
|
+
ignores: ['eslint.config.mjs'],
|
|
10
|
+
},
|
|
11
|
+
eslint.configs.recommended,
|
|
12
|
+
...tseslint.configs.recommendedTypeChecked,
|
|
13
|
+
eslintPluginPrettierRecommended,
|
|
14
|
+
{
|
|
15
|
+
languageOptions: {
|
|
16
|
+
globals: {
|
|
17
|
+
...globals.node,
|
|
18
|
+
...globals.jest,
|
|
19
|
+
},
|
|
20
|
+
sourceType: 'commonjs',
|
|
21
|
+
parserOptions: {
|
|
22
|
+
projectService: true,
|
|
23
|
+
tsconfigRootDir: import.meta.dirname,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
rules: {
|
|
29
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
30
|
+
'@typescript-eslint/no-floating-promises': 'warn',
|
|
31
|
+
'@typescript-eslint/no-unsafe-argument': 'warn',
|
|
32
|
+
"prettier/prettier": ["error", { endOfLine: "auto" }],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const hasDuplicates = (arr, prop) => {
|
|
2
|
+
const seen = new Set();
|
|
3
|
+
return arr.some((obj) => {
|
|
4
|
+
const value = obj[prop];
|
|
5
|
+
// If the value is already in the Set, it's a duplicate
|
|
6
|
+
if (seen.has(value)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
// Otherwise, add the value to the Set
|
|
10
|
+
seen.add(value);
|
|
11
|
+
return false;
|
|
12
|
+
});
|
|
13
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Column, HasMany, Model, Table } from 'sequelize-typescript';
|
|
2
|
+
import { RoleHasPermissions } from './RoleHasPermissions';
|
|
3
|
+
import { type NonAttribute } from 'sequelize';
|
|
4
|
+
|
|
5
|
+
@Table
|
|
6
|
+
export class PermissionKey extends Model {
|
|
7
|
+
@Column
|
|
8
|
+
resource: string;
|
|
9
|
+
|
|
10
|
+
@Column
|
|
11
|
+
action: string;
|
|
12
|
+
|
|
13
|
+
@Column
|
|
14
|
+
description: string;
|
|
15
|
+
|
|
16
|
+
@HasMany(() => RoleHasPermissions)
|
|
17
|
+
declare roles?: NonAttribute<RoleHasPermissions[]>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Column, HasMany, Model, Table } from 'sequelize-typescript';
|
|
2
|
+
import { RoleHasPermissions } from './RoleHasPermissions';
|
|
3
|
+
import { type NonAttribute } from 'sequelize';
|
|
4
|
+
|
|
5
|
+
@Table
|
|
6
|
+
export class Role extends Model {
|
|
7
|
+
@Column
|
|
8
|
+
name: string;
|
|
9
|
+
|
|
10
|
+
@HasMany(() => RoleHasPermissions)
|
|
11
|
+
declare permissions?: NonAttribute<RoleHasPermissions[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BelongsTo,
|
|
3
|
+
Column,
|
|
4
|
+
ForeignKey,
|
|
5
|
+
Model,
|
|
6
|
+
Table,
|
|
7
|
+
} from 'sequelize-typescript';
|
|
8
|
+
import { PermissionKey } from './PermissionKey';
|
|
9
|
+
import { type NonAttribute } from 'sequelize';
|
|
10
|
+
import { Role } from './Role';
|
|
11
|
+
|
|
12
|
+
@Table
|
|
13
|
+
export class RoleHasPermissions extends Model {
|
|
14
|
+
@ForeignKey(() => PermissionKey)
|
|
15
|
+
declare permissionKeyId: number;
|
|
16
|
+
|
|
17
|
+
@BelongsTo(() => PermissionKey)
|
|
18
|
+
declare permissionKey: NonAttribute<PermissionKey>;
|
|
19
|
+
|
|
20
|
+
@ForeignKey(() => Role)
|
|
21
|
+
declare roleId: number;
|
|
22
|
+
|
|
23
|
+
@BelongsTo(() => PermissionKey)
|
|
24
|
+
declare role: NonAttribute<Role>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface SecuredResource {
|
|
2
|
+
/**
|
|
3
|
+
* The name of the Secured Resource.
|
|
4
|
+
*/
|
|
5
|
+
name: string;
|
|
6
|
+
/**
|
|
7
|
+
* A list of Actions that a User can do on the Secured Resource.
|
|
8
|
+
*/
|
|
9
|
+
actions: SecuredResourceAction[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SecuredResourceAction {
|
|
13
|
+
/**
|
|
14
|
+
* The permission key to identify this Action on the Secured Resource.
|
|
15
|
+
*/
|
|
16
|
+
key: string;
|
|
17
|
+
/**
|
|
18
|
+
* An optional description to define this Action.
|
|
19
|
+
*/
|
|
20
|
+
description?: string;
|
|
21
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Model, Options } from 'sequelize';
|
|
2
|
+
import { SecuredResource } from './SecuredResource';
|
|
3
|
+
|
|
4
|
+
export interface SentinelModuleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A list of Secured Resources to apply Sentinel (Role-Based Access Control) to.
|
|
7
|
+
*/
|
|
8
|
+
resources: SecuredResource[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { SentinelService } from './sentinel.service';
|
|
3
|
+
import {
|
|
4
|
+
ConfigurableModuleClass,
|
|
5
|
+
MODULE_OPTIONS_TOKEN,
|
|
6
|
+
} from './sentinel.module-definition';
|
|
7
|
+
import { SequelizeModule } from '@nestjs/sequelize';
|
|
8
|
+
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
9
|
+
import { PermissionKey } from './models/PermissionKey';
|
|
10
|
+
import { Role } from './models/Role';
|
|
11
|
+
import { RoleHasPermissions } from './models/RoleHasPermissions';
|
|
12
|
+
|
|
13
|
+
@Module({
|
|
14
|
+
imports: [
|
|
15
|
+
SequelizeModule.forRootAsync({
|
|
16
|
+
imports: [ConfigModule],
|
|
17
|
+
inject: [ConfigService],
|
|
18
|
+
useFactory: (configService: ConfigService) => ({
|
|
19
|
+
dialect: 'postgres',
|
|
20
|
+
host: configService.getOrThrow('SENTINEL_DB_HOST'),
|
|
21
|
+
port: configService.get('SENTINEL_DB_PORT') || 5432,
|
|
22
|
+
username: configService.getOrThrow('SENTINEL_DB_USER'),
|
|
23
|
+
password: configService.getOrThrow('SENTINEL_DB_PASS'),
|
|
24
|
+
database: configService.getOrThrow('SENTINEL_DB'),
|
|
25
|
+
models: [PermissionKey, Role, RoleHasPermissions],
|
|
26
|
+
autoLoadModels: true,
|
|
27
|
+
}),
|
|
28
|
+
}),
|
|
29
|
+
SequelizeModule.forFeature([PermissionKey, Role, RoleHasPermissions]),
|
|
30
|
+
],
|
|
31
|
+
providers: [SentinelService],
|
|
32
|
+
exports: [SentinelService],
|
|
33
|
+
})
|
|
34
|
+
export class SentinelModule extends ConfigurableModuleClass {}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
+
import { SentinelService } from './sentinel.service';
|
|
3
|
+
|
|
4
|
+
describe('SentinelService', () => {
|
|
5
|
+
let service: SentinelService;
|
|
6
|
+
|
|
7
|
+
beforeEach(async () => {
|
|
8
|
+
const module: TestingModule = await Test.createTestingModule({
|
|
9
|
+
providers: [SentinelService],
|
|
10
|
+
}).compile();
|
|
11
|
+
|
|
12
|
+
service = module.get<SentinelService>(SentinelService);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should be defined', () => {
|
|
16
|
+
expect(service).toBeDefined();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
OnModuleInit,
|
|
5
|
+
Logger,
|
|
6
|
+
LoggerService,
|
|
7
|
+
} from '@nestjs/common';
|
|
8
|
+
import { MODULE_OPTIONS_TOKEN } from './sentinel.module-definition';
|
|
9
|
+
import { type SentinelModuleOptions } from './models/SentinelModuleOptions';
|
|
10
|
+
import { Sequelize } from 'sequelize';
|
|
11
|
+
import { InjectModel } from '@nestjs/sequelize';
|
|
12
|
+
import { PermissionKey } from './models/PermissionKey';
|
|
13
|
+
import { hasDuplicates } from './helpers/hasDuplicates';
|
|
14
|
+
@Injectable()
|
|
15
|
+
export class SentinelService implements OnModuleInit {
|
|
16
|
+
private logger: LoggerService = new Logger(SentinelService.name);
|
|
17
|
+
constructor(
|
|
18
|
+
@Inject(MODULE_OPTIONS_TOKEN) private options: SentinelModuleOptions,
|
|
19
|
+
@InjectModel(PermissionKey)
|
|
20
|
+
private permissionKeyRepository: typeof PermissionKey,
|
|
21
|
+
) {}
|
|
22
|
+
async onModuleInit() {
|
|
23
|
+
//initial load flags
|
|
24
|
+
let noMatchingPermissionKey = false;
|
|
25
|
+
let duplicateResourceName = hasDuplicates(this.options.resources, 'name');
|
|
26
|
+
|
|
27
|
+
if (duplicateResourceName) {
|
|
28
|
+
this.logger.error(
|
|
29
|
+
`Duplicate name property value detected from list of Secured Resources. Please ensure that all Secured Resources have unique names.`,
|
|
30
|
+
);
|
|
31
|
+
throw new Error(
|
|
32
|
+
`Duplicate name property value detected from list of Secured Resources. Please ensure that all Secured Resources have unique names.`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//get all permissions
|
|
37
|
+
const permissions: PermissionKey[] =
|
|
38
|
+
await this.permissionKeyRepository.findAll();
|
|
39
|
+
this.logger.log(`${permissions.length} Permission Keys were found.`);
|
|
40
|
+
|
|
41
|
+
/*start verifying if all Secured Resources have their Actions persisted as Permission Keys
|
|
42
|
+
in the database */
|
|
43
|
+
this.options.resources.forEach((resource) => {
|
|
44
|
+
resource.actions.forEach((action) => {
|
|
45
|
+
const matchingKey = permissions.find(
|
|
46
|
+
(permission) =>
|
|
47
|
+
permission.action == action.key &&
|
|
48
|
+
permission.resource == resource.name,
|
|
49
|
+
);
|
|
50
|
+
if (!matchingKey) {
|
|
51
|
+
this.logger.warn(
|
|
52
|
+
`Action "${action.key}" from Secured Resource "${resource.name}" does not have a matching Permission Key in the database. If this Secured Resource was newly registered, did you run the Permission Key Migration?`,
|
|
53
|
+
);
|
|
54
|
+
noMatchingPermissionKey = true;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//Initial tests
|
|
2
|
+
try {
|
|
3
|
+
//Test database connection
|
|
4
|
+
await this.sequelize.authenticate();
|
|
5
|
+
this.logger.log('Sentinel Module connected to Database');
|
|
6
|
+
init(this.sequelize, this.table);
|
|
7
|
+
|
|
8
|
+
//Check if permissions table exists
|
|
9
|
+
let [results, metadata] = await this.sequelize.query(
|
|
10
|
+
`SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '${this.table}');`,
|
|
11
|
+
);
|
|
12
|
+
if (!(results[0] as any).exists) {
|
|
13
|
+
this.logger.error(
|
|
14
|
+
`Permission table "${this.table}" does not exist in the database. Please try to run the permission migration again, or set "synchronize" to true (only within development environments).`,
|
|
15
|
+
);
|
|
16
|
+
throw new Error(
|
|
17
|
+
`Permission table "${this.table}" does not exist in the database. Please try to run the permission migration again, or set "synchronize" to true (only within development environments).`,
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Role(this.sequelize, this.roleTable);
|
|
22
|
+
|
|
23
|
+
//Check if roles table exists
|
|
24
|
+
let [roleResults] = await this.sequelize.query(
|
|
25
|
+
`SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '${this.roleTable}');`,
|
|
26
|
+
);
|
|
27
|
+
if (!(roleResults[0] as any).exists) {
|
|
28
|
+
this.logger.error(
|
|
29
|
+
`Role table "${this.roleTable}" does not exist in the database. Please try to run the role migration again, or set "synchronize" to true (only within development environments).`,
|
|
30
|
+
);
|
|
31
|
+
throw new Error(
|
|
32
|
+
`Role table "${this.roleTable}" does not exist in the database. Please try to run the role migration again, or set "synchronize" to true (only within development environments).`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
RoleHasPermissions(this.sequelize, this.roleHasPermissionsTable);
|
|
37
|
+
|
|
38
|
+
//Check if permissions table conforms to model
|
|
39
|
+
let [modelResults] = await this.sequelize.query(
|
|
40
|
+
`SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${this.table}'`,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const expectedColumns = [
|
|
44
|
+
{ column_name: 'id', data_type: 'integer' },
|
|
45
|
+
{ column_name: 'resource', data_type: 'character varying' },
|
|
46
|
+
{ column_name: 'action', data_type: 'character varying' },
|
|
47
|
+
{ column_name: 'description', data_type: 'character varying' },
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
if (JSON.stringify(expectedColumns) != JSON.stringify(modelResults)) {
|
|
51
|
+
this.logger.error(
|
|
52
|
+
`Permission table "${this.table}" does not conform to the schema expected by Sentinel. Drop the table then try to run the permission migration again, or set "synchronize" to true (only within development environments).`,
|
|
53
|
+
);
|
|
54
|
+
throw new Error(
|
|
55
|
+
`Permission table "${this.table}" does not conform to the schema expected by Sentinel. Drop the table then try to run the permission migration again, or set "synchronize" to true (only within development environments).`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
this.logger.log('Permission table for Sentinel Module found and verified');
|
|
59
|
+
|
|
60
|
+
//check all passed Secured Resources and verify that all of their actions exist as keys in the database
|
|
61
|
+
|
|
62
|
+
const promises: Promise<void>[] = [];
|
|
63
|
+
this.options.resources.forEach((_resource) => {
|
|
64
|
+
const fn = async (resource: SecuredResource) => {
|
|
65
|
+
const foundActions = await PermissionKey.findAll({
|
|
66
|
+
where: {
|
|
67
|
+
resource: resource.name,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
resource.actions.forEach((action) => {
|
|
72
|
+
if (!foundActions.find((key) => key.get('action') == action.key)) {
|
|
73
|
+
this.logger.warn(
|
|
74
|
+
`Action "${action.key}" for Secured Resource "${resource.name}" does not exist in the Permission Table. Did you run the permission migration correctly?`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
promises.push(fn(_resource));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
await Promise.all(promises);
|
|
84
|
+
|
|
85
|
+
this.logger.log('Sentinel is ready to enforce access controls');
|
|
86
|
+
} catch (error) {
|
|
87
|
+
throw new Error(error);
|
|
88
|
+
}
|
package/nest-cli.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@illustrisinteractive/sentinel",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"author": "John Dela Vega <john@illustrisinteractive.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "nest build",
|
|
9
|
+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"libs/**/*.ts\"",
|
|
10
|
+
"start": "nest start",
|
|
11
|
+
"start:dev": "nest start --watch",
|
|
12
|
+
"start:debug": "nest start --debug --watch",
|
|
13
|
+
"start:prod": "node dist/main",
|
|
14
|
+
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
15
|
+
"test": "jest",
|
|
16
|
+
"test:watch": "jest --watch",
|
|
17
|
+
"test:cov": "jest --coverage",
|
|
18
|
+
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
|
19
|
+
"test:e2e": "jest --config ./test/jest-e2e.json"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@casl/ability": "^6.8.0",
|
|
23
|
+
"@nestjs/common": "^11.0.1",
|
|
24
|
+
"@nestjs/config": "^4.0.3",
|
|
25
|
+
"@nestjs/core": "^11.0.1",
|
|
26
|
+
"@nestjs/platform-express": "^11.0.1",
|
|
27
|
+
"@nestjs/sequelize": "^11.0.1",
|
|
28
|
+
"mysql2": "^3.17.0",
|
|
29
|
+
"pg": "^8.18.0",
|
|
30
|
+
"reflect-metadata": "^0.2.2",
|
|
31
|
+
"rxjs": "^7.8.1",
|
|
32
|
+
"sequelize": "^6.37.7",
|
|
33
|
+
"sequelize-typescript": "^2.1.6"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@eslint/eslintrc": "^3.2.0",
|
|
37
|
+
"@eslint/js": "^9.18.0",
|
|
38
|
+
"@nestjs/cli": "^11.0.0",
|
|
39
|
+
"@nestjs/schematics": "^11.0.0",
|
|
40
|
+
"@nestjs/testing": "^11.0.1",
|
|
41
|
+
"@types/express": "^5.0.0",
|
|
42
|
+
"@types/jest": "^30.0.0",
|
|
43
|
+
"@types/node": "^22.10.7",
|
|
44
|
+
"@types/sequelize": "^4.28.20",
|
|
45
|
+
"@types/supertest": "^6.0.2",
|
|
46
|
+
"eslint": "^9.18.0",
|
|
47
|
+
"eslint-config-prettier": "^10.0.1",
|
|
48
|
+
"eslint-plugin-prettier": "^5.2.2",
|
|
49
|
+
"globals": "^16.0.0",
|
|
50
|
+
"jest": "^30.0.0",
|
|
51
|
+
"prettier": "^3.4.2",
|
|
52
|
+
"source-map-support": "^0.5.21",
|
|
53
|
+
"supertest": "^7.0.0",
|
|
54
|
+
"ts-jest": "^29.2.5",
|
|
55
|
+
"ts-loader": "^9.5.2",
|
|
56
|
+
"ts-node": "^10.9.2",
|
|
57
|
+
"tsconfig-paths": "^4.2.0",
|
|
58
|
+
"typescript": "^5.7.3",
|
|
59
|
+
"typescript-eslint": "^8.20.0"
|
|
60
|
+
},
|
|
61
|
+
"jest": {
|
|
62
|
+
"moduleFileExtensions": [
|
|
63
|
+
"js",
|
|
64
|
+
"json",
|
|
65
|
+
"ts"
|
|
66
|
+
],
|
|
67
|
+
"rootDir": ".",
|
|
68
|
+
"testRegex": ".*\\.spec\\.ts$",
|
|
69
|
+
"transform": {
|
|
70
|
+
"^.+\\.(t|j)s$": "ts-jest"
|
|
71
|
+
},
|
|
72
|
+
"collectCoverageFrom": [
|
|
73
|
+
"**/*.(t|j)s"
|
|
74
|
+
],
|
|
75
|
+
"coverageDirectory": "./coverage",
|
|
76
|
+
"testEnvironment": "node",
|
|
77
|
+
"roots": [
|
|
78
|
+
"<rootDir>/src/",
|
|
79
|
+
"<rootDir>/libs/"
|
|
80
|
+
],
|
|
81
|
+
"moduleNameMapper": {
|
|
82
|
+
"^@app/sentinel(|/.*)$": "<rootDir>/libs/sentinel/src/$1"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
+
import { AppController } from './app.controller';
|
|
3
|
+
import { AppService } from './app.service';
|
|
4
|
+
|
|
5
|
+
describe('AppController', () => {
|
|
6
|
+
let appController: AppController;
|
|
7
|
+
|
|
8
|
+
beforeEach(async () => {
|
|
9
|
+
const app: TestingModule = await Test.createTestingModule({
|
|
10
|
+
controllers: [AppController],
|
|
11
|
+
providers: [AppService],
|
|
12
|
+
}).compile();
|
|
13
|
+
|
|
14
|
+
appController = app.get<AppController>(AppController);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('root', () => {
|
|
18
|
+
it('should return "Hello World!"', () => {
|
|
19
|
+
expect(appController.getHello()).toBe('Hello World!');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Controller, Get } from '@nestjs/common';
|
|
2
|
+
import { AppService } from './app.service';
|
|
3
|
+
|
|
4
|
+
@Controller()
|
|
5
|
+
export class AppController {
|
|
6
|
+
constructor(private readonly appService: AppService) {}
|
|
7
|
+
|
|
8
|
+
@Get()
|
|
9
|
+
getHello(): string {
|
|
10
|
+
return this.appService.getHello();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { AppController } from './app.controller';
|
|
3
|
+
import { AppService } from './app.service';
|
|
4
|
+
import { SentinelModule } from '@app/sentinel';
|
|
5
|
+
import { TransactionResource } from './models/sentinel/TransactionResource';
|
|
6
|
+
import { TestResource } from './models/sentinel/TestResource';
|
|
7
|
+
|
|
8
|
+
@Module({
|
|
9
|
+
imports: [
|
|
10
|
+
SentinelModule.register({
|
|
11
|
+
resources: [TransactionResource],
|
|
12
|
+
}),
|
|
13
|
+
],
|
|
14
|
+
controllers: [AppController],
|
|
15
|
+
providers: [AppService],
|
|
16
|
+
})
|
|
17
|
+
export class AppModule {}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SecuredResource,
|
|
3
|
+
SecuredResourceAction,
|
|
4
|
+
} from '@app/sentinel/models/SecuredResource';
|
|
5
|
+
|
|
6
|
+
export const TestResource: SecuredResource = {
|
|
7
|
+
name: 'Transaction',
|
|
8
|
+
actions: [
|
|
9
|
+
{
|
|
10
|
+
key: 'create',
|
|
11
|
+
description: 'Allows the User to create a new Transaction',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
key: 'read',
|
|
15
|
+
description: 'Allows the user to read existing Transactions',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
key: 'delete',
|
|
19
|
+
description: 'Allows the user to delete existing Transactions',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SecuredResource,
|
|
3
|
+
SecuredResourceAction,
|
|
4
|
+
} from '@app/sentinel/models/SecuredResource';
|
|
5
|
+
|
|
6
|
+
export const TransactionResource: SecuredResource = {
|
|
7
|
+
name: 'Transaction',
|
|
8
|
+
actions: [
|
|
9
|
+
{
|
|
10
|
+
key: 'create',
|
|
11
|
+
description: 'Allows the User to create a new Transaction',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
key: 'read',
|
|
15
|
+
description: 'Allows the user to read existing Transactions',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
key: 'delete',
|
|
19
|
+
description: 'Allows the user to delete existing Transactions',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
+
import { INestApplication } from '@nestjs/common';
|
|
3
|
+
import request from 'supertest';
|
|
4
|
+
import { App } from 'supertest/types';
|
|
5
|
+
import { AppModule } from './../src/app.module';
|
|
6
|
+
|
|
7
|
+
describe('AppController (e2e)', () => {
|
|
8
|
+
let app: INestApplication<App>;
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
12
|
+
imports: [AppModule],
|
|
13
|
+
}).compile();
|
|
14
|
+
|
|
15
|
+
app = moduleFixture.createNestApplication();
|
|
16
|
+
await app.init();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('/ (GET)', () => {
|
|
20
|
+
return request(app.getHttpServer())
|
|
21
|
+
.get('/')
|
|
22
|
+
.expect(200)
|
|
23
|
+
.expect('Hello World!');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"moduleFileExtensions": [
|
|
3
|
+
"js",
|
|
4
|
+
"json",
|
|
5
|
+
"ts"
|
|
6
|
+
],
|
|
7
|
+
"rootDir": ".",
|
|
8
|
+
"testEnvironment": "node",
|
|
9
|
+
"testRegex": ".e2e-spec.ts$",
|
|
10
|
+
"transform": {
|
|
11
|
+
"^.+\\.(t|j)s$": "ts-jest"
|
|
12
|
+
},
|
|
13
|
+
"moduleNameMapper": {
|
|
14
|
+
"^@app/sentinel(|/.*)$": "<rootDir>/../libs/sentinel/src/$1"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "nodenext",
|
|
4
|
+
"moduleResolution": "nodenext",
|
|
5
|
+
"resolvePackageJsonExports": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"isolatedModules": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"removeComments": true,
|
|
10
|
+
"emitDecoratorMetadata": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"target": "ES2023",
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"outDir": "./dist",
|
|
16
|
+
"baseUrl": "./",
|
|
17
|
+
"incremental": true,
|
|
18
|
+
"skipLibCheck": true,
|
|
19
|
+
"strictNullChecks": true,
|
|
20
|
+
"forceConsistentCasingInFileNames": true,
|
|
21
|
+
"noImplicitAny": false,
|
|
22
|
+
"strictBindCallApply": false,
|
|
23
|
+
"noFallthroughCasesInSwitch": false,
|
|
24
|
+
"paths": {
|
|
25
|
+
"@app/sentinel": [
|
|
26
|
+
"libs/sentinel/src"
|
|
27
|
+
],
|
|
28
|
+
"@app/sentinel/*": [
|
|
29
|
+
"libs/sentinel/src/*"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|