...

Text file src/github.com/emissary-ingress/emissary/v3/docker/test-auth/auth.py

Documentation: github.com/emissary-ingress/emissary/v3/docker/test-auth

     1#!/usr/bin/env python
     2
     3# Copyright 2018 Datawire. All rights reserved.
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License");
     6# you may not use this file except in compliance with the License.
     7# You may obtain a copy of the License at
     8#
     9#     http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS,
    13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14# See the License for the specific language governing permissions and
    15# limitations under the License
    16
    17import sys
    18
    19# import functools
    20import json
    21import logging
    22import os
    23import pprint
    24
    25from flask import Flask, Response, jsonify, request
    26
    27__version__ = '0.0.2'
    28
    29app = Flask(__name__)
    30
    31@app.before_request
    32def before():
    33    print("---- Incoming Request Headers")
    34    pprint.pprint(request)
    35
    36    for header in sorted(request.headers.keys()):
    37        print("%s: %s" % (header, request.headers[header]))
    38
    39    print("----")
    40
    41@app.route('/', defaults={'path': ''})
    42@app.route('/<path:path>')
    43def catch_all(path):
    44    if not path.startswith('/'):
    45        path = '/' + path
    46
    47    if path.startswith('//'):
    48        path = path[1:]
    49
    50    status_code = 403
    51    headers = { 'X-Test': 'Should not be seen.' }
    52
    53    if path.startswith('/ambassador/'):
    54        status_code = 200
    55    elif path.endswith("/good/") or path.endswith("/demo/"):
    56        status_code = 200
    57        headers['X-Auth-Route'] = 'Route'
    58    elif path.endswith("/nohdr/"):
    59        status_code = 200
    60        # Don't add the header.
    61
    62    backend_name = os.environ.get('BACKEND')
    63
    64    body = 'You want path: %s' % path
    65    mimetype = 'text/plain'
    66
    67    if backend_name:
    68        body_dict = {
    69            'backend': backend_name,
    70            'path': path
    71        }
    72
    73        body = json.dumps(body_dict)
    74        mimetype = 'application/json'
    75
    76        extauth_dict = {
    77            'request': {
    78                'url': request.url,
    79                'path': path,
    80                'headers': dict(request.headers)
    81            },
    82            'resp_headers': headers
    83        }
    84
    85        headers['extauth'] = extauth_dict
    86
    87    return Response(body,
    88                    mimetype=mimetype,
    89                    status=status_code,
    90                    headers=headers)
    91
    92if __name__ == "__main__":
    93    ssl_context = None
    94    conn_type = "HTTP"
    95    port = 80
    96
    97    if (len(sys.argv) > 1) and (sys.argv[1] == "--tls"):
    98        ssl_context = ('authsvc.crt', 'authsvc.key')
    99        conn_type = "HTTPS"
   100        port = 443
   101
   102    if os.environ.get('PORT'):
   103        port = int(os.environ['PORT'])
   104
   105    logging.basicConfig(
   106        # filename=logPath,
   107        level=logging.DEBUG, # if appDebug else logging.INFO,
   108        format="%%(asctime)s auth %s %s %%(levelname)s: %%(message)s" % (__version__, conn_type),
   109        datefmt="%Y-%m-%d %H:%M:%S"
   110    )
   111
   112    logging.info("initializing")
   113    app.run(host='0.0.0.0', port=port, debug=True, ssl_context=ssl_context)

View as plain text