diff --git a/lib/NGCP/Panel/Controller/API/Contacts.pm b/lib/NGCP/Panel/Controller/API/Contacts.pm index 86d7001e27..860594c7bd 100644 --- a/lib/NGCP/Panel/Controller/API/Contacts.pm +++ b/lib/NGCP/Panel/Controller/API/Contacts.pm @@ -37,11 +37,11 @@ __PACKAGE__->config( action_roles => [qw(HTTPMethods)], ); -sub auto :Allow { +sub auto :Private { my ($self, $c) = @_; $self->set_body($c); - $c->log->debug("++++++++++++++++ request body: " . $c->stash->{body}); + $self->log_request($c); } sub GET :Allow { @@ -234,16 +234,8 @@ sub valid_id : Private { sub end : Private { my ($self, $c) = @_; - $c->forward(qw(Controller::Root render)); - $c->response->content_type('') - if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way - if (@{ $c->error }) { - my $msg = join ', ', @{ $c->error }; - $c->log->error($msg); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); - $c->clear_errors; - } - $c->log->debug("++++++++++++++++ response body: " . $c->response->body // ''); + + $self->log_response($c); } # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/ContactsItem.pm b/lib/NGCP/Panel/Controller/API/ContactsItem.pm index 9b679f0a8a..151f7cbd11 100644 --- a/lib/NGCP/Panel/Controller/API/ContactsItem.pm +++ b/lib/NGCP/Panel/Controller/API/ContactsItem.pm @@ -41,10 +41,11 @@ __PACKAGE__->config( action_roles => [qw(HTTPMethods)], ); -sub auto :Allow { +sub auto :Private { my ($self, $c) = @_; + $self->set_body($c); - $c->log->debug("++++++++++++++++ request body: " . $c->stash->{body}); + $self->log_request($c); } sub GET :Allow { @@ -404,17 +405,8 @@ sub hal_from_contact : Private { sub end : Private { my ($self, $c) = @_; - $c->forward(qw(Controller::Root render)); - $c->response->content_type('') - if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way - if (@{ $c->error }) { - my $msg = join ', ', @{ $c->error }; - $c->log->error($msg); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); - $c->clear_errors; - } - $c->log->debug("++++++++++++++++ response body: " . ($c->response->body // '')); + $self->log_response($c); } # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/Contracts.pm b/lib/NGCP/Panel/Controller/API/Contracts.pm index de1eefce1a..f10bd9f307 100644 --- a/lib/NGCP/Panel/Controller/API/Contracts.pm +++ b/lib/NGCP/Panel/Controller/API/Contracts.pm @@ -41,14 +41,13 @@ __PACKAGE__->config( action_roles => [qw(HTTPMethods)], ); -sub auto :Allow { +sub auto :Private { my ($self, $c) = @_; $self->set_body($c); - $c->log->debug("++++++++++++++++ request body: " . $c->stash->{body}); + $self->log_request($c); } - sub GET :Allow :Args(0) { my ($self, $c) = @_; my $page = $c->request->params->{page} // 1; @@ -223,17 +222,8 @@ sub valid_id : Private { sub end : Private { my ($self, $c) = @_; - $c->forward(qw(Controller::Root render)); - $c->response->content_type('') - if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way - if (@{ $c->error }) { - my $msg = join ', ', @{ $c->error }; - $c->log->error($msg); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); - $c->clear_errors; - } - $c->log->debug("++++++++++++++++ response body: " . ($c->response->body // '')); -} + $self->log_response($c); +} # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/ContractsItem.pm b/lib/NGCP/Panel/Controller/API/ContractsItem.pm index c113003fe2..11e10da6eb 100644 --- a/lib/NGCP/Panel/Controller/API/ContractsItem.pm +++ b/lib/NGCP/Panel/Controller/API/ContractsItem.pm @@ -42,11 +42,11 @@ __PACKAGE__->config( action_roles => [qw(HTTPMethods)], ); -sub auto :Allow { +sub auto :Private { my ($self, $c) = @_; $self->set_body($c); - $c->log->debug("++++++++++++++++ request body: " . $c->stash->{body}); + $self->log_request($c); } sub GET :Allow { @@ -355,16 +355,8 @@ sub hal_from_contract :Private { sub end : Private { my ($self, $c) = @_; - $c->forward(qw(Controller::Root render)); - $c->response->content_type('') - if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way - if (@{ $c->error }) { - my $msg = join ', ', @{ $c->error }; - $c->log->error($msg); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); - $c->clear_errors; - } - $c->log->debug("++++++++++++++++ response body: " . ($c->response->body // '')); + + $self->log_response($c); } # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/ResellersItem.pm b/lib/NGCP/Panel/Controller/API/ResellersItem.pm index 33b4461dcd..38251e5a99 100644 --- a/lib/NGCP/Panel/Controller/API/ResellersItem.pm +++ b/lib/NGCP/Panel/Controller/API/ResellersItem.pm @@ -30,11 +30,11 @@ __PACKAGE__->config( action_roles => [qw(HTTPMethods)], ); -sub auto :Allow { +sub auto :Private { my ($self, $c) = @_; $self->set_body($c); - $c->log->debug("++++++++++++++++ request body: " . $c->stash->{body}); + $self->log_request($c); } sub GET :Allow { @@ -65,15 +65,10 @@ sub OPTIONS :Allow { return; } -sub end :Private { +sub end : Private { my ($self, $c) = @_; - $c->forward(qw(Controller::Root render)); - $c->response->content_type('') - if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way - if (@{ $c->error }) { - my $msg = join ', ', @{ $c->error }; - $c->log->error($msg); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); - $c->clear_errors; - } + + $self->log_response($c); } + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/Root.pm b/lib/NGCP/Panel/Controller/API/Root.pm index 5c753a0f52..a06b7972db 100644 --- a/lib/NGCP/Panel/Controller/API/Root.pm +++ b/lib/NGCP/Panel/Controller/API/Root.pm @@ -35,6 +35,13 @@ __PACKAGE__->config( action_roles => [qw(HTTPMethods)], ); +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); +} + sub GET : Allow { my ($self, $c) = @_; my $response = $self->cached($c); diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index 3106d33c04..c455c9cb35 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -278,5 +278,43 @@ sub set_body { $c->stash->{body} = $c->request->body ? (do { local $/; $c->request->body->getline }) : ''; } +sub log_request { + my ($self, $c) = @_; + + my $params = join(', ', map { "'".$_."'='".($c->request->query_params->{$_} // '')."'" } + keys %{ $c->request->query_params } + ); + my $user; + if($c->user->roles eq "api_admin" || $c->user->roles eq "api_reseller") { + $user = $c->user->login; + } else { + $user = $c->user->username . '@' . $c->user->domain; + } + + $c->log->info("API function '".$c->request->path."' called by '" . $user . + "' ('" . $c->user->roles . "') from host '".$c->request->address."' with params " . + (length $params ? $params : "''") . + " and body '" . $c->stash->{body} . "'"); +} + +sub log_response { + my ($self, $c) = @_; + + # TODO: should be put a UUID to stash in log_request and use it here to correlate + # req/res lines? + $c->forward(qw(Controller::Root render)); + $c->response->content_type('') + if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way + if (@{ $c->error }) { + my $msg = join ', ', @{ $c->error }; + $c->log->error($msg); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); + $c->clear_errors; + } + $c->log->info("API function '".$c->request->path."' generated response with code '" . + $c->response->code . "' and body '" . + $c->response->body . "'"); +} + 1; # vim: set tabstop=4 expandtab: diff --git a/t/api-root.t b/t/api-root.t index b1b18b8604..c418deaecf 100644 --- a/t/api-root.t +++ b/t/api-root.t @@ -23,6 +23,11 @@ $ua->ssl_opts( # OPTIONS tests { + # test some uri params + $req = HTTP::Request->new('OPTIONS', $uri.'/api/?foo=bar&bla'); + $res = $ua->request($req); + ok($res->code == 200, "check options request with uri params"); + $req = HTTP::Request->new('OPTIONS', $uri.'/api/'); $res = $ua->request($req); ok($res->code == 200, "check options request");