aboutsummaryrefslogtreecommitdiffstats
path: root/old/moon_bouchon/moon_bouchon/server.py
blob: 29e9101e1a870f62ad838e012edc21d41d9b197e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import sys
import flask
from flask import Flask
from flask import request
import json
import logging
import random

logger = logging.getLogger(__name__)
app = Flask(__name__)


@app.route("/interface/authz/grant/<string:project_id>/<string:subject_name>/"
           "<string:object_name>/<string:action_name>",
           methods=["GET"])
def interface_grant(project_id, subject_name, object_name, action_name):
    logger.info("Requesting interface authz on {} {} {} {}".format(
        project_id, subject_name, object_name, action_name))
    return json.dumps({
        "result": True,
        "context": {
            "project_id": project_id,
            "subject_name": subject_name,
            "object_name": object_name,
            "action_name": action_name
        }
    })


@app.route("/interface/authz/deny/<string:project_id>/<string:subject_name>/"
           "<string:object_name>/<string:action_name>",
           methods=["GET"])
def interface_deny(project_id, subject_name, object_name, action_name):
    logger.info("Requesting interface authz on {} {} {} {}".format(
        project_id, subject_name, object_name, action_name))
    return json.dumps({
        "result": False,
        "context": {
            "project_id": project_id,
            "subject_name": subject_name,
            "object_name": object_name,
            "action_name": action_name
        }
    })


@app.route("/interface/authz/<string:project_id>/<string:subject_name>/"
           "<string:object_name>/<string:action_name>",
           methods=["GET"])
def interface_authz(project_id, subject_name, object_name, action_name):
    logger.info("Requesting interface authz on {} {} {} {}".format(
        project_id, subject_name, object_name, action_name))
    return json.dumps({
        "result": random.choice((True, False)),
        "context": {
            "project_id": project_id,
            "subject_name": subject_name,
            "object_name": object_name,
            "action_name": action_name
        }
    })


def test_data():
    data = request.form
    if not dict(request.form):
        data = json.loads(request.data.decode("utf-8"))
    try:
        target = json.loads(data.get('target', {}))
    except Exception:
        raise Exception("Error reading target")
    try:
        credentials = json.loads(data.get('credentials', {}))
    except Exception:
        raise Exception("Error reading credentials")
    try:
        rule = data.get('rule', "")
    except Exception:
        raise Exception("Error reading rule")


@app.route("/wrapper/authz/grant", methods=["POST"])
def wrapper_grant():
    logger.info("Requesting wrapper authz")
    try:
        test_data()
    except Exception as e:
        logger.exception(e)
        return str(e), 400
    response = flask.make_response("True")
    response.headers['content-type'] = 'application/octet-stream'
    return response


@app.route("/wrapper/authz/deny", methods=["POST"])
def wrapper_deny():
    logger.info("Requesting wrapper authz")
    try:
        test_data()
    except Exception as e:
        logger.exception(e)
        return str(e), 400
    response = flask.make_response("False")
    response.headers['content-type'] = 'application/octet-stream'
    return response


@app.route("/wrapper/authz", methods=["POST"])
def wrapper_authz():
    logger.info("Requesting wrapper authz")
    try:
        test_data()
    except Exception as e:
        logger.exception(e)
        return str(e), 400
    response = flask.make_response(random.choice(("True", "False")))
    response.headers['content-type'] = 'application/octet-stream'
    return response


def main():
    port = 31002
    if len(sys.argv) > 1:
        try:
            port = int(sys.argv[1])
        except ValueError:
            logger.error("Argument for Port in command line is not an integer")
            sys.exit(1)
    app.run(host="0.0.0.0", port=port)


if __name__ == "__main__":
    main()