New schema
diff --git a/.gitignore b/.gitignore index 54e1ced..f47e206 100644 --- a/.gitignore +++ b/.gitignore
@@ -1,3 +1,6 @@ profile.out cover.html coverage.txt +.idea +*.lock +*.iml
diff --git a/api.go b/api.go index 1aae5b6..5e517b8 100644 --- a/api.go +++ b/api.go
@@ -78,19 +78,25 @@ db := getDB() key := f.Get("key") - org := f.Get("organization") + scopeuuid := f.Get("scopeuuid") path := f.Get("uriPath") - env := f.Get("environment") action := f.Get("action") - if key == "" || org == "" || path == "" || env == "" || action != "verify" { + if key == "" || scopeuuid == "" || path == "" || action != "verify" { log.Error("Input params Invalid/Incomplete") reason := "Input Params Incomplete or Invalid" errorCode := "INCORRECT_USER_INPUT" return errorResponse(reason, errorCode) } - sSql := "SELECT ap.api_resources, ap.environments, c.issued_at, c.status, a.callback_url, d.username, d.id FROM APP_CREDENTIAL AS c INNER JOIN APP AS a ON c.app_id = a.id INNER JOIN DEVELOPER AS d ON a.developer_id = d.id INNER JOIN APP_CREDENTIAL_APIPRODUCT_MAPPER as mp ON mp.appcred_id = c.id INNER JOIN API_PRODUCT as ap ON ap.id = mp.apiprdt_id WHERE (UPPER(d.status) = 'ACTIVE' AND mp.apiprdt_id = ap.id AND mp.app_id = a.id AND mp.appcred_id = c.id AND UPPER(mp.status) = 'APPROVED' AND UPPER(a.status) = 'APPROVED' AND c.id = '" + key + "' AND c._apid_scope = '" + org + "');" + sSql := "SELECT ap.api_resources, ap.environments, c.issued_at, c.status, a.callback_url, d.username, d.id " + + "FROM APP_CREDENTIAL AS c INNER JOIN APP AS a ON c.app_id = a.id " + + "INNER JOIN DEVELOPER AS d ON a.developer_id = d.id " + + "INNER JOIN APP_CREDENTIAL_APIPRODUCT_MAPPER as mp ON mp.appcred_id = c.id " + + "INNER JOIN API_PRODUCT as ap ON ap.id = mp.apiprdt_id " + + "WHERE (UPPER(d.status) = 'ACTIVE' AND mp.apiprdt_id = ap.id AND mp.app_id = a.id " + + "AND mp.appcred_id = c.id AND UPPER(mp.status) = 'APPROVED' AND UPPER(a.status) = 'APPROVED' " + + "AND c.id = '" + key + "');" var status, redirectionURIs, developerAppName, developerId, resName, resEnv string var issuedAt int64 @@ -98,7 +104,7 @@ &redirectionURIs, &developerAppName, &developerId) switch { case err == sql.ErrNoRows: - reason := "API Key verify failed for (" + key + ", " + org + ", " + path + ", " + env + ")" + reason := "API Key verify failed for (" + key + ", " + scopeuuid + ", " + path + ")" errorCode := "REQ_ENTRY_NOT_FOUND" return errorResponse(reason, errorCode) @@ -120,6 +126,8 @@ } + env := getEnvByScopeUUID(scopeuuid); + /* Verify if the ENV matches */ result = validateEnv(resEnv, env) if result == false { @@ -143,6 +151,23 @@ return json.Marshal(resp) } +func getEnvByScopeUUID(scopeuuid string) (string) { + db := getDB() + + sSql := "SELECT env FROM DATA_SCOPE WHERE id = ?;" + + var env string + err := db.QueryRow(sSql, scopeuuid).Scan(&env) + + switch { + case err == sql.ErrNoRows: + log.Debug("No env with that scopeuuid.") + case err != nil: + log.Debug(err) + } + return env; +} + func errorResponse(reason, errorCode string) ([]byte, error) { log.Error(reason)
diff --git a/api_test.go b/api_test.go index 50e6107..369d540 100644 --- a/api_test.go +++ b/api_test.go
@@ -19,8 +19,7 @@ v := url.Values{ "key": []string{"credential_x"}, "uriPath": []string{"/test"}, - "environment": []string{"Env_0"}, - "organization": []string{"Org_0"}, + "scopeuuid": []string{"ABCDE"}, "action": []string{"verify"}, } rsp, err := verifyAPIKey(v) @@ -62,8 +61,7 @@ v := url.Values{ "key": []string{"app_credential_"+resulti}, "uriPath": []string{"/test"}, - "environment": []string{"Env_0"}, - "organization": []string{"Org_0"}, + "scopeuuid": []string{"ABCDE"}, "action": []string{"verify"}, } rsp, err := verifyAPIKey(v) @@ -85,9 +83,8 @@ uri.Path = apiPath v := url.Values{} - v.Add("organization", "Org_0") v.Add("key", "credential_x") - v.Add("environment", "Env_0") + v.Add("scopeuuid", "ABCDE") v.Add("uriPath", "/test") v.Add("action", "verify") @@ -113,9 +110,8 @@ uri.Path = apiPath v := url.Values{} - v.Add("organization", "Org_0") v.Add("key", "app_credential_1") - v.Add("environment", "Env_0") + v.Add("scopeuuid", "ABCDE") v.Add("uriPath", "/test") v.Add("action", "verify") @@ -131,6 +127,7 @@ body, err := ioutil.ReadAll(res.Body) Expect(err).ShouldNot(HaveOccurred()) json.Unmarshal(body, &respj) + log.Info("Result test",respj) Expect(respj.Type).Should(Equal("APIKeyContext")) Expect(respj.RspInfo.Key).Should(Equal("app_credential_1")) })
diff --git a/init.go b/init.go index ad04f1c..a475420 100644 --- a/init.go +++ b/init.go
@@ -59,7 +59,7 @@ description text, api_resources text[], approval_type text, - _apid_scope text, + _change_selector text, proxies text[], environments text[], quota text, @@ -81,7 +81,7 @@ status text, encrypted_password text, salt text, - _apid_scope text, + _change_selector text, created_at int64, created_by text, updated_at int64, @@ -98,7 +98,7 @@ created_by text, updated_at int64, updated_by text, - _apid_scope text, + _change_selector text, PRIMARY KEY (tenant_id, id) ); CREATE TABLE IF NOT EXISTS company_developer ( @@ -110,7 +110,7 @@ created_by text, updated_at int64, updated_by text, - _apid_scope text, + _change_selector text, PRIMARY KEY (tenant_id, company_id,developer_id) ); CREATE TABLE IF NOT EXISTS app ( @@ -129,7 +129,7 @@ created_by text, updated_at int64, updated_by text, - _apid_scope text, + _change_selector text, PRIMARY KEY (tenant_id, id) ); CREATE TABLE IF NOT EXISTS app_credential ( @@ -142,7 +142,7 @@ issued_at int64, expires_at int64, app_status text, - _apid_scope text, + _change_selector text, PRIMARY KEY (tenant_id, id) ); CREATE TABLE IF NOT EXISTS app_credential_apiproduct_mapper ( @@ -150,7 +150,7 @@ appcred_id text, app_id text, apiprdt_id text, - _apid_scope text, + _change_selector text, status text, PRIMARY KEY (appcred_id, app_id, apiprdt_id,tenant_id) ); @@ -159,3 +159,41 @@ log.Panic("Unable to initialize DB", err) } } + + +func createApidClusterTables(db apid.DB) { + _, err := db.Exec(` +CREATE TABLE apid_cluster ( + id text, + instance_id text, + name text, + description text, + umbrella_org_app_name text, + created int64, + created_by text, + updated int64, + updated_by text, + _change_selector text, + snapshotInfo text, + lastSequence text, + PRIMARY KEY (id) +); +CREATE TABLE data_scope ( + id text, + apid_cluster_id text, + scope text, + org text, + env text, + created int64, + created_by text, + updated int64, + updated_by text, + _change_selector text, + PRIMARY KEY (id) +); +`) + if err != nil { + log.Panic("Unable to initialize DB", err) + } +} +
diff --git a/listener.go b/listener.go index e9398cd..b2e3f34 100644 --- a/listener.go +++ b/listener.go
@@ -87,14 +87,14 @@ var scope, id, appId, consumerSecret, appstatus, status, tenantId string var issuedAt int64 - prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL (_apid_scope, id, app_id, consumer_secret, app_status, status, issued_at, tenant_id)VALUES($1,$2,$3,$4,$5,$6,$7,$8);") + prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL (_change_selector, id, app_id, consumer_secret, app_status, status, issued_at, tenant_id)VALUES($1,$2,$3,$4,$5,$6,$7,$8);") if err != nil { log.Error("INSERT Cred Failed: ", err) return false } defer prep.Close() for _, ele := range rows { - ele.Get("_apid_scope", &scope) + ele.Get("_change_selector", &scope) ele.Get("id", &id) ele.Get("app_id", &appId) ele.Get("consumer_secret", &consumerSecret) @@ -130,7 +130,7 @@ var scope, EntityIdentifier, DeveloperId, CallbackUrl, Status, AppName, AppFamily, tenantId, CreatedBy, LastModifiedBy string var CreatedAt, LastModifiedAt int64 - prep, err := txn.Prepare("INSERT INTO APP (_apid_scope, id, developer_id,callback_url,status, name, app_family, created_at, created_by,updated_at, updated_by,tenant_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);") + prep, err := txn.Prepare("INSERT INTO APP (_change_selector, id, developer_id,callback_url,status, name, app_family, created_at, created_by,updated_at, updated_by,tenant_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);") if err != nil { log.Error("INSERT APP Failed: ", err) return false @@ -139,7 +139,7 @@ defer prep.Close() for _, ele := range rows { - ele.Get("_apid_scope", &scope) + ele.Get("_change_selector", &scope) ele.Get("id", &EntityIdentifier) ele.Get("developer_id", &DeveloperId) ele.Get("callback_url", &CallbackUrl) @@ -185,7 +185,7 @@ var scope, EntityIdentifier, Email, Status, UserName, FirstName, LastName, tenantId, CreatedBy, LastModifiedBy, Username string var CreatedAt, LastModifiedAt int64 - prep, err := txn.Prepare("INSERT INTO DEVELOPER (_apid_scope,email,id,tenant_id,status,username,first_name,last_name,created_at,created_by,updated_at,updated_by) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);") + prep, err := txn.Prepare("INSERT INTO DEVELOPER (_change_selector,email,id,tenant_id,status,username,first_name,last_name,created_at,created_by,updated_at,updated_by) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);") if err != nil { log.Error("INSERT DEVELOPER Failed: ", err) return false @@ -194,7 +194,7 @@ defer prep.Close() for _, ele := range rows { - ele.Get("_apid_scope", &scope) + ele.Get("_change_selector", &scope) ele.Get("email", &Email) ele.Get("id", &EntityIdentifier) ele.Get("tenant_id", &tenantId) @@ -238,7 +238,7 @@ var scope, apiProduct, res, env, tenantId string - prep, err := txn.Prepare("INSERT INTO API_PRODUCT (id, api_resources, environments, tenant_id,_apid_scope) VALUES($1,$2,$3,$4,$5)") + prep, err := txn.Prepare("INSERT INTO API_PRODUCT (id, api_resources, environments, tenant_id,_change_selector) VALUES($1,$2,$3,$4,$5)") if err != nil { log.Error("INSERT API_PRODUCT Failed: ", err) return false @@ -247,7 +247,7 @@ defer prep.Close() for _, ele := range rows { - ele.Get("_apid_scope", &scope) + ele.Get("_change_selector", &scope) ele.Get("id", &apiProduct) ele.Get("api_resources", &res) ele.Get("environments", &env) @@ -277,7 +277,7 @@ var ApiProduct, AppId, EntityIdentifier, tenantId, Scope, Status string - prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL_APIPRODUCT_MAPPER(apiprdt_id, app_id, appcred_id, tenant_id, _apid_scope, status) VALUES($1,$2,$3,$4,$5,$6);") + prep, err := txn.Prepare("INSERT INTO APP_CREDENTIAL_APIPRODUCT_MAPPER(apiprdt_id, app_id, appcred_id, tenant_id, _change_selector, status) VALUES($1,$2,$3,$4,$5,$6);") if err != nil { log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err) return false @@ -290,7 +290,7 @@ ele.Get("app_id", &AppId) ele.Get("appcred_id", &EntityIdentifier) ele.Get("tenant_id", &tenantId) - ele.Get("_apid_scope", &Scope) + ele.Get("_change_selector", &Scope) ele.Get("status", &Status) /* @@ -438,14 +438,14 @@ func deleteObject(object string, ele common.Row, txn *sql.Tx) bool { var scope, apiProduct string - ssql := "DELETE FROM " + object + " WHERE id = $1 AND _apid_scope = $2" + ssql := "DELETE FROM " + object + " WHERE id = $1 AND _change_selector = $2" prep, err := txn.Prepare(ssql) if err != nil { log.Error("DELETE ", object, " Failed: ", err) return false } defer prep.Close() - ele.Get("_apid_scope", &scope) + ele.Get("_change_selector", &scope) ele.Get("id", &apiProduct) _, err = txn.Stmt(prep).Exec(apiProduct, scope) @@ -465,7 +465,7 @@ func deleteAPIproductMapper(ele common.Row, txn *sql.Tx) bool { var ApiProduct, AppId, EntityIdentifier, apid_scope string - prep, err := txn.Prepare("DELETE FROM APP_CREDENTIAL_APIPRODUCT_MAPPER WHERE apiprdt_id=$1 AND app_id=$2 AND appcred_id=$3 AND _apid_scope=$4;") + prep, err := txn.Prepare("DELETE FROM APP_CREDENTIAL_APIPRODUCT_MAPPER WHERE apiprdt_id=$1 AND app_id=$2 AND appcred_id=$3 AND _change_selector=$4;") if err != nil { log.Error("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err) return false @@ -476,7 +476,7 @@ ele.Get("apiprdt_id", &ApiProduct) ele.Get("app_id", &AppId) ele.Get("appcred_id", &EntityIdentifier) - ele.Get("_apid_scope", &apid_scope) + ele.Get("_change_selector", &apid_scope) _, err = txn.Stmt(prep).Exec(ApiProduct, AppId, EntityIdentifier, apid_scope) if err != nil {
diff --git a/listener_test.go b/listener_test.go index af8b78a..e50fe0e 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -34,7 +34,7 @@ Value: "tenant_id_0", Type: 1, }, - "_apid_scope": { + "_change_selector": { Value: "test_org0", Type: 1, }, @@ -54,7 +54,7 @@ Value: "tenant_id_0", Type: 1, }, - "_apid_scope": { + "_change_selector": { Value: "test_org0", Type: 1, }, @@ -78,7 +78,7 @@ Value: "tenant_id_0", Type: 1, }, - "_apid_scope": { + "_change_selector": { Value: "test_org0", Type: 1, }, @@ -102,7 +102,7 @@ Value: "Approved", Type: 1, }, - "_apid_scope": { + "_change_selector": { Value: "test_org0", Type: 1, }, @@ -126,7 +126,7 @@ Value: "Approved", Type: 1, }, - "_apid_scope": { + "_change_selector": { Value: "test_org0", Type: 1, }, @@ -200,8 +200,7 @@ v := url.Values{ "key": []string{"ch_app_credential_0"}, "uriPath": []string{"/test"}, - "environment": []string{"Env_0"}, - "organization": []string{"test_org0"}, + "scopeuuid": []string{"ABCDE"}, "action": []string{"verify"}, } rsp, err := verifyAPIKey(v)
diff --git a/pluginData.go b/pluginData.go index 8c3335d..b261d7c 100644 --- a/pluginData.go +++ b/pluginData.go
@@ -4,8 +4,8 @@ var pluginData = apid.PluginData{ Name: "apidVerifyAPIKey", - Version: "0.0.1", + Version: "0.0.2", ExtraData: map[string]interface{}{ - "schemaVersion": "0.0.1", + "schemaVersion": "0.0.2", }, }
diff --git a/validate_env.go b/validate_env.go index d90e27c..9fede78 100644 --- a/validate_env.go +++ b/validate_env.go
@@ -6,7 +6,9 @@ * Ensure the ENV matches. */ func validateEnv(envLocal string, envInPath string) bool { - + if (envInPath == "") { + return false; + } s := strings.TrimPrefix(envLocal, "{") s = strings.TrimSuffix(s, "}") fs := strings.Split(s, ",")
diff --git a/verifyAPIKey_suite_test.go b/verifyAPIKey_suite_test.go index 610d941..d1f8e6d 100644 --- a/verifyAPIKey_suite_test.go +++ b/verifyAPIKey_suite_test.go
@@ -37,6 +37,7 @@ Expect(err).NotTo(HaveOccurred()) setDB(db) createTables(db) + createApidClusterTables(db) insertTestData(db) testServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == apiPath { @@ -90,7 +91,7 @@ Value: "Org_0", Type: 1, } - srvItems["_apid_scope"] = scv + srvItems["_change_selector"] = scv scv = &common.ColumnVal{ Value: "tenant_id_xxxx", @@ -141,7 +142,7 @@ Value: "Org_0", Type: 1, } - srvItems["_apid_scope"] = scv + srvItems["_change_selector"] = scv scv = &common.ColumnVal{ Value: "tenant_id_xxxx", @@ -157,7 +158,7 @@ var j, k int for i := 0; i < 10; i++ { resulti := strconv.FormatInt(int64(i), 10) - for j = k; j < 10+k; j++ { + for j = k; j < 10 + k; j++ { var rows []common.Row srvItems := common.Row{} @@ -191,7 +192,7 @@ Value: "Org_0", Type: 1, } - srvItems["_apid_scope"] = scv + srvItems["_change_selector"] = scv scv = &common.ColumnVal{ Value: "tenant_id_xxxx", @@ -232,7 +233,7 @@ Value: "Org_0", Type: 1, } - srvItems["_apid_scope"] = scv + srvItems["_change_selector"] = scv scv = &common.ColumnVal{ Value: "tenant_id_xxxx", @@ -275,7 +276,7 @@ Value: "Org_0", Type: 1, } - srvItems["_apid_scope"] = scv + srvItems["_change_selector"] = scv scv = &common.ColumnVal{ Value: "tenant_id_xxxx", @@ -286,6 +287,20 @@ res := insertAPIProductMappers(rows, txn) Expect(res).Should(BeTrue()) } - + txn.Exec("INSERT INTO DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) " + + "VALUES" + + "($1,$2,$3,$4,$5,$6)", + "ABCDE", + "some_cluster_id", + "some_cluster_id", + "tenant_id_0", + "test_org0", + "Env_0", + ); + log.Info("Inserted DATA_SCOPE for test") txn.Commit() + var count int64 + db.QueryRow("select count(*) from data_scope").Scan(&count); + log.Info("Found ", count) + }