Debian Codegraph @

src/apps/sailpoint/identitynow/restapi/custom/api.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::sailpoint::identitynow::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use centreon::plugins::statefile;
27 use JSON::XS;
28 use Digest::MD5 qw(md5_hex);
29
30 sub new {
31 my ($class, %options) = @_;
32 my $self = {};
33 bless $self, $class;
34
35 if (!defined($options{output})) {
36 print "Class Custom: Need to specify 'output' argument.\n";
37 exit 3;
38 }
39 if (!defined($options{options})) {
40 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
41 $options{output}->option_exit();
42 }
43
44 if (!defined($options{noptions})) {
45 $options{options}->add_options(arguments => {
46 'tenant:s' => { name => 'tenant' },
47 'domain:s' => { name => 'domain' },
48 'port:s' => { name => 'port' },
49 'proto:s' => { name => 'proto' },
50 'api-version:s' => { name => 'api_version' },
51 'client-id:s' => { name => 'client_id' },
52 'client-secret:s' => { name => 'client_secret' },
53 'timeout:s' => { name => 'timeout' }
54 });
55 }
56 $options{options}->add_help(package => __PACKAGE__, sections => 'SAILPOINT IDENTITYNOW REST API OPTIONS', once => 1);
57
58 $self->{output} = $options{output};
59 $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
60 $self->{cache} = centreon::plugins::statefile->new(%options);
61
62 return $self;
63 }
64
65 sub set_options {
66 my ($self, %options) = @_;
67
68 $self->{option_results} = $options{option_results};
69 }
70
71 sub set_defaults {}
72
73 sub check_options {
74 my ($self, %options) = @_;
75
76 $self->{tenant} = (defined($self->{option_results}->{tenant})) ? $self->{option_results}->{tenant} : '';
77 $self->{domain} = (defined($self->{option_results}->{domain})) ? $self->{option_results}->{domain} : 'identitynow';
78 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
79 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
80 $self->{api_version} = (defined($self->{option_results}->{api_version})) ? $self->{option_results}->{api_version} : 'v3';
81 $self->{client_id} = (defined($self->{option_results}->{client_id})) ? $self->{option_results}->{client_id} : '';
82 $self->{client_secret} = (defined($self->{option_results}->{client_secret})) ? $self->{option_results}->{client_secret} : '';
83 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
84
85 if ($self->{tenant} eq '') {
86 $self->{output}->add_option_msg(short_msg => 'Need to specify --tenant option.');
87 $self->{output}->option_exit();
88 }
89 if ($self->{client_id} eq '') {
90 $self->{output}->add_option_msg(short_msg => "Need to specify --client-id option.");
91 $self->{output}->option_exit();
92 }
93 if ($self->{client_secret} eq '') {
94 $self->{output}->add_option_msg(short_msg => "Need to specify --client-secret option.");
95 $self->{output}->option_exit();
96 }
97
98 $self->{hostname} = $self->{tenant} . '.api.' . $self->{domain} . '.com';
99
100 $self->{cache}->check_options(option_results => $self->{option_results});
101
102 return 0;
103 }
104
105 sub get_connection_infos {
106 my ($self, %options) = @_;
107
108 return $self->{hostname} . '_' . $self->{http}->get_port();
109 }
110
111 sub get_hostname {
112 my ($self, %options) = @_;
113
114 return $self->{hostname};
115 }
116
117 sub get_port {
118 my ($self, %options) = @_;
119
120 return $self->{port};
121 }
122
123 sub build_options_for_httplib {
124 my ($self, %options) = @_;
125
126 $self->{option_results}->{port} = $self->{port};
127 $self->{option_results}->{proto} = $self->{proto};
128 }
129
130 sub settings {
131 my ($self, %options) = @_;
132
133 $self->build_options_for_httplib();
134 $self->{http}->add_header(key => 'Accept', value => 'application/json');
135 $self->{http}->set_options(%{$self->{option_results}});
136 }
137
138 sub clean_token {
139 my ($self, %options) = @_;
140
141 my $datas = {};
142 $options{statefile}->write(data => $datas);
143 $self->{access_token} = undef;
144 $self->{http}->add_header(key => 'Authorization', value => undef);
145 }
146
147 sub get_auth_token {
148 my ($self, %options) = @_;
149
150 my $has_cache_file = $options{statefile}->read(
151 statefile => 'sailpoint_identitynow_api_' . md5_hex($self->{option_results}->{tenant}) . '_' . md5_hex($self->{option_results}->{client_id})
152 );
153 my $access_token = $options{statefile}->get(name => 'access_token');
154 my $expires_on = $options{statefile}->get(name => 'expires_on');
155 my $md5_secret_cache = $self->{cache}->get(name => 'md5_secret');
156 my $md5_secret = md5_hex($self->{client_id} . $self->{client_secret});
157
158 if ($has_cache_file == 0 || !defined($access_token) || (time() > $expires_on) ||
159 (defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)) {
160 my ($content) = $self->{http}->request(
161 method => 'POST',
162 hostname => $self->{hostname},
163 url_path => '/oauth/token',
164 post_param => [
165 'grant_type=client_credentials',
166 'client_id=' . $self->{client_id},
167 'client_secret=' . $self->{client_secret}
168 ],
169 unknown_status => '',
170 warning_status => '',
171 critical_status => ''
172 );
173
174 if (!defined($content) || $content eq '') {
175 $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
176 $self->{output}->option_exit();
177 }
178
179 my $decoded;
180 eval {
181 $decoded = JSON::XS->new->utf8->decode($content);
182 };
183 if ($@) {
184 $self->{output}->output_add(long_msg => $@, debug => 1);
185 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
186 $self->{output}->option_exit();
187 }
188 if (ref($decoded) eq 'HASH' && defined($decoded->{error})) {
189 $self->{output}->output_add(long_msg => "Error message : " . $decoded->{error_description}, debug => 1);
190 $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns error code '" . $decoded->{error} . "' (add --debug option for detailed message)");
191 $self->{output}->option_exit();
192 }
193 if (ref($decoded) eq 'HASH' && defined($decoded->{detailCode})) {
194 $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns error code '" . $decoded->{detailCode} . "' (add --debug option for detailed message)");
195 $self->{output}->option_exit();
196 }
197
198 if (!defined($decoded->{access_token})) {
199 $self->{output}->add_option_msg(short_msg => "Cannot get token");
200 $self->{output}->option_exit();
201 }
202
203 $access_token = $decoded->{access_token};
204 my $datas = {
205 access_token => $access_token,
206 expires_on => time() + $decoded->{expires_in},
207 md5_secret => $md5_secret
208 };
209 $options{statefile}->write(data => $datas);
210 }
211
212 $self->{access_token} = $access_token;
213 $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{access_token});
214 }
215
216 sub request_api {
217 my ($self, %options) = @_;
218
219 $self->settings();
220 if (!defined($self->{access_token})) {
221 $self->get_auth_token(statefile => $self->{cache});
222 }
223
224 my @results;
225 my $decoded;
226
227 my @get_param;
228 @get_param = (@get_param, @{$options{get_param}}) if (defined($options{get_param}) && scalar(@{$options{get_param}}));
229 # Dealing with pagination without using count parameter as recommended
230 # here https://developer.sailpoint.com/idn/api/standard-collection-parameters#paginating-results
231 my $limit = 250;
232 push @get_param, 'limit=' . $limit;
233 my $offset = 0;
234 push @get_param, 'offset=' . $offset;
235
236 do {
237 my $content = $self->{http}->request(
238 method => 'GET',
239 hostname => $self->{hostname},
240 url_path => $options{endpoint},
241 get_param => \@get_param,
242 unknown_status => '',
243 warning_status => '',
244 critical_status => ''
245 );
246
247 eval {
248 $decoded = JSON::XS->new->utf8->decode($content);
249 };
250 if ($@) {
251 $self->{output}->output_add(long_msg => $@, debug => 1);
252 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
253 $self->{output}->option_exit();
254 }
255 if (ref($decoded) eq 'HASH' && defined($decoded->{error})) {
256 $self->{output}->add_option_msg(short_msg => "Endpoint returns error code '" . $decoded->{error} . "' (add --debug option for detailed message)");
257 $self->{output}->option_exit();
258 }
259 if (ref($decoded) eq 'HASH' && defined($decoded->{detailCode})) {
260 $self->{output}->add_option_msg(short_msg => "Endpoint returns error code '" . $decoded->{detailCode} . "' (add --debug option for detailed message)");
261 $self->{output}->option_exit();
262 }
263 if ($self->{http}->get_code() != 200) {
264 $self->{output}->add_option_msg(short_msg => "Endpoint error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "'] (add --debug option for detailed message)");
265 $self->{output}->option_exit();
266 }
267
268 push @results, @$decoded if (ref($decoded) eq 'ARRAY');
269 push @results, $decoded if (ref($decoded) ne 'ARRAY');
270 pop @get_param;
271 $offset += $limit;
272 push @get_param, 'offset=' . $offset;
273 } while (ref($decoded) eq 'ARRAY' && scalar(@$decoded) eq $limit);
274
275 return \@results;
276 }
277
278 sub get_sources {
279 my ($self, %options) = @_;
280
281 my $result;
282
283 if (defined($options{id}) && $options{id} ne '') {
284 $result = $self->request_api(
285 endpoint => '/' . $self->{api_version} . '/sources/' . $options{id}
286 );
287 } else {
288 $result = $self->request_api(
289 endpoint => '/' . $self->{api_version} . '/sources'
290 );
291 }
292
293 return $result;
294 }
295
296 sub search_count {
297 my ($self, %options) = @_;
298
299 $self->settings();
300 if (!defined($self->{access_token})) {
301 $self->get_auth_token(statefile => $self->{cache});
302 }
303 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
304
305 my ($content) = $self->{http}->request(
306 method => 'POST',
307 hostname => $self->{hostname},
308 url_path => '/' . $self->{api_version} . '/search/count',
309 query_form_post => $options{query},
310 unknown_status => '%{http_code} < 200 or %{http_code} >= 300',
311 warning_status => '',
312 critical_status => ''
313 );
314
315 if ($self->{http}->get_code() != 204) {
316 my $decoded;
317 eval {
318 $decoded = JSON::XS->new->utf8->decode($content);
319 };
320 if ($@) {
321 $self->{output}->output_add(long_msg => $@, debug => 1);
322 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
323 $self->{output}->option_exit();
324 }
325 if (ref($decoded) eq 'HASH' && defined($decoded->{detailCode})) {
326 $self->{output}->add_option_msg(short_msg => "Endpoint returns error code '" . $decoded->{detailCode} . "' (add --debug option for detailed message)");
327 $self->{output}->option_exit();
328 } else {
329 $self->{output}->add_option_msg(short_msg => "Endpoint error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "'] (add --debug option for detailed message)");
330 $self->{output}->option_exit();
331 }
332 }
333
334 return $self->{http}->get_header(name => 'X-Total-Count');
335 }
336
337 1;
338
339 __END__
340
341 =head1 NAME
342
343 SailPoint IdentityNow API
344
345 =head1 SYNOPSIS
346
347 SailPoint IdentityNow API
348
349 =head1 SAILPOINT IDENTITYNOW REST API OPTIONS
350
351 =over 8
352
353 =item B<--tenant>
354
355 SailPoint IdentityNow API tenant.
356
357 =item B<--domain>
358
359 SailPoint IdentityNow API domain (default: identitynow)
360
361 =item B<--port>
362
363 SailPoint IdentityNow API port (default: 443)
364
365 =item B<--proto>
366
367 Specify https if needed (default: 'https')
368
369 =item B<--client-id>
370
371 SailPoint IdentityNow Client ID
372
373 =item B<--client-secret>
374
375 SailPoint IdentityNow Client Secret
376
377 =item B<--timeout>
378
379 Set HTTP timeout
380
381 =back
382
383 =head1 DESCRIPTION
384
385 B<custom>.
386
387 =cut