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

Re-implement JWT generation script using pyjwt #1830

Merged
merged 4 commits into from
May 28, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions local-dev/api-data-watcher-pusher/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
FROM alpine:3.11

RUN apk add --no-cache mysql-client tini openssl bash wget curl nodejs nodejs-npm \
&& npm config set unsafe-perm true \
&& npm -g install jwtgen
RUN apk add --no-cache tini bash wget py3-jwt

ENV JWTSECRET=super-secret-string \
JWTAUDIENCE=api.dev

COPY api-watch-push.sh create_jwt.sh /home/
COPY api-watch-push.sh create_jwt.py /home/

CMD ["tini", "--", "/home/api-watch-push.sh"]
2 changes: 1 addition & 1 deletion local-dev/api-data-watcher-pusher/api-watch-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ populate_kubernetes_gql_file_path="/api-data/03-populate-api-data-kubernetes.gql
send_graphql_query() {
local file_path=${1}

API_ADMIN_JWT_TOKEN=$(/home/create_jwt.sh)
API_ADMIN_JWT_TOKEN=$(/home/create_jwt.py)

bearer="Authorization: bearer $API_ADMIN_JWT_TOKEN"

Expand Down
9 changes: 9 additions & 0 deletions local-dev/api-data-watcher-pusher/create_jwt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3

import os
import jwt

payload = {'role': 'admin', 'iss': 'api-data-watcher-pusher',
'aud': os.environ['JWTAUDIENCE'], 'sub': 'api-data-watcher-pusher'}

print(jwt.encode(payload, os.environ['JWTSECRET'], algorithm='HS256').decode())
12 changes: 0 additions & 12 deletions local-dev/api-data-watcher-pusher/create_jwt.sh

This file was deleted.

6 changes: 2 additions & 4 deletions services/auto-idler/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ FROM ${IMAGE_REPO:-lagoon}/oc

ENV LAGOON=auto-idler

RUN apk add --no-cache tini jq openssl bash curl nodejs nodejs-npm \
&& npm config set unsafe-perm true \
&& npm -g install jwtgen
RUN apk add --no-cache tini jq bash curl py3-jwt

COPY create_jwt.sh idle-services.sh idle-clis.sh openshift-clis.sh openshift-services.sh /
COPY create_jwt.py idle-services.sh idle-clis.sh openshift-clis.sh openshift-services.sh /

ENV JWTSECRET=super-secret-string \
JWTAUDIENCE=api.dev \
Expand Down
9 changes: 9 additions & 0 deletions services/auto-idler/create_jwt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3

import os
import jwt

payload = {'role': 'admin', 'iss': 'auto-idler',
'aud': os.environ['JWTAUDIENCE'], 'sub': 'auto-idler'}

print(jwt.encode(payload, os.environ['JWTSECRET'], algorithm='HS256').decode())
12 changes: 0 additions & 12 deletions services/auto-idler/create_jwt.sh

This file was deleted.

4 changes: 2 additions & 2 deletions services/auto-idler/idle-clis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ prefixwith() {
}

# Create an JWT Admin Token to talk to the API
API_ADMIN_JWT_TOKEN=$(./create_jwt.sh)
API_ADMIN_JWT_TOKEN=$(./create_jwt.py)
BEARER="Authorization: bearer $API_ADMIN_JWT_TOKEN"

# Load all projects and their environments
Expand Down Expand Up @@ -45,4 +45,4 @@ do
done
sleep 5
# clean up the tmp file
rm $TMP_DATA
rm $TMP_DATA
2 changes: 1 addition & 1 deletion services/auto-idler/idle-services.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ prefixwith() {
}

# Create an JWT Admin Token to talk to the API
API_ADMIN_JWT_TOKEN=$(./create_jwt.sh)
API_ADMIN_JWT_TOKEN=$(./create_jwt.py)
BEARER="Authorization: bearer $API_ADMIN_JWT_TOKEN"

# Load all projects and their environments, but only development environments
Expand Down
4 changes: 2 additions & 2 deletions services/ssh/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ENV LAGOON=ssh \
COPY services/ssh/libnss-mysql-1.5.tar.gz /tmp/libnss-mysql-1.5.tar.gz

RUN apt-get update \
&& apt-get install -y curl build-essential libmysqlclient-dev ssh curl vim jq \
&& apt-get install -y curl build-essential libmysqlclient-dev ssh curl vim jq python3-jwt \
&& ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so /usr/lib/libmysqlclient.so \
&& mkdir /tmp/libnss-mysql \
&& tar -xzf /tmp/libnss-mysql-1.5.tar.gz -C /tmp/libnss-mysql --strip-components=1 \
Expand Down Expand Up @@ -73,7 +73,7 @@ COPY services/ssh/authorize.sh /authorize.sh
RUN chmod 755 /authorize.sh

# create_60_sec_jwt to create a JWT Admin Token which is valid for 60 secs
COPY services/ssh/create_60_sec_jwt.sh /create_60_sec_jwt.sh
COPY services/ssh/create_60_sec_jwt.py /create_60_sec_jwt.py

# Create /authorize.env file and give api right to write it, it will be filled
# within docker-entrypoint with all environment variables and then sourced
Expand Down
2 changes: 1 addition & 1 deletion services/ssh/authorize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# variables during the container entrypoint.
source /authorize.env

API_ADMIN_TOKEN=$(/create_60_sec_jwt.sh)
API_ADMIN_TOKEN=$(/create_60_sec_jwt.py)

# This token will be required for accessing the sshKeys in the lagoon api
bearer="Authorization: bearer $API_ADMIN_TOKEN"
Expand Down
11 changes: 11 additions & 0 deletions services/ssh/create_60_sec_jwt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3

import os
import jwt
from datetime import datetime, timezone, timedelta

iat = datetime.now(timezone.utc)
exp = iat + timedelta(minutes=1)
payload = {'exp': exp, 'iat': iat, 'role': 'admin', 'aud': os.environ['JWTAUDIENCE'], 'sub': 'ssh'}

print(jwt.encode(payload, os.environ['JWTSECRET'], algorithm='HS256').decode())
44 changes: 0 additions & 44 deletions services/ssh/create_60_sec_jwt.sh

This file was deleted.

6 changes: 2 additions & 4 deletions services/storage-calculator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ FROM ${IMAGE_REPO:-lagoon}/oc

ENV LAGOON=storage-calculator

RUN apk add --no-cache tini jq openssl bash curl nodejs nodejs-npm \
&& npm config set unsafe-perm true \
&& npm -g install jwtgen
RUN apk add --no-cache tini jq bash curl py3-jwt

COPY create_jwt.sh calculate-storage.sh /
COPY create_jwt.py calculate-storage.sh /

ENV JWTSECRET=super-secret-string \
JWTAUDIENCE=api.dev \
Expand Down
2 changes: 1 addition & 1 deletion services/storage-calculator/calculate-storage.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

API_ADMIN_JWT_TOKEN=$(./create_jwt.sh)
API_ADMIN_JWT_TOKEN=$(./create_jwt.py)
BEARER="Authorization: bearer $API_ADMIN_JWT_TOKEN"

# Load all projects and their environments
Expand Down
9 changes: 9 additions & 0 deletions services/storage-calculator/create_jwt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3

import os
import jwt

payload = {'role': 'admin', 'iss': 'storage-calculator',
Schnitzel marked this conversation as resolved.
Show resolved Hide resolved
'aud': os.environ['JWTAUDIENCE'], 'sub': 'storage-calculator'}

print(jwt.encode(payload, os.environ['JWTSECRET'], algorithm='HS256').decode())
12 changes: 0 additions & 12 deletions services/storage-calculator/create_jwt.sh

This file was deleted.