Pablo Solar VilariƱo
2020-05-11 dd2dcc0fa232f60f26364b79c4fe7100e2422a6a
Added AB proxy application (#13)

6 files added
132 ■■■■■ changed files
python-flask-ab-proxy/.gitignore 11 ●●●●● patch | view | raw | blame | history
python-flask-ab-proxy/Dockerfile 14 ●●●●● patch | view | raw | blame | history
python-flask-ab-proxy/README.md 58 ●●●●● patch | view | raw | blame | history
python-flask-ab-proxy/requirements.txt 2 ●●●●● patch | view | raw | blame | history
python-flask-ab-proxy/src/ab-proxy.py 46 ●●●●● patch | view | raw | blame | history
python-flask-ab-proxy/src/data/headers.json 1 ●●●● patch | view | raw | blame | history
python-flask-ab-proxy/.gitignore
New file
@@ -0,0 +1,11 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# IDEs
.idea
# dependencies
/venv
__pycache__
# misc
.DS_Store
python-flask-ab-proxy/Dockerfile
New file
@@ -0,0 +1,14 @@
FROM ubi8/python-36
ENV FLASK_APP="ab-proxy.py"
COPY src /app
COPY requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD [ "flask", "run", "--host=0.0.0.0"]
python-flask-ab-proxy/README.md
New file
@@ -0,0 +1,58 @@
# AB Proxy
The AB Proxy is a small application written in [Python](https://www.python.org/) + [Flask](https://flask.palletsprojects.com)
which acts as a proxy for an endpoint and appends custom headers to all requests.
## How it works
The application exposes several endpoints to manage the behaviour of the proxy:
 - `GET /headers`: returns the list of headers the proxy is going to add to all requests.
 - `DELETE /headers`: removes any configuration.
 - `POST /headers`: sets the list of headers the proxy is going to add to all requests.
Setting headers example:
```
curl -d '{"version":"beta"}' -H "Content-Type: application/json" -X POST http://localhost:5000/headers
```
## Installation
The AB proxy is a Python application with Flask so, in order to run in your local machine you need Python + some
libraries.
A simple approach is to have different Python virtual environments per project. Execute the following command in the
root of this project ot install a virtual environment:
```
$ python3 -m virtualenv venv
```
Once the virtual environment is installed in the project, you need to activate this new virtual environment. Execute the
following command to activate it:
```
. venv/bin/activate
```
After the activation of the virtual environment you need to install all the requirements for the service (unless you did
this step before). Execute the following command to install all the required libraries:
```
pip install -r requirements.txt
```
## Running the AB proxy in local
To run the AB proxy in local, execute the following commands:
```
$ cd src
$ export FLASK_APP=ab-proxy.py
$ flask run
```
 ## Running the AB proxy in a containers environment
In order to run the AB proxy in a containers environment you need a container image. A prebuilt image is
available in [quay.io](https://quay.io/repository/redhattraining/ossm-ab-proxy)
python-flask-ab-proxy/requirements.txt
New file
@@ -0,0 +1,2 @@
Flask
requests
python-flask-ab-proxy/src/ab-proxy.py
New file
@@ -0,0 +1,46 @@
import http
import json
import os
from flask import abort, Flask, request
from requests import get
app = Flask(__name__)
HEADERS_FILE_PATH = './data/headers.json'
INGRESS_GATEWAY = os.environ.get('INGRESS_GATEWAY')
@app.errorhandler(404)
def topicNotFound(error):
    return 'Route not found', 404
@app.route('/headers', methods=['POST'])
def setHeaders():
    newHeaders = request.get_json()
    with open(HEADERS_FILE_PATH, 'w') as headersFile:
        json.dump(newHeaders, headersFile)
    return '', http.HTTPStatus.NO_CONTENT
@app.route('/headers', methods=['DELETE'])
def deleteHeaders():
    with open(HEADERS_FILE_PATH, 'w') as headersFile:
        json.dump({}, headersFile)
    return '', http.HTTPStatus.NO_CONTENT
@app.route('/headers', methods=['GET'])
def getHeaders():
    try:
        with open(HEADERS_FILE_PATH) as headersFile:
            return json.load(headersFile)
    except IOError:
        abort(404)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
  with open(HEADERS_FILE_PATH) as headersFile:
    customHeaders = json.load(headersFile)
  return get(f'{INGRESS_GATEWAY}{path}', headers=customHeaders).content
python-flask-ab-proxy/src/data/headers.json
New file
@@ -0,0 +1 @@
{}