line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Armadito::Agent::Task::Enrollment; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
7306383
|
use strict; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
44
|
|
4
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
66
|
|
5
|
1
|
|
|
1
|
|
4
|
use base 'Armadito::Agent::Task'; |
|
1
|
|
|
|
|
35
|
|
|
1
|
|
|
|
|
421
|
|
6
|
1
|
|
|
1
|
|
7
|
use Armadito::Agent::Tools::File qw( readFile ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
7
|
1
|
|
|
1
|
|
6
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
8
|
1
|
|
|
1
|
|
5
|
use JSON; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
18
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(%params); |
14
|
|
|
|
|
|
|
|
15
|
0
|
0
|
|
|
|
|
if ( $params{debug} ) { |
16
|
0
|
|
|
|
|
|
$self->{debug} = 1; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $task = { |
20
|
|
|
|
|
|
|
name => "Enrollment", |
21
|
|
|
|
|
|
|
antivirus => $self->{agent}->{antivirus}->getJobj() |
22
|
0
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
$self->{jobj}->{task} = $task; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
return $self; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub run { |
30
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
$self = $self->SUPER::run(%params); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$self->_setEnrollmentKey(); |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $json_text = to_json( $self->{jobj} ); |
37
|
0
|
|
|
|
|
|
print $json_text. "\n"; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $response = $self->{glpi_client}->sendRequest( |
40
|
0
|
|
|
|
|
|
"url" => $self->{agent}->{config}->{server}[0] . "/api/agents", |
41
|
|
|
|
|
|
|
message => $json_text, |
42
|
|
|
|
|
|
|
method => "POST" |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
0
|
|
|
|
if ( $response->is_success() && $response->content() =~ /^\s*\{/ms ) { |
46
|
0
|
|
|
|
|
|
$self->_handleResponse($response); |
47
|
0
|
|
|
|
|
|
$self->{logger}->info("Enrollment successful..."); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else { |
50
|
0
|
|
|
|
|
|
$self->_handleError($response); |
51
|
0
|
|
|
|
|
|
$self->{logger}->info("Enrollment failed..."); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return $self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _handleError { |
58
|
0
|
|
|
0
|
|
|
my ( $self, $response ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
$self->{logger}->error( "Error Response : " . $response->content() . "\n" ); |
61
|
0
|
0
|
|
|
|
|
if ( $response->content() =~ /^\s*\{/ ) { |
62
|
0
|
|
|
|
|
|
my $obj = from_json( $response->content(), { utf8 => 1 } ); |
63
|
0
|
|
|
|
|
|
$self->{logger}->error( $obj->{message} ); |
64
|
|
|
|
|
|
|
} |
65
|
0
|
|
|
|
|
|
return $self; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _handleResponse { |
69
|
0
|
|
|
0
|
|
|
my ( $self, $response ) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$self->{logger}->info( $response->content() ); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $jobj = from_json( $response->content(), { utf8 => 1 } ); |
74
|
0
|
|
|
|
|
|
$self->_updateStorage($jobj); |
75
|
0
|
|
|
|
|
|
$self->_rmEnrollmentKey(); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
return $self; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _updateStorage { |
81
|
0
|
|
|
0
|
|
|
my ( $self, $jobj ) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
$self->{agent}->{agent_id} = defined( $jobj->{agent_id} ) ? $jobj->{agent_id} : 0; |
84
|
0
|
0
|
|
|
|
|
$self->{agent}->{scheduler_id} = defined( $jobj->{scheduler_id} ) ? $jobj->{scheduler_id} : 0; |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if ( $jobj->{agent_id} > 0 ) { |
87
|
0
|
|
|
|
|
|
$self->{agent}->_storeArmaditoIds(); |
88
|
0
|
|
|
|
|
|
$self->{logger}->info( "Agent successfully enrolled with id " . $jobj->{agent_id} ); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _rmEnrollmentKey { |
93
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my $keyfile = $self->_getEnrollmentKeyPath(); |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if ( -f $keyfile ) { |
98
|
0
|
|
|
|
|
|
unlink $keyfile; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _setEnrollmentKey { |
103
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $key = ''; |
106
|
0
|
|
|
|
|
|
my $jobj = {}; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$key = $self->_getEnrollmentKey(); |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ( $self->_isValidKeyFormat($key) ) { |
111
|
0
|
|
|
|
|
|
$jobj->{key} = $key; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
else { |
114
|
0
|
|
|
|
|
|
die "Invalid Key. Expected key format is : \n" |
115
|
|
|
|
|
|
|
. " [A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}\n" |
116
|
|
|
|
|
|
|
. " For example : AAAAF-111AF-DZ78F-EE78F-DDD1F"; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
$self->{jobj}->{task}->{obj} = $jobj; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _getEnrollmentKey { |
123
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my $key = ''; |
126
|
0
|
|
|
|
|
|
my $keyfile = $self->_getEnrollmentKeyPath(); |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if ( $self->{agent}->{key} ne "" ) { |
|
|
0
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
$key = $self->{agent}->{key}; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
elsif ( -f $keyfile ) { |
132
|
0
|
|
|
|
|
|
$key = readFile( filepath => $keyfile ); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
else { |
135
|
0
|
|
|
|
|
|
die "No enrollment key found."; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
return $key; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub _getEnrollmentKeyPath { |
142
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return $self->{agent}->{vardir} . "enrollment.key"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _isValidKeyFormat { |
148
|
0
|
|
|
0
|
|
|
my ( $self, $key ) = @_; |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
return $key =~ m/^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$/msi; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
__END__ |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 NAME |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Armadito::Agent::Task::Enrollment - Enrollment task of Armadito Agent. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 DESCRIPTION |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
This task inherits from L<Armadito::Agent::Task>. Enroll the device into Armadito plugin for GLPI. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 FUNCTIONS |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 run ( $self, %params ) |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Run the task. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 new ( $self, %params ) |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Instanciate Task. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|