Bug 34587: Fix url validation

We need to check whether the url ends in a trailing "/" and also whether it ends in "/reports" for a SUSHI harvest. Two new functions are added to manage this and prevent failed harvests.

report_type is also now lowercase in the url and the dates are truncated to YYYY-MM format

Signed-off-by: Jessica Zairo <jzairo@bywatersolutions.com>
Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Matt Blenkinsop 2023-09-08 14:33:50 +00:00 committed by Tomas Cohen Arazi
parent 3ab4036636
commit ebc98070d6
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F

View file

@ -266,7 +266,7 @@ Tests the connection of the harvester to the SUSHI service and returns any alert
sub test_connection {
my ($self) = @_;
my $url = $self->service_url;
my $url = _validate_url($self->service_url, 'status');
$url .= 'status';
$url .= '?customer_id=' . $self->customer_id;
$url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id;
@ -383,17 +383,59 @@ sub _build_url_query {
$self->erm_usage_data_provider_id;
}
# FIXME: service_url needs to end in 'reports/'
# below concat will result in a badly formed URL otherwise
# Either validate this on UI form, here, or both
my $url = $self->service_url;
my $url = _validate_url($self->service_url, 'harvest');
$url .= $self->{report_type};
$url .= lc $self->{report_type};
$url .= '?customer_id=' . $self->customer_id;
$url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id;
$url .= '&api_key=' . $self->api_key if $self->api_key;
$url .= '&begin_date=' . $self->{begin_date} if $self->{begin_date};
$url .= '&end_date=' . $self->{end_date} if $self->{end_date};
$url .= '&begin_date=' . substr $self->{begin_date}, 0, 7 if $self->{begin_date};
$url .= '&end_date=' . substr $self->{end_date}, 0, 7 if $self->{end_date};
return $url;
}
=head3 _validate_url
Checks whether the url ends in a trailing "/" and adds one if not
my $url = _validate_url($url, 'harvest')
$caller is either the harvest_sushi function ("harvest") or the test_connection function ("status")
=cut
sub _validate_url {
my ( $url, $caller ) = @_;
if($caller eq 'harvest') {
# Not all urls will end in "/" - add one so they are standardised
$url = _check_trailing_character($url);
# All SUSHI report requests should be to the "/reports" endpoint
# Not all providers in the counter registry include this in their data so we need to check and add it
my $reports_param = substr $url, -8;
$url .= 'reports/' if $reports_param ne 'reports/';
} else {
$url = _check_trailing_character($url);
}
return $url;
}
=head3 _check_trailing_character
Checks whether a url string ends in a "/" before we concatenate further params to the end of the url
=cut
sub _check_trailing_character {
my ( $url ) = @_;
my $trailing_char = substr $url, -1;
if ( $trailing_char ne '/' ) {
$url .= '/';
}
return $url;
}