You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by da...@apache.org on 2018/07/03 16:22:37 UTC

[trafficcontrol] 01/02: added validation rules on the required input fields for SSL key generation

This is an automated email from the ASF dual-hosted git repository.

dangogh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 468a3cdd51a547cacf5f54343c446a28531ce1b2
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Wed Jun 27 07:55:45 2018 -0600

    added validation rules on the required input fields for SSL key generation
---
 traffic_ops/app/lib/API/DeliveryService/SslKeys.pm | 53 ++++++++++++++++++----
 1 file changed, 43 insertions(+), 10 deletions(-)

diff --git a/traffic_ops/app/lib/API/DeliveryService/SslKeys.pm b/traffic_ops/app/lib/API/DeliveryService/SslKeys.pm
index b75d71d..0af73a1 100644
--- a/traffic_ops/app/lib/API/DeliveryService/SslKeys.pm
+++ b/traffic_ops/app/lib/API/DeliveryService/SslKeys.pm
@@ -25,6 +25,7 @@ use JSON;
 use MIME::Base64;
 use UI::DeliveryService;
 use Data::Dumper;
+use Validate::Tiny ':all';
 
 sub add {
 	my $self     = shift;
@@ -79,17 +80,25 @@ sub add {
 #named like this because there is a plugin called generate_ssl_keys in Mojoplugins/SslKeys.pm
 sub generate {
 	my $self         = shift;
+	my $params = $self->req->json;
+
+	my ( $is_valid, $result ) = $self->is_valid( $params );
+
+	if ( !$is_valid ) {
+		return $self->alert($result);
+	}
+
 	my $key_type     = "ssl";
-	my $key          = $self->req->json->{key};
-	my $version      = $self->req->json->{version};        # int
-	my $hostname     = $self->req->json->{hostname};
-	my $country      = $self->req->json->{country};
-	my $state        = $self->req->json->{state};
-	my $city         = $self->req->json->{city};
-	my $org          = $self->req->json->{organization};
-	my $unit         = $self->req->json->{businessUnit};
-	my $cdn = $self->req->json->{cdn};
-	my $deliveryservice = $self->req->json->{deliveryservice};
+	my $key          = $params->{key};
+	my $version      = $params->{version};        # int
+	my $hostname     = $params->{hostname};
+	my $country      = $params->{country};
+	my $state        = $params->{state};
+	my $city         = $params->{city};
+	my $org          = $params->{organization};
+	my $unit         = $params->{businessUnit};
+	my $cdn          = $params->{cdn};
+	my $deliveryservice = $params->{deliveryservice};
 	my $tmp_location = "/var/tmp";
 
 	if ( !&is_oper($self) ) {
@@ -133,6 +142,30 @@ sub generate {
 	}
 }
 
+sub is_valid {
+	my $self   = shift;
+	my $params = shift;
+
+	my $rules = {
+		fields => [ qw/cdn deliveryservice key version hostname country state city organization businessUnit/ ],
+
+		# Validation checks to perform
+		checks => [
+			deliveryservice	=> [ is_required("is required") ],
+		]
+	};
+
+	# Validate the input against the rules
+	my $result = validate( $params, $rules );
+
+	if ( $result->{success} ) {
+		return ( 1, $result->{data} );
+	}
+	else {
+		return ( 0, $result->{error} );
+	}
+}
+
 sub view_by_xml_id {
 	my $self    = shift;
 	my $xml_id     = $self->param('xmlid');