Skip to content

Commit 84ec180

Browse files
authored
Upgrade bcrypt to use node v16 (#666)
* Upgrade bcrypt to use node v16 * Update Dockerfile and versions * Update test Dockerfile to match production image * Install git because it is used by scripts * Copy version file * Update user permissions * Create image folder * Remove unused files and folders * Update test Dockerfile * Update restify to v7.7.0 * Update nodejs base image to latest v16
1 parent ac1fa31 commit 84ec180

File tree

9 files changed

+381
-284
lines changed

9 files changed

+381
-284
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ version.js
1313
*.md
1414
*-dev.sh
1515
*.log
16-
.github
1716
tests
1817
apidoc
1918
apidoc.json
19+
images

Dockerfile

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
1-
FROM node:14.18-alpine as build
1+
# Image selected based on https://snyk.io/blog/choosing-the-best-node-js-docker-image/
2+
# Used best practices from https://snyk.io/blog/10-best-practices-to-containerize-nodejs-web-applications-with-docker/
23

3-
ENV NODE_ENV=production
4+
# --------------> The build image
5+
FROM node:16.19.0-bullseye-slim as build
46

5-
RUN apk --no-cache --virtual .build add build-base python2 git
7+
RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends git dumb-init
68

7-
# taken from node:6-onbuild
8-
#RUN mkdir -p /usr/src/app
99
WORKDIR /usr/src/app
1010

11-
# copy in main package.json and yarn.lock
11+
# Copy in main package.json and yarn.lock
1212
COPY package.json /usr/src/app/
1313
COPY yarn.lock /usr/src/app/
14-
# copy in workspace package.json files
14+
15+
# Copy in workspace package.json files
1516
COPY packages/api/package.json /usr/src/app/packages/api/
1617
COPY packages/models/package.json /usr/src/app/packages/models/
1718

1819
RUN yarn install --pure-lockfile --production
1920

2021
COPY . /usr/src/app
2122

22-
RUN yarn create-version-file \
23-
&& rm -rf .git .scripts
23+
RUN yarn create-version-file
2424

25-
# Final stage
26-
FROM node:14.18-alpine
25+
# --------------> The production image
26+
FROM node:16.19.0-bullseye-slim
2727

2828
ENV NODE_ENV=production
29+
COPY --from=build /usr/bin/dumb-init /usr/bin/dumb-init
30+
USER node
2931

3032
WORKDIR /usr/src/app
31-
COPY --from=build /usr/src/app /usr/src/app
3233

33-
CMD [ "yarn", "start" ]
34+
COPY --chown=node:node --from=build /usr/src/app/node_modules /usr/src/app/node_modules
35+
COPY --chown=node:node --from=build /usr/src/app/version.js /usr/src/app/version.js
36+
COPY --chown=node:node . /usr/src/app
37+
38+
# Remove unused files and folders
39+
RUN rm -rf .git .scripts
40+
41+
# Create and change ownership of folder to store uploaded images
42+
RUN mkdir -p /usr/src/app/dist/userimages && chown node:node /usr/src/app/dist/userimages
43+
44+
CMD ["dumb-init", "node", "packages/api/app.js"]

packages/api/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const log = bunyan.createLogger({ name: 'opensensemap-api', serializers: bunyan.
2323

2424
const server = restify.createServer({
2525
name: `opensensemap-api (${getVersion})`,
26-
log
26+
log,
27+
onceNext: true,
28+
strictNext: false,
2729
});
2830

2931
// We're using caddy as proxy. It supplies a 'X-Forwarded-Proto' header

packages/api/lib/routes.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ const routes = {
116116
{ path: `${managementPath}/boxes/:boxId`, method: 'get', handler: managementController.getBox, reference: 'api-Admin-getBox' },
117117
{ path: `${managementPath}/boxes/:boxId`, method: 'put', handler: managementController.updateBox, reference: 'api-Admin-updateBox' },
118118
{ path: `${managementPath}/boxes/delete`, method: 'post', handler: managementController.deleteBoxes, reference: 'api-Admin-deleteBoxes' },
119-
120119
{ path: `${managementPath}/users`, method: 'get', handler: managementController.listUsers, reference: 'api-Admin-listUsers' },
121120
{ path: `${managementPath}/users/:userId`, method: 'get', handler: managementController.getUser, reference: 'api-Admin-getUser' },
122121
{ path: `${managementPath}/users/:userId`, method: 'put', handler: managementController.updateUser, reference: 'api-Admin-updateUser' },
@@ -136,16 +135,21 @@ const initRoutes = function initRoutes (server) {
136135
}
137136

138137
// Attach secured routes (needs authorization through jwt)
139-
server.use(verifyJwt);
140-
138+
// The .use() method runs now for all routes
139+
// https://github.com/restify/node-restify/issues/1685
141140
for (const route of routes.auth) {
142-
server[route.method]({ path: route.path }, route.handler);
141+
server[route.method]({ path: route.path }, [verifyJwt, route.handler]);
143142
}
144143

145-
server.use(checkPrivilege);
146-
144+
// Attach verifyJwt and checkPrivilage routes (needs authorization through jwt)
145+
// The .use() method runs now for all routes
146+
// https://github.com/restify/node-restify/issues/1685
147147
for (const route of routes.management) {
148-
server[route.method]({ path: route.path }, route.handler);
148+
server[route.method]({ path: route.path }, [
149+
verifyJwt,
150+
checkPrivilege,
151+
route.handler,
152+
]);
149153
}
150154
};
151155

packages/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"millify": "^5.0.1",
3232
"moment": "^2.29.4",
3333
"ms": "^2.1.3",
34-
"restify": "^5.2.0",
34+
"restify": "7.7.0",
3535
"restify-errors": "^8.0.2",
3636
"simple-statistics": "^7.7.0",
3737
"stringify-stream": "^1.0.5",

packages/models/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@grpc/proto-loader": "^0.6.4",
1010
"@sensebox/osem-protos": "^1.1.0",
1111
"@sensebox/sketch-templater": "1.12.1",
12-
"bcrypt": "^5.0.1",
12+
"bcrypt": "^5.1.0",
1313
"bunyan": "^1.8.15",
1414
"config": "^3.3.6",
1515
"got": "^11.8.2",

tests/tests-Dockerfile

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
FROM node:14.18-alpine
1+
FROM node:16.19.0-bullseye-slim
22

33
# YARN_PRODUCTION=false is a workaround for https://github.com/yarnpkg/yarn/issues/4557
44
ENV NODE_ENV=production \
55
YARN_PRODUCTION=false
66

7-
# taken from node:6-onbuild
8-
RUN mkdir -p /usr/src/app
7+
RUN apt-get update \
8+
&& apt-get upgrade -y \
9+
&& apt-get install -y --no-install-recommends git dumb-init
10+
911
WORKDIR /usr/src/app
1012

1113
# COPY in dev versions
1214
COPY . /usr/src/app
1315

14-
RUN apk --no-cache --virtual .build add build-base python2 git \
15-
&& yarn install --pure-lockfile --production=false \
16-
&& apk del .build
17-
COPY . /usr/src/app
16+
RUN yarn install --pure-lockfile --production=false
1817

19-
# for git 2.1.4
20-
RUN apk --no-cache --virtual .git add git \
21-
&& yarn create-version-file \
22-
&& rm -rf .git \
23-
&& apk del .git
18+
RUN yarn create-version-file \
19+
&& rm -rf .git
2420

25-
CMD [ "yarn", "start" ]
21+
CMD ["dumb-init", "yarn", "start"]

tests/tests/004-users-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ describe('openSenseMap API Routes: /users', function () {
129129
});
130130

131131
it('should deny to change email and password at the same time', function () {
132+
this.timeout(120000);
133+
132134
return chakram.put(`${BASE_URL}/users/me`, { email: '[email protected]', newPassword: '87654321' }, { headers: { 'Authorization': `Bearer ${jwt}` } })
133135
.then(function (response) {
134136
expect(response).to.have.status(400);

0 commit comments

Comments
 (0)