@@ -33,18 +33,6 @@ import (
33
33
"github.com/crossplane/crossplane-runtime/pkg/resource"
34
34
)
35
35
36
- // Error strings.
37
- const (
38
- errConnectStore = "cannot connect to secret store"
39
- errWriteStore = "cannot write to secret store"
40
- errReadStore = "cannot read from secret store"
41
- errDeleteFromStore = "cannot delete from secret store"
42
- errGetStoreConfig = "cannot get store config"
43
- errSecretConflict = "cannot establish control of existing connection secret"
44
-
45
- errFmtNotOwnedBy = "existing secret is not owned by UID %q"
46
- )
47
-
48
36
// StoreBuilderFn is a function that builds and returns a Store with a given
49
37
// store config.
50
38
type StoreBuilderFn func (ctx context.Context , local client.Client , tcfg * tls.Config , cfg v1.SecretStoreConfig ) (Store , error )
@@ -110,11 +98,11 @@ func (m *DetailsManager) PublishConnection(ctx context.Context, so resource.Conn
110
98
111
99
ss , err := m .connectStore (ctx , p )
112
100
if err != nil {
113
- return false , errors .Wrap (err , errConnectStore )
101
+ return false , errors .Wrap (err , "cannot connect to secret store" )
114
102
}
115
103
116
104
changed , err := ss .WriteKeyValues (ctx , store .NewSecret (so , store .KeyValues (conn )), SecretToWriteMustBeOwnedBy (so ))
117
- return changed , errors .Wrap (err , errWriteStore )
105
+ return changed , errors .Wrap (err , "cannot write to secret store" )
118
106
}
119
107
120
108
// UnpublishConnection deletes connection details secret to the configured
@@ -128,10 +116,10 @@ func (m *DetailsManager) UnpublishConnection(ctx context.Context, so resource.Co
128
116
129
117
ss , err := m .connectStore (ctx , p )
130
118
if err != nil {
131
- return errors .Wrap (err , errConnectStore )
119
+ return errors .Wrap (err , "cannot connect to secret store" )
132
120
}
133
121
134
- return errors .Wrap (ss .DeleteKeyValues (ctx , store .NewSecret (so , store .KeyValues (conn )), SecretToDeleteMustBeOwnedBy (so )), errDeleteFromStore )
122
+ return errors .Wrap (ss .DeleteKeyValues (ctx , store .NewSecret (so , store .KeyValues (conn )), SecretToDeleteMustBeOwnedBy (so )), "cannot delete from secret store" )
135
123
}
136
124
137
125
// FetchConnection fetches connection details of a given ConnectionSecretOwner.
@@ -144,11 +132,11 @@ func (m *DetailsManager) FetchConnection(ctx context.Context, so resource.Connec
144
132
145
133
ss , err := m .connectStore (ctx , p )
146
134
if err != nil {
147
- return nil , errors .Wrap (err , errConnectStore )
135
+ return nil , errors .Wrap (err , "cannot connect to secret store" )
148
136
}
149
137
150
138
s := & store.Secret {}
151
- return managed .ConnectionDetails (s .Data ), errors .Wrap (ss .ReadKeyValues (ctx , store.ScopedName {Name : p .Name , Scope : so .GetNamespace ()}, s ), errReadStore )
139
+ return managed .ConnectionDetails (s .Data ), errors .Wrap (ss .ReadKeyValues (ctx , store.ScopedName {Name : p .Name , Scope : so .GetNamespace ()}, s ), "cannot read from secret store" )
152
140
}
153
141
154
142
// PropagateConnection propagate connection details from one resource to another.
@@ -160,37 +148,37 @@ func (m *DetailsManager) PropagateConnection(ctx context.Context, to resource.Lo
160
148
161
149
ssFrom , err := m .connectStore (ctx , from .GetPublishConnectionDetailsTo ())
162
150
if err != nil {
163
- return false , errors .Wrap (err , errConnectStore )
151
+ return false , errors .Wrap (err , "cannot connect to secret store" )
164
152
}
165
153
166
154
sFrom := & store.Secret {}
167
155
if err = ssFrom .ReadKeyValues (ctx , store.ScopedName {
168
156
Name : from .GetPublishConnectionDetailsTo ().Name ,
169
157
Scope : from .GetNamespace (),
170
158
}, sFrom ); err != nil {
171
- return false , errors .Wrap (err , errReadStore )
159
+ return false , errors .Wrap (err , "cannot read from secret store" )
172
160
}
173
161
174
162
// Make sure 'from' is the controller of the connection secret it references
175
163
// before we propagate it. This ensures a resource cannot use Crossplane to
176
164
// circumvent RBAC by propagating a secret it does not own.
177
165
if sFrom .GetOwner () != string (from .GetUID ()) {
178
- return false , errors .New (errSecretConflict )
166
+ return false , errors .New ("cannot establish control of existing connection secret" )
179
167
}
180
168
181
169
ssTo , err := m .connectStore (ctx , to .GetPublishConnectionDetailsTo ())
182
170
if err != nil {
183
- return false , errors .Wrap (err , errConnectStore )
171
+ return false , errors .Wrap (err , "cannot connect to secret store" )
184
172
}
185
173
186
174
changed , err := ssTo .WriteKeyValues (ctx , store .NewSecret (to , sFrom .Data ), SecretToWriteMustBeOwnedBy (to ))
187
- return changed , errors .Wrap (err , errWriteStore )
175
+ return changed , errors .Wrap (err , "cannot write to secret store" )
188
176
}
189
177
190
178
func (m * DetailsManager ) connectStore (ctx context.Context , p * v1.PublishConnectionDetailsTo ) (Store , error ) {
191
179
sc := m .newConfig ()
192
180
if err := m .client .Get (ctx , types.NamespacedName {Name : p .SecretStoreConfigRef .Name }, sc ); err != nil {
193
- return nil , errors .Wrap (err , errGetStoreConfig )
181
+ return nil , errors .Wrap (err , "cannot get store config" )
194
182
}
195
183
196
184
return m .storeBuilder (ctx , m .client , m .tcfg , sc .GetStoreConfig ())
@@ -214,7 +202,7 @@ func SecretToDeleteMustBeOwnedBy(so metav1.Object) store.DeleteOption {
214
202
215
203
func secretMustBeOwnedBy (so metav1.Object , secret * store.Secret ) error {
216
204
if secret .Metadata == nil || secret .Metadata .GetOwnerUID () != string (so .GetUID ()) {
217
- return errors .Errorf (errFmtNotOwnedBy , string (so .GetUID ()))
205
+ return errors .Errorf ("existing secret is not woned by UID %q" , string (so .GetUID ()))
218
206
}
219
207
return nil
220
208
}
0 commit comments