Skip to content

Commit

Permalink
Allow certs endpoint to be run locally
Browse files Browse the repository at this point in the history
  • Loading branch information
abeverley committed Jan 10, 2025
1 parent 56dafbc commit c3a7ec5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
5 changes: 3 additions & 2 deletions bin/configdb.pl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@
}

my ($pwdpass, $schema);
if ($is_local && $type eq 'pwd')
if ($is_local)
{
# If we are running directly on the server, get passphrase to
# ecnrypt/decrypt passwords (this is kept locally in /.configdb if
# accessing the database remotely)
$pwdpass = _get_passphrase("Please enter the passphrase for password encyrption and decryption:");
$pwdpass = _get_passphrase("Please enter the passphrase for password encyrption and decryption:")
if $type eq 'pwd';

# Get direct connection from database - not needed for running on remote
# server
Expand Down
74 changes: 6 additions & 68 deletions lib/Brass/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,78 +111,16 @@ get 'api/cert/' => sub {

my $schema = schema;

my $action = query_parameters->get('action')
or error __"Need required action";
my $server = query_parameters->get('server');
my $param = query_parameters->get('param');

my $output;
if ($action eq 'summary')
{
$server or error __"Please specify server";
$param or error __"Please specify certificate use";

my @certs;
my @uses = $schema->resultset('ServerCert')->search({
'server.name' => $server,
'use.name' => $param,
},{
join => ['use', 'server'],
})->all;

error __x"Certificate use {use} not found for server {name}",
use => $param, name => $server
if !@uses;

foreach my $use (@uses)
{
my $cert = $schema->resultset('Cert')->search({
'me.id' => $use->cert_id,
'cert_location_uses.use_id' => $use->get_column('use'),
},{
prefetch => {
cert_locations => 'cert_location_uses',
},
});

error __x"More than one location configured for use \"{use}\" of certificate {id}",
use => $use->use->name, id => $use->cert_id
if $cert->count > 1;

error __x"Location information not configured for use \"{use}\" of certificate {id}",
use => $use->use->name, id => $use->cert_id
if !$cert->count;

push @certs, $cert->next->as_hash_single;
}

$output = \@certs;
}
elsif ($action eq 'servers')
{
$param or error __"Please specify certificate ID";

my $cert = $schema->resultset('Cert')->find($param)
or error __x"Certificate ID {id} not found", id => $param;

my @servers = $schema->resultset('Server')->search({
'cert.id' => $param,
},{
prefetch => {
server_certs => 'cert' ,
},
})->all;

$output = $cert->as_hash_multiple;
}
else {
error __x"Unknown action {action}", action => $action;
}
my $return = $cdb->run_cert(
server => query_parameters->get('server'),
action => query_parameters->get('action'),
param => query_parameters->get('param'),
);

content_type 'application/json';
encode_json({
"is_error" => 0,
"result" => encode_json($output),
"result" => encode_json($return),
});
};

Expand Down
77 changes: 77 additions & 0 deletions lib/Brass/ConfigDB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ sub _run_local
{
$self->run_pwd(%params);
}
elsif ($type eq 'cert')
{
$self->run_cert(%params);
}
}

sub _run_remote
Expand Down Expand Up @@ -257,6 +261,79 @@ sub run_pwd
return $pass;
}

sub run_cert
{ my ($self, %params) = @_;

my $server = $params{server};
my $param = $params{param};
my $action = $params{action}
or error __"Need required action";

my $return;

if ($action eq 'summary')
{
$server or error __"Please specify server";
$param or error __"Please specify certificate use";

my @certs;
my @uses = $self->schema->resultset('ServerCert')->search({
'server.name' => $server,
'use.name' => $param,
},{
join => ['use', 'server'],
})->all;

error __x"Certificate use {use} not found for server {name}",
use => $param, name => $server
if !@uses;

foreach my $use (@uses)
{
my $cert = $self->schema->resultset('Cert')->search({
'me.id' => $use->cert_id,
'cert_location_uses.use_id' => $use->get_column('use'),
},{
prefetch => {
cert_locations => 'cert_location_uses',
},
});

error __x"More than one location configured for use \"{use}\" of certificate {id}",
use => $use->use->name, id => $use->cert_id
if $cert->count > 1;

error __x"Location information not configured for use \"{use}\" of certificate {id}",
use => $use->use->name, id => $use->cert_id
if !$cert->count;

push @certs, $cert->next->as_hash_single;
}

return \@certs;
}
elsif ($action eq 'servers')
{
$param or error __"Please specify certificate ID";

my $cert = $self->schema->resultset('Cert')->find($param)
or error __x"Certificate ID {id} not found", id => $param;

my @servers = $self->schema->resultset('Server')->search({
'cert.id' => $param,
},{
prefetch => {
server_certs => 'cert' ,
},
})->all;

return $cert->as_hash_multiple;
}
else {
error __x"Unknown action {action}", action => $action;
}
}

sub randompw()
{ my $pwgen = CtrlO::Crypt::XkcdPassword->new;
$pwgen->xkcd( words => 3, digits => 2 );
Expand Down

0 comments on commit c3a7ec5

Please sign in to comment.