Skip to content

Commit e269414

Browse files
Vaibhav  BhallaVaibhav  Bhalla
authored andcommitted
feat(ci-cd): add trivy scan instead of synk
add trivy scan instead of synk and refactor the code GH-151
1 parent dfa5f25 commit e269414

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

.github/workflows/trivy.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This is a basic workflow to help you get started with Actions
2+
3+
name: Trivy Scan
4+
5+
# Controls when the action will run. Triggers the workflow on push or pull request
6+
# events but only for the master branch
7+
on:
8+
pull_request:
9+
branches: [master]
10+
types: [opened, synchronize, reopened]
11+
12+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
13+
jobs:
14+
# This workflow contains a single job called "trivy"
15+
trivy:
16+
# The type of runner that the job will run on
17+
runs-on: [self-hosted, linux, codebuild]
18+
19+
# Steps represent a sequence of tasks that will be executed as part of the job
20+
steps:
21+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
22+
- uses: actions/checkout@v3
23+
24+
- name: Run Trivy vulnerability scanner in repo mode
25+
uses: aquasecurity/[email protected]
26+
with:
27+
scan-type: "fs"
28+
scan-ref: "${{ github.workspace }}"
29+
trivy-config: "${{ github.workspace }}/trivy.yaml"

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
<a href="https://sonarcloud.io/summary/overall?id=sourcefuse_loopback4-authorization" target="_blank">
1010
<img alt="Sonar Quality Gate" src="https://img.shields.io/sonar/quality_gate/sourcefuse_loopback4-authorization?server=https%3A%2F%2Fsonarcloud.io">
1111
</a>
12-
<a href="https://app.snyk.io/org/ashishkaushik/reporting?context[page]=issues-detail&project_target=%255B%2522sourcefuse%252Floopback4-authorization%2522%255D&project_origin=%255B%2522github%2522%255D&issue_status=%255B%2522Open%2522%255D&issue_by=Severity&table_issues_detail_cols=SCORE%257CCVE%257CCWE%257CPROJECT%257CEXPLOIT%2520MATURITY%257CAUTO%2520FIXABLE%257CINTRODUCED%257CSNYK%2520PRODUCT&v=1">
13-
<img alt="Synk Status" src="https://img.shields.io/badge/SYNK_SECURITY-MONITORED-GREEN">
14-
</a>
1512
<a href="https://github.com/sourcefuse/loopback4-authorization/graphs/contributors" target="_blank">
1613
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/sourcefuse/loopback4-authorization">
1714
</a>

src/providers/authorization-action.provider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export class AuthorizeActionProvider implements Provider<AuthorizeFn> {
4040
await this.requestContext.get(CoreBindings.CONTROLLER_METHOD_NAME);
4141
return false;
4242
} catch (error) {
43+
// sonarignore:start
44+
console.log('API not found', error);
45+
// sonarignore:end
4346
throw new HttpErrors.NotFound('API not found !');
4447
}
4548
}

src/providers/casbin-authorization-action.provider.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,11 @@ export class CasbinAuthorizationProvider
3333
resource: string,
3434
request?: Request,
3535
): Promise<boolean> {
36-
let authDecision = false;
3736
try {
3837
// fetch decorator metadata
3938
const metadata: AuthorizationMetadata = await this.getCasbinMetadata();
4039

41-
if (request && this.checkIfAllowedAlways(request)) {
42-
return true;
43-
}
44-
45-
if (metadata?.permissions?.indexOf('*') === 0) {
46-
// Return immediately with true, if allowed to all
47-
// This is for publicly open routes only
40+
if (this.isAlwaysAllowed(request, metadata)) {
4841
return true;
4942
}
5043

@@ -96,16 +89,15 @@ export class CasbinAuthorizationProvider
9689
return false;
9790
}
9891

99-
// Use casbin enforce method to get authorization decision
100-
for (const permission of desiredPermissions) {
101-
const decision = await enforcer.enforce(subject, resource, permission);
102-
authDecision = authDecision || decision;
103-
}
92+
return await this.checkPermissions(
93+
enforcer,
94+
subject,
95+
resource,
96+
desiredPermissions,
97+
);
10498
} catch (err) {
10599
throw new HttpErrors.Unauthorized(err.message);
106100
}
107-
108-
return authDecision;
109101
}
110102

111103
// Generate the user name according to the naming convention
@@ -125,6 +117,33 @@ export class CasbinAuthorizationProvider
125117
}
126118
}
127119

120+
isAlwaysAllowed(
121+
request?: Request,
122+
metadata?: AuthorizationMetadata,
123+
): boolean {
124+
if (request && this.checkIfAllowedAlways(request)) {
125+
return true;
126+
}
127+
if (metadata?.permissions?.indexOf('*') === 0) {
128+
return true;
129+
}
130+
return false;
131+
}
132+
133+
async checkPermissions(
134+
enforcer: casbin.Enforcer,
135+
subject: string,
136+
resource: string,
137+
permissions: string[],
138+
): Promise<boolean> {
139+
for (const permission of permissions) {
140+
if (await enforcer.enforce(subject, resource, permission)) {
141+
return true;
142+
}
143+
}
144+
return false;
145+
}
146+
128147
// Create casbin policy for user based on ResourcePermission data provided by extension client
129148
createCasbinPolicy(
130149
resPermObj: ResourcePermissionObject[],

trivy.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
format: table
2+
exit-code: 1
3+
severity:
4+
- HIGH
5+
- CRITICAL
6+
skip-files:
7+
- db.env
8+
security-checks:
9+
- vuln
10+
- secret
11+
- license
12+
vulnerability:
13+
type:
14+
- os
15+
- library
16+
ignore-unfixed: true

0 commit comments

Comments
 (0)