From b2c9f4c61d00bf18e7a253c410479f8124640106 Mon Sep 17 00:00:00 2001 From: Rajamohan Raj Date: Wed, 26 Aug 2020 00:28:12 +0000 Subject: Adding validations for all JSON data across modules Issue-ID: MULTICLOUD-1198 Signed-off-by: Rajamohan Raj Change-Id: Idc2fbfd6e875bd7b662c7d60925aeb776b3f7bea --- src/clm/api/clusterhandler.go | 31 +++++++++++++++++++ src/clm/api/clusterhandler_test.go | 6 ++++ src/clm/json-schemas/cluster-kv.json | 54 +++++++++++++++++++++++++++++++++ src/clm/json-schemas/cluster-label.json | 13 ++++++++ src/clm/json-schemas/metadata.json | 37 ++++++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 src/clm/json-schemas/cluster-kv.json create mode 100644 src/clm/json-schemas/cluster-label.json create mode 100644 src/clm/json-schemas/metadata.json (limited to 'src/clm') diff --git a/src/clm/api/clusterhandler.go b/src/clm/api/clusterhandler.go index 84dd3230..75fcc561 100644 --- a/src/clm/api/clusterhandler.go +++ b/src/clm/api/clusterhandler.go @@ -28,10 +28,16 @@ import ( "net/textproto" clusterPkg "github.com/onap/multicloud-k8s/src/clm/pkg/cluster" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" "github.com/gorilla/mux" ) +var cpJSONFile string = "json-schemas/metadata.json" +var ckvJSONFile string = "json-schemas/cluster-kv.json" +var clJSONFile string = "json-schemas/cluster-label.json" + + // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes type clusterHandler struct { @@ -55,6 +61,12 @@ func (h clusterHandler) createClusterProviderHandler(w http.ResponseWriter, r *h return } + err, httpError := validation.ValidateJsonSchemaData(cpJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + // Name is required. if p.Metadata.Name == "" { http.Error(w, "Missing name in POST request", http.StatusBadRequest) @@ -148,6 +160,12 @@ func (h clusterHandler) createClusterHandler(w http.ResponseWriter, r *http.Requ return } + err, httpError := validation.ValidateJsonSchemaData(cpJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + //Read the file section and ignore the header file, _, err := r.FormFile("file") if err != nil { @@ -333,6 +351,12 @@ func (h clusterHandler) createClusterLabelHandler(w http.ResponseWriter, r *http err := json.NewDecoder(r.Body).Decode(&p) + err, httpError := validation.ValidateJsonSchemaData(clJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + // LabelName is required. if p.LabelName == "" { http.Error(w, "Missing label name in POST request", http.StatusBadRequest) @@ -413,6 +437,13 @@ func (h clusterHandler) createClusterKvPairsHandler(w http.ResponseWriter, r *ht err := json.NewDecoder(r.Body).Decode(&p) + // Verify JSON Body + err, httpError := validation.ValidateJsonSchemaData(ckvJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + // KvPairsName is required. if p.Metadata.Name == "" { http.Error(w, "Missing Key Value pair name in POST request", http.StatusBadRequest) diff --git a/src/clm/api/clusterhandler_test.go b/src/clm/api/clusterhandler_test.go index 076718df..a866835b 100644 --- a/src/clm/api/clusterhandler_test.go +++ b/src/clm/api/clusterhandler_test.go @@ -186,6 +186,12 @@ func (m *mockClusterManager) DeleteClusterKvPairs(provider, clusterName, kvpair return m.Err } +func init() { + cpJSONFile = "../json-schemas/metadata.json" + ckvJSONFile = "../json-schemas/cluster-kv.json" + clJSONFile = "../json-schemas/cluster-label.json" +} + func TestClusterProviderCreateHandler(t *testing.T) { testCases := []struct { label string diff --git a/src/clm/json-schemas/cluster-kv.json b/src/clm/json-schemas/cluster-kv.json new file mode 100644 index 00000000..c7013bab --- /dev/null +++ b/src/clm/json-schemas/cluster-kv.json @@ -0,0 +1,54 @@ +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "spec": { + "required": [ + "kv" + ], + "type": "object", + "properties": { + "kv": { + "items": { + "additionalProperties": { + "type": "string", + "maxLength": 128 + }, + "type": "object" + }, + "type": "array" + } + } + }, + "metadata": { + "required": ["name"], + "properties": { + "userData2": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some more data", + "maxLength": 512 + }, + "userData1": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some data", + "maxLength": 512 + }, + "name": { + "description": "Name of the resource", + "type": "string", + "example": "ResName", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "description": { + "description": "Description for the resource", + "type": "string", + "example": "Resource description", + "maxLength": 1024 + } + } + } + } + } \ No newline at end of file diff --git a/src/clm/json-schemas/cluster-label.json b/src/clm/json-schemas/cluster-label.json new file mode 100644 index 00000000..22267b3d --- /dev/null +++ b/src/clm/json-schemas/cluster-label.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "label-name": { + "description": "Logical Cloud to use for this intent", + "type": "string", + "example": "cluster-label-1", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + } + } + } \ No newline at end of file diff --git a/src/clm/json-schemas/metadata.json b/src/clm/json-schemas/metadata.json new file mode 100644 index 00000000..960545ee --- /dev/null +++ b/src/clm/json-schemas/metadata.json @@ -0,0 +1,37 @@ + +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "metadata": { + "required": ["name"], + "properties": { + "userData2": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some more data", + "maxLength": 512 + }, + "userData1": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some data", + "maxLength": 512 + }, + "name": { + "description": "Name of the resource", + "type": "string", + "example": "ResName", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "description": { + "description": "Description for the resource", + "type": "string", + "example": "Resource description", + "maxLength": 1024 + } + } + } + } + } \ No newline at end of file -- cgit 1.2.3-korg