Skip to content

Commit

Permalink
Minor refactor on the policy engine organization (#155)
Browse files Browse the repository at this point in the history
* Format the whole code base

Move service to the right directory

* Rename types directory
  • Loading branch information
wcalderipe authored Mar 5, 2024
1 parent 0473682 commit c3a5611
Show file tree
Hide file tree
Showing 33 changed files with 52 additions and 88 deletions.
41 changes: 7 additions & 34 deletions apps/devtool/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,23 @@
{
"extends": [
"plugin:@nx/react-typescript",
"next",
"next/core-web-vitals",
"../../.eslintrc.json"
],
"ignorePatterns": [
"!**/*",
".next/**/*"
],
"extends": ["plugin:@nx/react-typescript", "next", "next/core-web-vitals", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*", ".next/**/*"],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@next/next/no-html-link-for-pages": [
"error",
"apps/devtool/pages"
]
"@next/next/no-html-link-for-pages": ["error", "apps/devtool/pages"]
}
},
{
"files": [
"*.ts",
"*.tsx"
],
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": [
"*.spec.ts",
"*.spec.tsx",
"*.spec.js",
"*.spec.jsx"
],
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
"env": {
"jest": true
}
Expand Down
8 changes: 3 additions & 5 deletions apps/devtool/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
declare module '*.svg' {
const content: any;
export const ReactComponent: any;
export default content;
const content: any
export const ReactComponent: any
export default content
}


2 changes: 1 addition & 1 deletion apps/devtool/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default {
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/apps/devtool'
};
}
16 changes: 7 additions & 9 deletions apps/devtool/next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//@ts-check

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { composePlugins, withNx } = require('@nx/next');

const { composePlugins, withNx } = require('@nx/next')

/**
* @type {import('@nx/next/plugins/with-nx').WithNxOptions}
Expand All @@ -11,14 +10,13 @@ const nextConfig = {
nx: {
// Set this to true if you would like to use SVGR
// See: https://github.com/gregberge/svgr
svgr: false,
},
};
svgr: false
}
}

const plugins = [
// Add more Next.js plugins to this list if needed.
withNx,
];

module.exports = composePlugins(...plugins)(nextConfig);
withNx
]

module.exports = composePlugins(...plugins)(nextConfig)
4 changes: 2 additions & 2 deletions apps/devtool/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
autoprefixer: {}
}
}
10 changes: 4 additions & 6 deletions apps/devtool/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,ts,jsx,tsx,mdx}",
],
content: ['./src/**/*.{js,ts,jsx,tsx,mdx}'],
theme: {
extend: {},
extend: {}
},
plugins: [],
}
plugins: []
}
5 changes: 1 addition & 4 deletions apps/devtool/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": [
"jest",
"node"
],
"types": ["jest", "node"],
"jsx": "react"
},
"include": [
Expand Down
4 changes: 2 additions & 2 deletions apps/policy-engine/src/app/__test__/e2e/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { EncryptionService } from '../../../encryption/core/encryption.service'
import { load } from '../../../policy-engine.config'
import { PersistenceModule } from '../../../shared/module/persistence/persistence.module'
import { TestPrismaService } from '../../../shared/module/persistence/service/test-prisma.service'
import { Organization } from '../../../shared/types/entities.types'
import { Criterion, Then, TimeWindow } from '../../../shared/types/policy.type'
import { Organization } from '../../../shared/type/entities.types'
import { Criterion, Then, TimeWindow } from '../../../shared/type/policy.type'
import { EntityRepository } from '../../persistence/repository/entity.repository'

const REQUEST_HEADER_ORG_ID = 'x-org-id'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Decision, EntityType } from '@narval/policy-engine-shared'
import { OpaResult } from '../../../shared/types/rego'
import { OpaResult } from '../../../shared/type/rego'
import { finalizeDecision } from '../../app.service'

describe('finalizeDecision', () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { load } from '../policy-engine.config'
import { KeyValueModule } from '../shared/module/key-value/key-value.module'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { AdminService } from './core/admin.service'
import { AdminService } from './core/service/admin.service'
import { EngineService } from './core/service/engine.service'
import { TenantService } from './core/service/tenant.service'
import { AdminController } from './http/rest/controller/admin.controller'
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { InputType } from 'packages/transaction-request-intent/src/lib/domain'
import { Intent } from 'packages/transaction-request-intent/src/lib/intent.types'
import { Hex, verifyMessage } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { OpaResult, RegoInput } from '../shared/types/domain.type'
import { OpaResult, RegoInput } from '../shared/type/domain.type'
import { OpaService } from './opa/opa.service'
import { EntityRepository } from './persistence/repository/entity.repository'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common'
import { Policy, SetPolicyRulesRequest } from '../../shared/types/policy.type'
import { OpaService } from '../opa/opa.service'
import { Policy, SetPolicyRulesRequest } from '../../../shared/type/policy.type'
import { OpaService } from '../../opa/opa.service'

@Injectable()
export class AdminService {
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/app/core/service/engine.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Config } from '../../../policy-engine.config'
import { Engine } from '../../../shared/types/domain.type'
import { Engine } from '../../../shared/type/domain.type'
import { EngineRepository } from '../../persistence/repository/engine.repository'
import { EngineNotProvisionedException } from '../exception/engine-not-provisioned.exception'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Alg, Curves, KeyTypes, Use } from '@narval/signature'
import { Injectable } from '@nestjs/common'
import { secp256k1 } from '@noble/curves/secp256k1'
import { publicKeyToAddress } from 'viem/utils'
import { EncryptionService } from '../../encryption/core/encryption.service'
import { EncryptionService } from '../../../encryption/core/encryption.service'

// Optional additional configs, such as for MPC-based DKG.
type KeyGenerationOptions = {
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/app/core/service/tenant.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpStatus, Injectable } from '@nestjs/common'
import { ApplicationException } from '../../../shared/exception/application.exception'
import { Tenant } from '../../../shared/types/domain.type'
import { Tenant } from '../../../shared/type/domain.type'
import { TenantRepository } from '../../persistence/repository/tenant.repository'

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Post } from '@nestjs/common'
import { SetPolicyRulesRequest } from '../../../../shared/types/policy.type'
import { AdminService } from '../../../core/admin.service'
import { SetPolicyRulesRequest } from '../../../../shared/type/policy.type'
import { AdminService } from '../../../core/service/admin.service'
import { SetPolicyRulesRequestDto } from '../dto/policy-rules/set-policy-rules-request.dto'
import { SetPolicyRulesResponseDto } from '../dto/policy-rules/set-policy-rules-response.dto'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Action, BaseActionDto, BaseActionRequestDto } from '@narval/policy-engi
import { ApiProperty } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { ArrayNotEmpty, IsDefined, Matches, ValidateNested } from 'class-validator'
import { Policy } from '../../../../../shared/types/policy.type'
import { Policy } from '../../../../../shared/type/policy.type'

export class SetPolicyRulesDto extends BaseActionDto {
@Matches(Action.SET_POLICY_RULES)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { ArrayNotEmpty, IsDefined, IsString, ValidateNested } from 'class-validator'
import { Policy } from '../../../../../shared/types/policy.type'
import { Policy } from '../../../../../shared/type/policy.type'

export class SetPolicyRulesResponseDto {
@IsDefined()
Expand Down
6 changes: 3 additions & 3 deletions apps/policy-engine/src/app/opa/opa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Handlebars from 'handlebars'
import { indexBy } from 'lodash/fp'
import path from 'path'
import { v4 as uuid } from 'uuid'
import { RegoData, UserGroup, WalletGroup } from '../../shared/types/entities.types'
import { Policy } from '../../shared/types/policy.type'
import { OpaResult, RegoInput } from '../../shared/types/rego'
import { RegoData, UserGroup, WalletGroup } from '../../shared/type/entities.types'
import { Policy } from '../../shared/type/policy.type'
import { OpaResult, RegoInput } from '../../shared/type/rego'
import { criterionToString, reasonToString } from '../../shared/utils/opa.utils'
import { EntityRepository } from '../persistence/repository/entity.repository'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApplicationException } from '../../../../../shared/exception/applicatio
import { KeyValueRepository } from '../../../../../shared/module/key-value/core/repository/key-value.repository'
import { KeyValueService } from '../../../../../shared/module/key-value/core/service/key-value.service'
import { InMemoryKeyValueRepository } from '../../../../../shared/module/key-value/persistence/repository/in-memory-key-value.repository'
import { Engine } from '../../../../../shared/types/domain.type'
import { Engine } from '../../../../../shared/type/domain.type'
import { EngineRepository } from '../../engine.repository'

describe(EngineRepository.name, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EncryptionRepository } from '../../../../../encryption/persistence/repo
import { KeyValueRepository } from '../../../../../shared/module/key-value/core/repository/key-value.repository'
import { KeyValueService } from '../../../../../shared/module/key-value/core/service/key-value.service'
import { InMemoryKeyValueRepository } from '../../../../../shared/module/key-value/persistence/repository/in-memory-key-value.repository'
import { Tenant } from '../../../../../shared/types/domain.type'
import { Tenant } from '../../../../../shared/type/domain.type'
import { TenantRepository } from '../../../repository/tenant.repository'

describe(TenantRepository.name, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpStatus, Injectable } from '@nestjs/common'
import { ApplicationException } from '../../../shared/exception/application.exception'
import { KeyValueService } from '../../../shared/module/key-value/core/service/key-value.service'
import { engineSchema } from '../../../shared/schema/engine.schema'
import { Engine } from '../../../shared/types/domain.type'
import { Engine } from '../../../shared/type/domain.type'

@Injectable()
export class EngineRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'
import { KeyValueService } from '../../../shared/module/key-value/core/service/key-value.service'
import { tenantSchema } from '../../../shared/schema/tenant.schema'
import { Tenant } from '../../../shared/types/domain.type'
import { Tenant } from '../../../shared/type/domain.type'

@Injectable()
export class TenantRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InputType, safeDecode } from '@narval/transaction-request-intent'
import { loadPolicy } from '@open-policy-agent/opa-wasm'
import { readFileSync } from 'fs'
import path from 'path'
import { OpaResult, RegoInput } from '../../shared/types/domain.type'
import { OpaResult, RegoInput } from '../../shared/type/domain.type'

export const evaluate = async (users: any[], wallets: any[], legacyActivityRequests: any[]) => {
const entities: { [key: string]: any } = { users: {}, wallets: {} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Policy,
PolicyCriterion,
Then
} from '../../shared/types/policy.type'
} from '../../shared/type/policy.type'

type LegacyPolicy = { [key: string]: string | null }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action, EntityType, UserRole } from '@narval/policy-engine-shared'
import { Criterion, Policy, Then } from '../../shared/types/policy.type'
import { Criterion, Policy, Then } from '../../shared/type/policy.type'

const metaPermissions = [
Action.CREATE_ORGANIZATION,
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/opa/template/mockData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Action, EntityType, FIXTURE, UserRole, ValueOperators } from '@narval/policy-engine-shared'
import { Intents } from '@narval/transaction-request-intent'
import { Criterion, Policy, Then } from '../../shared/types/policy.type'
import { Criterion, Policy, Then } from '../../shared/type/policy.type'

export const examplePermitPolicy: Policy = {
then: Then.PERMIT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NonceRequiredCriterion,
Then,
WalletAddressCriterion
} from '../../types/policy.type'
} from '../../type/policy.type'
import { criterionToString, reasonToString } from '../../utils/opa.utils'

describe('criterionToString', () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/policy-engine/src/shared/utils/opa.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEmpty } from 'lodash'
import { Criterion, Policy, PolicyCriterion, Then } from '../types/policy.type'
import { Criterion, Policy, PolicyCriterion, Then } from '../type/policy.type'

export const criterionToString = (item: PolicyCriterion) => {
const criterion: Criterion = item.criterion
Expand Down

0 comments on commit c3a5611

Please sign in to comment.