Skip to content

be able to use secrets for credentials #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ docker-examples:

openshift-deploy: openshift-undeploy
@echo Deploying Components to OpenShift
oc adm policy add-cluster-role-to-user cluster-reader system:serviceaccount:openshift-infra:hawkular-openshift-agent
oc create -f deploy/openshift/hawkular-openshift-agent-configmap.yaml -n openshift-infra
oc process -f deploy/openshift/hawkular-openshift-agent.yaml | oc create -n openshift-infra -f -
oc adm policy add-cluster-role-to-user hawkular-openshift-agent system:serviceaccount:openshift-infra:hawkular-openshift-agent

openshift-undeploy:
@echo Undeploying the Agent from OpenShift
oc delete all,secrets,sa,templates,configmaps,daemonsets --selector=metrics-infra=agent -n openshift-infra
oc delete all,secrets,sa,templates,configmaps,daemonsets,clusterroles --selector=metrics-infra=agent -n openshift-infra
oc delete clusterroles hawkular-openshift-agent
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line that deletes the cluster role can be deleted once this is fixed: openshift/origin#12450
There is already a PR that fixes it, just wait for it to be merged: openshift/origin#12461


install:
@echo Installing...
Expand Down
25 changes: 25 additions & 0 deletions deploy/openshift/hawkular-openshift-agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ parameters:
name: IMAGE_VERSION
value: dev
objects:
- apiVersion: v1
kind: ClusterRole
metadata:
name: hawkular-openshift-agent
labels:
metrics-infra: agent
rules:
- apiGroups:
- ""
resources:
- configmaps
- namespaces
- nodes
- pods
- projects
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down
51 changes: 50 additions & 1 deletion k8s/node_event_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hawkular/hawkular-openshift-agent/collector"
"github.com/hawkular/hawkular-openshift-agent/collector/manager"
"github.com/hawkular/hawkular-openshift-agent/config"
"github.com/hawkular/hawkular-openshift-agent/config/security"
"github.com/hawkular/hawkular-openshift-agent/log"
"github.com/hawkular/hawkular-openshift-agent/util/expand"
)
Expand Down Expand Up @@ -200,13 +201,19 @@ func (nec *NodeEventConsumer) startCollecting(ne *NodeEvent) {
endpointTenant = os.Expand(nec.Config.Kubernetes.Tenant, mappingFunc)
}

endpointCredentials, err := nec.determineCredentials(ne.Pod, cmeEndpoint.Credentials)
if err != nil {
glog.Warningf("Will not start collecting for endpoint in pod [%v] - cannot determine credentials. err=%v", ne.Pod.GetIdentifier(), err)
continue
}

// We need to convert the k8s endpoint to the generic endpoint struct.
newEndpoint := &collector.Endpoint{
URL: url.String(),
Type: cmeEndpoint.Type,
Enabled: cmeEndpoint.Enabled,
Tenant: endpointTenant,
Credentials: cmeEndpoint.Credentials,
Credentials: endpointCredentials,
Collection_Interval_Secs: cmeEndpoint.Collection_Interval_Secs,
Metrics: cmeEndpoint.Metrics,
Tags: cmeEndpoint.Tags,
Expand Down Expand Up @@ -253,6 +260,48 @@ func (nec *NodeEventConsumer) stopCollecting(ne *NodeEvent) {
}
}

// determineCredentials will build a Credentials object that contains the credentials needed to
// communicate with the endpoint.
func (nec *NodeEventConsumer) determineCredentials(p *Pod, cmeCredentials security.Credentials) (creds security.Credentials, err error) {
// function that will extract a credential string based on its value.
// If the string is prefixed with "secret:" it is assumed to be a key/value from a k8s secret.
// If the string is not prefixed, it is used as-is.
f := func(v string) string {
if strings.HasPrefix(v, "secret:") {
v = strings.TrimLeft(v, "secret:")
pair := strings.SplitN(v, "/", 2)
if len(pair) != 2 {
err = fmt.Errorf("Secret credentials are invalid for pod [%v]", p.GetIdentifier())
return ""
}
secret, e := nec.Discovery.Client.Secrets(p.Namespace.Name).Get(pair[0])
if e != nil {
err = fmt.Errorf("There is no secret named [%v] - credentials are invalid for pod [%v]. err=%v",
pair[0], p.GetIdentifier(), e)
return ""
}
secretValue, ok := secret.Data[pair[1]]
if !ok {
err = fmt.Errorf("There is no key named [%v] in secret named [%v] - credentials are invalid for pod [%v]",
pair[1], pair[0], p.GetIdentifier())
return ""
}
log.Debugf("Credentials obtained from secret [%v/%v] for pod [%v]", pair[0], pair[1], p.GetIdentifier())
return string(secretValue)
} else {
return v
}
}

creds = security.Credentials{
Username: f(cmeCredentials.Username),
Password: f(cmeCredentials.Password),
Token: f(cmeCredentials.Token),
}

return
}

func getIdForEndpoint(p *Pod, e K8SEndpoint) (id string, err error) {
url, err := e.GetUrl(p.PodIP)
if err != nil {
Expand Down