Using json parser to parse json configuration output from cake

pull/6052/head
Sebastien Tricaud 2020-06-23 12:32:43 -07:00
parent 2bccfb027e
commit 1115a7fe53
1 changed files with 48 additions and 59 deletions

View File

@ -28,6 +28,8 @@ use strict;
use warnings; use warnings;
use File::Path qw(make_path); use File::Path qw(make_path);
use JSON;
my $misp_user="www-data"; my $misp_user="www-data";
my $misp_path="/usr/share/misp"; my $misp_path="/usr/share/misp";
my $misp_config_path="/etc/misp/"; my $misp_config_path="/etc/misp/";
@ -106,68 +108,55 @@ if ($create) {
close $internalfh; close $internalfh;
} }
die "foo"; my $settings = `sudo -u $misp_user $misp_path/app/Console/cake admin getSetting all | tail -n +7`;
my $settings = `sudo -u $misp_user $misp_path/app/Console/cake admin getSetting all`; my $jsonconfig = decode_json($settings);
my @lines = split("\n",$settings);
my $value = 0;
my $setting = 0;
foreach(@lines) {
if ($_ =~ m/\"value\": (.*)/) {
$value = substr $1, 0, -1;
}
if ($_ =~ m/\"setting\": \"(.*)\"/) {
$setting = $1;
}
if ($value && $setting) {
#print "$setting -> $value\n";
my $category = "";
my $key = "";
my $section = "";
# Plugin.S3_aws_secret_key -> false foreach my $item (@$jsonconfig) {
# ^ ^ my $setting = $item->{'setting'};
# | |- Section (S3) my $value = $item->{'value'};
# | |- Key (S3_aws_secret_key)
# |-- Category my $category = "";
# my $key = "";
# We also have: my $section = "";
# Proxy.host -> "" # Plugin.S3_aws_secret_key -> false
# Which won't create a section because of the missing _ # ^ ^
if ($setting =~ m/(\w+)\.(\w+)/) { # | |- Section (S3)
$category = $1; # | |- Key (S3_aws_secret_key)
$key = $2; # |-- Category
# print "Setting category: $1 and key: $2\n"; #
if ($key =~ m/(.*?)_/) { # We also have:
$section = $1 # Proxy.host -> ""
# print "Section: $1\n"; # Which won't create a section because of the missing _
} else { # There is no _, so the section is the key if ($setting =~ m/(\w+)\.(\w+)/) {
$section = $key; $category = $1;
} $key = $2;
} else { # print "Setting category: $1 and key: $2\n";
# Orphan settings are debug settings: if ($key =~ m/(.*?)_/) {
$category = "Debug"; $section = $1
$key = $setting; # print "Section: $1\n";
# print "Orphan setting:$setting\n"; } else { # There is no _, so the section is the key
$section = $key;
} }
} else {
# # Orphan settings are debug settings:
# We have all we needed, we can write the files $category = "Debug";
# $key = $setting;
my $conf_to_write = "$misp_config_path/$category.conf"; # print "Orphan setting:$setting\n";
unless($CATEGORIES_CREATED{"$category"}) {
if (-e $conf_to_write) {
die "We cannot overwrite $conf_to_write, it already exists. Remove it manually.\n";
}
}
$CATEGORIES_CREATED{"$category"} = 1;
open(my $fh, ">>", "$conf_to_write");
print $fh "#$key $value\n";
close $fh;
# Unset those two variables to be filled again!
$value = 0;
$setting = 0;
} }
#
# We have all we needed, we can write the files
#
my $conf_to_write = "$misp_config_path/$category.conf";
unless($CATEGORIES_CREATED{"$category"}) {
if (-e $conf_to_write) {
die "We cannot overwrite $conf_to_write, it already exists. Remove it manually.\n";
}
}
$CATEGORIES_CREATED{"$category"} = 1;
open(my $fh, ">>", "$conf_to_write");
print $fh "#$key $value\n";
close $fh;
} }
} }