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