From 432f14274b2c39c4fcb6b8090fce8d8634621b61 Mon Sep 17 00:00:00 2001 From: Johannes Dillmann Date: Thu, 21 Sep 2023 19:10:44 +0200 Subject: [PATCH] Convert nested binding credentials to JSON (#256) Signed-off-by: Johannes Dillmann Signed-off-by: Pavel Busko Co-authored-by: Pavel Busko --- platform.go | 30 ++++++++-- platform_test.go | 37 ++++++++++-- testdata/vcap_services.json | 110 ++++++++++++++++++++++-------------- 3 files changed, 124 insertions(+), 53 deletions(-) diff --git a/platform.go b/platform.go index 6d5177b..a9c09c5 100644 --- a/platform.go +++ b/platform.go @@ -177,9 +177,22 @@ func NewBindingsFromPath(path string) (Bindings, error) { } type vcapServicesBinding struct { - Name string `json:"name"` - Label string `json:"label"` - Credentials map[string]string `json:"credentials"` + Name string `json:"name"` + Label string `json:"label"` + Credentials map[string]interface{} `json:"credentials"` +} + +func toJSONString(input interface{}) (string, error) { + switch in := input.(type) { + case string: + return in, nil + default: + jsonProperty, err := json.Marshal(in) + if err != nil { + return "", err + } + return string(jsonProperty), nil + } } // NewBindingsFromVcapServicesEnv creates a new instance from all the bindings given from the VCAP_SERVICES. @@ -188,17 +201,24 @@ func NewBindingsFromVcapServicesEnv(content string) (Bindings, error) { err := json.Unmarshal([]byte(content), &contentTyped) if err != nil { - return Bindings{}, nil + return Bindings{}, err } bindings := Bindings{} for p, bArray := range contentTyped { for _, b := range bArray { + secret := map[string]string{} + for k, v := range b.Credentials { + secret[k], err = toJSONString(v) + if err != nil { + return nil, err + } + } bindings = append(bindings, Binding{ Name: b.Name, Type: b.Label, Provider: p, - Secret: b.Credentials, + Secret: secret, }) } } diff --git a/platform_test.go b/platform_test.go index 17e09b8..f3a2b60 100644 --- a/platform_test.go +++ b/platform_test.go @@ -55,11 +55,38 @@ func testPlatform(t *testing.T, context spec.G, it spec.S) { bindings, err := libcnb.NewBindings("") Expect(err).NotTo(HaveOccurred()) - Expect(bindings).To(HaveLen(2)) - types := []string{bindings[0].Type, bindings[1].Type} - Expect(types).To(ContainElements("elephantsql-type", "sendgrid-type")) - providers := []string{bindings[0].Provider, bindings[1].Provider} - Expect(providers).To(ContainElements("elephantsql-provider", "sendgrid-provider")) + Expect(bindings).To(ConsistOf(libcnb.Bindings{ + { + Name: "elephantsql-binding-c6c60", + Type: "elephantsql-type", + Provider: "elephantsql-provider", + Secret: map[string]string{ + "bool": "true", + "int": "1", + "uri": "postgres://exampleuser:examplepass@postgres.example.com:5432/exampleuser", + }, + }, + { + Name: "mysendgrid", + Type: "sendgrid-type", + Provider: "sendgrid-provider", + Secret: map[string]string{ + "username": "QvsXMbJ3rK", + "password": "HCHMOYluTv", + "hostname": "smtp.example.com", + }, + }, + { + Name: "postgres", + Type: "postgres", + Provider: "postgres", + Secret: map[string]string{ + "urls": "{\"example\":\"http://example.com\"}", + "username": "foo", + "password": "bar", + }, + }, + })) }) it("creates empty bindings from empty VCAP_SERVICES", func() { diff --git a/testdata/vcap_services.json b/testdata/vcap_services.json index eda3a60..9fb4c5c 100644 --- a/testdata/vcap_services.json +++ b/testdata/vcap_services.json @@ -1,44 +1,68 @@ { - "elephantsql-provider": [ - { - "name": "elephantsql-binding-c6c60", - "binding_guid": "44ceb72f-100b-4f50-87a2-7809c8b42b8d", - "binding_name": "elephantsql-binding-c6c60", - "instance_guid": "391308e8-8586-4c42-b464-c7831aa2ad22", - "instance_name": "elephantsql-c6c60", - "label": "elephantsql-type", - "tags": [ - "postgres", - "postgresql", - "relational" - ], - "plan": "turtle", - "credentials": { - "uri": "postgres://exampleuser:examplepass@postgres.example.com:5432/exampleuser" - }, - "syslog_drain_url": null, - "volume_mounts": [] - } - ], - "sendgrid-provider": [ - { - "name": "mysendgrid", - "binding_guid": "6533b1b6-7916-488d-b286-ca33d3fa0081", - "binding_name": null, - "instance_guid": "8c907d0f-ec0f-44e4-87cf-e23c9ba3925d", - "instance_name": "mysendgrid", - "label": "sendgrid-type", - "tags": [ - "smtp" - ], - "plan": "free", - "credentials": { - "hostname": "smtp.example.com", - "username": "QvsXMbJ3rK", - "password": "HCHMOYluTv" - }, - "syslog_drain_url": null, - "volume_mounts": [] - } - ] -} \ No newline at end of file + "elephantsql-provider": [ + { + "name": "elephantsql-binding-c6c60", + "binding_guid": "44ceb72f-100b-4f50-87a2-7809c8b42b8d", + "binding_name": "elephantsql-binding-c6c60", + "instance_guid": "391308e8-8586-4c42-b464-c7831aa2ad22", + "instance_name": "elephantsql-c6c60", + "label": "elephantsql-type", + "tags": [ + "postgres", + "postgresql", + "relational" + ], + "plan": "turtle", + "credentials": { + "uri": "postgres://exampleuser:examplepass@postgres.example.com:5432/exampleuser", + "int": 1, + "bool": true + }, + "syslog_drain_url": null, + "volume_mounts": [] + } + ], + "sendgrid-provider": [ + { + "name": "mysendgrid", + "binding_guid": "6533b1b6-7916-488d-b286-ca33d3fa0081", + "binding_name": null, + "instance_guid": "8c907d0f-ec0f-44e4-87cf-e23c9ba3925d", + "instance_name": "mysendgrid", + "label": "sendgrid-type", + "tags": [ + "smtp" + ], + "plan": "free", + "credentials": { + "hostname": "smtp.example.com", + "username": "QvsXMbJ3rK", + "password": "HCHMOYluTv" + }, + "syslog_drain_url": null, + "volume_mounts": [] + } + ], + "postgres": [ + { + "name": "postgres", + "label": "postgres", + "plan": "default", + "tags": [ + "postgres" + ], + "binding_guid": "6533b1b6-7916-488d-b286-ca33d3fa0081", + "binding_name": null, + "instance_guid": "8c907d0f-ec0f-44e4-87cf-e23c9ba3925d", + "credentials": { + "username": "foo", + "password": "bar", + "urls": { + "example": "http://example.com" + } + }, + "syslog_drain_url": null, + "volume_mounts": [] + } + ] +}