forked from infobyte/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api_credentials.py
234 lines (214 loc) · 9.6 KB
/
test_api_credentials.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'''
Faraday Penetration Test IDE
Copyright (C) 2013 Infobyte LLC (http://www.infobytesec.com/)
See the file 'doc/LICENSE' for the license information
'''
import pytest
from test_cases import factories
from test_api_workspaced_base import (
ReadWriteAPITests,
)
from server.api.modules.credentials import CredentialView
from server.models import Credential
from test_cases.factories import HostFactory, ServiceFactory
class TestCredentialsAPIGeneric(ReadWriteAPITests):
model = Credential
factory = factories.CredentialFactory
view_class = CredentialView
api_endpoint = 'credential'
update_fields = ['username', 'password']
def test_get_list_backwards_compatibility(self, test_client, session, second_workspace):
cred = self.factory.create(workspace=second_workspace)
session.commit()
res = test_client.get(self.url())
assert res.status_code == 200
assert 'rows' in res.json
for vuln in res.json['rows']:
assert set([u'_id', u'id', u'key', u'value']) == set(vuln.keys())
object_properties = [
u'_id',
u'couchdbid',
u'description',
u'metadata',
u'name',
u'owner',
u'password',
u'username',
u'host_ip',
u'service_name',
u'target'
]
expected = set(object_properties)
result = set(vuln['value'].keys())
assert expected - result == set()
def test_create_from_raw_data_host_as_parent(self, session, test_client,
workspace, host_factory):
host = host_factory.create(workspace=workspace)
session.commit()
raw_data = {
"_id":"1.e5069bb0718aa519852e6449448eedd717f1b90d",
"name":"name",
"username":"username",
"metadata":{"update_time":1508794240799,"update_user":"",
"update_action":0,"creator":"UI Web",
"create_time":1508794240799,"update_controller_action":"",
"owner":""},
"password":"pass",
"type":"Cred",
"owner":"",
"description":"",
"parent": host.id,
"parent_type": "Host"
}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert res.json['host_ip'] == host.ip
assert res.json['service_name'] is None
assert res.json['target'] == host.ip
def test_create_from_raw_data_service_as_parent(
self, session, test_client, workspace, service_factory):
service = service_factory.create(workspace=workspace)
session.commit()
raw_data = {
"_id":"1.e5069bb0718aa519852e6449448eedd717f1b90d",
"name":"name",
"username":"username",
"metadata":{"update_time":1508794240799,"update_user":"",
"update_action":0,"creator":"UI Web",
"create_time":1508794240799,"update_controller_action":"",
"owner":""},
"password":"pass",
"type":"Cred",
"owner":"",
"description":"",
"parent": service.id,
"parent_type": "Service"
}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 201
assert res.json['host_ip'] is None
assert res.json['service_name'] == service.name
assert res.json['target'] == service.host.ip + '/' + service.name
def test_get_credentials_for_a_host_backwards_compatibility(
self, session, test_client, host):
credential = self.factory.create(host=host, service=None,
workspace=self.workspace)
session.commit()
res = test_client.get(self.url(workspace=credential.workspace) + '?host_id={0}'.format(credential.host.id))
assert res.status_code == 200
assert map(lambda cred: cred['value']['parent'],res.json['rows']) == [credential.host.id]
assert map(lambda cred: cred['value']['parent_type'], res.json['rows']) == [u'Host']
def test_get_credentials_for_a_service_backwards_compatibility(self, session, test_client):
service = ServiceFactory.create()
credential = self.factory.create(service=service, host=None, workspace=service.workspace)
session.commit()
res = test_client.get(self.url(workspace=credential.workspace) + '?service={0}'.format(credential.service.id))
assert res.status_code == 200
assert map(lambda cred: cred['value']['parent'],res.json['rows']) == [credential.service.id]
assert map(lambda cred: cred['value']['parent_type'], res.json['rows']) == [u'Service']
def _generate_raw_update_data(self, name, username, password, parent_id):
return {
"id": 7,
"name": name,
"username": username,
"metadata": {"update_time": 1508960699994, "create_time": 1508965372,
"update_user": "", "update_action": 0, "creator": "Metasploit", "owner": "",
"update_controller_action": "No model controller call",
"command_id": "e1a042dd0e054c1495e1c01ced856438"},
"password": password,
"type": "Cred",
"parent_type": "Host",
"parent": parent_id,
"owner": "",
"description": "",
"_rev": ""}
def test_update_credentials_with_invalid_parent(self, test_client, session):
credential = self.factory.create()
session.add(credential)
session.commit()
raw_data = self._generate_raw_update_data('Name1', 'Username2', 'Password3', parent_id=43)
res = test_client.put(self.url(workspace=credential.workspace) + str(credential.id) + '/', data=raw_data)
assert res.status_code == 400
def test_create_with_invalid_parent_type(
self, session, test_client, workspace, service_factory):
service = service_factory.create(workspace=workspace)
session.commit()
raw_data = {
"_id":"1.e5069bb0718aa519852e6449448eedd717f1b90d",
"name":"name",
"username":"username",
"metadata":{"update_time":1508794240799,"update_user":"",
"update_action":0,"creator":"UI Web",
"create_time":1508794240799,"update_controller_action":"",
"owner":""},
"password":"pass",
"type":"Cred",
"owner":"",
"description":"",
"parent": service.id,
"parent_type": "Vulnerability"
}
res = test_client.post(self.url(), data=raw_data)
assert res.status_code == 400
assert res.json['messages']['_schema'] == ['Unknown parent type: Vulnerability']
def test_update_credentials(self, test_client, session, host):
credential = self.factory.create(host=host, service=None,
workspace=self.workspace)
session.commit()
raw_data = self._generate_raw_update_data(
'Name1', 'Username2', 'Password3', parent_id=credential.host.id)
res = test_client.put(self.url(workspace=credential.workspace) + str(credential.id) + '/', data=raw_data)
assert res.status_code == 200
assert res.json['username'] == u'Username2'
assert res.json['password'] == u'Password3'
assert res.json['name'] == u'Name1'
@pytest.mark.parametrize("parent_type, parent_factory", [
("Host", HostFactory),
("Service", ServiceFactory),
], ids=["with host parent", "with service parent"])
def test_create_with_parent_of_other_workspace(
self, parent_type, parent_factory, test_client, session,
second_workspace):
parent = parent_factory.create(workspace=second_workspace)
session.commit()
assert parent.workspace_id != self.workspace.id
data = {
"username": "admin",
"password": "admin",
"name": "test",
"parent_type": parent_type,
"parent": parent.id
}
res = test_client.post(self.url(), data=data)
assert res.status_code == 400
assert 'Parent id not found' in res.data
@pytest.mark.parametrize("parent_type, parent_factory", [
("Host", HostFactory),
("Service", ServiceFactory),
], ids=["with host parent", "with service parent"])
def test_update_with_parent_of_other_workspace(
self, parent_type, parent_factory, test_client, session,
second_workspace, credential_factory):
parent = parent_factory.create(workspace=second_workspace)
if parent_type == 'Host':
credential = credential_factory.create(
host=HostFactory.create(workspace=self.workspace),
service=None,
workspace=self.workspace)
else:
credential = credential_factory.create(
host=None,
service=ServiceFactory.create(workspace=self.workspace),
workspace=self.workspace)
session.commit()
assert parent.workspace_id != self.workspace.id
data = {
"username": "admin",
"password": "admin",
"name": "test",
"parent_type": parent_type,
"parent": parent.id
}
res = test_client.put(self.url(credential), data=data)
assert res.status_code == 400
assert 'Parent id not found' in res.data