Browse Source

auth

master
Mayank 4 years ago
parent
commit
c80e6cdb93
No known key found for this signature in database GPG Key ID: D037D60476CE748C
  1. 1
      .gitignore
  2. 2
      Dockerfile
  3. 33
      Dockerfile.armhf
  4. 19
      build-docker.sh
  5. 88
      buildspec.yml
  6. 39
      middlewares/auth.js
  7. 4
      routes/v1/bitcoind/info.js
  8. 4
      routes/v1/lnd/info.js
  9. 4
      utils/const.js
  10. 10
      utils/logger.js

1
.gitignore

@ -10,3 +10,4 @@ package-lock.json
lb_settings.json
.nyc_output
coverage
.todo

2
Dockerfile

@ -24,5 +24,5 @@ COPY . .
RUN mkdir -p /root/.lnd
EXPOSE 3005
EXPOSE 3006
CMD [ "npm", "start" ]

33
Dockerfile.armhf

@ -1,33 +0,0 @@
# specify the node base image with your desired version
FROM balenalib/armv7hf-node:8-stretch-run
# need qemu to emulate arm architecture
# can be downloaded here, $ docker run -v /usr/bin/qemu-arm-static:/usr/bin/qemu-arm-static --rm -ti arm32v7/debian:stretch-slim
COPY ./qemu-arm-static /usr/bin/qemu-arm-static
# install tools
RUN apt-get update --no-install-recommends \
&& apt-get install -y --no-install-recommends curl \
&& apt-get install -y --no-install-recommends rsync \
&& apt-get install -y --no-install-recommends vim \
&& rm -rf /var/lib/apt/lists/*
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
RUN mkdir -p /root/.lnd
EXPOSE 3005
CMD [ "npm", "start" ]

19
build-docker.sh

@ -1,19 +0,0 @@
#!/usr/bin/env bash
if [[ "$ARCH" != "arm" && "$ARCH" != "x86" ]] ; then
echo 'Please set an ARCH to x86 or arm.'
return
fi
if [[ -z "$ENV" ]] ; then
echo 'Please set an ENV variable.'
return
fi
DOCKERFILE="Dockerfile"
if [ "$ARCH" = "arm" ]; then
DOCKERFILE="Dockerfile.armhf"
fi
docker build . -f $DOCKERFILE -t casanodeinternal/lnapi:$ARCH-$ENV
docker push casanodeinternal/lnapi:$ARCH-$ENV

88
buildspec.yml

@ -1,88 +0,0 @@
version: 0.2
env:
parameter-store:
DOCKER_HUB_USER: "/Casanode/DockerHub/User"
DOCKER_HUB_PASS: "/Casanode/DockerHub/Pass"
GITHUB_USER: "/Casanode/Git/User"
GITHUB_PASS: "/Casanode/Git/Pass"
phases:
pre_build:
commands:
- echo Installing source NPM dependencies...
- npm install
install:
commands:
# CodePipeline creates artifacts using zip format, which does not preserve the permissions/modes.
# we must reset permissions here
- chmod 755 pre-commit qemu-arm-static
build:
commands:
- echo Running tests
- if [ -z $CODECOV_TOKEN ]; then npm run test; else npm run coverage; fi
- echo Building the Docker image ...
# building
# Remove qemu-static if non-arm, or register
- |
if [ $ARCH != arm ]; then
rm qemu-arm-static
else
docker run --rm --privileged multiarch/qemu-user-static:register --reset
fi
- docker build . -f $DOCKERFILE -t $ORGANIZATION/$REPOSITORY:$ARCH
- docker tag $ORGANIZATION/$REPOSITORY:$ARCH $ORGANIZATION/$REPOSITORY:$ARCH
# Hacky way to determine what branch we are in. $CODEBUILD_SOURCE_VERSION is the git commit we are currently
# building. We search all local branches to get a list of branches that include that git commit. We return all
# alpha characters from the branch we are looking for.
#
# Ex.
# master => master
# release/1.0.0 => release
- masterBranchText=$(git branch --contains $CODEBUILD_SOURCE_VERSION | grep master | sed 's/[^a-zA-Z]//g')
- releaseBranchText=$(git branch --contains $CODEBUILD_SOURCE_VERSION | grep release | sed 's/[^a-zA-Z]//g')
- developBranchText=$(git branch --contains $CODEBUILD_SOURCE_VERSION | grep develop | sed 's/[^a-zA-Z]//g')
# push image to docker
- docker login --username=$DOCKER_HUB_USER --password=$DOCKER_HUB_PASS
# Since a git commit can be in multiple branches, we will start with master and work our way down. If a git commit
# exists in master, release, and develop, it really means we want to deploy master. Likewise, if a git commit exists
# in release and develop, we release want to deploy release.
#
# Public vs Private
# Casa Inc releases code publicly for various reasons. We also develop features in private before the public
# release. Historically we have used the private casacomputer organization on docker hub. We have since migrated
# to casanode for our public releases. We will support legacy nodes running on casacomputer until March 2020.
- |
if [ "$masterBranchText" = "master" ] && [ "$PUBLIC" = "false" ]; then
echo "pushing master branch to docker hub"
docker tag $ORGANIZATION/$REPOSITORY:$ARCH $ORGANIZATION/$REPOSITORY:$ARCH
docker push $ORGANIZATION/$REPOSITORY:$ARCH
elif [ "$masterBranchText" = "master" ] && [ "$PUBLIC" = "true" ]; then
echo "pushing master branch to docker hub"
docker tag $ORGANIZATION/$REPOSITORY:$ARCH $ORGANIZATION/$REPOSITORY:$ARCH
docker push $ORGANIZATION/$REPOSITORY:$ARCH
echo "pushing master branch to legacy docker hub"
docker tag casacomputer/$REPOSITORY:$ARCH casacomputer/$REPOSITORY:$ARCH
docker push casacomputer/$REPOSITORY:$ARCH
elif [ "$releaseBranchText" = "release" ] && [ "$PUBLIC" = "false" ]; then
echo "pushing release branch to docker hub"
docker tag $ORGANIZATION/$REPOSITORY:$ARCH $ORGANIZATION/$REPOSITORY:$ARCH-stage
docker push $ORGANIZATION/$REPOSITORY:$ARCH-stage
elif [ "$developBranchText" = "develop" ] && [ "$PUBLIC" = "false" ]; then
echo "pushing develop branch to docker hub"
docker tag $ORGANIZATION/$REPOSITORY:$ARCH $ORGANIZATION/$REPOSITORY:$ARCH-develop
docker push $ORGANIZATION/$REPOSITORY:$ARCH-develop
else
echo "docker image has been built, but not pushed to docker hub"
fi
post_build:
commands:
- echo Build completed on `date`
cache:
paths:
- 'node_modules/**/*'

39
middlewares/auth.js

@ -2,6 +2,7 @@ const passport = require('passport');
const passportJWT = require('passport-jwt');
const constants = require('utils/const.js');
const NodeError = require('models/errors.js').NodeError;
const diskService = require('services/disk.js');
var JwtStrategy = passportJWT.Strategy;
var ExtractJwt = passportJWT.ExtractJwt;
@ -12,31 +13,41 @@ passport.serializeUser(function (user, done) {
return done(null, user.id);
});
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('jwt'),
secretOrKey: Buffer.from(constants.JWT_PUBLIC_KEY, 'hex'), // The `manager` will pass the public key as hex.
algorithm: 'RS256'
};
async function createJwtOptions() {
const pubKey = await diskService.readFile(constants.JWT_PUBLIC_KEY_FILE);
return {
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('jwt'),
secretOrKey: pubKey,
algorithm: 'RS256'
};
}
createJwtOptions().then(function (data) {
const jwtOptions = data;
passport.use(JWT_AUTH, new JwtStrategy(jwtOptions, function (jwtPayload, done) {
return done(null, { id: jwtPayload.id });
}));
passport.use(JWT_AUTH, new JwtStrategy(jwtOptions, function (jwtPayload, done) {
return done(null, { id: jwtPayload.id });
}));
});
function jwt(req, res, next) {
passport.authenticate(JWT_AUTH, { session: false }, function (error, user) {
//if (error || user === false) {
//return next(new NodeError('Invalid JWT', 401)); // eslint-disable-line no-magic-numbers
//}
if (error || user === false) {
return next(new NodeError('Invalid JWT', 401)); // eslint-disable-line no-magic-numbers
}
req.logIn(user, function (err) {
//if (err) {
//return next(new NodeError('Unable to authenticate', 401)); // eslint-disable-line no-magic-numbers
//}
if (err) {
return next(new NodeError('Unable to authenticate', 401)); // eslint-disable-line no-magic-numbers
}
return next(null, user);
});
})(req, res, next);
}
module.exports = {
jwt,
};

4
routes/v1/bitcoind/info.js

@ -25,7 +25,9 @@ router.get('/connections', auth.jwt, safeHandler((req, res) =>
.then(connections => res.json(connections))
));
router.get('/status', auth.jwt, safeHandler((req, res) =>
//requires no authentication as it is used to fetch loading status
//which could be fetched at login/signup page
router.get('/status', safeHandler((req, res) =>
bitcoind.getStatus()
.then(status => res.json(status))
));

4
routes/v1/lnd/info.js

@ -11,7 +11,9 @@ router.get('/uris', auth.jwt, safeHandler((req, res) =>
.then(uris => res.json(uris))
));
router.get('/status', auth.jwt, safeHandler((req, res) =>
//requires no authentication as it is used to fetch loading status
//which could be fetched at login/signup page
router.get('/status', safeHandler((req, res) =>
lightning.getStatus()
.then(status => res.json(status))
));

4
utils/const.js

@ -5,9 +5,9 @@ module.exports = {
UNAVAILABLE: 14,
UNKNOWN: 2,
},
JWT_PUBLIC_KEY: process.env.JWT_PUBLIC_KEY || 'UNKNOWN',
JWT_PUBLIC_KEY_FILE: process.env.JWT_PUBLIC_KEY_FILE || 'UNKNOWN',
MANAGED_CHANNELS_FILE: '/channel-data/managedChannels.json',
REQUEST_CORRELATION_NAMESPACE_KEY: 'lnapi-request',
REQUEST_CORRELATION_NAMESPACE_KEY: 'umbrel-middleware-request',
REQUEST_CORRELATION_ID_KEY: 'reqId',
STATUS_CODES: {
BAD_GATEWAY: 502,

10
utils/logger.js

@ -4,8 +4,8 @@ const constants = require('utils/const.js');
const fs = require('fs');
const path = require('path');
const winston = require('winston');
const {format} = require('winston');
const {combine, timestamp, printf} = format;
const { format } = require('winston');
const { combine, timestamp, printf } = format;
const getNamespace = require('continuation-local-storage').getNamespace;
const LOCAL = 'local';
@ -43,7 +43,7 @@ const apiFileTransport = new winston.transports.DailyRotateFile({
const localLogFormat = printf(info => {
var data = '';
if (info.data) {
data = JSON.stringify({data: info.data});
data = JSON.stringify({ data: info.data });
}
return `${info.timestamp} ${info.level.toUpperCase()}: ${info.internalCorrelationId} [${info._module}] ${info.message} ${data}`;
@ -70,8 +70,8 @@ winston.loggers.add(LOCAL, {
const morganConfiguration = {
stream: {
write: function(message) {
info(message, 'lnapi');
write: function (message) {
info(message, 'umbrel-middleware');
}
}
};

Loading…
Cancel
Save