Skip to content

Commit 8b3eac0

Browse files
committed
be able to use secrets for credentials
1 parent 4a99186 commit 8b3eac0

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ openshift-deploy: openshift-undeploy
4444
oc adm policy add-cluster-role-to-user cluster-reader system:serviceaccount:openshift-infra:hawkular-openshift-agent
4545
oc create -f deploy/openshift/hawkular-openshift-agent-configmap.yaml -n openshift-infra
4646
oc process -f deploy/openshift/hawkular-openshift-agent.yaml | oc create -n openshift-infra -f -
47+
oc adm policy add-cluster-role-to-user hawkular-openshift-agent system:serviceaccount:openshift-infra:hawkular-openshift-agent
4748

4849
openshift-undeploy:
4950
@echo Undeploying the Agent from OpenShift
5051
oc delete all,secrets,sa,templates,configmaps,daemonsets --selector=metrics-infra=agent -n openshift-infra
52+
oc delete clusterroles hawkular-openshift-agent
5153

5254
install:
5355
@echo Installing...

deploy/openshift/hawkular-openshift-agent.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ parameters:
1010
name: IMAGE_VERSION
1111
value: dev
1212
objects:
13+
- apiVersion: v1
14+
kind: ClusterRole
15+
metadata:
16+
name: hawkular-openshift-agent
17+
labels:
18+
metrics-infra: agent
19+
rules:
20+
- apiGroups:
21+
- ""
22+
resources:
23+
- secrets
24+
verbs:
25+
- 'get'
1326
- apiVersion: v1
1427
kind: ServiceAccount
1528
metadata:

k8s/node_event_consumer.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/hawkular/hawkular-openshift-agent/collector"
2828
"github.com/hawkular/hawkular-openshift-agent/collector/manager"
2929
"github.com/hawkular/hawkular-openshift-agent/config"
30+
"github.com/hawkular/hawkular-openshift-agent/config/security"
3031
"github.com/hawkular/hawkular-openshift-agent/log"
3132
"github.com/hawkular/hawkular-openshift-agent/util/expand"
3233
)
@@ -195,13 +196,19 @@ func (nec *NodeEventConsumer) startCollecting(ne *NodeEvent) {
195196
endpointTenant = os.Expand(nec.Config.Kubernetes.Tenant, mappingFunc)
196197
}
197198

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+
198205
// We need to convert the k8s endpoint to the generic endpoint struct.
199206
newEndpoint := &collector.Endpoint{
200207
URL: url.String(),
201208
Type: cmeEndpoint.Type,
202209
Enabled: cmeEndpoint.Enabled,
203210
Tenant: endpointTenant,
204-
Credentials: cmeEndpoint.Credentials,
211+
Credentials: endpointCredentials,
205212
Collection_Interval_Secs: cmeEndpoint.Collection_Interval_Secs,
206213
Metrics: cmeEndpoint.Metrics,
207214
Tags: cmeEndpoint.Tags,
@@ -248,6 +255,47 @@ func (nec *NodeEventConsumer) stopCollecting(ne *NodeEvent) {
248255
}
249256
}
250257

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+
251299
func getIdForEndpoint(p *Pod, e K8SEndpoint) (id string, err error) {
252300
url, err := e.GetUrl(p.PodIP)
253301
if err != nil {

0 commit comments

Comments
 (0)