This repository was archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathContext.php
More file actions
302 lines (274 loc) · 9.63 KB
/
Copy pathContext.php
File metadata and controls
302 lines (274 loc) · 9.63 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Copyright 2013 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.
*/
/**
* Enables clients to issue HTTP requests to a Splunk server.
*
* @package Splunk
*/
class Splunk_Context
{
private $username;
private $password;
private $token;
private $host;
private $port;
private $scheme;
private $namespace;
private $http;
/**
* Constructs a new context with the specified parameters.
*
* @param array $args {<br/>
* **username**: (optional) The username to login with. Defaults to
* "admin".<br/>
* **password**: (optional) The password to login with. Defaults to
* "changeme".<br/>
* **token**: (optional) The authentication token to use. If provided,
* the username and password are ignored and there is no
* need to call login(). In the format "Splunk SESSION_KEY".
* <br/>
* **host**: (optional) The hostname of the Splunk server. Defaults to
* "localhost".<br/>
* **port**: (optional) The port of the Splunk server. Defaults to
* 8089.<br/>
* **scheme**: (optional) The scheme to use: either "http" or "https".
* Defaults to "https".<br/>
* **namespace**: (optional) Namespace that all object lookups will
* occur in by default. Defaults to
* `Splunk_Namespace::createDefault()`.<br/>
* **http**: (optional) An Http object that will be used for
* performing HTTP requests. This is intended for testing
* only.<br/>
* }
*/
public function __construct($args=array())
{
$args = array_merge(array(
'username' => 'admin',
'password' => 'changeme',
'token' => NULL,
'host' => 'localhost',
'port' => 8089,
'scheme' => 'https',
'namespace' => Splunk_Namespace::createDefault(),
'http' => new Splunk_Http(),
), $args);
$this->username = $args['username'];
$this->password = $args['password'];
$this->token = $args['token'];
$this->host = $args['host'];
$this->port = $args['port'];
$this->scheme = $args['scheme'];
$this->namespace = $args['namespace'];
$this->http = $args['http'];
}
// === Operations ===
/**
* Authenticates to the Splunk server.
*/
public function login()
{
$response = $this->http->post($this->url('/services/auth/login'), array(
'username' => $this->username,
'password' => $this->password,
));
$sessionKey = Splunk_XmlUtil::getTextContentAtXpath(
new SimpleXMLElement($response->body),
'/response/sessionKey');
$this->token = "Splunk {$sessionKey}";
}
// === HTTP ===
/**
* Sends an HTTP GET request to the endpoint at the specified path.
*
* @param string $path relative or absolute URL path.
* @param array $args (optional) query parameters, merged with {<br/>
* **namespace**: (optional) namespace to use, or NULL to use
* this context's default namespace.<br/>
* }
* @return Splunk_HttpResponse
* @throws Splunk_IOException
* @see Splunk_Http::get()
*/
public function sendGet($path, $args=array())
{
return $this->sendSimpleRequest('get', $path, $args);
}
/**
* Sends an HTTP POST request to the endpoint at the specified path.
*
* @param string $path relative or absolute URL path.
* @param array $args (optional) form parameters to send in the
* request body, merged with {<br/>
* **namespace**: (optional) namespace to use, or NULL to use
* this context's default namespace.<br/>
* }
* @return Splunk_HttpResponse
* @throws Splunk_IOException
* @see Splunk_Http::post()
*/
public function sendPost($path, $args=array())
{
return $this->sendSimpleRequest('post', $path, $args);
}
/**
* Sends an HTTP DELETE request to the endpoint at the specified path.
*
* @param string $path relative or absolute URL path.
* @param array $args (optional) query parameters, merged with {<br/>
* **namespace**: (optional) namespace to use, or NULL to use
* this context's default namespace.<br/>
* }
* @return Splunk_HttpResponse
* @throws Splunk_IOException
* @see Splunk_Http::delete()
*/
public function sendDelete($path, $args=array())
{
return $this->sendSimpleRequest('delete', $path, $args);
}
/**
* Sends a simple HTTP request to the endpoint at the specified path.
*/
private function sendSimpleRequest($method, $path, $args)
{
list($params, $namespace) =
Splunk_Util::extractArgument($args, 'namespace', NULL);
return $this->http->$method(
$this->url($path, $namespace),
$params,
$this->getRequestHeaders());
}
/**
* Sends an HTTP request to the endpoint at the specified path.
*
* @param string $method the HTTP method (ex: 'GET' or 'POST').
* @param string $path relative or absolute URL path.
* @param array $requestHeaders (optional) dictionary of header names and
* values.
* @param string $requestBody (optional) content to send in the request.
* @param array $args (optional) query parameters, merged with
* {<br/>
* **namespace**: (optional) namespace to use, or NULL to use
* this context's default namespace.<br/>
* }
* @return Splunk_HttpResponse
* @throws Splunk_IOException
* @see Splunk_Http::request()
*/
public function sendRequest(
$method, $path, $requestHeaders=array(), $requestBody='', $args=array())
{
list($params, $namespace) =
Splunk_Util::extractArgument($args, 'namespace', NULL);
$url = $this->url($path, $namespace);
$fullUrl = (count($params) == 0)
? $url
: $url . '?' . http_build_query($params);
$requestHeaders = array_merge(
$this->getRequestHeaders(),
$requestHeaders);
return $this->http->request(
$method,
$fullUrl,
$requestHeaders,
$requestBody);
}
/** Returns the standard headers to send on each HTTP request. */
private function getRequestHeaders()
{
return array(
'Authorization' => $this->token,
);
}
// === Accessors ===
/**
* Gets the default namespace for collection and entity operations.
*
* @return Splunk_Namespace The default namespace that will be used
* to perform collection and entity operations
* when none is explicitly specified.
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* Gets the token used to authenticate HTTP requests after logging in.
*
* @return string The token used to authenticate HTTP requests
* after logging in.
*/
public function getToken()
{
return $this->token;
}
/**
* Gets the hostname of the Splunk server.
*
* @return string The hostname of the Splunk server.
*/
public function getHost()
{
return $this->host;
}
/**
* Gets the port of the Splunk server.
*
* @return string The port of the Splunk server.
*/
public function getPort()
{
return $this->port;
}
/**
* Gets the scheme to use.
*
* @return string The scheme to use: either "http" or "https".
*/
public function getScheme()
{
return $this->scheme;
}
// === Utility ===
/**
* Returns the absolute URL.
*
* @param string $path Relative or absolute URL path.
* @param Splunk_Namespace|NULL $namespace
* @return string Absolute URL.
*/
private function url($path, $namespace=NULL)
{
return "{$this->scheme}://{$this->host}:{$this->port}{$this->abspath($path, $namespace)}";
}
/**
* Returns the absolute URL path.
*
* @param string $path Relative or absolute URL path.
* @param Splunk_Namespace|NULL $namespace
* @return string Absolute URL path.
*/
private function abspath($path, $namespace=NULL)
{
if ((strlen($path) >= 1) && ($path[0] == '/'))
return $path;
if ($namespace === NULL)
$namespace = $this->namespace;
return $namespace->getPathPrefix() . $path;
}
}