This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
146 lines (124 loc) · 4.74 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"encoding/json"
"code.cloudfoundry.org/credhub-cli/credhub"
"code.cloudfoundry.org/lager"
provideriface "github.com/alphagov/paas-service-broker-base/provider"
"github.com/pivotal-cf/brokerapi"
)
type CredHubProvider struct {
prefix string
credhubClient *credhub.CredHub
logger lager.Logger
}
func NewCredHubProvider(prefix string, credhubClient *credhub.CredHub, logger lager.Logger) *CredHubProvider {
logger = logger.Session("credhub-provider", lager.Data{"prefix": prefix})
return &CredHubProvider{prefix, credhubClient, logger}
}
func (p *CredHubProvider) Provision(ctx context.Context, provisionData provideriface.ProvisionData) (dashboardURL, operationData string, isAsync bool, err error) {
serviceInstance := NewServiceInstance(
provisionData.InstanceID,
provisionData.Details.SpaceGUID,
provisionData.Details.OrganizationGUID,
)
path := serviceInstance.Path(p.prefix)
if err := ensureNoExistingPermissions(path, p.credhubClient); err != nil {
return "", "", false, err
}
var secrets map[string]interface{}
err = json.Unmarshal(provisionData.Details.RawParameters, &secrets)
if err != nil {
return "", "", false, err
}
if err = writeJSONCredential(path, secrets, p.credhubClient); err != nil {
return "", "", false, err
}
serviceInstance.UpdateSecretsList(secrets)
err = serviceInstance.Save(p.prefix, p.credhubClient)
return "", "", true, err
}
func (p *CredHubProvider) Deprovision(ctx context.Context, deprovisionData provideriface.DeprovisionData) (operationData string, isAsync bool, err error) {
serviceInstance, err := LoadServiceInstance(deprovisionData.InstanceID, p.prefix, p.credhubClient)
if err != nil {
return "", false, err
}
path := serviceInstance.Path(p.prefix)
if err := deletePermissions(path, p.credhubClient); err != nil {
return "", false, err
}
if err := deleteCredential(path, p.credhubClient); err != nil {
return "", false, err
}
err = serviceInstance.Remove(p.prefix, p.credhubClient)
return "", false, err
}
func (p *CredHubProvider) Update(ctx context.Context, updateData provideriface.UpdateData) (operationData string, isAsync bool, err error) {
serviceInstance, err := LoadServiceInstance(updateData.InstanceID, p.prefix, p.credhubClient)
if err != nil {
return "", false, err
}
var secrets map[string]interface{}
err = json.Unmarshal(updateData.Details.RawParameters, &secrets)
if err != nil {
return "", false, err
}
path := serviceInstance.Path(p.prefix)
if err = writeJSONCredential(path, secrets, p.credhubClient); err != nil {
return "", false, err
}
serviceInstance.UpdateSecretsList(secrets)
err = serviceInstance.Save(p.prefix, p.credhubClient)
return "", true, err
}
func (p *CredHubProvider) Bind(ctx context.Context, bindData provideriface.BindData) (binding brokerapi.Binding, err error) {
serviceInstance, err := LoadServiceInstance(bindData.InstanceID, p.prefix, p.credhubClient)
if err != nil {
return brokerapi.Binding{}, err
}
appBinding := BoundApp{
BindingGUID: bindData.BindingID,
AppGUID: bindData.Details.AppGUID,
SpaceGUID: bindData.Details.BindResource.SpaceGuid,
}
actor := appBinding.ActorName()
path := serviceInstance.Path(p.prefix)
if err = grantReadAccess(actor, path, p.credhubClient); err != nil {
return brokerapi.Binding{}, err
}
if serviceInstance.AppBindings == nil {
serviceInstance.AppBindings = map[string]BoundApp{}
}
serviceInstance.AppBindings[appBinding.BindingGUID] = appBinding
err = serviceInstance.Save(p.prefix, p.credhubClient)
return brokerapi.Binding{
Credentials: map[string]string{
"credhub-ref": path,
},
}, err
}
func (p *CredHubProvider) Unbind(ctx context.Context, unbindData provideriface.UnbindData) (unbinding brokerapi.UnbindSpec, err error) {
serviceInstance, err := LoadServiceInstance(unbindData.InstanceID, p.prefix, p.credhubClient)
if err != nil {
return brokerapi.UnbindSpec{}, err
}
appBinding, ok := serviceInstance.AppBindings[unbindData.BindingID]
if !ok {
return brokerapi.UnbindSpec{}, nil
}
actor := appBinding.ActorName()
path := serviceInstance.Path(p.prefix)
if err = revokeReadAccess(actor, path, p.credhubClient); err != nil {
return brokerapi.UnbindSpec{}, err
}
delete(serviceInstance.AppBindings, appBinding.BindingGUID)
err = serviceInstance.Save(p.prefix, p.credhubClient)
return brokerapi.UnbindSpec{}, nil
}
func (p *CredHubProvider) LastOperation(ctx context.Context, lastOperationData provideriface.LastOperationData) (state brokerapi.LastOperationState, description string, err error) {
serviceInstance, err := LoadServiceInstance(lastOperationData.InstanceID, p.prefix, p.credhubClient)
if err != nil {
return brokerapi.Failed, "", err
}
return brokerapi.Succeeded, serviceInstance.DescriptionForUsers(), nil
}