From 170ccb6d540cc29dabbcecea2890abb36de84d37 Mon Sep 17 00:00:00 2001 From: Andy Beverley Date: Mon, 7 Oct 2024 15:20:30 +0100 Subject: [PATCH] Add site config API endpoint --- lib/Brass/API.pm | 33 +++++++++++++++++++++++++++++++ lib/Brass/ConfigDB.pm | 13 ++++++------ lib/Brass/Schema/Result/Config.pm | 7 ++++++- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/lib/Brass/API.pm b/lib/Brass/API.pm index 28bacdd..4df8cd4 100644 --- a/lib/Brass/API.pm +++ b/lib/Brass/API.pm @@ -392,3 +392,36 @@ get 'api/server/' => sub { }); }; +get 'api/site/' => sub { + my $user = var 'api_user' + or error __"Authentication required"; + + my $action = query_parameters->get('action') + or error __"Need required action"; + + my $schema = schema; + + my $config = $schema->resultset('Config')->next + or error __x"Configuration information not defined. Please define in Brass in the Config => Site menu"; + + my $output; + + if ($action eq 'smtp') + { + $return = [$config->smtp_relayhost]; + } + elsif ($action eq 'internal_networks') + { + $return = [$config->internal_networks_all]; + } + else { + error __x"Unknown action {action}", action => $action; + } + + content_type 'application/json'; + encode_json({ + "is_error" => 0, + "result" => encode_json($output), + }); +}; + diff --git a/lib/Brass/ConfigDB.pm b/lib/Brass/ConfigDB.pm index a8feb3e..ef7aed4 100755 --- a/lib/Brass/ConfigDB.pm +++ b/lib/Brass/ConfigDB.pm @@ -54,12 +54,8 @@ sub run my $email = $cfg->val($namespace, 'email') or die "Email config parameter missing"; - if ($type eq 'smtp') - { - my $smtp = $cfg->val($namespace, 'smtp'); - $smtp or error __"smtp parameter is missing from .configdb"; - return $smtp; - } + error __"smtp parameter type has been replaced by site type" + if $type eq 'smtp'; $type or error __"Please provide type of request with --type"; $action or error __"Please specify action with --action"; @@ -129,6 +125,11 @@ sub run die "Unknown action $action"; } } + elsif ($type eq 'site') + { + push @path, 'site'; + push @query, (action => $action); + } else { die "Invalid request $type: should be pwd, server or cert"; diff --git a/lib/Brass/Schema/Result/Config.pm b/lib/Brass/Schema/Result/Config.pm index c77ffe0..b6d6fe4 100644 --- a/lib/Brass/Schema/Result/Config.pm +++ b/lib/Brass/Schema/Result/Config.pm @@ -24,10 +24,15 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("id"); +sub internal_networks_all +{ my $self = shift; + split /[\s,]+/, $self->internal_networks; +} + sub validate { my $self = shift; - foreach my $range (split /[\s,]+/, $self->internal_networks) + foreach my $range ($self->internal_networks_all) { Net::CIDR::cidrvalidate($range) or error __x"Invalid IP range restriction: {range}", range => $range;