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

local setup enhancement #984

Merged
merged 7 commits into from
Feb 14, 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
8 changes: 5 additions & 3 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
LOG_LEVEL=info
DISABLE_LOGGING='true'
AUTH_STRATEGY=Oauth2Proxy
KNEX_HOST=kong-db
KNEX_PORT=5432
Expand All @@ -9,18 +11,18 @@ MONGO_URL=mongodb://mongodb:27017/keystonedb4
MONGO_USER=
MONGO_PASSWORD=
KONG_URL=http://kong.localtest.me:8001
JWKS_URL=http://keycloak.localtest.me:9080/auth/realms/master/protocol/openid-connect/certs
JWKS_URL=http://keycloak.localtest.me:9081/auth/realms/master/protocol/openid-connect/certs
FEEDER_URL=http://feeder.localtest.me:6000
NEXT_PUBLIC_API_ROOT=http://oauth2proxy.localtest.me:4180
GWA_API_URL=http://gwa-api.localtest.me:2000
GWA_PROD_ENV_SLUG=E0000000
GWA_RES_SVR_CLIENT_ID=gwa-api
GWA_RES_SVR_CLIENT_SECRET=18900468-3db1-43f7-a8af-e75f079eb742
KEYCLOAK_AUTH_URL=http://keycloak.localtest.me:9080/auth
KEYCLOAK_AUTH_URL=http://keycloak.localtest.me:9081/auth
KEYCLOAK_REALM=master
EMAIL_ENABLED=false
EXTERNAL_URL=http://oauth2proxy.localtest.me:4180
OIDC_ISSUER=http://keycloak.localtest.me:9080/auth/realms/master
OIDC_ISSUER=http://keycloak.localtest.me:9081/auth/realms/master
LOCAL_ENV=true
WORKING_PATH=/tmp
DESTINATION_URL=
Expand Down
57 changes: 28 additions & 29 deletions .github/workflows/aps-cypress-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and Deploy Cypress and Execute Tests

on:
push:
branches: ['test', 'cypress*']
branches: ['test', 'cypress*', 'local-dev']

env:
DASHBOARD_PROJECT_ID: ${{ secrets.CY_DASHBOARD_PRJ_ID }}
Expand All @@ -27,36 +27,35 @@ jobs:
- name: Checkout Portal
uses: actions/checkout@v2

# - name: Determine Download file name
# id: set_variable
# run: |
# echo ${{ runner.arch }}
# if [ "${{ runner.arch }}" == "X64" ]; then
# echo "::set-output name=my_variable::gwa_Linux_x86_64.tgz"
# elif [ "${{ runner.arch }}" == "ARM64" ]; then
# echo "::set-output name=my_variable::gwa_Linux_arm64.tgz"
# else
# echo "unsupported architecture"
# fi

- name: Determine Download file name
id: set_variable
run: |
echo ${{ runner.arch }}
if [ "${{ runner.arch }}" == "X64" ]; then
echo "::set-output name=my_variable::gwa_Linux_x86_64.tgz"
elif [ "${{ runner.arch }}" == "ARM64" ]; then
echo "::set-output name=my_variable::gwa_Linux_arm64.tgz"
else
echo "unsupported architecture"
fi

- name: Download Binar
uses: robinraju/release-downloader@v1.8
with:
repository: "bcgov/gwa-cli"
latest: true
fileName: ${{ steps.set_variable.outputs.my_variable }}
out-file-path: "${{ github.workspace }}/e2e"
# - name: Download Binary
# uses: robinraju/release-downloader@v1.8
# with:
# repository: "bcgov/gwa-cli"
# latest: true
# fileName: ${{ steps.set_variable.outputs.my_variable }}
# out-file-path: "${{ github.workspace }}/e2e"

# - name: Unzip file
# run: |
# cd ${{ github.workspace }}/e2e
# tar xvzf ${{ steps.set_variable.outputs.my_variable }}

- name: Unzip file
run: |
cd ${{ github.workspace }}/e2e
tar xvzf ${{ steps.set_variable.outputs.my_variable }}

- name: Build Docker Images
run: |
docker-compose build
docker compose --profile testsuite build

- name: Spin up API Services Portal and Run E2E Tests
run: |
export CY_EXECUTION_ENV=${{ env.EXECUTION_ENV }}
Expand All @@ -68,14 +67,14 @@ jobs:
export CY_COMMIT_MESSAGE="${{ env.GIT_COMMIT_MESSAGE }}"
export CY_REPO_URL=${{ env.GIT_REPO_URL }}
export CY_COMMIT_AUTHOR_EMAIL=$(git --no-pager show -s --format='%ae' ${{ env.GIT_COMMIT_SHA }})
docker-compose up -d
docker compose --profile testsuite up -d

- name: Execute Tests & Clean Up
run: |
while true; do
if [ "$(docker ps -aq -f status=exited -f name=cypress-e2e)" ]; then
# cleanup
docker-compose down
docker compose down
break
else
echo "Waiting for Cypress to Complete E2E Tests....."
Expand Down
92 changes: 47 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,69 @@

The `API Services Portal` is a frontend for API Providers to manage the lifecycle of their APIs and for Developers to discover and access these APIs. It works in combination with the Kong Community Edition Gateway and Keycloak IAM solution.

## Running the Project.
## Running the Project

### Installation

#### 1. Manual
#### 1. Docker

To run this project first run `npm install`.
##### Steps

This application requires to have an Authentication proxy in front of it. Go to [oauth2-proxy](oauth2-proxy) for instructions on starting the proxy locally.
1. Run build steps [here](https://github.com/bcgov/api-services-portal/tree/dev/e2e#build-gateway-api-image)
2. Run `docker compose --profile testsuite build`
3. Run `docker compose up` to spin up a local development environment with services (Postgres, Keycloak, OAuth2-proxy, APS-Portal, Feeder and Kong Gateway)
4. Go to: http://oauth2proxy.localtest.me:4180
5. To login, use username `local` and password `local`, or username `janis@idir` and password `awsummer`
6. `docker compose down` : Removes all the hosted services

> To run the Cypress test automation suite, run `docker compose --profile testsuite up`
>
> To use the `gwa` command line, configure it with:
>
> `gwa config set host oauth2proxy.localtest.me:4180`
>
> `gwa config set scheme http`
>
> `gwa login`
>
> `gwa namespace create --name gw-12345`
>
> `gwa apply -i local/gwa-cli/gw-config.yml`
>
> `curl http://oauthproxy.localtest.me:8000/headers -H "Host: my-service.dev.api.gov.bc.ca"`

**Note:**

You can then run `npm run dev` to start the application on port 3000. The proxy runs on port 4180.
- Please wait until keycloak service starts and is initialized with `master` realm. The realm configuration is saved in `./keycloak/master-realm.json`. It also creates a realm user `local` with admin privileges.
- You may want to run `docker compose build` if there are new changes that are not reflected in the last time you built the container images

```
hostip=$(ifconfig en0 | awk '$1 == "inet" {print $2}')
#### 2. Development using Docker backend

export AUTH_STRATEGY=Oauth2Proxy
export ADAPTER=knex
export KNEX_HOST=$hostip
export KNEX_DATABASE=keystonejs
export KNEX_USER=""
export KNEX_PASSWORD=""
export MONGO_URL=mongodb://$hostip:17017/keystonedb3
export MONGO_USER=""
export MONGO_PASSWORD=""
Use the following configuration to run the Portal locally against the components deployed with docker-compose.

export FEEDER_URL=http://localhost:6000
To run this project first run `npm install`. Note: You may need to add `--legacy-peer-deps` to `npm install` if using Node version greater than `17`.

export KONG_URL=""
export OIDC_ISSUER=""
export JWKS_URL=${OIDC_ISSUER}/protocol/openid-connect/certs
To run the portal locally and leverage the `oauth2-proxy` that is running in docker:

export NEXT_PUBLIC_API_ROOT=http://localhost:4180
export SSR_API_ROOT=http://localhost:4180
export EXTERNAL_URL="http://localhost:4180"
- turn off the docker compose Portal: `docker stop apsportal`
- update the `oauth2-proxy/oauth2-proxy-local.cfg` `upstreams` to be `hostip=$(ifconfig en0 | awk '$1 == "inet" {print $2}')`
- restart the oauth2-proxy `docker compose restart oauth2-proxy`

export GWA_API_URL=http://localhost:2000
Then run the following to start the Portal locally:

```sh
cd src
set -o allexport
source ../.env.local
LOG_LEVEL=debug
KNEX_HOST=kong-db.localtest.me
NEXT_PUBLIC_MOCKS=off
set +o allexport

npm run dev
```

Once running, the `api services portal` application is reachable via `localhost:4180`.

#### 2. Docker

##### Steps

1. Create a `.env` from `.env.local` file
2. Create a `.env` from `.env.local` file under `feeds` directory
3. Remove cypress from docker-compose file (L106-129 & L217-229)
4. Run build steps [here](https://github.com/bcgov/api-services-portal/tree/dev/e2e#build-gateway-api-image)
5. Run `docker-compose build`
5. Run `docker-compose up` to spin up a local development environment with services (Postgres, Keycloak, OAuth2-proxy, APS-Portal, Feeder and Kong Gateway)
6. Go to: http://oauth2proxy.localtest.me:4180
7. To login, use username `local` and password `local`, or username `janis@idir` and password `awsummer`
8. `docker-compose down` : Removes all the hosted services

##### Note:

- Please wait until keycloak service starts and is initialized with `master` realm. The realm configuration is saved in `./keycloak/master-realm.json`. It also creates a realm user `local` with admin privileges.
- You may want to run `docker-compose build` if there are new changes that are not reflected in the last time you built the container images
Go to: http://oauth2proxy.localtest.me:4180

## Design

Expand Down Expand Up @@ -255,4 +258,3 @@ select 'drop table "' || tablename || '" cascade;' from pg_tables where schemana
```

In the mean time, it is possible to drop the tables and re-run the `init-aps-portal-keystonejs-batch-job`.

62 changes: 33 additions & 29 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ x-common-variables: &common-variables

services:
keycloak:
image: jboss/keycloak:11.0.3
image: jboss/keycloak:15.1.1
container_name: keycloak
hostname: keycloak
depends_on:
Expand All @@ -18,15 +18,15 @@ services:
[
'-b',
'0.0.0.0',
'-Djboss.socket.binding.port-offset=1000',
'-Djboss.socket.binding.port-offset=1001',
'-Dkeycloak.migration.action=import',
'-Dkeycloak.migration.provider=singleFile',
'-Dkeycloak.migration.file=/tmp/realm-config/master-realm.json',
'-Dkeycloak.migration.strategy=OVERWRITE_EXISTING',
'-Dkeycloak.profile.feature.upload_scripts=enabled',
]
ports:
- 9080:9080/tcp
- 9081:9081/tcp
environment:
#KEYCLOAK_USER: local
#KEYCLOAK_PASSWORD: local
Expand Down Expand Up @@ -103,30 +103,6 @@ services:
- ./local/feeder-init:/tmp
networks:
- aps-net
cypress:
image: aps-cypress-e2e:latest
container_name: cypress-e2e
entrypoint: sh -c "chmod +x /tmp/entrypoint.sh && /tmp/entrypoint.sh"
environment:
- CYPRESS_RECORD_KEY=${CY_RECORD_KEY}
- CYPRESS_PROJECT_ID=${CY_PROJECT_ID}
- RUN_ENV=${CY_EXECUTION_ENV}
- COMMIT_INFO_BRANCH=${CY_COMMIT_BRANCH}
- COMMIT_INFO_SHA=${CY_COMMIT_SHA}
- COMMIT_INFO_AUTHOR=${CY_COMMIT_AUTHOR}
- COMMIT_INFO_MESSAGE=${CY_COMMIT_MESSAGE}
- COMMIT_INFO_REMOTE=${CY_REPO_URL}
- COMMIT_INFO_EMAIL=${CY_COMMIT_AUTHOR_EMAIL}
depends_on:
- feeder-seeding
build:
context: ./e2e
dockerfile: Dockerfile
volumes:
- ./e2e/coverage:/e2e/coverage
- ./e2e/results:/e2e/results
networks:
- aps-net
kong-db:
image: postgres:12.8
container_name: kong-db
Expand All @@ -141,7 +117,9 @@ services:
- ./local/db/database-init.sql:/docker-entrypoint-initdb.d/1-init.sql
- ./local/db/keystone-init.sql:/docker-entrypoint-initdb.d/2-init.sql
networks:
- aps-net
aps-net:
aliases:
- kong-db.localtest.me
kong-migrations:
image: kong:kong-local
command: kong migrations bootstrap
Expand Down Expand Up @@ -189,7 +167,7 @@ services:
restart: on-failure:5
redis:
image: bitnami/redis:latest
container_name: redis
container_name: redis-master
environment:
- REDIS_PASSWORD=s3cr3t
restart: on-failure
Expand Down Expand Up @@ -227,5 +205,31 @@ services:
aps-net:
aliases:
- cypress-jwks-url.localtest.me
cypress:
image: aps-cypress-e2e:latest
container_name: cypress-e2e
entrypoint: sh -c "chmod +x /tmp/entrypoint.sh && /tmp/entrypoint.sh"
environment:
- CYPRESS_RECORD_KEY=${CY_RECORD_KEY}
- CYPRESS_PROJECT_ID=${CY_PROJECT_ID}
- RUN_ENV=${CY_EXECUTION_ENV}
- COMMIT_INFO_BRANCH=${CY_COMMIT_BRANCH}
- COMMIT_INFO_SHA=${CY_COMMIT_SHA}
- COMMIT_INFO_AUTHOR=${CY_COMMIT_AUTHOR}
- COMMIT_INFO_MESSAGE=${CY_COMMIT_MESSAGE}
- COMMIT_INFO_REMOTE=${CY_REPO_URL}
- COMMIT_INFO_EMAIL=${CY_COMMIT_AUTHOR_EMAIL}
depends_on:
- feeder-seeding
build:
context: ./e2e
dockerfile: Dockerfile
volumes:
- ./e2e/coverage:/e2e/coverage
- ./e2e/results:/e2e/results
networks:
- aps-net
profiles:
- testsuite
networks:
aps-net: {}
12 changes: 8 additions & 4 deletions e2e/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ FROM cypress/included:12.17.4
WORKDIR /e2e

RUN apt-get -y update; apt-get -y install curl
COPY cypress.config.ts /e2e
COPY tsconfig.json /e2e
COPY package.json /e2e
COPY package-lock.json /e2e
RUN npm install

COPY cypress.config.ts /e2e
COPY tsconfig.json /e2e
COPY *.yml /e2e
COPY entrypoint.sh /tmp
COPY gwa /usr/local/bin
ADD cypress /e2e/cypress

RUN npm install
RUN curl -v -L -O https://github.com/bcgov/gwa-cli/releases/download/v2.0.15/gwa_Linux_x86_64.tgz \
&& tar -xzf gwa_Linux_x86_64.tgz \
&& mv gwa /usr/local/bin/.



ENTRYPOINT ["npm", "run", "cy:run:html"]
6 changes: 3 additions & 3 deletions e2e/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export default defineConfig({
env: {
CLIENT_ID: 'aps-portal',
CLIENT_SECRET: '8e1a17ed-cb93-4806-ac32-e303d1c86018',
OIDC_ISSUER: 'http://keycloak.localtest.me:9080',
OIDC_ISSUER: 'http://keycloak.localtest.me:9081',
TOKEN_URL:
'http://keycloak.localtest.me:9080/auth/realms/master/protocol/openid-connect/token',
'http://keycloak.localtest.me:9081/auth/realms/master/protocol/openid-connect/token',
GWA_API_URL: 'http://gwa-api.localtest.me:2000/v2',
KONG_URL: 'http://kong.localtest.me:8000',
JWKS_URL: 'http://cypress-jwks-url.localtest.me:3500',
KONG_CONFIG_URL: 'http://kong.localtest.me:8001',
BASE_URL: 'http://oauth2proxy.localtest.me:4180',
KEYCLOAK_URL: 'http://keycloak.localtest.me:9080',
KEYCLOAK_URL: 'http://keycloak.localtest.me:9081',
WEBAPP_URL: 'http://html-sample-app.localtest.me:4242'
},
retries: {
Expand Down
Loading
Loading