forked from splunk/splunk-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.py
286 lines (248 loc) · 9.77 KB
/
binding.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright 2011 Splunk, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"): you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Low-level 'binding' interface to the Splunk REST API."""
import httplib
import socket
import ssl
import urllib
from xml.etree.ElementTree import XML
from splunk.data import record
__all__ = [
"connect",
"Context",
"handler",
"HTTPError",
]
DEFAULT_HOST = "localhost"
DEFAULT_PORT = "8089"
DEFAULT_SCHEME = "https"
# Construct an URL prefix from the given scheme, host and port.
# kwargs: scheme, host, port
def prefix(**kwargs):
scheme = kwargs.get("scheme", DEFAULT_SCHEME)
host = kwargs.get("host", DEFAULT_HOST)
port = kwargs.get("port", DEFAULT_PORT)
return "%s://%s:%s" % (scheme, host, port)
class Context:
# kwargs: scheme, host, port, username, password, namespace
def __init__(self, handler=None, **kwargs):
self.http = HttpLib(handler)
self.token = None
self.prefix = prefix(**kwargs)
self.scheme = kwargs.get("scheme", DEFAULT_SCHEME)
self.host = kwargs.get("host", DEFAULT_HOST)
self.port = kwargs.get("port", DEFAULT_PORT)
self.username = kwargs.get("username", "")
self.password = kwargs.get("password", "")
self.namespace = kwargs.get("namespace", None)
# Shared per-context request headers
def _headers(self):
return [("Authorization", self.token)]
def connect(self):
"""Open a connection (socket) to the service (host:port)."""
cn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cn.connect((self.host, int(self.port)))
return ssl.wrap_socket(cn) if self.scheme == "https" else cn
def delete(self, path, **kwargs):
"""Issue a DELETE request to the given path."""
return self.http.delete(self.url(https://github.com/path), self._headers(), **kwargs)
def get(self, path, **kwargs):
"""Issue a GET request to the given path."""
return self.http.get(self.url(https://github.com/path), self._headers(), **kwargs)
def post(self, path, **kwargs):
"""Issue a POST request to the given path."""
return self.http.post(self.url(https://github.com/path), self._headers(), **kwargs)
def request(self, path, message):
"""Issue the given HTTP request message to the given endpoint."""
return self.http.request(
self.url(https://github.com/path), {
'method': message.get("method", "GET"),
'headers': message.get("headers", []) + self._headers(),
'body': message.get("body", "")})
def login(self):
"""Issue a Splunk login request using the context's credentials and
store the session token for use on subsequent requests."""
response = self.http.post(
self.url(https://github.com/"/services/auth/login"),
username=self.username,
password=self.password)
body = response.body.read()
session = XML(body).findtext("./sessionKey")
self.token = "Splunk %s" % session
return self
def logout(self):
"""Forget the current session token."""
self.token = None
return self
def fullpath(self, path):
"""If the given path is a fragment, qualify with segments corresponding
to the binding context's namespace."""
if path.startswith('/'):
return path
if self.namespace is None:
return "/services/%s" % path
username, appname = self.namespace.split(':')
if username == "*":
username = '-'
if appname == "*":
appname = '-'
return "/servicesNS/%s/%s/%s" % (username, appname, path)
# Convet the given path into a fully qualified URL by first qualifying
# the given path with namespace segments if necessarry and then prefixing
# with the scheme, host and port.
def url(https://github.com/self,%20path):
"""Converts the given path or path fragment into a complete URL."""
return self.prefix + self.fullpath(path)
# kwargs: scheme, host, port, username, password, namespace
def connect(**kwargs):
"""Establishes an authenticated context with the given host."""
return Context(**kwargs).login()
# Note: the error response schema supports multiple messages but we only
# return the first, although we do return the body so that an exception
# handler that wants to read multiple messages can do so.
def read_error_message(response):
body = response.body.read()
return body, XML(body).findtext("./messages/msg")
class HTTPError(Exception):
def __init__(self, response):
status = response.status
reason = response.reason
body, detail = read_error_message(response)
message = "HTTP %d %s%s" % (
status, reason, "" if detail is None else " -- %s" % detail)
Exception.__init__(self, message)
self.status = status
self.reason = reason
self.headers = response.headers
self.body = body
#
# The HTTP interface used by the Splunk binding layer abstracts the unerlying
# HTTP library using request & response 'messages' which are implemented as
# dictionaries with the following structure:
#
# # HTTP request message (only method required)
# request {
# method : str,
# headers? : [(str, str)*],
# body? : str,
# }
#
# # HTTP response message (all keys present)
# response {
# status : int,
# reason : str,
# headers : [(str, str)*],
# body : file,
# }
#
# Encode the given kwargs as a query string. This wrapper will also encode
# a list value as a sequence of assignemnts to the corresponding arg name,
# for example an argument such as 'foo=[1,2,3]' will be encoded as
# 'foo=1&foo=2&foo=3'.
def encode(**kwargs):
items = []
for key, value in kwargs.iteritems():
if isinstance(value, list):
items.extend([(key, item) for item in value])
else:
items.append((key, value))
return urllib.urlencode(items)
# Crack the given url into (scheme, host, port, path)
def split/service/https://github.com/url(url):
scheme, part = url.split(':', 1)
host, path = urllib.splithost(part)
host, port = urllib.splitnport(host, 80)
return scheme, host, port, path
# Given an HTTP request handler, this wrapper objects provides a related
# family of convenience methods built using that handler.
class HttpLib(object):
def __init__(self, custom_handler=None):
self.handler = handler() if custom_handler is None else custom_handler
def delete(self, url, headers=None, **kwargs):
if headers is None: headers = []
if kwargs:
url = url + '?' + encode(**kwargs)
message = {
'method': "DELETE",
'headers': headers,
}
return self.request(url, message)
def get(self, url, headers=None, **kwargs):
if headers is None: headers = []
if kwargs:
url = url + '?' + encode(**kwargs)
return self.request(url, { 'method': "GET", 'headers': headers })
def post(self, url, headers=None, **kwargs):
if headers is None: headers = []
headers.append(("Content-Type", "application/x-www-form-urlencoded")),
message = {
'method': "POST",
'headers': headers,
'body': encode(**kwargs)
}
return self.request(url, message)
def request(self, url, headers=None, **kwargs):
response = self.handler(url, headers, **kwargs)
response = record(response)
if 400 <= response.status:
raise HTTPError(response)
return response
# Converts an httplib response into a file-like object.
class ResponseReader:
def __init__(self, response):
self._response = response
def __str__(self):
return self.read()
def read(self, size = None):
return self._response.read(size)
# The default HTTP request handler.
def handler(key_file=None, cert_file=None, timeout=None):
"""Creates an HTTP request handler parameterized with the given args."""
def connect(scheme, host, port):
kwargs = {}
if timeout is not None: kwargs['timeout'] = timeout
if scheme == "http":
return httplib.HTTPConnection(host, port, **kwargs)
if scheme == "https":
if key_file is not None: kwargs['key_file'] = key_file
if cert_file is not None: kwargs['cert_file'] = cert_file
return httplib.HTTPSConnection(host, port, **kwargs)
raise ValueError("unsupported scheme: %s" % scheme)
def request(url, message, **kwargs):
scheme, host, port, path = split/service/https://github.com/url(url)
body = message.get("body", "")
head = {
"Content-Length": str(len(body)),
"Host": host,
"User-Agent": "splunk-sdk-python/0.1",
"Accept": "*/*",
} # defaults
for key, value in message["headers"]:
head[key] = value
method = message.get("method", "GET")
connection = connect(scheme, host, port)
try:
connection.request(method, path, body, head)
if timeout is not None:
connection.sock.settimeout(timeout)
response = connection.getresponse()
finally:
connection.close()
return {
"status": response.status,
"reason": response.reason,
"headers": response.getheaders(),
"body": ResponseReader(response),
}
return request