Skip to content

Commit

Permalink
ci(centreon-vault): move vault lib to it's own package
Browse files Browse the repository at this point in the history
Refs:MON-106121
  • Loading branch information
Evan-Adam committed Oct 8, 2024
1 parent 591981b commit cad6602
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 41 deletions.
144 changes: 144 additions & 0 deletions .github/workflows/connector-vault.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: connector-vault

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

on:
workflow_dispatch:
pull_request:
paths:
- 'connectors/centreonPerlLibs/**'
push:
branches:
- develop
- master
paths:
- 'connectors/centreonPerlLibs/**'

jobs:
get-environment:
uses: ./.github/workflows/get-environment.yml
with:
version_file: connectors/centreonPerlLibs/src/version.yaml

package:
needs: [get-environment]
if: ${{ needs.get-environment.outputs.stability != 'stable' }}
runs-on: ubuntu-22.04
strategy:
matrix:
include:
- package_extension: rpm
image: packaging-plugins-alma8
distrib: el8
- package_extension: rpm
image: packaging-plugins-alma9
distrib: el9
- package_extension: deb
image: packaging-plugins-bookworm
distrib: bookworm
- package_extension: deb
image: packaging-plugins-jammy
distrib: jammy

container:
image: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ matrix.image }}
credentials:
username: ${{ secrets.DOCKER_REGISTRY_ID }}
password: ${{ secrets.DOCKER_REGISTRY_PASSWD }}

name: package ${{ matrix.distrib }}

steps:
- name: Checkout sources
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Package
uses: ./.github/actions/package-nfpm
with:
nfpm_file_pattern: "connectors/centreonPerlLibs/packaging/centreon-vault-common-lib.yaml"
distrib: ${{ matrix.distrib }}
package_extension: ${{ matrix.package_extension }}
version: ${{ needs.get-environment.outputs.version }}
release: 1
commit_hash: ${{ github.sha }}
cache_key: ${{ github.sha }}-${{ github.run_id }}-${{ matrix.package_extension }}-${{ matrix.distrib }}
rpm_gpg_key: ${{ secrets.RPM_GPG_SIGNING_KEY }}
rpm_gpg_signing_key_id: ${{ secrets.RPM_GPG_SIGNING_KEY_ID }}
rpm_gpg_signing_passphrase: ${{ secrets.RPM_GPG_SIGNING_PASSPHRASE }}
stability: ${{ needs.get-environment.outputs.stability }}

- name: Upload library artifacts
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
with:
name: centreon-perl-common-${{ matrix.distrib }}
path: |
log.html
/var/log/centreon-gorgone
/etc/centreon-gorgone
retention-days: 1

deliver-rpm:
needs: [get-environment, package]
if: ${{ contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability) }}
runs-on: [self-hosted, common]

strategy:
matrix:
distrib: [el8, el9]

steps:
- name: Checkout sources
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Delivery
uses: ./.github/actions/rpm-delivery
with:
module_name: centreon-perl-common
distrib: ${{ matrix.distrib }}
cache_key: ${{ github.sha }}-${{ github.run_id }}-rpm-${{ matrix.distrib }}
stability: ${{ needs.get-environment.outputs.stability }}
artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}

deliver-deb:
needs: [get-environment, package]
if: ${{ contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability) }}
runs-on: [self-hosted, common]

strategy:
matrix:
distrib: [bullseye, bookworm, jammy]

steps:
- name: Checkout sources
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Delivery
uses: ./.github/actions/deb-delivery
with:
module_name: centreon-perl-common
distrib: ${{ matrix.distrib }}
cache_key: ${{ github.sha }}-${{ github.run_id }}-deb-${{ matrix.distrib }}
stability: ${{ needs.get-environment.outputs.stability }}
artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}

promote:
needs: [get-environment]
if: ${{ contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) }}
runs-on: [self-hosted, common]
strategy:
matrix:
distrib: [el8, el9, bullseye, bookworm]

steps:
- name: Checkout sources
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Promote ${{ matrix.distrib }} to stable
uses: ./.github/actions/promote-to-stable
with:
artifactory_token: ${{ secrets.ARTIFACTORY_ACCESS_TOKEN }}
module: centreon-perl-common
distrib: ${{ matrix.distrib }}
stability: ${{ needs.get-environment.outputs.stability }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: "centreon-Vault-Common-Lib"
arch: "all"
platform: "linux"
version_schema: "none"
version: "${VERSION}"
release: "${RELEASE}${DIST}"
section: "default"
priority: "optional"
maintainer: "Centreon <[email protected]>"
description: |
Perl library to communicate with an hashicorp vault API
Commit: @COMMIT_HASH@
vendor: "Centreon"
homepage: "https://centreon.com"
license: "Apache-2.0"

provides:
- centreon-Vault-Common-Lib

contents:
- src: "../src/"
dst: "/usr/share/perl5/vendor_perl/centreon/common"
packager: rpm
- src: "../src/"
dst: "/usr/share/perl5/centreon/common"
packager: deb

overrides:
rpm:
depends:
- perl(Crypt::OpenSSL::AES)
- perl(JSON::XS)
- perl-Net-Curl
deb:
depends:
- libcrypt-openssl-aes-perl
- libio-socket-inet6-perl
- libjson-xs-perl
- libnet-curl-perl

rpm:
signature:
key_file: ${RPM_SIGNING_KEY_FILE}
key_id: ${RPM_SIGNING_KEY_ID}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
# limitations under the License.
#

package centreon::script::centreonvault;
package centreon::common::centreonvault;

use strict;
use warnings;
use JSON::XS;

use MIME::Base64;
use Crypt::OpenSSL::AES;
use Net::Curl::Easy qw(:constants);
use centreon::vmware::common;
use JSON::XS;

my $VAULT_PATH_REGEX = qr/^secret::hashicorp_vault::([^:]+)::(.+)$/;

Expand Down Expand Up @@ -55,7 +55,7 @@ sub init {

# check if the following information is available
$self->{logger}->writeLogDebug("Reading Vault configuration from file " . $self->{config_file} . ".");
$self->{vault_config} = centreon::vmware::common::parse_json_file( 'json_file' => $self->{config_file} );
$self->{vault_config} = parse_json_file( 'json_file' => $self->{config_file} );
if (defined($self->{vault_config}->{error_message})) {
$self->{logger}->writeLogError("Error while parsing " . $self->{config_file} . ": "
. $self->{vault_config}->{error_message});
Expand Down Expand Up @@ -128,9 +128,6 @@ sub check_configuration {
$self->{encryption_key} = $ENV{'APP_SECRET'}; # key for aes-256-cbc
}




return 1;
}

Expand Down Expand Up @@ -224,7 +221,7 @@ sub authenticate {

$self->{logger}->writeLogInfo("Authentication to the vault passed." );

my $auth_result_obj = centreon::vmware::common::transform_json_to_object($auth_result_json);
my $auth_result_obj = transform_json_to_object($auth_result_json);
if (defined($auth_result_obj->{error_message})) {
$self->{logger}->writeLogError("Error while decoding JSON '$auth_result_json'. Message: "
. $auth_result_obj->{error_message});
Expand Down Expand Up @@ -325,6 +322,40 @@ sub get_secret {
return $get_result_obj->{data}->{data}->{$secret_name};
}

sub transform_json_to_object {
my ($json_data) = @_;

my $json_as_object;
eval {
$json_as_object = decode_json($json_data);
};
if ($@) {
return ('error_message' => "Could not decode JSON from '$json_data'. Reason: " . $@);
};
return($json_as_object);
}

sub parse_json_file {
my (%options) = @_;

my $fh;
my $json_data = '';

if ( !defined($options{json_file}) ) {
return ('error_message' => "parse_json_file: json_file option is mandatory");
}

my $json_file = $options{json_file};

open($fh, '<', $json_file) or return ('error_message' => "parse_json_file: Cannot open " . $json_file);
for my $line (<$fh>) {
chomp $line;
$json_data .= $line;
}
close($fh);
return transform_json_to_object($json_data);
}

1;

__END__
Expand Down
2 changes: 2 additions & 0 deletions connectors/centreonPerlLibs/version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
version: 1.0.0
33 changes: 0 additions & 33 deletions connectors/vmware/src/centreon/vmware/common.pm
Original file line number Diff line number Diff line change
Expand Up @@ -794,38 +794,5 @@ sub vsan_get_performances {
return $result;
}

sub transform_json_to_object {
my ($json_data) = @_;

my $json_as_object;
eval {
$json_as_object = decode_json($json_data);
};
if ($@) {
return ('error_message' => "Could not decode JSON from '$json_data'. Reason: " . $@);
};
return($json_as_object);
}

sub parse_json_file {
my (%options) = @_;

my $fh;
my $json_data = '';

if ( !defined($options{json_file}) ) {
return ('error_message' => "parse_json_file: json_file option is mandatory");
}

my $json_file = $options{json_file};

open($fh, '<', $json_file) or return ('error_message' => "parse_json_file: Cannot open " . $json_file);
for my $line (<$fh>) {
chomp $line;
$json_data .= $line;
}
close($fh);
return transform_json_to_object($json_data);
}

1;

0 comments on commit cad6602

Please sign in to comment.