[XAPID-1088] fix resource leak, address comments
diff --git a/api.go b/api.go index 4da5b50..36c5058 100644 --- a/api.go +++ b/api.go
@@ -16,7 +16,6 @@ import ( "encoding/json" - "errors" "io" "io/ioutil" "net/http" @@ -51,19 +50,30 @@ var returnValue interface{} - if verifyApiKeyReq, err := validateRequest(r.Body, w); err == nil { - verifyApiKeyResponse, errorResponse := a.verifyAPIKey(verifyApiKeyReq) - - if errorResponse != nil { - setResponseHeader(errorResponse, w) - returnValue = errorResponse - } else { - returnValue = verifyApiKeyResponse + verifyApiKeyReq, err := validateRequest(r.Body, w) + if err != nil { + errorResponse, jsonErr := json.Marshal(errorResponse("Bad_REQUEST", err.Error(), http.StatusBadRequest)) + if jsonErr != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(jsonErr.Error())) } - b, _ := json.Marshal(returnValue) - log.Debugf("handleVerifyAPIKey result %s", b) - w.Write(b) + w.WriteHeader(http.StatusBadRequest) + w.Write(errorResponse) + return } + + verifyApiKeyResponse, errorResponse := a.verifyAPIKey(verifyApiKeyReq) + + if errorResponse != nil { + setResponseHeader(errorResponse, w) + returnValue = errorResponse + } else { + returnValue = verifyApiKeyResponse + } + b, _ := json.Marshal(returnValue) + log.Debugf("handleVerifyAPIKey result %s", b) + w.Write(b) + } func setResponseHeader(errorResponse *ErrorResponse, w http.ResponseWriter) { @@ -75,30 +85,24 @@ } func validateRequest(requestBody io.ReadCloser, w http.ResponseWriter) (VerifyApiKeyRequest, error) { + defer requestBody.Close() // 1. read request boby var verifyApiKeyReq VerifyApiKeyRequest body, err := ioutil.ReadAll(requestBody) if err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - return verifyApiKeyReq, errors.New("Bad_REQUEST") + return verifyApiKeyReq, err } - log.Debug(string(body)) + log.Debug("request body: ", string(body)) // 2. umarshall json to struct err = json.Unmarshal(body, &verifyApiKeyReq) if err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) - return verifyApiKeyReq, errors.New("Bad_REQUEST") + return verifyApiKeyReq, err } log.Debug(verifyApiKeyReq) // 2. verify params if isValid, err := verifyApiKeyReq.validate(); !isValid { - errorResponse, _ := json.Marshal(errorResponse("Bad_REQUEST", err.Error(), http.StatusBadRequest)) - w.WriteHeader(http.StatusBadRequest) - w.Write(errorResponse) - return verifyApiKeyReq, errors.New("Bad_REQUEST") + return verifyApiKeyReq, err } return verifyApiKeyReq, nil } @@ -164,9 +168,7 @@ func shortListApiProduct(details []ApiProductDetails, verifyApiKeyReq VerifyApiKeyRequest) ApiProductDetails { var bestMathcedProduct ApiProductDetails - rankedProducts := make(map[int][]ApiProductDetails) - rankedProducts[2] = []ApiProductDetails{} - rankedProducts[3] = []ApiProductDetails{} + rankedProducts := make([][]ApiProductDetails, 2) for _, apiProd := range details { if len(apiProd.Resources) == 0 || validatePath(apiProd.Resources, verifyApiKeyReq.UriPath) { @@ -177,19 +179,21 @@ // set rank 1 or just return } else { // set rank to 2 - rankedProducts[2] = append(rankedProducts[2], apiProd) + rankedProducts[0] = append(rankedProducts[0], apiProd) } } else { // set rank to 3, - rankedProducts[3] = append(rankedProducts[3], apiProd) + rankedProducts[1] = append(rankedProducts[1], apiProd) } } } - if len(rankedProducts[2]) > 0 { - return rankedProducts[2][0] - } else if len(rankedProducts[3]) > 0 { - return rankedProducts[3][0] + if len(rankedProducts[0]) > 0 { + return rankedProducts[0][0] + } + + if len(rankedProducts[1]) > 0 { + return rankedProducts[1][0] } return bestMathcedProduct
diff --git a/api_test.go b/api_test.go index 8ba4ebc..10b5eca 100644 --- a/api_test.go +++ b/api_test.go
@@ -61,7 +61,6 @@ dbMux: sync.RWMutex{}, } dbMan.setDbVersion(dataTestTempDir) - dbMan.initDb() apiMan := apiManager{ dbMan: dbMan,
diff --git a/apidVerifyAPIKey-api.yaml b/apidVerifyAPIKey-api.yaml index 7ad84ce..8742045 100644 --- a/apidVerifyAPIKey-api.yaml +++ b/apidVerifyAPIKey-api.yaml
@@ -17,7 +17,6 @@ info: version: "0.0.1" title: Swagger API -host: playground.apistudio.io basePath: /try/35cd6835-f2ed-4582-a1ae-d10ed29d062b schemes: - http @@ -138,8 +137,7 @@ description: Error response returned properties: response_code: - type: integer - format: int32 + type: string response_message: type: string kind:
diff --git a/data.go b/data.go index 65583b7..1d02c56 100644 --- a/data.go +++ b/data.go
@@ -46,21 +46,12 @@ return dbc.db } -func (dbc *dbManager) initDb() error { - db := dbc.getDb() - if db == nil { - return errors.New("DB not initialized") - } - return nil -} - func (dbc *dbManager) getDbVersion() string { return dbc.dbVersion } type dbManagerInterface interface { setDbVersion(string) - initDb() error getDb() apid.DB getDbVersion() string getKmsAttributes(tenantId string, entities ...string) map[string][]Attribute @@ -70,12 +61,12 @@ func (dbc *dbManager) getKmsAttributes(tenantId string, entities ...string) map[string][]Attribute { db := dbc.db - var attName, attValue sql.NullString - var entity_id string + var attName, attValue, entity_id sql.NullString // TODO : is there no other better way to do in caluse??? sql := sql_GET_KMS_ATTRIBUTES_FOR_TENANT + ` and entity_id in ('` + strings.Join(entities, `','`) + `')` mapOfAttributes := make(map[string][]Attribute) attributes, err := db.Query(sql, tenantId) + defer attributes.Close() if err != nil { log.Error("Error while fetching attributes for tenant id : %s and entityId : %s", tenantId, err) return mapOfAttributes @@ -88,10 +79,11 @@ ) if err != nil { log.Error("error fetching attributes for entityid ", entities, err) + return nil } - if attName.String != "" { + if attName.Valid && entity_id.Valid { att := Attribute{Name: attName.String, Value: attValue.String} - mapOfAttributes[entity_id] = append(mapOfAttributes[entity_id], att) + mapOfAttributes[entity_id.String] = append(mapOfAttributes[entity_id.String], att) } } log.Debug("attributes returned for query ", sql, " are ", mapOfAttributes) @@ -157,7 +149,7 @@ var proxies, environments, resources string rows, err := db.Query(sql_GET_API_PRODUCTS_FOR_KEY_SQL, key, tenantId) - + defer rows.Close() if err != nil { log.Error("error fetching apiProduct details", err) return allProducts
diff --git a/data_test.go b/data_test.go index 4d4e8e9..6766e89 100644 --- a/data_test.go +++ b/data_test.go
@@ -43,7 +43,6 @@ dbMux: sync.RWMutex{}, } dbMan.setDbVersion(dataTestTempDir) - dbMan.initDb() })
diff --git a/init.go b/init.go index a60b40e..3b6d4bb 100644 --- a/init.go +++ b/init.go
@@ -44,7 +44,6 @@ data: services.Data(), dbMux: sync.RWMutex{}, } - dbMan.initDb() apiMan := apiManager{ dbMan: dbMan, verifiersEndpoint: apiPath,
diff --git a/listener_test.go b/listener_test.go index 78ea736..7095e23 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -48,7 +48,6 @@ dbMux: sync.RWMutex{}, db: db, } - dbMan.initDb() listnerTestSyncHandler = apigeeSyncHandler{ dbMan: dbMan,