Skip to content

Commit 8212d0c

Browse files
committed
Add API Token auth
1 parent be7917a commit 8212d0c

File tree

3 files changed

+112
-39
lines changed

3 files changed

+112
-39
lines changed

doc/03-Configuration.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ Create a new Icinga Director Import Source and use "ServiceNow Table API" as the
1515
An official list of available tables can be found [here](https://www.servicenow.com/docs/bundle/yokohama-servicenow-platform/page/product/configuration-management/reference/cmdb-tables-details.html?state=seamless).
1616
Since we wanted to make it as customizable as possible, we don't have a hard-coded API method. Therefore, you have to provide the full endpoint for the table here (e.g. `api/now/table/cmdb_ci_server`).
1717

18-
**ServiceNow API Username:** User to authenticate against the ServiceNow API.
19-
Currently only Basic Auth is supported to authenticate against ServiceNow.
20-
This user needs to have read access to the table you want to import.
21-
22-
**ServiceNow API Password:** Password to authenticate against the ServiceNow API.
23-
2418
**ServiceNow API Timeout:** Timeout for the API request in seconds. Default is `20` seconds.
2519

2620
**ServiceNow Columns:** List of columns to fetch.
@@ -32,3 +26,19 @@ Per default, we fetch all available columns. In case you want to limit the colum
3226
Per default, no filter limitation is applied to the query. The whole table will be fetched. In case you want to limit the objects, you can provide a filter query here.
3327

3428
To filter, you will need to follow the [official filter syntax from ServiceNow](https://www.servicenow.com/docs/bundle/yokohama-platform-user-interface/page/use/using-lists/concept/c_EncodedQueryStrings.html) An example could be `nameSTARTSWITHlnux`.
29+
30+
### Authentification methods
31+
32+
Currently supported authentification methods:
33+
34+
HTTP Basic Auth:
35+
36+
**ServiceNow API Username:** User to authenticate against the ServiceNow API.
37+
38+
This user needs to have read access to the table you want to import.
39+
40+
**ServiceNow API Password:** Password to authenticate against the ServiceNow API.
41+
42+
API Token:
43+
44+
**ServiceNow API Token:** Bearer token to authenticate against the ServiceNow API. Used in the header key `x-sn-apikey`.

library/Servicenowimport/Api/Servicenow.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,39 @@ class Servicenow
1212
{
1313
protected $client = null;
1414

15-
public function __construct(string $baseUri, string $username, string $password, bool $tlsVerify = true, int $timeout = 10)
15+
// $auth = [
16+
// 'method' => 'BASIC',
17+
// 'username' => 'user1',
18+
// 'password' => '123',
19+
// 'token' => '',
20+
// ];
21+
public function __construct(string $baseUri, bool $tlsVerify = true, int $timeout = 10, array $auth = [])
1622
{
17-
$this->client = new Client(
18-
[
19-
'base_uri' => $baseUri,
20-
'auth' => [$username, $password],
21-
'timeout' => $timeout,
22-
'verify' => $tlsVerify,
23-
'headers' => [
24-
'Accept' => 'application/json',
25-
'Content-Type' => 'application/json',
26-
],
27-
]
28-
);
23+
$c = [
24+
'base_uri' => $baseUri,
25+
'timeout' => $timeout,
26+
'verify' => $tlsVerify,
27+
'headers' => [
28+
'Accept' => 'application/json',
29+
'Content-Type' => 'application/json',
30+
],
31+
];
32+
33+
$method = $auth['method'] ?? 'UNKNOWN';
34+
35+
try {
36+
if ($method === 'BASIC') {
37+
$c['auth'] = [$auth['username'], $auth['password']];
38+
}
39+
40+
if ($method === 'BEARER') {
41+
$c['headers']['x-sn-apikey'] = $auth['token'];
42+
}
43+
} catch (\Exception $e) {
44+
throw new RuntimeException(sprintf("Failed to set authentication method %s", $e->getMessage()));
45+
}
46+
47+
$this->client = new Client($c);
2948
}
3049

3150
/**

library/Servicenowimport/ProvidedHook/Director/ImportSource.php

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,47 @@ public function getName()
1919
return "ServiceNow Table API";
2020
}
2121

22+
protected static function addAuthOptions(QuickForm $form)
23+
{
24+
$method = $form->getSentOrObjectSetting('servicenow_authmethod');
25+
26+
if ($method === 'BASIC') {
27+
$form->addElement(
28+
'text',
29+
'servicenow_username',
30+
[
31+
'label' => 'ServiceNow API username',
32+
'required' => true,
33+
'description' => 'Username to authenticate at the ServiceNow API',
34+
]
35+
);
36+
37+
$form->addElement(
38+
'password',
39+
'servicenow_password',
40+
[
41+
'label' => 'ServiceNow API password',
42+
'required' => true,
43+
'renderPassword' => true,
44+
'description' => 'Password to authenticate at the ServiceNow API',
45+
]
46+
);
47+
}
48+
49+
if ($method === 'BEARER') {
50+
$form->addElement(
51+
'password',
52+
'servicenow_token',
53+
[
54+
'label' => 'ServiceNow API token',
55+
'required' => true,
56+
'renderPassword' => true,
57+
'description' => 'Token to authenticate at the ServiceNow API',
58+
]
59+
);
60+
}
61+
}
62+
2263
/**
2364
* @param QuickForm $form
2465
* @return void
@@ -40,38 +81,34 @@ public static function addSettingsFormFields(QuickForm $form)
4081
'text',
4182
'servicenow_endpoint',
4283
[
43-
'label' => 'ServiceNow CMDB Table Endpoint',
84+
'label' => 'ServiceNow CMDB table endpoint',
4485
'required' => true,
4586
'description' => 'API endpoint to fetch objects from (e.g.: api/now/table/cmdb_ci_computer)',
4687
]
4788
);
4889

4990
$form->addElement(
50-
'text',
51-
'servicenow_username',
91+
'select',
92+
'servicenow_authmethod',
5293
[
53-
'label' => 'ServiceNow API Username',
94+
'label' => 'ServiceNow API authentification method',
95+
'description' => 'Authentification method for the ServiceNow API',
96+
'multiOptions' => [
97+
'BASIC' => 'Basic Auth',
98+
'BEARER' => 'API Token',
99+
],
100+
'class' => 'autosubmit',
54101
'required' => true,
55-
'description' => 'Username to authenticate at the ServiceNow API',
56102
]
57103
);
58104

59-
$form->addElement(
60-
'password',
61-
'servicenow_password',
62-
[
63-
'label' => 'ServiceNow API Password',
64-
'required' => true,
65-
'renderPassword' => true,
66-
'description' => 'Password to authenticate at the ServiceNow API',
67-
]
68-
);
105+
static::addAuthOptions($form);
69106

70107
$form->addElement(
71108
'text',
72109
'servicenow_timeout',
73110
[
74-
'label' => 'ServiceNow API Timeout',
111+
'label' => 'ServiceNow API timeout',
75112
'required' => false,
76113
'description' => 'Timeout in seconds used to query data from ServiceNow. Default is 20.',
77114
]
@@ -81,7 +118,7 @@ public static function addSettingsFormFields(QuickForm $form)
81118
'text',
82119
'servicenow_columns',
83120
[
84-
'label' => 'ServiceNow Columns',
121+
'label' => 'ServiceNow columns',
85122
'required' => false,
86123
'description' => 'Comma separated list of columns to fetch. Leave empty to fetch all columns.',
87124
]
@@ -91,7 +128,7 @@ public static function addSettingsFormFields(QuickForm $form)
91128
'text',
92129
'servicenow_query',
93130
[
94-
'label' => 'ServiceNow Query',
131+
'label' => 'ServiceNow query',
95132
'required' => false,
96133
'description' => 'Query to filter records. Leave empty to fetch all records. '
97134
. 'The query filter follows the official query syntax from ServiceNow: '
@@ -158,12 +195,19 @@ private function getRecordsFromTable(): array
158195
// Set endpoint
159196
$endpoint = sprintf('%s?sysparm_display_value=true', $this->getSetting('servicenow_endpoint'));
160197

198+
199+
$auth = [
200+
'method' => $this->getSetting('servicenow_authmethod'),
201+
'token' => $this->getSetting('servicenow_token'),
202+
'username' => $this->getSetting('servicenow_username'),
203+
'password' => $this->getSetting('servicenow_password'),
204+
];
205+
161206
$client = new Servicenow(
162207
$this->getSetting('servicenow_url'),
163-
$this->getSetting('servicenow_username'),
164-
$this->getSetting('servicenow_password'),
165208
self::CLIENT_TLS_VERIFY,
166209
$this->getSetting('servicenow_timeout') ?: self::CLIENT_TIMEOUT,
210+
$auth
167211
);
168212

169213
$result = $client->request(

0 commit comments

Comments
 (0)