-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluminate_python.py
More file actions
191 lines (145 loc) · 7.81 KB
/
luminate_python.py
File metadata and controls
191 lines (145 loc) · 7.81 KB
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
#!/usr/bin/python
"""
This module implements a friendly interface between the raw JSON
responses from Luminate and the Resource/dict abstractions provided by this library. Users
will construct a Luminate object as described below.
"""
import json
from oauthlib.oauth2 import BackendApplicationClient
import logging
from token_refetcher_oauth2session import TokenReFetcherOAuth2Session
class Luminate(object):
"""User interface to Luminate.
Clients interact with Luminate by constructing an instance of this object and calling its methods.
"""
def __init__(self, server, rest_api_version, client_id, client_secret, verify_ssl=True):
"""Construct a JIRA client instance.
:param server -- the server address and context path to use. Defaults to ``http://localhost:2990/jira``.
:param rest_api_version -- the version of the REST resources under rest_path to use. Defaults to ``1``.
:param client_id -- client_id as provided by the OAuth Provider (Luminate Security)
:param client_secret -- client_secret as provided by the OAuth Provider (Luminate Security)
"""
self._options = {'server': server, 'rest_api_version': rest_api_version}
self._create_oauth_session(client_id, client_secret, verify_ssl)
self._logger = logging.getLogger(__name__)
def _create_oauth_session(self, client_id, client_secret, verify_ssl=True):
token_url = '{}/v1/oauth/token'.format(self._options['server'])
client = BackendApplicationClient(client_id=client_id)
client.prepare_request_body()
oauth = TokenReFetcherOAuth2Session(token_url=token_url,
client_secret=client_secret,
client=client,
verify=verify_ssl)
self._session = oauth
def create_app(self, app_name, description, app_type, internal_address, site_name, ssh_users):
"""Create a new Application at a specific Site.
:param app_name: Application Name
:param description: A string which describes the application
:param app_type: Application type - Valid values are HTTP, SSH.
:param internal_address: Application internal IP
:param site_name: The name of the site on which this application resides.
:param ssh_users: A list of user names that are available for SSH log-in on the remote ssh machine.
"""
connection_settings = {'internal_address': internal_address}
url_template = '{}/v{}/applications'
url = url_template.format(self._options['server'], self._options['rest_api_version'])
payload = {
'name': app_name,
'description': description,
'type': app_type,
'connection_settings': connection_settings,
'site_name': site_name,
}
if app_type == 'SSH':
if ssh_users:
payload['ssh_users'] = ssh_users
else:
raise ValueError('A request for creating an SSH application must include SSH users')
response = self._session.post(url, data=json.dumps(payload))
self._logger.debug("Request to Luminate for creating an application :%s returned response: %s, status code:%s"
% (app_name, response.content, response.status_code))
if response.status_code != 201:
response.raise_for_status()
return None
data = response.json()
return data['id']
def update_app(self, app_id, app_name, description, app_type, internal_address, site_name, ssh_users):
"""Updates an existing application.
:param app_id: Application ID
:param app_name: Application Name
:param description: A string which describes the application
:param app_type: Application type - Valid values are HTTP, SSH.
:param internal_address: Application internal IP
:param site_name: The name of the site on which this application resides.
:param ssh_users: A list of user names that are available for SSH log-in on the remote ssh machine.
"""
connection_settings = {'internal_address': internal_address}
url_template = '{}/v{}/applications/{}'
url = url_template.format(self._options['server'], self._options['rest_api_version'], app_id)
payload = {
'name': app_name,
'description': description,
'type': app_type,
'connection_settings': connection_settings,
'site_name': site_name,
}
if app_type == 'SSH':
if ssh_users:
payload['ssh_users'] = ssh_users
else:
raise ValueError(
'Request to Luminate for updating an application %s failed - missing SSH users' % app_name)
response = self._session.put(url, data=json.dumps(payload))
self._logger.debug("Request to Luminate for updating an application :%s returned response: %s, status code:%s"
% (app_name, response.content, response.status_code))
if response.status_code != 200:
response.raise_for_status()
return -1
return 0
def assign_user_to_app(self, app_id, email, idp, ssh_users):
"""
Assign a user to an application.
:param app_id: Application ID
:param email: The e-mail address of the user to whom you would like to grant access to the application.
:param idp: Identity Provider of the user.
:param ssh_users: A list of user names with which the user will be able to log-in to the ssh machine.
"""
url_template = '{}/v{}/applications/{}/assign-user'
url = url_template.format(self._options['server'], self._options['rest_api_version'], app_id)
payload = {
'email': email,
'idp_name': idp
}
if ssh_users:
payload['ssh_users'] = ssh_users
self._logger.debug("SSH users: %s were defined for user: %s" % (ssh_users, email))
response = self._session.post(url, data=json.dumps(payload))
self._logger.debug("Request to Luminate for assigning a user :%s to application %s returned response:\n %s,\
status code:%s" % (email, app_id, response.content, response.status_code))
if response.status_code != 200:
response.raise_for_status()
return -1
return 0
def assign_group_to_app(self, app, name, idp, ssh_users):
"""
Assign a group to an application.
:param app: Application ID
:param name: The name of the group to which you would like to grant access to the application.
:param idp: Identity Provider of the group.
:param ssh_users: A list of user names with which the group members will be able to log-in to the ssh machine.
"""
url_template = '{}/v{}/applications/{}/assign-group'
url = url_template.format(self._options['server'], self._options['rest_api_version'], app)
payload = {
'name': name,
'idp_name': idp
}
if ssh_users:
payload['ssh_users'] = ssh_users
self._logger.debug("SSH users: %s were defined for group: %s" % (ssh_users, name))
response = self._session.post(url, data=json.dumps(payload))
self._logger.debug("Request to Luminate for assigning a group :%s to application %s returned response:\n %s,\
status code:%s" % (name, app, response.content, response.status_code))
if response.status_code != 200:
response.raise_for_status()
return 0