Skip to content

Commit

Permalink
Add site config API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
abeverley committed Oct 7, 2024
1 parent 44efec6 commit 170ccb6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
33 changes: 33 additions & 0 deletions lib/Brass/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
};

13 changes: 7 additions & 6 deletions lib/Brass/ConfigDB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
7 changes: 6 additions & 1 deletion lib/Brass/Schema/Result/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 170ccb6

Please sign in to comment.