Merge branch 'poc2'
diff --git a/api.go b/api.go
index 241e472..3361c74 100644
--- a/api.go
+++ b/api.go
@@ -98,12 +98,7 @@
return errorResponse(reason, errorCode)
}
- /*
- * NOTE: that here c.expired_at has been commented out because it is not
- * kept track of by Cassandra (hence always defaults to -1). FIXME
- */
-
- sSql = "SELECT ap.res_names, ap.env, c.issued_at, c.status, a.cback_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.dev_id = d.id INNER JOIN APP_AND_API_PRODUCT_MAPPER as mp ON mp.app_cred_id = c.id INNER JOIN API_PRODUCT as ap ON ap.id = mp.api_prdt_id WHERE (UPPER(d.sts) = 'ACTIVE' AND mp.api_prdt_id = ap.id AND mp.app_id = a.id AND mp.app_cred_id = c.id AND UPPER(mp.api_prdt_status) = 'APPROVED' AND UPPER(a.status) = 'APPROVED' AND UPPER(c.status) = 'APPROVED' AND c.id = '" + key + "' AND c.org = '" + 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 + "' AND c._apid_scope = '" + org + "');"
err = db.QueryRow(sSql).Scan(&resName, &resEnv, &issuedAt, &status,
&redirectionURIs, &developerAppName, &developerId)
diff --git a/api_test.go b/api_test.go
index 2d9d921..50d1567 100644
--- a/api_test.go
+++ b/api_test.go
@@ -5,7 +5,7 @@
"encoding/json"
"github.com/30x/apid"
"github.com/30x/apid/factory"
- . "github.com/30x/apidApigeeSync" // for direct access to Payload types
+ "github.com/apigee-labs/transicator/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
@@ -39,8 +39,10 @@
db, err = apid.Data().DB()
Expect(err).NotTo(HaveOccurred())
- insertTestData(db)
-
+ txn, err := db.Begin()
+ Expect(err).ShouldNot(HaveOccurred())
+ insertTestData(db, txn)
+ txn.Commit()
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == apiPath {
handleRequest(w, req)
@@ -66,33 +68,33 @@
Expect(respj.ErrInfo.ErrorCode).Should(Equal("REQ_ENTRY_NOT_FOUND"))
})
+ /*
+ It("should reject a key once it's deleted", func() {
+ pd0 := &DataPayload{
+ EntityIdentifier: "app_credential_0",
+ }
+ res := deleteCredential(*pd0, db)
+ Expect(res).Should(BeTrue())
- It("should reject a key once it's deleted", func() {
- pd0 := &DataPayload{
- EntityIdentifier: "credential_0",
- }
- res := deleteCredential(*pd0, db, "Org_0")
- Expect(res).Should(BeTrue())
+ var respj kmsResponseFail
+ rsp, err := verifyAPIKey("app_credential_0", "/test", "Env_0", "Org_0", "verify")
+ Expect(err).ShouldNot(HaveOccurred())
- var respj kmsResponseFail
- rsp, err := verifyAPIKey("credential_0", "/test", "Env_0", "Org_0", "verify")
- Expect(err).ShouldNot(HaveOccurred())
-
- json.Unmarshal(rsp, &respj)
- Expect(respj.Type).Should(Equal("ErrorResult"))
- Expect(respj.ErrInfo.ErrorCode).Should(Equal("REQ_ENTRY_NOT_FOUND"))
- })
-
+ json.Unmarshal(rsp, &respj)
+ Expect(respj.Type).Should(Equal("ErrorResult"))
+ Expect(respj.ErrInfo.ErrorCode).Should(Equal("REQ_ENTRY_NOT_FOUND"))
+ })
+ */
It("should successfully verify good keys", func() {
for i := 1; i < 10; i++ {
resulti := strconv.FormatInt(int64(i), 10)
- rsp, err := verifyAPIKey("credential_"+resulti, "/test", "Env_0", "Org_0", "verify")
+ rsp, err := verifyAPIKey("app_credential_"+resulti, "/test", "Env_0", "Org_0", "verify")
Expect(err).ShouldNot(HaveOccurred())
var respj kmsResponseSuccess
json.Unmarshal(rsp, &respj)
Expect(respj.Type).Should(Equal("APIKeyContext"))
- Expect(respj.RspInfo.Key).Should(Equal("credential_" + resulti))
+ Expect(respj.RspInfo.Key).Should(Equal("app_credential_" + resulti))
}
})
})
@@ -134,7 +136,7 @@
v := url.Values{}
v.Add("organization", "Org_0")
- v.Add("key", "credential_1")
+ v.Add("key", "app_credential_1")
v.Add("environment", "Env_0")
v.Add("uriPath", "/test")
v.Add("action", "verify")
@@ -152,42 +154,101 @@
Expect(err).ShouldNot(HaveOccurred())
json.Unmarshal(body, &respj)
Expect(respj.Type).Should(Equal("APIKeyContext"))
- Expect(respj.RspInfo.Key).Should(Equal("credential_1"))
+ Expect(respj.RspInfo.Key).Should(Equal("app_credential_1"))
})
})
})
-func insertTestData(db *sql.DB) {
+func insertTestData(db *sql.DB, txn *sql.Tx) {
for i := 0; i < 10; i++ {
+ var rows []common.Row
+ srvItems := common.Row{}
result := strconv.FormatInt(int64(i), 10)
- pd0 := &DataPayload{
- PldCont: Payload{
- AppName: "Api_product_" + result,
- Resources: []string{"/**", "/test"},
- Environments: []string{"Env_0", "Env_1"},
- },
- }
- res := insertAPIproduct(*pd0, db, "Org_0")
+ scv := &common.ColumnVal{
+ Value: "api_product_" + result,
+ Type: 1,
+ }
+ srvItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "{/**, /test}",
+ Type: 1,
+ }
+ srvItems["api_resources"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "{Env_0, Env_1}",
+ Type: 1,
+ }
+ srvItems["environments"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Org_0",
+ Type: 1,
+ }
+ srvItems["_apid_scope"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_xxxx",
+ Type: 1,
+ }
+ srvItems["tenant_id"] = scv
+ rows = append(rows, srvItems)
+ res := insertAPIproducts(rows, db, txn)
Expect(res).Should(BeTrue())
}
for i := 0; i < 10; i++ {
+ var rows []common.Row
+ srvItems := common.Row{}
result := strconv.FormatInt(int64(i), 10)
- pd1 := &DataPayload{
- EntityIdentifier: "developer_id_" + result,
- PldCont: Payload{
- Email: "person_0@apigee.com",
- Status: "Active",
- UserName: "user_0",
- FirstName: "user_first_name0",
- LastName: "user_last_name0",
- },
+ scv := &common.ColumnVal{
+ Value: "developer_id_" + result,
+ Type: 1,
}
+ srvItems["id"] = scv
- res := insertCreateDeveloper(*pd1, db, "Org_0")
+ scv = &common.ColumnVal{
+ Value: "test@apigee.com",
+ Type: 1,
+ }
+ srvItems["email"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Active",
+ Type: 1,
+ }
+ srvItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Apigee",
+ Type: 1,
+ }
+ srvItems["firstName"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Google",
+ Type: 1,
+ }
+ srvItems["lastName"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Org_0",
+ Type: 1,
+ }
+ srvItems["_apid_scope"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_xxxx",
+ Type: 1,
+ }
+ srvItems["tenant_id"] = scv
+
+ rows = append(rows, srvItems)
+ res := insertDevelopers(rows, db, txn)
Expect(res).Should(BeTrue())
}
@@ -195,44 +256,133 @@
for i := 0; i < 10; i++ {
resulti := strconv.FormatInt(int64(i), 10)
for j = k; j < 10+k; j++ {
- resultj := strconv.FormatInt(int64(j), 10)
- pd2 := &DataPayload{
- EntityIdentifier: "application_id_" + resultj,
- PldCont: Payload{
- Email: "person_0@apigee.com",
- Status: "Approved",
- AppName: "application_id_" + resultj,
- DeveloperId: "developer_id_" + resulti,
- CallbackUrl: "call_back_url_0",
- },
- }
+ var rows []common.Row
- res := insertCreateApplication(*pd2, db, "Org_0")
+ srvItems := common.Row{}
+ resultj := strconv.FormatInt(int64(j), 10)
+
+ scv := &common.ColumnVal{
+ Value: "application_id_" + resultj,
+ Type: 1,
+ }
+ srvItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "developer_id_" + resulti,
+ Type: 1,
+ }
+ srvItems["developer_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "approved",
+ Type: 1,
+ }
+ srvItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "http://apigee.com",
+ Type: 1,
+ }
+ srvItems["callback_url"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Org_0",
+ Type: 1,
+ }
+ srvItems["_apid_scope"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_xxxx",
+ Type: 1,
+ }
+ srvItems["tenant_id"] = scv
+ rows = append(rows, srvItems)
+ res := insertApplications(rows, db, txn)
Expect(res).Should(BeTrue())
}
k = j
}
- j = 0
- k = 0
for i := 0; i < 10; i++ {
- resulti := strconv.FormatInt(int64(i), 10)
- for j = k; j < 10+k; j++ {
- resultj := strconv.FormatInt(int64(j), 10)
- pd3 := &DataPayload{
- EntityIdentifier: "credential_" + resultj,
- PldCont: Payload{
- AppId: "application_id_" + resulti,
- Status: "Approved",
- ConsumerSecret: "consumer_secret_0",
- IssuedAt: 349583485,
- ApiProducts: []Apip{{ApiProduct: "Api_product_0", Status: "Approved"}},
- },
- }
+ var rows []common.Row
+ srvItems := common.Row{}
+ result := strconv.FormatInt(int64(i), 10)
- res := insertCreateCredential(*pd3, db, "Org_0")
- Expect(res).Should(BeTrue())
+ scv := &common.ColumnVal{
+ Value: "app_credential_" + result,
+ Type: 1,
}
- k = j
+ srvItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "application_id_" + result,
+ Type: 1,
+ }
+ srvItems["app_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "approved",
+ Type: 1,
+ }
+ srvItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Org_0",
+ Type: 1,
+ }
+ srvItems["_apid_scope"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_xxxx",
+ Type: 1,
+ }
+ srvItems["tenant_id"] = scv
+ rows = append(rows, srvItems)
+ res := insertCredentials(rows, db, txn)
+ Expect(res).Should(BeTrue())
}
+
+ for i := 0; i < 10; i++ {
+ var rows []common.Row
+ srvItems := common.Row{}
+ result := strconv.FormatInt(int64(i), 10)
+
+ scv := &common.ColumnVal{
+ Value: "api_product_" + result,
+ Type: 1,
+ }
+ srvItems["apiprdt_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "application_id_" + result,
+ Type: 1,
+ }
+ srvItems["app_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "app_credential_" + result,
+ Type: 1,
+ }
+ srvItems["appcred_id"] = scv
+ scv = &common.ColumnVal{
+ Value: "approved",
+ Type: 1,
+ }
+ srvItems["status"] = scv
+ scv = &common.ColumnVal{
+ Value: "Org_0",
+ Type: 1,
+ }
+ srvItems["_apid_scope"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_xxxx",
+ Type: 1,
+ }
+ srvItems["tenant_id"] = scv
+ rows = append(rows, srvItems)
+ res := insertAPIProductMappers(rows, db, txn)
+ Expect(res).Should(BeTrue())
+ }
+
}
diff --git a/init.go b/init.go
index 5c243ce..bae3a3f 100644
--- a/init.go
+++ b/init.go
@@ -7,7 +7,7 @@
)
const (
- apiPath = "/verifyAPIKey"
+ apiPath = "/verifiers/apikey"
)
var (
@@ -33,7 +33,7 @@
}
var count int
- row := db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='company';")
+ row := db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='API_PRODUCT' COLLATE NOCASE;")
if err := row.Scan(&count); err != nil {
log.Panic("Unable to setup database", err)
}
@@ -50,7 +50,111 @@
}
func createTables(db *sql.DB) {
- _, err := db.Exec("CREATE TABLE COMPANY (org varchar(255), id varchar(255), PRIMARY KEY (id, org));CREATE TABLE DEVELOPER (org varchar(255), email varchar(255), id varchar(255), sts varchar(255), username varchar(255), firstname varchar(255), lastname varchar(255), apigee_scope varchar(255), enc_password varchar(255), salt varchar(255), created_at integer, created_by varchar(255), updated_at integer, updated_by varchar(255), PRIMARY KEY (id, org));CREATE TABLE APP (org varchar(255), id varchar(255), dev_id varchar(255) null, cmp_id varchar(255) null, display_name varchar(255), apigee_scope varchar(255), type varchar(255), access_type varchar(255), cback_url varchar(255), status varchar(255), name varchar(255), app_family varchar(255), created_at integer, created_by varchar(255), updated_at integer, updated_by varchar(255), PRIMARY KEY (id, org), FOREIGN KEY (dev_id, org) references DEVELOPER (id, org) ON DELETE CASCADE);CREATE TABLE APP_CREDENTIAL (org varchar(255), id varchar(255), app_id varchar(255), cons_secret varchar(255), method_type varchar(255), status varchar(255), issued_at integer, expire_at integer, created_at integer, created_by varchar(255), updated_at integer, updated_by varchar(255), PRIMARY KEY (id, org), FOREIGN KEY (app_id, org) references app (id, org) ON DELETE CASCADE);CREATE TABLE API_PRODUCT (org varchar(255), id varchar(255), res_names varchar(255), env varchar(255), PRIMARY KEY (id, org));CREATE TABLE COMPANY_DEVELOPER (org varchar(255), dev_id varchar(255), id varchar(255), cmpny_id varchar(255), PRIMARY KEY (id, org), FOREIGN KEY (cmpny_id) references company(id) ON DELETE CASCADE, FOREIGN KEY (dev_id, org) references DEVELOPER(id, org) ON DELETE CASCADE);CREATE TABLE APP_AND_API_PRODUCT_MAPPER (org varchar(255), api_prdt_id varchar(255), app_id varchar(255), app_cred_id varchar(255), api_prdt_status varchar(255), PRIMARY KEY (org, api_prdt_id, app_id, app_cred_id), FOREIGN KEY (api_prdt_id, org) references api_product(id, org) ON DELETE CASCADE, FOREIGN KEY (app_cred_id, org) references app_credential(id, org) ON DELETE CASCADE, FOREIGN KEY (app_id, org) references app(id, org) ON DELETE CASCADE);")
+ _, err := db.Exec(`
+CREATE TABLE api_product (
+ id text,
+ tenant_id text,
+ name text,
+ display_name text,
+ description text,
+ api_resources text[],
+ approval_type text,
+ _apid_scope text,
+ proxies text[],
+ environments text[],
+ quota text,
+ quota_time_unit text,
+ quota_interval int,
+ created_at int64,
+ created_by text,
+ updated_at int64,
+ updated_by text,
+ PRIMARY KEY (tenant_id, id));
+CREATE TABLE developer (
+ id text,
+ tenant_id text,
+ username text,
+ first_name text,
+ last_name text,
+ password text,
+ email text,
+ status text,
+ encrypted_password text,
+ salt text,
+ _apid_scope text,
+ created_at int64,
+ created_by text,
+ updated_at int64,
+ updated_by text,
+ PRIMARY KEY (tenant_id, id)
+);
+CREATE TABLE company (
+ id text,
+ tenant_id text,
+ name text,
+ display_name text,
+ status text,
+ created_at int64,
+ created_by text,
+ updated_at int64,
+ updated_by text,
+ _apid_scope text,
+ PRIMARY KEY (tenant_id, id)
+);
+CREATE TABLE company_developer (
+ tenant_id text,
+ company_id text,
+ developer_id text,
+ roles text[],
+ created_at int64,
+ created_by text,
+ updated_at int64,
+ updated_by text,
+ _apid_scope text,
+ PRIMARY KEY (tenant_id, company_id,developer_id)
+);
+CREATE TABLE app (
+ id text,
+ tenant_id text,
+ name text,
+ display_name text,
+ access_type text,
+ callback_url text,
+ status text,
+ app_family text,
+ company_id text,
+ developer_id text,
+ type int,
+ created_at int64,
+ created_by text,
+ updated_at int64,
+ updated_by text,
+ _apid_scope text,
+ PRIMARY KEY (tenant_id, id)
+);
+CREATE TABLE app_credential (
+ id text,
+ tenant_id text,
+ consumer_secret text,
+ app_id text,
+ method_type text,
+ status text,
+ issued_at int64,
+ expires_at int64,
+ app_status text,
+ _apid_scope text,
+ PRIMARY KEY (tenant_id, id)
+);
+CREATE TABLE app_credential_apiproduct_mapper (
+ tenant_id text,
+ appcred_id text,
+ app_id text,
+ apiprdt_id text,
+ _apid_scope text,
+ status text,
+ PRIMARY KEY (appcred_id, app_id, apiprdt_id,tenant_id)
+);
+`)
if err != nil {
log.Panic("Unable to initialize DB", err)
}
diff --git a/listener.go b/listener.go
index 6aec26f..9908f8d 100644
--- a/listener.go
+++ b/listener.go
@@ -2,9 +2,8 @@
import (
"database/sql"
- "encoding/json"
"github.com/30x/apid"
- "github.com/30x/apidApigeeSync"
+ "github.com/apigee-labs/transicator/common"
)
type handler struct {
@@ -14,241 +13,475 @@
return "verifyAPIKey"
}
-// todo: The following was basically just copied from old APID - needs review.
-
func (h *handler) Handle(e apid.Event) {
- changeSet, ok := e.(*apidApigeeSync.ChangeSet)
- if !ok {
- log.Errorf("Received non-ChangeSet event. This shouldn't happen!")
+
+ res := true
+ db, err := data.DB()
+ if err != nil {
+ panic("Unable to access Sqlite DB")
+ }
+
+ txn, err := db.Begin()
+ if err != nil {
+ log.Error("Unable to create Sqlite transaction")
return
}
- log.Debugf("apigeeSyncEvent: %d changes", len(changeSet.Changes))
-
- db, err := data.DB()
- if err != nil {
- panic("help me!") // todo: handle
- }
-
- for _, payload := range changeSet.Changes {
-
- org := payload.Data.PldCont.Organization
-
- switch payload.Data.EntityType {
- case "developer":
- switch payload.Data.Operation {
- case "create":
- insertCreateDeveloper(payload.Data, db, org)
- }
-
- case "app":
- switch payload.Data.Operation {
- case "create":
- insertCreateApplication(payload.Data, db, org)
- }
-
- case "credential":
- switch payload.Data.Operation {
- case "create":
- insertCreateCredential(payload.Data, db, org)
-
- case "delete":
- deleteCredential(payload.Data, db, org)
- }
-
- case "apiproduct":
- switch payload.Data.Operation {
- case "create":
- insertAPIproduct(payload.Data, db, org)
- }
+ snapData, ok := e.(*common.Snapshot)
+ if ok {
+ res = processSnapshot(snapData, txn)
+ } else {
+ changeSet, ok := e.(*common.ChangeList)
+ if ok {
+ res = processChange(changeSet, txn)
+ } else {
+ log.Errorf("Received Invalid event. This shouldn't happen!")
}
-
}
+ if res == true {
+ txn.Commit()
+ } else {
+ txn.Rollback()
+ }
+ return
+}
+
+func processSnapshot(snapshot *common.Snapshot, txn *sql.Tx) bool {
+
+ res := true
+ log.Debugf("Process Snapshot data")
+ /*
+ * Iterate the tables, and insert the rows,
+ * Commit them in bulk.
+ */
+ for _, payload := range snapshot.Tables {
+ switch payload.Name {
+ case "kms.developer":
+ res = insertDevelopers(payload.Rows, txn)
+ case "kms.app":
+ res = insertApplications(payload.Rows, txn)
+ case "kms.app_credential":
+ res = insertCredentials(payload.Rows, txn)
+ case "kms.api_product":
+ res = insertAPIproducts(payload.Rows, txn)
+ case "kms.app_credential_apiproduct_mapper":
+ res = insertAPIProductMappers(payload.Rows, txn)
+ }
+ if res == false {
+ log.Error("Error encountered in Downloading Snapshot for VerifyApiKey")
+ return false
+ }
+ }
+ log.Debug("Downloading Snapshot for VerifyApiKey complete")
+ return true
}
/*
- * INSERT INTO APP_CREDENTIAL op
+ * Performs bulk insert of credentials
*/
-func insertCreateCredential(ele apidApigeeSync.DataPayload, db *sql.DB, org string) bool {
+func insertCredentials(rows []common.Row, txn *sql.Tx) bool {
- txn, _ := db.Begin()
- isPass := true
- _, err := txn.Exec("INSERT INTO APP_CREDENTIAL (org, id, app_id, cons_secret, status, issued_at)VALUES(?,?,?,?,?,?);",
- org,
- ele.EntityIdentifier,
- ele.PldCont.AppId,
- ele.PldCont.ConsumerSecret,
- ele.PldCont.Status,
- ele.PldCont.IssuedAt)
+ 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);")
if err != nil {
- isPass = false
- log.Error("INSERT CRED Failed: ", ele.EntityIdentifier, org, ")", err)
- goto OT
- } else {
- log.Info("INSERT CRED Success: (", ele.EntityIdentifier, org, ")")
+ log.Error("INSERT Cred Failed: ", err)
+ return false
}
-
- /*
- * If the credentials has been successfully inserted, insert the
- * mapping entries associated with the credential
- */
-
- for _, elem := range ele.PldCont.ApiProducts {
-
- _, err = txn.Exec("INSERT INTO APP_AND_API_PRODUCT_MAPPER (org, api_prdt_id, app_id, app_cred_id, api_prdt_status) VALUES(?,?,?,?,?);",
- org,
- elem.ApiProduct,
- ele.PldCont.AppId,
- ele.EntityIdentifier,
- elem.Status)
+ defer prep.Close()
+ for _, ele := range rows {
+ ele.Get("_apid_scope", &scope)
+ ele.Get("id", &id)
+ ele.Get("app_id", &appId)
+ ele.Get("consumer_secret", &consumerSecret)
+ ele.Get("app_status", &appstatus)
+ ele.Get("status", &status)
+ ele.Get("issued_at", &issuedAt)
+ ele.Get("tenant_id", &tenantId)
+ _, err = txn.Stmt(prep).Exec(
+ scope,
+ id,
+ appId,
+ consumerSecret,
+ appstatus,
+ status,
+ issuedAt,
+ tenantId)
if err != nil {
- isPass = false
- log.Error("INSERT APP_AND_API_PRODUCT_MAPPER Failed: (",
- org,
- elem.ApiProduct,
- ele.PldCont.AppId,
- ele.EntityIdentifier,
+ log.Error("INSERT CRED Failed: ", id, ", ", scope, ")", err)
+ return false
+ } else {
+ log.Debug("INSERT CRED Success: (", id, ", ", scope, ")")
+ }
+ }
+ return true
+}
+
+/*
+ * Performs Bulk insert of Applications
+ */
+func insertApplications(rows []common.Row, txn *sql.Tx) bool {
+
+ 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);")
+ if err != nil {
+ log.Error("INSERT APP Failed: ", err)
+ return false
+ }
+
+ defer prep.Close()
+ for _, ele := range rows {
+
+ ele.Get("_apid_scope", &scope)
+ ele.Get("id", &EntityIdentifier)
+ ele.Get("developer_id", &DeveloperId)
+ ele.Get("callback_url", &CallbackUrl)
+ ele.Get("status", &Status)
+ ele.Get("name", &AppName)
+ ele.Get("app_family", &AppFamily)
+ ele.Get("created_at", &CreatedAt)
+ ele.Get("created_by", &CreatedBy)
+ ele.Get("updated_at", &LastModifiedAt)
+ ele.Get("updated_by", &LastModifiedBy)
+ ele.Get("tenant_id", &tenantId)
+
+ _, err = txn.Stmt(prep).Exec(
+ scope,
+ EntityIdentifier,
+ DeveloperId,
+ CallbackUrl,
+ Status,
+ AppName,
+ AppFamily,
+ CreatedAt,
+ CreatedBy,
+ LastModifiedAt,
+ LastModifiedBy,
+ tenantId)
+
+ if err != nil {
+ log.Error("INSERT APP Failed: (", EntityIdentifier, ", ", tenantId, ")", err)
+ return false
+ } else {
+ log.Debug("INSERT APP Success: (", EntityIdentifier, ", ", tenantId, ")")
+ }
+ }
+ return true
+
+}
+
+/*
+ * Performs bulk insert of Developers
+ */
+func insertDevelopers(rows []common.Row, txn *sql.Tx) bool {
+
+ 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);")
+ if err != nil {
+ log.Error("INSERT DEVELOPER Failed: ", err)
+ return false
+ }
+
+ defer prep.Close()
+ for _, ele := range rows {
+
+ ele.Get("_apid_scope", &scope)
+ ele.Get("email", &Email)
+ ele.Get("id", &EntityIdentifier)
+ ele.Get("tenant_id", &tenantId)
+ ele.Get("status", &Status)
+ ele.Get("username", &Username)
+ ele.Get("first_name", &FirstName)
+ ele.Get("last_name", &LastName)
+ ele.Get("created_at", &CreatedAt)
+ ele.Get("created_by", &CreatedBy)
+ ele.Get("updated_at", &LastModifiedAt)
+ ele.Get("updated_by", &LastModifiedBy)
+
+ _, err = txn.Stmt(prep).Exec(
+ scope,
+ Email,
+ EntityIdentifier,
+ tenantId,
+ Status,
+ UserName,
+ FirstName,
+ LastName,
+ CreatedAt,
+ CreatedBy,
+ LastModifiedAt,
+ LastModifiedBy)
+
+ if err != nil {
+ log.Error("INSERT DEVELOPER Failed: (", EntityIdentifier, ", ", scope, ")", err)
+ return false
+ } else {
+ log.Debug("INSERT DEVELOPER Success: (", EntityIdentifier, ", ", scope, ")")
+ }
+ }
+ return true
+}
+
+/*
+ * Performs Bulk insert of API products
+ */
+func insertAPIproducts(rows []common.Row, txn *sql.Tx) bool {
+
+ 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)")
+ if err != nil {
+ log.Error("INSERT API_PRODUCT Failed: ", err)
+ return false
+ }
+
+ defer prep.Close()
+ for _, ele := range rows {
+
+ ele.Get("_apid_scope", &scope)
+ ele.Get("id", &apiProduct)
+ ele.Get("api_resources", &res)
+ ele.Get("environments", &env)
+ ele.Get("tenant_id", &tenantId)
+
+ _, err = txn.Stmt(prep).Exec(
+ apiProduct,
+ res,
+ env,
+ tenantId,
+ scope)
+
+ if err != nil {
+ log.Error("INSERT API_PRODUCT Failed: (", apiProduct, ", ", tenantId, ")", err)
+ return false
+ } else {
+ log.Debug("INSERT API_PRODUCT Success: (", apiProduct, ", ", tenantId, ")")
+ }
+ }
+ return true
+}
+
+/*
+ * Performs a bulk insert of all APP_CREDENTIAL_APIPRODUCT_MAPPER rows
+ */
+func insertAPIProductMappers(rows []common.Row, txn *sql.Tx) bool {
+
+ 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);")
+ if err != nil {
+ log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err)
+ return false
+ }
+
+ defer prep.Close()
+ for _, ele := range rows {
+
+ ele.Get("apiprdt_id", &ApiProduct)
+ ele.Get("app_id", &AppId)
+ ele.Get("appcred_id", &EntityIdentifier)
+ ele.Get("tenant_id", &tenantId)
+ ele.Get("_apid_scope", &Scope)
+ ele.Get("status", &Status)
+
+ /*
+ * If the credentials has been successfully inserted, insert the
+ * mapping entries associated with the credential
+ */
+
+ _, err = txn.Stmt(prep).Exec(
+ ApiProduct,
+ AppId,
+ EntityIdentifier,
+ tenantId,
+ Scope,
+ Status)
+
+ if err != nil {
+ log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: (",
+ ApiProduct, ", ",
+ AppId, ", ",
+ EntityIdentifier, ", ",
+ tenantId, ", ",
+ Scope, ", ",
+ Status,
")",
err)
- break
+
+ return false
} else {
- log.Info("INSERT APP_AND_API_PRODUCT_MAPPER Success: (",
- org,
- elem.ApiProduct,
- ele.PldCont.AppId,
- ele.EntityIdentifier,
+ log.Debug("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Success: (",
+ ApiProduct, ", ",
+ AppId, ", ",
+ EntityIdentifier, ", ",
+ tenantId, ", ",
+ Scope, ", ",
+ Status,
")")
}
}
-OT:
- if isPass == true {
- txn.Commit()
- } else {
- txn.Rollback()
- }
- return isPass
+ return true
+}
+func processChange(changes *common.ChangeList, txn *sql.Tx) bool {
+
+ var rows []common.Row
+ res := true
+
+ log.Debugf("apigeeSyncEvent: %d changes", len(changes.Changes))
+ for _, payload := range changes.Changes {
+ rows = nil
+ switch payload.Table {
+ case "kms.developer":
+ switch payload.Operation {
+ case common.Insert:
+ rows = append(rows, payload.NewRow)
+ res = insertDevelopers(rows, txn)
+
+ case common.Update:
+ res = deleteObject("DEVELOPER", payload.OldRow, txn)
+ rows = append(rows, payload.NewRow)
+ res = insertDevelopers(rows, txn)
+
+ case common.Delete:
+ res = deleteObject("DEVELOPER", payload.OldRow, txn)
+ }
+ case "kms.app":
+ switch payload.Operation {
+ case common.Insert:
+ rows = append(rows, payload.NewRow)
+ res = insertApplications(rows, txn)
+
+ case common.Update:
+ res = deleteObject("APP", payload.OldRow, txn)
+ rows = append(rows, payload.NewRow)
+ res = insertApplications(rows, txn)
+
+ case common.Delete:
+ res = deleteObject("APP", payload.OldRow, txn)
+ }
+
+ case "kms.app_credential":
+ switch payload.Operation {
+ case common.Insert:
+ rows = append(rows, payload.NewRow)
+ res = insertCredentials(rows, txn)
+
+ case common.Update:
+ res = deleteObject("APP_CREDENTIAL", payload.OldRow, txn)
+ rows = append(rows, payload.NewRow)
+ res = insertCredentials(rows, txn)
+
+ case common.Delete:
+ res = deleteObject("APP_CREDENTIAL", payload.OldRow, txn)
+ }
+ case "kms.api_product":
+ switch payload.Operation {
+ case common.Insert:
+ rows = append(rows, payload.NewRow)
+ res = insertAPIproducts(rows, txn)
+
+ case common.Update:
+ res = deleteObject("API_PRODUCT", payload.OldRow, txn)
+ rows = append(rows, payload.NewRow)
+ res = insertAPIproducts(rows, txn)
+
+ case common.Delete:
+ res = deleteObject("API_PRODUCT", payload.OldRow, txn)
+ }
+
+ case "kms.app_credential_apiproduct_mapper":
+ switch payload.Operation {
+ case common.Insert:
+ rows = append(rows, payload.NewRow)
+ res = insertAPIProductMappers(rows, txn)
+
+ case common.Update:
+ res = deleteAPIproductMapper(payload.OldRow, txn)
+ rows = append(rows, payload.NewRow)
+ res = insertAPIProductMappers(rows, txn)
+
+ case common.Delete:
+ res = deleteAPIproductMapper(payload.OldRow, txn)
+ }
+ }
+ if res == false {
+ log.Error("Sql Operation error. Operation rollbacked")
+ return false
+ }
+ }
+ return true
}
/*
- * DELETE CRED
+ * DELETE OBJECT as passed in the input
*/
-func deleteCredential(ele apidApigeeSync.DataPayload, db *sql.DB, org string) bool {
+func deleteObject(object string, ele common.Row, txn *sql.Tx) bool {
- txn, _ := db.Begin()
-
- _, err := txn.Exec("DELETE FROM APP_CREDENTIAL WHERE org=? AND id=?;", org, ele.EntityIdentifier)
-
+ var scope, apiProduct string
+ ssql := "DELETE FROM " + object + " WHERE id = $1 AND _apid_scope = $2"
+ prep, err := txn.Prepare(ssql)
if err != nil {
- log.Error("DELETE CRED Failed: (", ele.EntityIdentifier, org, ")", err)
- txn.Rollback()
+ log.Error("DELETE ", object, " Failed: ", err)
+ return false
+ }
+ defer prep.Close()
+ ele.Get("_apid_scope", &scope)
+ ele.Get("id", &apiProduct)
+
+ _, err = txn.Stmt(prep).Exec(apiProduct, scope)
+ if err != nil {
+ log.Error("DELETE ", object, " Failed: (", apiProduct, ", ", scope, ")", err)
return false
} else {
- log.Info("DELETE CRED Success: (", ele.EntityIdentifier, org, ")")
- txn.Commit()
+ log.Debug("DELETE ", object, " Success: (", apiProduct, ", ", scope, ")")
return true
}
}
/*
- * Helper function to convert string slice in to JSON format
+ * DELETE APIPRDT MAPPER
*/
-func convertSlicetoStringFormat(inpslice []string) string {
+func deleteAPIproductMapper(ele common.Row, txn *sql.Tx) bool {
+ var ApiProduct, AppId, EntityIdentifier, apid_scope string
- bytes, _ := json.Marshal(inpslice)
- return string(bytes)
-}
-
-/*
- * INSERT INTO API product op
- */
-func insertAPIproduct(ele apidApigeeSync.DataPayload, db *sql.DB, org string) bool {
-
- txn, _ := db.Begin()
- restr := convertSlicetoStringFormat(ele.PldCont.Resources)
- envstr := convertSlicetoStringFormat(ele.PldCont.Environments)
-
- _, err := txn.Exec("INSERT INTO API_PRODUCT (org, id, res_names, env) VALUES(?,?,?,?)",
- org,
- ele.PldCont.AppName,
- restr,
- envstr)
-
+ 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;")
if err != nil {
- log.Error("INSERT API_PRODUCT Failed: (", ele.PldCont.AppName, org, ")", err)
- txn.Rollback()
+ log.Error("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err)
return false
- } else {
- log.Info("INSERT API_PRODUCT Success: (", ele.PldCont.AppName, org, ")")
- txn.Commit()
- return true
}
-}
+ defer prep.Close()
-/*
- * INSERT INTO APP op
- */
-func insertCreateApplication(ele apidApigeeSync.DataPayload, db *sql.DB, org string) bool {
+ ele.Get("apiprdt_id", &ApiProduct)
+ ele.Get("app_id", &AppId)
+ ele.Get("appcred_id", &EntityIdentifier)
+ ele.Get("_apid_scope", &apid_scope)
- txn, _ := db.Begin()
-
- _, err := txn.Exec("INSERT INTO APP (org, id, dev_id,cback_url,status, name, app_family, created_at, created_by,updated_at, updated_by) VALUES(?,?,?,?,?,?,?,?,?,?,?);",
- org,
- ele.EntityIdentifier,
- ele.PldCont.DeveloperId,
- ele.PldCont.CallbackUrl,
- ele.PldCont.Status,
- ele.PldCont.AppName,
- ele.PldCont.AppFamily,
- ele.PldCont.CreatedAt,
- ele.PldCont.CreatedBy,
- ele.PldCont.LastModifiedAt,
- ele.PldCont.LastModifiedBy)
-
+ _, err = txn.Stmt(prep).Exec(ApiProduct, AppId, EntityIdentifier, apid_scope)
if err != nil {
- log.Error("INSERT APP Failed: (", ele.EntityIdentifier, org, ")", err)
- txn.Rollback()
+ log.Error("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: (",
+ ApiProduct, ", ",
+ AppId, ", ",
+ EntityIdentifier, ", ",
+ apid_scope,
+ ")",
+ err)
return false
} else {
- log.Info("INSERT APP Success: (", ele.EntityIdentifier, org, ")")
- txn.Commit()
+ log.Debug("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Success: (",
+ ApiProduct, ", ",
+ AppId, ", ",
+ EntityIdentifier, ", ",
+ apid_scope,
+ ")")
return true
}
-
-}
-
-/*
- * INSERT INTO DEVELOPER op
- */
-func insertCreateDeveloper(ele apidApigeeSync.DataPayload, db *sql.DB, org string) bool {
-
- txn, _ := db.Begin()
-
- _, err := txn.Exec("INSERT INTO DEVELOPER (org, email, id, sts, username, firstname, lastname, created_at,created_by, updated_at, updated_by) VALUES(?,?,?,?,?,?,?,?,?,?,?);",
- org,
- ele.PldCont.Email,
- ele.EntityIdentifier,
- ele.PldCont.Status,
- ele.PldCont.UserName,
- ele.PldCont.FirstName,
- ele.PldCont.LastName,
- ele.PldCont.CreatedAt,
- ele.PldCont.CreatedBy,
- ele.PldCont.LastModifiedAt,
- ele.PldCont.LastModifiedBy)
-
- if err != nil {
- log.Error("INSERT DEVELOPER Failed: (", ele.PldCont.UserName, org, ")", err)
- txn.Rollback()
- return false
- } else {
- log.Info("INSERT DEVELOPER Success: (", ele.PldCont.UserName, org, ")")
- txn.Commit()
- return true
- }
-
}
diff --git a/listener_test.go b/listener_test.go
index cb283d7..6a51136 100644
--- a/listener_test.go
+++ b/listener_test.go
@@ -4,105 +4,251 @@
"encoding/json"
"github.com/30x/apid"
. "github.com/30x/apidApigeeSync" // for direct access to Payload types
+ "github.com/apigee-labs/transicator/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
+var count int = 0
var _ = Describe("listener", func() {
It("should store data from ApigeeSync in the database", func(done Done) {
- var event = ChangeSet{}
- event.Changes = []ChangePayload{
+ var event = common.ChangeList{}
+ var event2 = common.ChangeList{}
+
+ /* API Product */
+ srvItems := common.Row{}
+ scv := &common.ColumnVal{
+ Value: "ch_api_product_0",
+ Type: 1,
+ }
+ srvItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "{}",
+ Type: 1,
+ }
+ srvItems["api_resources"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "{Env_0, Env_1}",
+ Type: 1,
+ }
+ srvItems["environments"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_0",
+ Type: 1,
+ }
+ srvItems["tenant_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "test_org0",
+ Type: 1,
+ }
+ srvItems["_apid_scope"] = scv
+
+ /* DEVELOPER */
+ devItems := common.Row{}
+ scv = &common.ColumnVal{
+ Value: "ch_developer_id_0",
+ Type: 1,
+ }
+ devItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Active",
+ Type: 1,
+ }
+ devItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_0",
+ Type: 1,
+ }
+ devItems["tenant_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "test_org0",
+ Type: 1,
+ }
+ devItems["_apid_scope"] = scv
+
+ /* APP */
+ appItems := common.Row{}
+ scv = &common.ColumnVal{
+ Value: "ch_application_id_0",
+ Type: 1,
+ }
+ appItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "ch_developer_id_0",
+ Type: 1,
+ }
+ appItems["developer_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_0",
+ Type: 1,
+ }
+ appItems["tenant_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Approved",
+ Type: 1,
+ }
+ appItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "test_org0",
+ Type: 1,
+ }
+ appItems["_apid_scope"] = scv
+
+ /* CRED */
+ credItems := common.Row{}
+ scv = &common.ColumnVal{
+ Value: "ch_app_credential_0",
+ Type: 1,
+ }
+ credItems["id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "ch_application_id_0",
+ Type: 1,
+ }
+ credItems["app_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "tenant_id_0",
+ Type: 1,
+ }
+ credItems["tenant_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Approved",
+ Type: 1,
+ }
+ credItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "test_org0",
+ Type: 1,
+ }
+ credItems["_apid_scope"] = scv
+
+ /* APP_CRED_APIPRD_MAPPER */
+ mpItems := common.Row{}
+ scv = &common.ColumnVal{
+ Value: "ch_api_product_0",
+ Type: 1,
+ }
+ mpItems["apiprdt_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "ch_application_id_0",
+ Type: 1,
+ }
+ mpItems["app_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "ch_app_credential_0",
+ Type: 1,
+ }
+ mpItems["appcred_id"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "Approved",
+ Type: 1,
+ }
+ mpItems["status"] = scv
+
+ scv = &common.ColumnVal{
+ Value: "test_org0",
+ Type: 1,
+ }
+ mpItems["_apid_scope"] = scv
+
+ event.Changes = []common.Change{
{
- Data: DataPayload{
- EntityType: "apiproduct",
- Operation: "create",
- PldCont: Payload{
- Organization: "test_org",
- AppName: "Api_product_sync",
- Resources: []string{"/**", "/test"},
- Environments: []string{"Env_0", "Env_1"},
- },
- },
+ Table: "kms.api_product",
+ NewRow: srvItems,
+ Operation: 1,
},
{
- Data: DataPayload{
- EntityType: "developer",
- Operation: "create",
- EntityIdentifier: "developer_id_sync",
- PldCont: Payload{
- Organization: "test_org",
- Email: "person_sync@apigee.com",
- Status: "Active",
- UserName: "user_sync",
- FirstName: "user_first_name_sync",
- LastName: "user_last_name_sync",
- },
- },
+ Table: "kms.developer",
+ NewRow: devItems,
+ Operation: 1,
},
{
- Data: DataPayload{
- EntityType: "app",
- Operation: "create",
- EntityIdentifier: "application_id_sync",
- PldCont: Payload{
- Organization: "test_org",
- Email: "person_sync@apigee.com",
- Status: "Approved",
- AppName: "application_id_sync",
- DeveloperId: "developer_id_sync",
- CallbackUrl: "call_back_url",
- },
- },
+ Table: "kms.app",
+ NewRow: appItems,
+ Operation: 1,
},
{
- Data: DataPayload{
- EntityType: "credential",
- Operation: "create",
- EntityIdentifier: "credential_sync",
- PldCont: Payload{
- Organization: "test_org",
- AppId: "application_id_sync",
- Status: "Approved",
- ConsumerSecret: "consumer_secret_sync",
- IssuedAt: 349583485,
- ApiProducts: []Apip{
- {
- ApiProduct: "Api_product_sync",
- Status: "Approved",
- },
- },
- },
- },
+ Table: "kms.app_credential",
+ NewRow: credItems,
+ Operation: 1,
+ },
+ {
+ Table: "kms.app_credential_apiproduct_mapper",
+ NewRow: mpItems,
+ Operation: 1,
},
}
+ event2.Changes = []common.Change{
+ {
+ Table: "kms.api_product",
+ OldRow: srvItems,
+ Operation: 3,
+ },
+ {
+ Table: "kms.developer",
+ OldRow: devItems,
+ Operation: 3,
+ },
+ {
+ Table: "kms.app",
+ OldRow: appItems,
+ Operation: 3,
+ },
+ {
+ Table: "kms.app_credential",
+ OldRow: credItems,
+ Operation: 3,
+ },
+ {
+ Table: "kms.app_credential_apiproduct_mapper",
+ OldRow: mpItems,
+ Operation: 3,
+ },
+ }
h := &test_handler{
- "checkDatabase",
+ "checkDatabase post Insertion",
func(e apid.Event) {
-
// ignore the first event, let standard listener process it
- changeSet := e.(*ChangeSet)
+ changeSet := e.(*common.ChangeList)
if len(changeSet.Changes) > 0 {
return
}
-
- rsp, err := verifyAPIKey("credential_sync", "/test", "Env_0", "test_org", "verify")
+ rsp, err := verifyAPIKey("ch_app_credential_0", "/test", "Env_0", "test_org0", "verify")
Expect(err).ShouldNot(HaveOccurred())
-
var respj kmsResponseSuccess
json.Unmarshal(rsp, &respj)
Expect(respj.Type).Should(Equal("APIKeyContext"))
- Expect(respj.RspInfo.Key).Should(Equal("credential_sync"))
-
+ Expect(respj.RspInfo.Key).Should(Equal("ch_app_credential_0"))
close(done)
},
}
apid.Events().Listen(ApigeeSyncEventSelector, h)
- apid.Events().Emit(ApigeeSyncEventSelector, &event) // for standard listener
- apid.Events().Emit(ApigeeSyncEventSelector, &ChangeSet{}) // for test listener
+ apid.Events().Emit(ApigeeSyncEventSelector, &event)
+ apid.Events().Emit(ApigeeSyncEventSelector, &event2)
+ apid.Events().Emit(ApigeeSyncEventSelector, &event)
+ apid.Events().Emit(ApigeeSyncEventSelector, &common.ChangeList{})
})
})
diff --git a/validate_env.go b/validate_env.go
index df0382d..d90e27c 100644
--- a/validate_env.go
+++ b/validate_env.go
@@ -1,15 +1,16 @@
package apidVerifyApiKey
-import "encoding/json"
+import "strings"
/*
* Ensure the ENV matches.
*/
-func validateEnv(envLocal, envInPath string) bool {
+func validateEnv(envLocal string, envInPath string) bool {
- var ePaths []string
- json.Unmarshal([]byte(envLocal), &ePaths)
- for _, a := range ePaths {
+ s := strings.TrimPrefix(envLocal, "{")
+ s = strings.TrimSuffix(s, "}")
+ fs := strings.Split(s, ",")
+ for _, a := range fs {
if a == envInPath {
return true
}
diff --git a/validate_env_test.go b/validate_env_test.go
index 3942d7a..268f9dd 100644
--- a/validate_env_test.go
+++ b/validate_env_test.go
@@ -8,19 +8,19 @@
var _ = Describe("Validate Env", func() {
It("validation1", func() {
- s := validateEnv("[\"foo\",\"bar\"]", "foo")
+ s := validateEnv("{foo,bar}", "foo")
Expect(s).Should(BeTrue())
})
It("validation2", func() {
- s := validateEnv("[\"foo\",\"bar\"]", "bar")
+ s := validateEnv("{foo,bar}", "bar")
Expect(s).Should(BeTrue())
})
It("validation3", func() {
- s := validateEnv("[\"foo\",\"bar\"]", "xxx")
+ s := validateEnv("{foo,bar}", "xxx")
Expect(s).Should(BeFalse())
})
It("validation4", func() {
- s := validateEnv("[]", "xxx")
+ s := validateEnv("{}", "xxx")
Expect(s).Should(BeFalse())
})
})
diff --git a/validate_path.go b/validate_path.go
index f3cfb99..cd6e021 100644
--- a/validate_path.go
+++ b/validate_path.go
@@ -1,7 +1,6 @@
package apidVerifyApiKey
import (
- "encoding/json"
"regexp"
"strings"
)
@@ -14,9 +13,10 @@
*/
func validatePath(basePath, requestBase string) bool {
- var basePaths []string
- json.Unmarshal([]byte(basePath), &basePaths)
- for _, a := range basePaths {
+ s := strings.TrimPrefix(basePath, "{")
+ s = strings.TrimSuffix(s, "}")
+ fs := strings.Split(s, ",")
+ for _, a := range fs {
str1 := strings.Replace(a, "**", "(.*)", -1)
str2 := strings.Replace(a, "*", "([^/]+)", -1)
if a != str1 {
@@ -39,7 +39,6 @@
* FIXME: SINGLE_FORWARD_SLASH_PATTERN not supported yet
*/
}
-
/* if the i/p resource is empty, no checks need to be made */
- return len(basePaths) == 0
+ return s == ""
}
diff --git a/validate_path_test.go b/validate_path_test.go
index 0b76b07..a5e5e63 100644
--- a/validate_path_test.go
+++ b/validate_path_test.go
@@ -16,79 +16,79 @@
Expect(s).Should(BeTrue())
})
It("validation3", func() {
- s := validatePath("[]", "foo")
+ s := validatePath("{}", "foo")
Expect(s).Should(BeTrue())
})
It("validation4", func() {
- s := validatePath("[\"/**\"]", "/foo")
+ s := validatePath("{/**}", "/foo")
Expect(s).Should(BeTrue())
})
It("validation5", func() {
- s := validatePath("[\"/**\"]", "foo")
+ s := validatePath("{/**}", "foo")
Expect(s).Should(BeFalse())
})
It("validation6", func() {
- s := validatePath("[\"/**\"]", "/")
+ s := validatePath("{/**}", "/")
Expect(s).Should(BeTrue())
})
It("validation7", func() {
- s := validatePath("[\"/foo/**\"]", "/")
+ s := validatePath("{/foo/**}", "/")
Expect(s).Should(BeFalse())
})
It("validation8", func() {
- s := validatePath("[\"/foo/**\"]", "/foo/")
+ s := validatePath("{/foo/**}", "/foo/")
Expect(s).Should(BeTrue())
})
It("validation9", func() {
- s := validatePath("[\"/foo/**\"]", "/foo/bar")
+ s := validatePath("{/foo/**}", "/foo/bar")
Expect(s).Should(BeTrue())
})
It("validation10", func() {
- s := validatePath("[\"/foo/**\"]", "foo")
+ s := validatePath("{/foo/**}", "foo")
Expect(s).Should(BeFalse())
})
It("validation11", func() {
- s := validatePath("[\"/foo/bar/**\"]", "/foo/bar/xx/yy")
+ s := validatePath("{/foo/bar/**}", "/foo/bar/xx/yy")
Expect(s).Should(BeTrue())
})
It("validation12", func() {
- s := validatePath("[\"/foo/bar/*\"]", "/foo/bar/xxx")
+ s := validatePath("/foo/bar/*}", "/foo/bar/xxx")
Expect(s).Should(BeTrue())
})
It("validation13", func() {
- s := validatePath("[\"/foo/bar/*/\"]", "/foo/bar/xxx")
+ s := validatePath("{/foo/bar/*/}", "/foo/bar/xxx")
Expect(s).Should(BeFalse())
})
It("validation14", func() {
- s := validatePath("[\"/foo/bar/**\"]", "/foo/bar/xx/yy")
+ s := validatePath("{/foo/bar/**}", "/foo/bar/xx/yy")
Expect(s).Should(BeTrue())
})
It("validation15", func() {
- s := validatePath("[\"/foo/*/**/\"]", "/foo/bar")
+ s := validatePath("{/foo/*/**/}", "/foo/bar")
Expect(s).Should(BeFalse())
})
It("validation16", func() {
- s := validatePath("[\"/foo/bar/*/xxx\"]", "/foo/bar/yyy/xxx")
+ s := validatePath("{/foo/bar/*/xxx}", "/foo/bar/yyy/xxx")
Expect(s).Should(BeTrue())
})
It("validation17", func() {
- s := validatePath("[\"/foo/bar/*/xxx/\"]", "/foo/bar/yyy/xxx")
+ s := validatePath("{/foo/bar/*/xxx/}", "/foo/bar/yyy/xxx")
Expect(s).Should(BeFalse())
})
It("validation18", func() {
- s := validatePath("[\"/foo/bar/**/xxx/\"]", "/foo/bar/aaa/bbb/xxx/")
+ s := validatePath("{/foo/bar/**/xxx/}", "/foo/bar/aaa/bbb/xxx/")
Expect(s).Should(BeTrue())
})
It("validation19", func() {
- s := validatePath("[\"/foo/bar/***/xxx/\"]", "/foo/bar/aaa/bbb/xxx/")
+ s := validatePath("{/foo/bar/***/xxx/}", "/foo/bar/aaa/bbb/xxx/")
Expect(s).Should(BeTrue())
})
It("validation20", func() {
- s := validatePath("[\"/foo/\", \"/bar/\"]", "/foo/")
+ s := validatePath("{/foo/, /bar/}", "/foo/")
Expect(s).Should(BeTrue())
})
It("validation21", func() {
- s := validatePath("[\"/foo/bar/yy*/xxx\"]", "/foo/bar/yyy/xxx")
+ s := validatePath("{/foo/bar/yy*/xxx}", "/foo/bar/yyy/xxx")
Expect(s).Should(BeTrue())
})
})