Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Apr 9, 2020
0 parents commit 002e5df
Show file tree
Hide file tree
Showing 192 changed files with 34,108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
dist/
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# package directories
node_modules
jspm_packages
.webpack
.env

# Serverless directories
.serverless

.envrc

coverage/
.DS_Store
dist/
yarn-error.log
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dist/
components.d.ts
www/
node_modules
templates
**/node_modules/**
config/events/*.json
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120,
"tabWidth": 2
}
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"javascript.format.enable": false,
}
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# # Biulder
FROM node:10.16-alpine
WORKDIR /app
# needed by babel
RUN apk add --update \
python \
python-dev \
build-base \
make

COPY package.json .
COPY yarn.lock .
ARG npm_token
RUN echo "//registry.npmjs.org/:_authToken=$npm_token" > .npmrc
RUN yarn install --frozen-lockfile

COPY package.json .
COPY yarn.lock .

COPY --from=builder /app/.webpack/src/ .
COPY --from=builder /app/.webpack/service/ .

RUN yarn add newrelic
RUN yarn add sqreen

ENV PORT=8080

CMD ["node", "server.js"]
22 changes: 22 additions & 0 deletions Dockerfile.release
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# # Builder
FROM node:10.16-alpine AS builder
WORKDIR /app
# needed by babel
RUN apk add --update \
python \
python-dev \
build-base \
make

COPY package.json .
COPY yarn.lock .
ARG npm_token
RUN echo "//registry.npmjs.org/:_authToken=$npm_token" > .npmrc
RUN yarn install --frozen-lockfile

COPY . .

ARG stage
ENV STAGE=$stage
RUN yarn generate:views
RUN yarn build:server
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# ApiRouter

| Scope | Stage | Package | URL |
| ----------- | ------------ | ---------------------------------- | ----------------------------------------------- |
| API | `dev` | `api-router-dev-ApiService` | `https://int.dev.bearer.sh/api/v1/{proxy+}` |
| HTML | `dev` | `api-router-dev-ApiService` | `https://int.dev.bearer.sh/v1/{proxy+}` |
| API | `staging` | `api-router-staging-ApiService` | `https://int.staging.bearer.sh/api/v1/{proxy+}` |
| HTML | `staging` | `api-router-staging-ApiService` | `https://int.staging.bearer.sh/v1/{proxy+}` |
| API | `production` | `api-router-production-ApiService` | `https://int.bearer.sh/api/v1/{proxy+}` |
| HTML | `production` | `api-router-production-ApiService` | `https://int.bearer.sh/v1/{proxy+}` |
| BACKEND_API | `production` | `api-router-production-ApiService` | `https://int.bearer.sh/backend/api/v1/{proxy+}` |
| WEBHOOK_API | `production` | `api-router-production-ApiService` | `https://int.bearer.sh/webhook/v1/{proxy+}` |

## Endpoints

| Scope | endpoint | implemented :white_check_mark: / :x: |
| ----------- | ---------------------------------- | :----------------------------------: |
| HTML | `/user/initialize` | :white_check_mark: |
| HTML | `/auth/:integration_uuid` | :white_check_mark: |
| HTML | `/auth/:integration_uuid/callback` | :white_check_mark: |
| HTML | `/auth/:service/callback` | :white_check_mark: |
| HTML | `/account/:integration_uuid` | :white_check_mark: |
| API | `/signup` | :white_check_mark: |
| API | `/login` | :white_check_mark: |
| API | `/refresh_token` | :white_check_mark: |
| API | `/items/:reference_id` | :white_check_mark: |
| API | DELETE `/items/:reference_id` | :white_check_mark: |
| API | POST `/items` | :white_check_mark: |
| API | PUT `/items/:reference_id` | :white_check_mark: |
| API | `/:integration_uuid/:action_id` | :white_check_mark: |
| API | `/:integration_uuid/:action_id` | :white_check_mark: |
| BACKEND_API | `/:integration_uuid/:action_id` | :white_check_mark: |
| WEBHOOK_API | `/config` | :white_check_mark: |
| WEBHOOK_API | `/t/:uuid` | :white_check_mark: |

### `/user/initialize`

Get access to the iframe. This will also check the cookie and generate a `uuid` if this one doesn't exist.

### `/auth/:integration_uuid`

When the user hits this endpoint, we will go through the OAuth dance to get the user a token.
We will pass a signed cookie so that it will be possible from the callback URL to attached the `integration_uuid`, the `user_id` and the `token`

### `/auth/:integration_uuid/callback`

**To Be Deprecated**

Once the user has successfully authorized the application, the user will be redirected to this callback URL.
It will store the `integration_uuid`, the `token` and the `user_id`.

### `/auth/:service/callback`

Once the user has successfully authorized the application, the user will be redirected to this callback URL.
It will store the `integration_uuid`, the `token` and the `user_id`.

### `/account/:integration_uuid`

Once the user has been authenticated, it will be redirected to this URL, which will basically return a `200`.

### `/:integration_uuid/:action_id`

Pass an `integration_uuid` along with an `action_id` (which is an intent defined within the package), to get it executed using the user's token.

## How to test?

We use jest to test the integration service pieces

```
$ # setup env variables correctly
$ cp .envrc{.example,}
$ # start a local dynamodb server (listening on 8000)
$ docker-compose up -d
$ yarn test
```

## How to deploy?

This repository uses `drone.io` as CI//CD. If you would like to try your code/branch on staging before mergin to master, you can do it by pushing on the `staging` branch.

## Running Locally

1. Use ssh tuneling to connect to staging dashboard-api

```
docker-compose up -d # To start the redis cluster
$ yarn start:staging
Killing the http tunnel to the dashboard
Import secrets from secret manager from arn:aws:secretsmanager:eu-west-1:794568872834:secret:local/is-Yn35MQ
It seems that the following modules have been required before Sqreen:
- /Users/tarikihadjadene/projects/bearer/integration-service/node_modules/newrelic/index.js
Sqreen may not be able to protect the whole application.
If you think this is an error, please report it to Sqreen team.
Read more on https://doc.sqreen.io/docs/nodejs-agent-installation
[bugsnag] Loaded!
express-session deprecated undefined resave option; provide resave option src/auth/v3/session.ts:18:37
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option src/auth/v3/session.ts:18:37
Integration service app listening on port 8002
```

2. Call proxy endpoint

To call the proxy endpoint you will need to set the Host header to `proxy.staging.bearer.sh`

Example :

```
curl -X GET \
-H 'Authorization: sk_product`on_OC6LEXmykI7zg4k0r9xQfBR_ZszfTYMF' \
-H "Host: proxy.staging.bearer.sh" \
-H 'Bearer-Auth-Id: 0da61ec0-e4f2-11e9-9297-7bb04faac2f4' \
"http://localhost:8002/github/user/repos"
```
50 changes: 50 additions & 0 deletions __mocks__/aws-sdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const AWS = jest.genMockFromModule('aws-sdk')

const cloudFormationInstance = {
describeStackResources: () => {}
}

const lambdaInstance = {
invoke() {},
config: { region: 'eu-west-3' }
}

AWS.Lambda = function() {
return lambdaInstance
}

const promiseResponse = {
promise: () => Promise.resolve({})
}

const mockDbClient = {
get: jest.fn(() => promiseResponse),
put: jest.fn(() => promiseResponse),
update: jest.fn(() => promiseResponse),
delete: jest.fn(() => promiseResponse)
}

AWS.DynamoDB = {
DocumentClient: jest.fn(() => {
return mockDbClient
})
}

const mockedFirehoseInstance = {
putRecord: jest.fn(() => ({ promise: () => Promise.resolve() })),
putRecordBatch: jest.fn(() => ({ promise: () => Promise.resolve() }))
}

const mockedCloudWatchLogsInstance = {
createLogStream: jest.fn(() => ({ promise: () => Promise.resolve() })),
putLogEvents: jest.fn(() => ({ promise: () => Promise.resolve() }))
}

const cloudWatchLogs = jest.fn(() => mockedCloudWatchLogsInstance)
const firehose = jest.fn(() => mockedFirehoseInstance)

AWS.CloudWatchLogs = cloudWatchLogs
AWS.Firehose = firehose

module.exports = AWS
module.exports.mockDbClient = mockDbClient
28 changes: 28 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "Pizzly",
"description": "A Node Application to simplify OAuth",
"image": "heroku/node",
"repository": "https://github.com/Bearer/pizzly",
"keywords": ["OAuth", "node"],
"env": {
"AUTH_CALLBACK_URL": {
"description": "",
"required": true
},
"AUTH_VHOST": {
"description": "",
"default": "auth.bearer.sh",
"required": false
},
"PROXY_VHOST": {
"description": "",
"default": "proxy.bearer.sh",
"required": false
},
"COOKIE_SECRET": {
"description": "",
"default": "cookie_secret",
"required": false
}
}
}
1 change: 1 addition & 0 deletions bin/ts-node
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node -r ts-node/register $@
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] }
17 changes: 17 additions & 0 deletions jestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs')
const path = require('path')
nock = require('nock')

nock.disableNetConnect()
nock.enableNetConnect(/127.0.0.1:.*/)

const env = fs.readFileSync(path.join(__dirname, 'config', 'config.test.json'), {
encoding: 'utf8'
})

process.env = {
...process.env,
STAGE: 'test',
COOKIE_SECRET: 'secret',
...JSON.parse(env)
}
5 changes: 5 additions & 0 deletions keybase-public.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

Keybase isn't running. To start, you can run:

open /Applications/Keybase.app

Loading

0 comments on commit 002e5df

Please sign in to comment.