| 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 centreon::script::centreonvault; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use JSON::XS; |
| 26 |
use MIME::Base64; |
| 27 |
use Crypt::OpenSSL::AES; |
| 28 |
use Net::Curl::Easy qw(:constants); |
| 29 |
use centreon::vmware::common; |
| 30 |
|
| 31 |
my $VAULT_PATH_REGEX = qr/^secret::hashicorp_vault::([^:]+)::(.+)$/; |
| 32 |
|
| 33 |
sub new { |
| 34 |
my ($class, %options) = @_; |
| 35 |
my $self = bless \%options, $class; |
| 36 |
# mandatory options: |
| 37 |
# - logger: logger object |
| 38 |
# - config_file: path of a JSON vault config file |
| 39 |
|
| 40 |
$self->{enabled} = 1; |
| 41 |
$self->{crypted_credentials} = 1; |
| 42 |
|
| 43 |
if ( !$self->init() ) { |
| 44 |
$self->{enabled} = 0; |
| 45 |
$self->{logger}->writeLogInfo("Something happened during init() method that makes Centreonvault not usable. Ignore this if you don't use Centreonvault."); |
| 46 |
} |
| 47 |
return $self; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
sub init { |
| 52 |
my ($self, %options) = @_; |
| 53 |
|
| 54 |
$self->check_options() or return undef; |
| 55 |
|
| 56 |
# check if the following information is available |
| 57 |
$self->{logger}->writeLogDebug("Reading Vault configuration from file " . $self->{config_file} . "."); |
| 58 |
$self->{vault_config} = centreon::vmware::common::parse_json_file( 'json_file' => $self->{config_file} ); |
| 59 |
if (defined($self->{vault_config}->{error_message})) { |
| 60 |
$self->{logger}->writeLogError("Error while parsing " . $self->{config_file} . ": " |
| 61 |
. $self->{vault_config}->{error_message}); |
| 62 |
return undef; |
| 63 |
} |
| 64 |
|
| 65 |
$self->check_configuration() or return undef; |
| 66 |
|
| 67 |
$self->{logger}->writeLogDebug("Vault configuration read. Name: " . $self->{vault_config}->{name} |
| 68 |
. ". Url: " . $self->{vault_config}->{url} . "."); |
| 69 |
|
| 70 |
# Create the Curl object, it will be used several times |
| 71 |
$self->{curl_easy} = Net::Curl::Easy->new(); |
| 72 |
$self->{curl_easy}->setopt( CURLOPT_USERAGENT, "Centreon VMware daemon's centreonvault.pm"); |
| 73 |
|
| 74 |
return 1; |
| 75 |
} |
| 76 |
|
| 77 |
sub check_options { |
| 78 |
my ($self, %options) = @_; |
| 79 |
|
| 80 |
if ( !defined($self->{logger}) ) { |
| 81 |
die "FATAL: No logger given to the constructor. Centreonvault cannot be used."; |
| 82 |
} |
| 83 |
if ( !defined($self->{config_file})) { |
| 84 |
$self->{logger}->writeLogError("No config file given to the constructor. Centreonvault cannot be used."); |
| 85 |
return undef; |
| 86 |
} |
| 87 |
if ( ! -f $self->{config_file} ) { |
| 88 |
$self->{logger}->writeLogError("The given configuration file " . $self->{config_file} |
| 89 |
. " does not exist. Passwords won't be retrieved from Centreonvault. Ignore this if you don't use Centreonvault."); |
| 90 |
return undef; |
| 91 |
} |
| 92 |
|
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
sub check_configuration { |
| 97 |
my ($self, %options) = @_; |
| 98 |
|
| 99 |
if ( !defined($self->{vault_config}->{url}) || $self->{vault_config}->{url} eq '') { |
| 100 |
$self->{logger}->writeLogInfo("Vault url is missing from configuration."); |
| 101 |
$self->{vault_config}->{url} = '127.0.0.1'; |
| 102 |
} |
| 103 |
if ( !defined($self->{vault_config}->{port}) || $self->{vault_config}->{port} eq '') { |
| 104 |
$self->{logger}->writeLogInfo("Vault port is missing from configuration."); |
| 105 |
$self->{vault_config}->{port} = '443'; |
| 106 |
} |
| 107 |
|
| 108 |
# Normally, the role_id and secret_id data are encrypted using AES wit the following information: |
| 109 |
# firstKey = APP_SECRET (environment variable) |
| 110 |
# secondKey = 'salt' (hashing) key given by vault.json configuration file |
| 111 |
# both are base64 encoded |
| 112 |
if ( !defined($self->{vault_config}->{salt}) || $self->{vault_config}->{salt} eq '') { |
| 113 |
$self->{logger}->writeLogError("Vault environment does not seem complete: 'salt' attribute missing from " |
| 114 |
. $self->{config_file} |
| 115 |
. ". 'role_id' and 'secret_id' won't be decrypted, so they'll be used as they're stored in the vault config file."); |
| 116 |
$self->{crypted_credentials} = 0; |
| 117 |
$self->{hash_key} = ''; |
| 118 |
} else { |
| 119 |
$self->{hash_key} = $self->{vault_config}->{salt}; # key for sha3-512 hmac |
| 120 |
} |
| 121 |
|
| 122 |
if ( !defined($ENV{'APP_SECRET'}) || $ENV{'APP_SECRET'} eq '' ) { |
| 123 |
$self->{logger}->writeLogError("Vault environment does not seem complete. 'APP_SECRET' environment variable missing." |
| 124 |
. " 'role_id' and 'secret_id' won't be decrypted, so they'll be used as they're stored in the vault config file."); |
| 125 |
$self->{crypted_credentials} = 0; |
| 126 |
$self->{encryption_key} = ''; |
| 127 |
} else { |
| 128 |
$self->{encryption_key} = $ENV{'APP_SECRET'}; # key for aes-256-cbc |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
sub extract_and_decrypt { |
| 138 |
my ($self, %options) = @_; |
| 139 |
|
| 140 |
my $input = decode_base64($options{data}); |
| 141 |
$self->{logger}->writeLogDebug("data to extract and decrypt: '" . $options{data} . "'"); |
| 142 |
|
| 143 |
# with AES-256, the IV length must 16 bytes |
| 144 |
my $iv_length = 16; |
| 145 |
# extract the IV, the hashed data, the encrypted data |
| 146 |
my $iv = substr($input, 0, $iv_length); # initialization vector |
| 147 |
my $hashed_data = substr($input, $iv_length, 64); # hmac of the original data, for integrity control |
| 148 |
my $encrypted_data = substr($input, $iv_length + 64); # data to decrypt |
| 149 |
|
| 150 |
# create the AES object |
| 151 |
$self->{logger}->writeLogDebug( |
| 152 |
"Creating the AES decryption object for initialization vector (IV) of length " |
| 153 |
. length($iv) . "B, key of length " . length($self->{encryption_key}) . "B." |
| 154 |
); |
| 155 |
my $cipher; |
| 156 |
eval { |
| 157 |
$cipher = Crypt::OpenSSL::AES->new( |
| 158 |
decode_base64( $self->{encryption_key} ), |
| 159 |
{ |
| 160 |
'cipher' => 'AES-256-CBC', |
| 161 |
'iv' => $iv, |
| 162 |
'padding' => 1 |
| 163 |
} |
| 164 |
); |
| 165 |
}; |
| 166 |
if ($@) { |
| 167 |
$self->{logger}->writeLogError("There was an error while creating the AES object: " . $@); |
| 168 |
return undef; |
| 169 |
} |
| 170 |
|
| 171 |
# decrypt |
| 172 |
$self->{logger}->writeLogDebug("Decrypting the data of length " . length($encrypted_data) . "B."); |
| 173 |
my $decrypted_data; |
| 174 |
eval {$decrypted_data = $cipher->decrypt($encrypted_data);}; |
| 175 |
if ($@) { |
| 176 |
$self->{logger}->writeLogError("There was an error while decrypting one of the AES-encrypted data: " . $@); |
| 177 |
return undef; |
| 178 |
} |
| 179 |
|
| 180 |
return $decrypted_data; |
| 181 |
} |
| 182 |
|
| 183 |
sub authenticate { |
| 184 |
my ($self) = @_; |
| 185 |
|
| 186 |
# initial value: assuming the role and secret id might not be encrypted |
| 187 |
my $role_id = $self->{vault_config}->{role_id}; |
| 188 |
my $secret_id = $self->{vault_config}->{secret_id}; |
| 189 |
|
| 190 |
|
| 191 |
if ($self->{crypted_credentials}) { |
| 192 |
# Then decrypt using https://github.com/perl-openssl/perl-Crypt-OpenSSL-AES |
| 193 |
# keep the decrypted data in local variables so that they stay in memory for as little time as possible |
| 194 |
$self->{logger}->writeLogDebug("Decrypting the credentials needed to authenticate to the vault."); |
| 195 |
$role_id = $self->extract_and_decrypt( ('data' => $role_id )); |
| 196 |
$secret_id = $self->extract_and_decrypt( ('data' => $secret_id )); |
| 197 |
$self->{logger}->writeLogDebug("role_id and secret_id have been decrypted."); |
| 198 |
} else { |
| 199 |
$self->{logger}->writeLogDebug("role_id and secret_id are not crypted"); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
# Authenticate to get the token |
| 204 |
my $url = "https://" . $self->{vault_config}->{url} . ":" . $self->{vault_config}->{port} . "/v1/auth/approle/login"; |
| 205 |
$self->{logger}->writeLogDebug("Authenticating to the vault server at URL: $url"); |
| 206 |
$self->{curl_easy}->setopt( CURLOPT_URL, $url ); |
| 207 |
|
| 208 |
my $post_data = "role_id=$role_id&secret_id=$secret_id"; |
| 209 |
my $auth_result_json; |
| 210 |
# to get more details (in STDERR) |
| 211 |
#$self->{curl_easy}->setopt(CURLOPT_VERBOSE, 1); |
| 212 |
$self->{curl_easy}->setopt(CURLOPT_POST, 1); |
| 213 |
$self->{curl_easy}->setopt(CURLOPT_POSTFIELDS, $post_data); |
| 214 |
$self->{curl_easy}->setopt(CURLOPT_POSTFIELDSIZE, length($post_data)); |
| 215 |
$self->{curl_easy}->setopt(CURLOPT_WRITEDATA(), \$auth_result_json); |
| 216 |
|
| 217 |
eval { |
| 218 |
$self->{curl_easy}->perform(); |
| 219 |
}; |
| 220 |
if ($@) { |
| 221 |
$self->{logger}->writeLogError("Error while authenticating to the vault: " . $@); |
| 222 |
return undef; |
| 223 |
} |
| 224 |
|
| 225 |
$self->{logger}->writeLogInfo("Authentication to the vault passed." ); |
| 226 |
|
| 227 |
my $auth_result_obj = centreon::vmware::common::transform_json_to_object($auth_result_json); |
| 228 |
if (defined($auth_result_obj->{error_message})) { |
| 229 |
$self->{logger}->writeLogError("Error while decoding JSON '$auth_result_json'. Message: " |
| 230 |
. $auth_result_obj->{error_message}); |
| 231 |
return undef; |
| 232 |
} |
| 233 |
|
| 234 |
# store the token (.auth.client_token) and its expiration date (current date + .lease_duration) |
| 235 |
my $expiration_epoch = -1; |
| 236 |
my $lease_duration = $auth_result_obj->{auth}->{lease_duration}; |
| 237 |
if ( defined($lease_duration) |
| 238 |
&& $lease_duration =~ /\d+/ |
| 239 |
&& $lease_duration > 0 ) { |
| 240 |
$expiration_epoch = time() + $lease_duration; |
| 241 |
} |
| 242 |
$self->{auth} = { |
| 243 |
'token' => $auth_result_obj->{auth}->{client_token}, |
| 244 |
'expiration_epoch' => $expiration_epoch |
| 245 |
}; |
| 246 |
|
| 247 |
$self->{logger}->writeLogInfo("Authenticating worked. Token valid until " |
| 248 |
. localtime($self->{auth}->{expiration_epoch})); |
| 249 |
|
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
sub is_token_still_valid { |
| 254 |
my ($self) = @_; |
| 255 |
if ( |
| 256 |
!defined($self->{auth}) |
| 257 |
|| !defined($self->{auth}->{token}) |
| 258 |
|| $self->{auth}->{token} eq '' |
| 259 |
|| $self->{auth}->{expiration_epoch} <= time() |
| 260 |
) { |
| 261 |
$self->{logger}->writeLogInfo("The token has expired or is invalid."); |
| 262 |
return undef; |
| 263 |
} |
| 264 |
$self->{logger}->writeLogDebug("The token is still valid."); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
sub get_secret { |
| 269 |
my ($self, $secret) = @_; |
| 270 |
|
| 271 |
# if vault not enabled, return the secret unchanged |
| 272 |
return $secret if ( ! $self->{enabled}); |
| 273 |
|
| 274 |
my ($secret_path, $secret_name) = $secret =~ $VAULT_PATH_REGEX; |
| 275 |
if (!defined($secret_path) || !defined($secret_name)) { |
| 276 |
$self->{logger}->writeLogInfo("A string given to get_secret does not look like a secret. Using it as a plain text credential?"); |
| 277 |
return $secret; |
| 278 |
} |
| 279 |
$self->{logger}->writeLogDebug("Secret path: $secret_path - Secret name: $secret_name"); |
| 280 |
|
| 281 |
if (!defined($self->{auth}) || !$self->is_token_still_valid() ) { |
| 282 |
$self->authenticate() or return $secret; |
| 283 |
} |
| 284 |
|
| 285 |
# prepare the GET statement |
| 286 |
my $get_result_json; |
| 287 |
my $url = "https://" . $self->{vault_config}->{url} . ":" . $self->{vault_config}->{port} . "/v1/" . $secret_path; |
| 288 |
$self->{logger}->writeLogDebug("Requesting URL: $url"); |
| 289 |
|
| 290 |
#$self->{curl_easy}->setopt( CURLOPT_VERBOSE, 1 ); |
| 291 |
$self->{curl_easy}->setopt( CURLOPT_URL, $url ); |
| 292 |
$self->{curl_easy}->setopt( CURLOPT_POST, 0 ); |
| 293 |
$self->{curl_easy}->setopt( CURLOPT_WRITEDATA(), \$get_result_json ); |
| 294 |
$self->{curl_easy}->setopt( CURLOPT_HTTPHEADER(), ["X-Vault-Token: " . $self->{auth}->{token}]); |
| 295 |
|
| 296 |
eval { |
| 297 |
$self->{curl_easy}->perform(); |
| 298 |
}; |
| 299 |
if ($@) { |
| 300 |
$self->{logger}->writeLogError("Error while getting a secret from the vault: " . $@); |
| 301 |
return $secret; |
| 302 |
} |
| 303 |
|
| 304 |
$self->{logger}->writeLogDebug("Request passed."); |
| 305 |
# request_id |
| 306 |
|
| 307 |
# the result is a json string, convert it into an object |
| 308 |
my $get_result_obj = centreon::vmware::common::transform_json_to_object($get_result_json); |
| 309 |
if (defined($get_result_obj->{error_message})) { |
| 310 |
$self->{logger}->writeLogError("Error while decoding JSON '$get_result_json'. Message: " |
| 311 |
. $get_result_obj->{error_message}); |
| 312 |
return $secret; |
| 313 |
} |
| 314 |
$self->{logger}->writeLogDebug("Request id is " . $get_result_obj->{request_id}); |
| 315 |
|
| 316 |
# .data.data will contain the stored macros |
| 317 |
if ( !defined($get_result_obj->{data}) |
| 318 |
|| !defined($get_result_obj->{data}->{data}) |
| 319 |
|| !defined($get_result_obj->{data}->{data}->{$secret_name}) ) { |
| 320 |
$self->{logger}->writeLogError("Could not get secret '$secret_name' from path '$secret_path' from the vault. Enable debug for more details."); |
| 321 |
$self->{logger}->writeLogDebug("Response: " . $get_result_json); |
| 322 |
return $secret; |
| 323 |
} |
| 324 |
$self->{logger}->writeLogInfo("Secret '$secret_name' from path '$secret_path' retrieved from the vault."); |
| 325 |
return $get_result_obj->{data}->{data}->{$secret_name}; |
| 326 |
} |
| 327 |
|
| 328 |
1; |
| 329 |
|
| 330 |
__END__ |
| 331 |
|
| 332 |
=head1 NAME |
| 333 |
|
| 334 |
Centreon Vault password manager |
| 335 |
|
| 336 |
=head1 SYNOPSIS |
| 337 |
|
| 338 |
Allows to retrieve secrets (usually username and password) from a Hashicorp vault compatible API. |
| 339 |
|
| 340 |
use centreon::vmware::logger; |
| 341 |
use centreon::script::centreonvault; |
| 342 |
my $vault = centreon::script::centreonvault->new( |
| 343 |
( |
| 344 |
'logger' => centreon::vmware::logger->new(), |
| 345 |
'config_file' => '/var/lib/centreon/vault/vault.json' |
| 346 |
) |
| 347 |
); |
| 348 |
my $password = $vault->get_secret('secret::hashicorp_vault::mypath/to/mysecrets::password'); |
| 349 |
|
| 350 |
=head1 METHODS |
| 351 |
|
| 352 |
=head2 new(\%options) |
| 353 |
|
| 354 |
Constructor of the vault object. |
| 355 |
|
| 356 |
%options must provide: |
| 357 |
|
| 358 |
- logger: an object of the centreon::vmware::logger class. |
| 359 |
|
| 360 |
- config_file: full path and file name of the Centreon Vault JSON config file. |
| 361 |
|
| 362 |
The default config_file path should be '/var/lib/centreon/vault/vault.json'. |
| 363 |
The expected file format for Centreon Vault is: |
| 364 |
|
| 365 |
{ |
| 366 |
"name": "hashicorp_vault", |
| 367 |
"url": "vault-server.my-domain.com", |
| 368 |
"salt": "<base64 encoded(<32 bytes long key used to hash the encrypted data)>", |
| 369 |
"port": 443, |
| 370 |
"root_path": "vmware_daemon", |
| 371 |
"role_id": "<base64 encoded(<iv><hmac_hash><encrypted_role_id>)", |
| 372 |
"secret_id": "<base64 encoded(<iv><hmac_hash><encrypted_secret_id>)" |
| 373 |
} |
| 374 |
|
| 375 |
=head2 get_secret($secret) |
| 376 |
|
| 377 |
Returns the secret stored in the Centreon Vault at the given path. |
| 378 |
If the format of the secret does not match the regular expression |
| 379 |
C</^secret::hashicorp_vault::([^:]+)::(.+)$/> |
| 380 |
or in case of any failure in the process, the method will return the secret unchanged. |
| 381 |
|
| 382 |
=cut |