Skip to content

Commit d8ff6d6

Browse files
Revert changes and add error handling (#113)
* πŸ”– chore: Revert express update * ✨ feat(user-auth-service): Add local error handling * πŸ”– chore: Bump version to 0.11.2 * πŸ› fix(chart): Add exception handling for the process
1 parent 7b79697 commit d8ff6d6

File tree

6 files changed

+69
-70
lines changed

6 files changed

+69
-70
lines changed

β€Žchart/Chart.yamlβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ annotations:
22
licenses: Apache-2.0
33
apiVersion: v2
44
name: unguard
5-
version: 0.11.0
5+
version: 0.11.2
66
description: Unguard is an insecure cloud-native microservices demo application.
77
type: application
88
home: https://github.com/dynatrace-oss/unguard
99
icon: https://github.com/dynatrace-oss/unguard/blob/main/docs/images/logo/unguard-logo-red-small.png
10-
appVersion: 0.11.1
10+
appVersion: 0.11.2

β€Žchart/README.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ The command removes all the Kubernetes components associated with the chart and
8484
To install Unguard in a specific version provide the `--version` flag with the version you want to install:
8585
8686
```sh
87-
helm install unguard oci://ghcr.io/dynatrace-oss/unguard/chart/unguard --version 0.11.1
87+
helm install unguard oci://ghcr.io/dynatrace-oss/unguard/chart/unguard --version 0.11.2
8888
```
8989
9090
## Parameters

β€Žchart/values.yamlβ€Ž

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ maliciousLoadGenerator:
5454
container:
5555
image:
5656
repository: ghcr.io/dynatrace-oss/unguard/unguard-malicious-load-generator
57-
tag: 0.11.1
57+
tag: 0.11.2
5858
pullPolicy: IfNotPresent
5959
ports:
6060
containerPort: 8083
@@ -113,7 +113,7 @@ userSimulator:
113113
container:
114114
image:
115115
repository: ghcr.io/dynatrace-oss/unguard/unguard-user-simulator
116-
tag: 0.11.1
116+
tag: 0.11.2
117117
pullPolicy: IfNotPresent
118118

119119
env:
@@ -144,7 +144,7 @@ membershipService:
144144
container:
145145
image:
146146
repository: ghcr.io/dynatrace-oss/unguard/unguard-membership-service
147-
tag: 0.11.1
147+
tag: 0.11.2
148148
pullPolicy: IfNotPresent
149149
ports:
150150
containerPort: 8083
@@ -174,7 +174,7 @@ userAuthService:
174174
container:
175175
image:
176176
repository: ghcr.io/dynatrace-oss/unguard/unguard-user-auth-service
177-
tag: 0.11.1
177+
tag: 0.11.2
178178
pullPolicy: IfNotPresent
179179
ports:
180180
containerPort: 9091
@@ -209,7 +209,7 @@ adService:
209209
container:
210210
image:
211211
repository: ghcr.io/dynatrace-oss/unguard/unguard-ad-service
212-
tag: 0.11.1
212+
tag: 0.11.2
213213
pullPolicy: IfNotPresent
214214
ports:
215215
containerPort: 8082
@@ -243,7 +243,7 @@ envoyProxy:
243243
container:
244244
image:
245245
repository: ghcr.io/dynatrace-oss/unguard/unguard-envoy-proxy
246-
tag: 0.11.1
246+
tag: 0.11.2
247247
pullPolicy: IfNotPresent
248248
ports:
249249
- name: http
@@ -267,7 +267,7 @@ microblogService:
267267
container:
268268
image:
269269
repository: ghcr.io/dynatrace-oss/unguard/unguard-microblog-service
270-
tag: 0.11.1
270+
tag: 0.11.2
271271
pullPolicy: IfNotPresent
272272
ports:
273273
containerPort: 8080
@@ -320,7 +320,7 @@ statusService:
320320
container:
321321
image:
322322
repository: ghcr.io/dynatrace-oss/unguard/unguard-status-service
323-
tag: 0.11.1
323+
tag: 0.11.2
324324
pullPolicy: IfNotPresent
325325
ports:
326326
containerPort: 8083
@@ -375,7 +375,7 @@ proxyService:
375375
container:
376376
image:
377377
repository: ghcr.io/dynatrace-oss/unguard/unguard-proxy-service
378-
tag: 0.11.1
378+
tag: 0.11.2
379379
pullPolicy: IfNotPresent
380380
ports:
381381
containerPort: 8081
@@ -402,7 +402,7 @@ likeService:
402402
container:
403403
image:
404404
repository: ghcr.io/dynatrace-oss/unguard/unguard-like-service
405-
tag: 0.11.1
405+
tag: 0.11.2
406406
pullPolicy: IfNotPresent
407407
ports:
408408
containerPort: 8000
@@ -438,7 +438,7 @@ paymentService:
438438
container:
439439
image:
440440
repository: ghcr.io/dynatrace-oss/unguard/unguard-payment-service
441-
tag: 0.11.1
441+
tag: 0.11.2
442442
pullPolicy: IfNotPresent
443443
env:
444444
API_PATH: /payment-service
@@ -468,7 +468,7 @@ frontend:
468468
container:
469469
image:
470470
repository: ghcr.io/dynatrace-oss/unguard/unguard-frontend
471-
tag: 0.11.1
471+
tag: 0.11.2
472472
pullPolicy: IfNotPresent
473473
ports:
474474
containerPort: 3000

β€Žsrc/user-auth-service/app.jsβ€Ž

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,24 @@ const authRouter = require('./routes/auth');
3232
const jwtRouter = require('./routes/jwt');
3333

3434
const app = express();
35+
process.on('uncaughtException', (err, origin) => {
36+
console.error(err);
37+
});
3538

3639
logger.token('userid', function (req) {
37-
try {
38-
const userid = req.body.userid ? `User ID: ${req.body.userid}`: '-';
39-
return userid;
40-
} catch (e) {
41-
return "{ }"
42-
}
43-
40+
const userid = req.body.userid ? `User ID: ${req.body.userid}`: '-';
41+
return userid;
4442
});
4543

4644
logger.token('body', function (req) {
47-
try {
48-
const maxLoggingLength = 30;
49-
Object.keys(req.body).forEach((key) => {
50-
if (req.body[key] && req.body[key].length > maxLoggingLength) {
51-
req.body[key] = req.body[key].substr(0, maxLoggingLength) + '...'
52-
}
53-
})
54-
55-
return JSON.stringify(req.body);
56-
} catch {
57-
return "{ }"
58-
}
45+
const maxLoggingLength = 30;
46+
Object.keys(req.body).forEach((key) => {
47+
if (req.body[key] && req.body[key].length > maxLoggingLength) {
48+
req.body[key] = req.body[key].substr(0, maxLoggingLength) + '...'
49+
}
50+
})
5951

52+
return JSON.stringify(req.body);
6053
});
6154

6255
if (process.env.NODE_ENV !== 'production') {

β€Žsrc/user-auth-service/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"colors": "^1.4.0",
1111
"cookie-parser": "~1.4.4",
1212
"debug": "~2.6.9",
13-
"express": "~5.1.0",
13+
"express": "~4.16.1",
1414
"http-errors": "~1.6.3",
1515
"jaeger-client": "^3.15.0",
1616
"jsonwebtoken": "^8.5.1",

β€Žsrc/user-auth-service/routes/user.jsβ€Ž

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,54 @@ router.get('/register', async function (req, res) {
5555
});
5656
});
5757

58-
router.get('/login', async function (req, res) {
59-
const username = req.query.username;
60-
const password = req.query.password;
61-
62-
// check if user exists
63-
// vulnerable to sql injection because prepared statements are not used
64-
// https://snyk.io/de/blog/preventing-sql-injection-attacks-node-js/
65-
const vulnerableQuery = database.checkUserExistsQuery.replace('?', `"${username}"`);
66-
const result = await database.dbConnection.query(vulnerableQuery);
67-
if (result[0].length < 1) {
68-
res.status(404).json({ message: "Given user does not exists!" })
69-
return
70-
}
71-
72-
const user = result[0][0];
73-
const roles = await database.dbConnection.query(database.selectUserWithRole, [ user.id ]).then((response) => {
74-
const userWithRoles = response[0];
75-
if (userWithRoles.length === 0) {
76-
return [];
58+
router.get('/login', async function (req, res, next) {
59+
try {
60+
const username = req.query.username;
61+
const password = req.query.password;
62+
63+
// check if user exists
64+
// vulnerable to sql injection because prepared statements are not used
65+
// https://snyk.io/de/blog/preventing-sql-injection-attacks-node-js/
66+
const vulnerableQuery = database.checkUserExistsQuery.replace('?', `"${username}"`);
67+
const result = await database.dbConnection.query(vulnerableQuery);
68+
if (result[0].length < 1) {
69+
res.status(404).json({message: "Given user does not exists!"})
70+
return
7771
}
7872

79-
const userRoles = [];
80-
userWithRoles.forEach(user => {
81-
if (user.role_name !== null) {
82-
userRoles.push("" + user.role_name);
73+
const user = result[0][0];
74+
const roles = await database.dbConnection.query(database.selectUserWithRole, [user.id]).then((response) => {
75+
const userWithRoles = response[0];
76+
if (userWithRoles.length === 0) {
77+
return [];
8378
}
79+
80+
const userRoles = [];
81+
userWithRoles.forEach(user => {
82+
if (user.role_name !== null) {
83+
userRoles.push("" + user.role_name);
84+
}
85+
});
86+
87+
return userRoles;
8488
});
8589

86-
return userRoles;
87-
});
8890

91+
bcrypt.compare(password, user.password_hash, function (err, compareResult) {
92+
if (compareResult) {
93+
res.json({
94+
result: "successfully logged in!",
95+
jwt: jwtUtil.generateJwtAccessToken(username, user.id, roles)
96+
})
97+
} else {
98+
res.status(401).json({message: 'Wrong password!'})
99+
}
100+
});
89101

90-
bcrypt.compare(password, user.password_hash, function (err, compareResult) {
91-
if (compareResult) {
92-
res.json({
93-
result: "successfully logged in!",
94-
jwt: jwtUtil.generateJwtAccessToken(username, user.id, roles)
95-
})
96-
} else {
97-
res.status(401).json({ message: 'Wrong password!' })
98-
}
99-
});
102+
} catch (err) {
103+
console.error(err);
104+
next(err);
105+
}
100106
});
101107

102108
router.post('/username', async function (req, res) {

0 commit comments

Comments
Β (0)