aboutsummaryrefslogtreecommitdiffstats
path: root/python_moonclient/python_moonclient/pdp.py
blob: 6841a2766c8780bd3c57bfb28cee3b3b4af9491e (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import sys
import logging
import requests
from python_moonclient import config

logger = logging.getLogger("python_moonclient.pdp")

URL = None
HEADERS = None
KEYSTONE_USER = None
KEYSTONE_PASSWORD = None
KEYSTONE_PROJECT = None
KEYSTONE_SERVER = None


pdp_template = {
    "name": "test_pdp",
    "security_pipeline": [],
    "keystone_project_id": None,
    "description": "test",
}


def init(consul_host, consul_port):
    conf_data = config.get_config_data(consul_host, consul_port)
    global URL, HEADERS, KEYSTONE_USER, KEYSTONE_PASSWORD, KEYSTONE_PROJECT, KEYSTONE_SERVER
    URL = "http://{}:{}".format(
        conf_data['manager_host'],
        conf_data['manager_port'])
    # URL = URL + "{}"
    HEADERS = {"content-type": "application/json"}
    KEYSTONE_USER = conf_data['keystone_user']
    KEYSTONE_PASSWORD = conf_data['keystone_password']
    KEYSTONE_PROJECT = conf_data['keystone_project']
    KEYSTONE_SERVER = conf_data['keystone_host']


def get_keystone_projects():
    global HEADERS
    HEADERS = {
        "Content-Type": "application/json"
    }

    data_auth = {
        "auth": {
            "identity": {
                "methods": [
                    "password"
                ],
                "password": {
                    "user": {
                        "name": KEYSTONE_USER,
                        "domain": {
                            "name": "Default"
                        },
                        "password": KEYSTONE_PASSWORD
                    }
                }
            }
        }
    }

    req = requests.post("{}/auth/tokens".format(KEYSTONE_SERVER), json=data_auth, headers=HEADERS)
    logger.debug("{}/auth/tokens".format(KEYSTONE_SERVER))
    logger.debug(req.text)
    assert req.status_code in (200, 201)
    TOKEN = req.headers['X-Subject-Token']
    HEADERS['X-Auth-Token'] = TOKEN
    req = requests.get("{}/projects".format(KEYSTONE_SERVER), headers=HEADERS)
    if req.status_code not in (200, 201):
        data_auth["auth"]["scope"] = {
            "project": {
                "name": KEYSTONE_PROJECT,
                "domain": {
                    "id": "default"
                }
            }
        }
        req = requests.post("{}/auth/tokens".format(KEYSTONE_SERVER), json=data_auth, headers=HEADERS)
        assert req.status_code in (200, 201)
        TOKEN = req.headers['X-Subject-Token']
        HEADERS['X-Auth-Token'] = TOKEN
        req = requests.get("{}/projects".format(KEYSTONE_SERVER), headers=HEADERS)
    assert req.status_code in (200, 201)
    return req.json()


def get_keystone_id(pdp_name):
    keystone_project_id = None
    for pdp_key, pdp_value in check_pdp()["pdps"].items():
        if pdp_name:
            if pdp_name != pdp_value["name"]:
                continue
        if pdp_value['security_pipeline'] and pdp_value["keystone_project_id"]:
            logger.debug("Found pdp with keystone_project_id={}".format(pdp_value["keystone_project_id"]))
            keystone_project_id = pdp_value["keystone_project_id"]

    if not keystone_project_id:
        logger.error("Cannot find PDP with keystone project ID")
        sys.exit(1)
    return keystone_project_id


def check_pdp(pdp_id=None, keystone_project_id=None, moon_url=None):
    _URL = URL
    if moon_url:
        _URL = moon_url
    req = requests.get(_URL + "/pdp")
    assert req.status_code == 200
    result = req.json()
    assert type(result) is dict
    assert "pdps" in result
    if pdp_id:
        assert result["pdps"]
        assert pdp_id in result['pdps']
        assert "name" in result['pdps'][pdp_id]
        assert pdp_template["name"] == result['pdps'][pdp_id]["name"]
    if keystone_project_id:
        assert result["pdps"]
        assert pdp_id in result['pdps']
        assert "keystone_project_id" in result['pdps'][pdp_id]
        assert keystone_project_id == result['pdps'][pdp_id]["keystone_project_id"]
    return result


def add_pdp(name="test_pdp", policy_id=None):
    pdp_template['name'] = name
    if policy_id:
        pdp_template['security_pipeline'].append(policy_id)
    req = requests.post(URL + "/pdp", json=pdp_template, headers=HEADERS)
    logger.debug(req.status_code)
    logger.debug(req)
    assert req.status_code == 200
    result = req.json()
    assert type(result) is dict
    pdp_id = list(result['pdps'].keys())[0]
    if "result" in result:
        assert result["result"]
    assert "name" in result['pdps'][pdp_id]
    assert pdp_template["name"] == result['pdps'][pdp_id]["name"]
    return pdp_id


def update_pdp(pdp_id, policy_id=None):
    req = requests.get(URL + "/pdp/{}".format(pdp_id))
    assert req.status_code == 200
    result = req.json()
    assert type(result) is dict
    assert "pdps" in result
    assert pdp_id in result['pdps']
    pipeline = result['pdps'][pdp_id]["security_pipeline"]
    if policy_id not in pipeline:
        pipeline.append(policy_id)
        req = requests.patch(URL + "/pdp/{}".format(pdp_id),
                             json={"security_pipeline": pipeline})
        assert req.status_code == 200
        result = req.json()
        assert type(result) is dict
        assert "pdps" in result
        assert pdp_id in result['pdps']

    req = requests.get(URL + "/pdp/{}".format(pdp_id))
    assert req.status_code == 200
    result = req.json()
    assert type(result) is dict
    assert "pdps" in result
    assert pdp_id in result['pdps']
    assert policy_id in pipeline


def map_to_keystone(pdp_id, keystone_project_id):
    req = requests.patch(URL + "/pdp/{}".format(pdp_id),
                         json={"keystone_project_id": keystone_project_id},
                         headers=HEADERS)
    assert req.status_code == 200
    result = req.json()
    assert type(result) is dict
    if "result" in result:
        assert result["result"]
    assert pdp_id in result['pdps']
    # assert "name" in result['pdps'][pdp_id]
    # assert pdp_template["name"] == result['pdps'][pdp_id]["name"]
    return pdp_id


def delete_pdp(pdp_id):
    req = requests.delete(URL + "/pdp/{}".format(pdp_id))
    assert req.status_code == 200
    result = req.json()
    assert type(result) is dict
    assert "result" in result
    assert result["result"]


def create_pdp(scenario, policy_id=None, project_id=None):
    logger.info("Creating PDP {}".format(scenario.pdp_name))
    projects = get_keystone_projects()
    # if not project_id:
    #     for _project in projects['projects']:
    #         if _project['name'] == "admin":
    #             project_id = _project['id']
    # assert project_id
    pdps = check_pdp()["pdps"]
    for pdp_id, pdp_value in pdps.items():
        if scenario.pdp_name == pdp_value["name"]:
            update_pdp(pdp_id, policy_id=policy_id)
            logger.debug("Found existing PDP named {} (will add policy {})".format(scenario.pdp_name, policy_id))
            return pdp_id
    _pdp_id = add_pdp(name=scenario.pdp_name, policy_id=policy_id)
    # map_to_keystone(pdp_id=_pdp_id, keystone_project_id=project_id)
    return _pdp_id