Skip to content

Commit

Permalink
Convert nested binding credentials to JSON
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Dillmann <[email protected]>
Signed-off-by: Johannes Dillmann <[email protected]>
  • Loading branch information
pbusko and modulo11 committed Sep 13, 2023
1 parent 59903af commit 0f1a809
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 48 deletions.
30 changes: 25 additions & 5 deletions platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func testPlatform(t *testing.T, context spec.G, it spec.S) {
bindings, err := libcnb.NewBindings("")
Expect(err).NotTo(HaveOccurred())

Expect(bindings).To(HaveLen(2))
Expect(bindings).To(HaveLen(3))
types := []string{bindings[0].Type, bindings[1].Type}
Expect(types).To(ContainElements("elephantsql-type", "sendgrid-type"))
providers := []string{bindings[0].Provider, bindings[1].Provider}
Expand Down
108 changes: 66 additions & 42 deletions testdata/vcap_services.json
Original file line number Diff line number Diff line change
@@ -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:[email protected]: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": []
}
]
"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:[email protected]: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": []
}
]
}

0 comments on commit 0f1a809

Please sign in to comment.