Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sandbox): fix the docker compose for sandbox #2132

Merged
merged 5 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"markdown.extension.toc.levels": "2..2",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
}
}
}
60,924 changes: 36,044 additions & 24,880 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"packages/cache",
"packages/feature-toggle",
"packages/custom-sf-changelog/",
"services/*"
"services/*",
"sandbox/*"
]
}
2 changes: 1 addition & 1 deletion sandbox/audit-ms-example/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:16-alpine
FROM node:18-alpine

# Set to a non-root built-in user `node`
USER node
Expand Down
70 changes: 5 additions & 65 deletions sandbox/auth-mfa-example/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,88 +1,28 @@
# Check out https://hub.docker.com/_/node to select a new base image
ARG nodeVer=18-alpine

# select a base image to build from
FROM node:$nodeVer AS BASE

# Take the build variables for image base
ARG SERVICE_NAME
ARG FROM_FOLDER
# This command is used to install some dependencies in the Docker image.
# Nessasary for running node-prune and npm install
RUN apk update && apk add --no-cache --virtual .gyp \
python3 \
make \
g++ \
bash \
curl

# This is used to download and install the `node-prune` tool in the Docker image.
RUN curl -sfL https://gobinaries.com/tj/node-prune | bash -s -- -b /usr/local/bin
FROM node:18-alpine

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

# Set the working directory to `/home/node/app`
WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

# The following two commands is used to copy the `packages`,`service` directory from the local file system to the Docker image.
# The `--chown=node` flag ensures that the ownership of the copied files/directories is set to the `node` user.
# This is important because the subsequent commands in the Dockerfile are executed with the `node` user,
# and it needs the appropriate permissions to access and modify the copied files/directories.
COPY --chown=node packages ./packages

COPY --chown=node $FROM_FOLDER/$SERVICE_NAME ./$FROM_FOLDER/$SERVICE_NAME

# Installing all dependencies
RUN npm install

# Building the app
# set the Working Directory to the service
WORKDIR /home/node/app/$FROM_FOLDER/$SERVICE_NAME
# Run Build Command
RUN npm run build

# Run node-prune
RUN npm prune --production
RUN /usr/local/bin/node-prune

# Start fresh for a smaller image size
FROM node:$nodeVer

# Take the build variables for image stage
ARG SERVICE_NAME
ARG FROM_FOLDER

RUN mkdir -p /home/node/app

USER node

WORKDIR /home/node/app


# These `COPY` commands are used to copy files and directories from the `BASE`
# stage of the Docker image to the current stage.
COPY --from=BASE --chown=node /home/node/app/node_modules ./node_modules
COPY --from=BASE --chown=node /home/node/app/package.json ./
COPY --from=BASE --chown=node /home/node/app/package-lock.json ./
COPY --from=BASE --chown=node /home/node/app/packages ./packages
COPY --from=BASE --chown=node /home/node/app/$FROM_FOLDER/$SERVICE_NAME ./$FROM_FOLDER/$SERVICE_NAME

# Set the working directory to `/home/node/app/services/auth-service`
WORKDIR /home/node/app/$FROM_FOLDER/$SERVICE_NAME
# Bundle app source code
COPY --chown=node . .

RUN npm run build

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}

CMD [ "node", "." ]
CMD [ "node", "." ]
2 changes: 1 addition & 1 deletion sandbox/auth-mfa-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"eslint": "eslint --report-unused-disable-directives .",
"eslint:fix": "npm run eslint -- --fix",
"pretest": "npm run rebuild",
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
"test": "echo 'No Tests'",
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js",
"docker:build": "DOCKER_BUILDKIT=1 sudo docker build --build-arg NR_ENABLED=$NR_ENABLED_VALUE --build-arg FROM_FOLDER=services --build-arg SERVICE_NAME=authentication-service -t $IMAGE_REPO_NAME/$npm_package_name:$npm_package_version ../../. -f ./Dockerfile",
"docker:run": "docker run -p 3000:3000 -d authentication",
Expand Down
2 changes: 1 addition & 1 deletion sandbox/auth-ms-basic-example/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:16-alpine
FROM node:18-alpine

# Set to a non-root built-in user `node`
USER node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Getter, inject } from '@loopback/core';
import {Getter, inject} from '@loopback/core';
import {
BelongsToAccessor,
DataObject,
Expand All @@ -8,7 +8,7 @@ import {
juggler,
repository,
} from '@loopback/repository';
import { HttpErrors } from '@loopback/rest';
import {HttpErrors} from '@loopback/rest';
import {
AuthDbSourceName,
OtpRepository,
Expand All @@ -30,7 +30,7 @@ import {
UserStatus,
} from '@sourceloop/core';
import * as bcrypt from 'bcrypt';
import { AuthErrorKeys } from 'loopback4-authentication';
import {AuthErrorKeys} from 'loopback4-authentication';

const saltRounds = 10;

Expand Down Expand Up @@ -119,13 +119,12 @@ export class UserRepository extends DefaultSoftCrudRepository<

async verifyPassword(username: string, password: string): Promise<User> {
const user = await super.findOne({
where: { username: username.toLowerCase() },
where: {username: username.toLowerCase()},
});
const creds = user && (await this.credentials(user.id).get());
if (!user || user.deleted) {
throw new HttpErrors.Unauthorized(AuthenticateErrorKeys.UserDoesNotExist);
} else if (
// eslint-disable-next-line
!creds?.password ||
creds.authProvider !== AuthProvider.INTERNAL ||
!(await bcrypt.compare(password, creds.password))
Expand All @@ -142,7 +141,7 @@ export class UserRepository extends DefaultSoftCrudRepository<
password: string,
newPassword: string,
): Promise<User> {
const user = await super.findOne({ where: { username } });
const user = await super.findOne({where: {username}});
const creds = user && (await this.credentials(user.id).get());
if ((!user || user.deleted) ?? !creds?.password) {
throw new HttpErrors.Unauthorized(AuthenticateErrorKeys.UserDoesNotExist);
Expand Down Expand Up @@ -170,7 +169,7 @@ export class UserRepository extends DefaultSoftCrudRepository<
newPassword: string,
oldPassword?: string,
): Promise<User> {
const user = await super.findOne({ where: { username } });
const user = await super.findOne({where: {username}});
const creds = user && (await this.credentials(user.id).get());

if (oldPassword) {
Expand Down Expand Up @@ -208,7 +207,7 @@ export class UserRepository extends DefaultSoftCrudRepository<
lastLogin: Date.now(),
},
{
currentUser: { id: userId },
currentUser: {id: userId},
},
);
}
Expand Down
2 changes: 2 additions & 0 deletions sandbox/auth-public-private-client/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules/
dist/
coverage/
migrations/
migration.js
.eslintrc.js
2 changes: 1 addition & 1 deletion sandbox/auth-public-private-client/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports = {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
};
};
70 changes: 5 additions & 65 deletions sandbox/auth-public-private-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,88 +1,28 @@
# Check out https://hub.docker.com/_/node to select a new base image
ARG nodeVer=18-alpine

# select a base image to build from
FROM node:$nodeVer AS BASE

# Take the build variables for image base
ARG SERVICE_NAME
ARG FROM_FOLDER
# This command is used to install some dependencies in the Docker image.
# Nessasary for running node-prune and npm install
RUN apk update && apk add --no-cache --virtual .gyp \
python3 \
make \
g++ \
bash \
curl

# This is used to download and install the `node-prune` tool in the Docker image.
RUN curl -sfL https://gobinaries.com/tj/node-prune | bash -s -- -b /usr/local/bin
FROM node:18-alpine

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

# Set the working directory to `/home/node/app`
WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

# The following two commands is used to copy the `packages`,`service` directory from the local file system to the Docker image.
# The `--chown=node` flag ensures that the ownership of the copied files/directories is set to the `node` user.
# This is important because the subsequent commands in the Dockerfile are executed with the `node` user,
# and it needs the appropriate permissions to access and modify the copied files/directories.
COPY --chown=node packages ./packages

COPY --chown=node $FROM_FOLDER/$SERVICE_NAME ./$FROM_FOLDER/$SERVICE_NAME

# Installing all dependencies
RUN npm install

# Building the app
# set the Working Directory to the service
WORKDIR /home/node/app/$FROM_FOLDER/$SERVICE_NAME
# Run Build Command
RUN npm run build

# Run node-prune
RUN npm prune --production
RUN /usr/local/bin/node-prune

# Start fresh for a smaller image size
FROM node:$nodeVer

# Take the build variables for image stage
ARG SERVICE_NAME
ARG FROM_FOLDER

RUN mkdir -p /home/node/app

USER node

WORKDIR /home/node/app


# These `COPY` commands are used to copy files and directories from the `BASE`
# stage of the Docker image to the current stage.
COPY --from=BASE --chown=node /home/node/app/node_modules ./node_modules
COPY --from=BASE --chown=node /home/node/app/package.json ./
COPY --from=BASE --chown=node /home/node/app/package-lock.json ./
COPY --from=BASE --chown=node /home/node/app/packages ./packages
COPY --from=BASE --chown=node /home/node/app/$FROM_FOLDER/$SERVICE_NAME ./$FROM_FOLDER/$SERVICE_NAME

# Set the working directory to `/home/node/app/services/auth-service`
WORKDIR /home/node/app/$FROM_FOLDER/$SERVICE_NAME
# Bundle app source code
COPY --chown=node . .

RUN npm run build

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}

CMD [ "node", "." ]
CMD [ "node", "." ]
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ exports.setup = function (options, seedLink) {
};

exports.up = function (db) {
var filePath = path.join(
__dirname,
'sqls',
'20240502140840-seed-up.sql',
);
var filePath = path.join(__dirname, 'sqls', '20240502140840-seed-up.sql');
return new Promise(function (resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
Expand All @@ -37,11 +33,7 @@ exports.up = function (db) {
};

exports.down = function (db) {
var filePath = path.join(
__dirname,
'sqls',
'20240502140840-seed-up.sql',
);
var filePath = path.join(__dirname, 'sqls', '20240502140840-seed-up.sql');
return new Promise(function (resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
Expand Down
2 changes: 1 addition & 1 deletion sandbox/auth-public-private-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"eslint": "eslint --report-unused-disable-directives .",
"eslint:fix": "npm run eslint -- --fix",
"pretest": "npm run rebuild",
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
"test": "echo 'No Tests'",
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js",
"docker:build": "DOCKER_BUILDKIT=1 sudo docker build --build-arg NR_ENABLED=$NR_ENABLED_VALUE --build-arg FROM_FOLDER=services --build-arg SERVICE_NAME=authentication-service -t $IMAGE_REPO_NAME/$npm_package_name:$npm_package_version ../../. -f ./Dockerfile",
"docker:run": "docker run -p 3000:3000 -d auth",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ export async function setupApplication(): Promise<AppWithClient> {
rest: restConfig,
});

app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});


app.bind(`datasources.config.${process.env.REDIS_NAME}`).to({
name: process.env.REDIS_NAME,
connector: 'kv-memory',
});


app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});

app.bind(`datasources.config.${process.env.REDIS_NAME}`).to({
name: process.env.REDIS_NAME,
connector: 'kv-memory',
});

await app.boot();
await app.start();

Expand All @@ -43,8 +41,8 @@ function setUpEnv() {
process.env.NODE_ENV = 'test';
process.env.ENABLE_TRACING = '0';
process.env.ENABLE_OBF = '0';
process.env.REDIS_NAME = 'redis';
}
process.env.REDIS_NAME = 'redis';
}

export interface AppWithClient {
app: AuthApplication;
Expand Down
Loading
Loading