@@ -27,6 +27,7 @@ import (
27
27
"github.com/hawkular/hawkular-openshift-agent/collector"
28
28
"github.com/hawkular/hawkular-openshift-agent/collector/manager"
29
29
"github.com/hawkular/hawkular-openshift-agent/config"
30
+ "github.com/hawkular/hawkular-openshift-agent/config/security"
30
31
"github.com/hawkular/hawkular-openshift-agent/log"
31
32
"github.com/hawkular/hawkular-openshift-agent/util/expand"
32
33
)
@@ -195,13 +196,19 @@ func (nec *NodeEventConsumer) startCollecting(ne *NodeEvent) {
195
196
endpointTenant = os .Expand (nec .Config .Kubernetes .Tenant , mappingFunc )
196
197
}
197
198
199
+ endpointCredentials , err := nec .determineCredentials (ne .Pod , cmeEndpoint .Credentials )
200
+ if err != nil {
201
+ glog .Warningf ("Will not start collecting for endpoint in pod [%v] - cannot determine credentials. err=%v" , ne .Pod .GetIdentifier (), err )
202
+ continue
203
+ }
204
+
198
205
// We need to convert the k8s endpoint to the generic endpoint struct.
199
206
newEndpoint := & collector.Endpoint {
200
207
URL : url .String (),
201
208
Type : cmeEndpoint .Type ,
202
209
Enabled : cmeEndpoint .Enabled ,
203
210
Tenant : endpointTenant ,
204
- Credentials : cmeEndpoint . Credentials ,
211
+ Credentials : endpointCredentials ,
205
212
Collection_Interval_Secs : cmeEndpoint .Collection_Interval_Secs ,
206
213
Metrics : cmeEndpoint .Metrics ,
207
214
Tags : cmeEndpoint .Tags ,
@@ -248,6 +255,47 @@ func (nec *NodeEventConsumer) stopCollecting(ne *NodeEvent) {
248
255
}
249
256
}
250
257
258
+ // determineCredentials will build a Credentials object that contains the credentials needed to
259
+ // communicate with the endpoint.
260
+ func (nec * NodeEventConsumer ) determineCredentials (p * Pod , cmeCredentials security.Credentials ) (creds security.Credentials , err error ) {
261
+ // function that will extract a credential string based on its value.
262
+ // If the string is prefixed with "secret:" it is assumed to be a key/value from a k8s secret.
263
+ // If the string is not prefixed, it is used as-is.
264
+ f := func (v string ) string {
265
+ if strings .HasPrefix (v , "secret:" ) {
266
+ v = strings .TrimLeft (v , "secret:" )
267
+ pair := strings .SplitN (v , "/" , 2 )
268
+ if len (pair ) != 2 {
269
+ err = fmt .Errorf ("Secret credentials are invalid for pod [%v]" , p .GetIdentifier ())
270
+ return ""
271
+ }
272
+ secret , e := nec .Discovery .Client .Secrets (p .Namespace .Name ).Get (pair [0 ])
273
+ if e != nil {
274
+ err = fmt .Errorf ("There is no secret named [%v] - credentials are invalid for pod [%v]. err=%v" ,
275
+ pair [0 ], p .GetIdentifier (), e )
276
+ return ""
277
+ }
278
+ secretValue , ok := secret .Data [pair [1 ]]
279
+ if ! ok {
280
+ err = fmt .Errorf ("There is no key named [%v] in secret named [%v] - credentials are invalid for pod [%v]" ,
281
+ pair [1 ], pair [0 ], p .GetIdentifier ())
282
+ return ""
283
+ }
284
+ return string (secretValue )
285
+ } else {
286
+ return v
287
+ }
288
+ }
289
+
290
+ creds = security.Credentials {
291
+ Username : f (cmeCredentials .Username ),
292
+ Password : f (cmeCredentials .Password ),
293
+ Token : f (cmeCredentials .Token ),
294
+ }
295
+
296
+ return
297
+ }
298
+
251
299
func getIdForEndpoint (p * Pod , e K8SEndpoint ) (id string , err error ) {
252
300
url , err := e .GetUrl (p .PodIP )
253
301
if err != nil {
0 commit comments