Skip to content
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

metrics scraping with local authentication and authorization #701

Merged
merged 1 commit into from
Jul 6, 2021

Conversation

deads2k
Copy link
Contributor

@deads2k deads2k commented Mar 18, 2021

Monitoring needs to be reliable and is the very useful when trying to debug clusters in an already degraded state. We want to ensure that metrics scraping can always work if the scraper can reach the target, even if the kube-apiserver is unavailable or unreachable. To do this, we will combine a local authorizer (already merged in many binaries and the rbac-proxy) and client-cert based authentication to have a fully local authentication and authorization path for scraper targets.

If networking (or part of networking) is down and a scraper target cannot reach the kube-apiserver to verify a token and a subjectaccessreview, then the metrics scraper can be rejected. The subjectaccessreview (authorization) is already largely addressed, but service account tokens are still used for scraping targets. Tokens require an external network call that we can avoid by using client certificates. Gathering metrics, especially client metrics, from partially functionally clusters helps narrow the search area between kube-apiserver, etcd, kubelet, and SDN considerably.

In addition, this will significantly reduce the load on the kube-apiserver. We have observed in the CI cluster that token and subject access reviews are a significant percentage of all kube-apiserver traffic.

/assign @s-urbaniak
/assign @sym3tri

@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please ask for approval from deads2k after the PR has been reviewed.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deads2k
Copy link
Contributor Author

deads2k commented Mar 19, 2021

In just the past week we have seen significant difficulties that would alleviated by this

  1. no metrics when the openstack service load balancer for kubernetes.default.svc is broken makes correlating the connection difficulties hard/impossible
  2. network checkers have no way to report failures during network outages because they cannot validate the metrics scraper to give results and cannot provide results to the kube-apiserver
  3. a significant portion of all kube-apiserver traffic is for token and subject accessreviews, making us more vulnerable to overload and rejection of requests

Given the relatively small scope and risk since, we should prioritize this highly for our coming releases. Monitoring is extremely valuable when things are not behaving, but certain classes of failures have nearly complete monitoring blackouts at this time.


1. The monitoriting operator creates a client cert/key pair valid against the kube-apiserver.
There is a CSR signer name specifically for this.
2. The kube-controller-manager operator runs an approver that allows monitoring operator SA to get signed certificate for
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the approver decision going to be based solely on exact username match (and maybe groups match?) or is there going to be any additional mechanism (like RBAC or something custom)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the approver decision going to be based solely on exact username match (and maybe groups match?) or is there going to be any additional mechanism (like RBAC or something custom)?

I think exact user match makes sense. The service-account for the monitoring operator can create a client cert for the metrics scraper. I think that permission is reasonable and happen to know exactly what identity that is.

4. The monitoring operator keeps these credentials up to date (the signer is only good for 30 days or so).
5. The metrics scraper mounts the client cert/key pair and ensures that it is hot-reloadable or builds a process suicider.
There is library-go code to suicide on file changes.
6. A participating metrics scraper target, can read the in-cluster client cert CA bundle and create an authenticator
Copy link
Contributor

Choose a reason for hiding this comment

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

Specify the location of the CM for readers unfamiliar with it.

Comment on lines +71 to +74
7. A servicemonitor grows a way to specify using this client cert.
Technically this is optional.
If you find it too difficult, just *always* use the client-cert for scraping since the secrets never leave disk.
Those targets which don't support it will simply ignore the client-cert.
Copy link
Contributor

Choose a reason for hiding this comment

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

note: it's a shame the less-secure way is the default one, that means you have to opt-in your service to not receiving to the scraper SA's credentials. I wish we could simply default to the cert.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: it's a shame the less-secure way is the default one, that means you have to opt-in your service to not receiving to the scraper SA's credentials. I wish we could simply default to the cert.

An e2e test trying to find offenders could be used to close gaps. But I don't see that as essential to building out the initial capability.


## Drawbacks

I have to stop here to make dinner.
Copy link
Contributor

Choose a reason for hiding this comment

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

🌮

Copy link
Contributor

Choose a reason for hiding this comment

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

🍕


## Proposal

1. The monitoriting operator creates a client cert/key pair valid against the kube-apiserver.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/monitoriting/monitoring

Copy link
Contributor

Choose a reason for hiding this comment

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

"cluster monitoring" to be nit-picky.

@s-urbaniak
Copy link
Contributor

/cc @openshift/openshift-team-monitoring

and a subjectaccessreview, then the metrics scraper can be rejected.
The subjectaccessreview (authorization) is already largely addressed, but service account tokens are still used
for scraping targets.
Tokens require an external network call that we can avoid by using client certificates.
Copy link
Contributor

@s-urbaniak s-urbaniak Mar 30, 2021

Choose a reason for hiding this comment

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

question: as tokens are signed, wouldn't a local signature check suffice?

Copy link
Contributor

Choose a reason for hiding this comment

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

as an after thought, potentially a signature-only check in addition to scoped tokens would potentially suffice?
At least the advantage would be that client-side no additional cert management would be necessary.

Copy link
Contributor Author

@deads2k deads2k Mar 30, 2021

Choose a reason for hiding this comment

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

question: as tokens are signed, wouldn't a local signature check suffice?

No. In general tokens are not signed. ServiceAccount tokens in particular are, but if you only check locally, then the serviceaccount tokens become irrevocable. It is important to be able to revoke tokens.

Can update the doc.

## Proposal

1. The monitoriting operator creates a client cert/key pair valid against the kube-apiserver.
There is a CSR signer name specifically for this.
Copy link
Contributor

Choose a reason for hiding this comment

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

which one is it specifically? is there an existing one or do we have to implement a new?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which one is it specifically? is there an existing one or do we have to implement a new?

kubernetes.io/kube-apiserver-client from https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/ . Can update the doc.


## Proposal

1. The monitoriting operator creates a client cert/key pair valid against the kube-apiserver.
Copy link
Contributor

Choose a reason for hiding this comment

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

"cluster monitoring" to be nit-picky.

2. The kube-controller-manager operator runs an approver that allows monitoring operator SA to get signed certificate for
The existing scraper identity.
3. The monitoring operator puts the client cert/key pair into a secret.
4. The monitoring operator keeps these credentials up to date (the signer is only good for 30 days or so).
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that the cluster monitoring operator needs to request a new client certificate before it reaches the expiration date, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess that the cluster monitoring operator needs to request a new client certificate before it reaches the expiration date, correct?

Yes. And because we only sign for 30 days or so.

The existing scraper identity.
3. The monitoring operator puts the client cert/key pair into a secret.
4. The monitoring operator keeps these credentials up to date (the signer is only good for 30 days or so).
5. The metrics scraper mounts the client cert/key pair and ensures that it is hot-reloadable or builds a process suicider.
Copy link
Contributor

Choose a reason for hiding this comment

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

Prometheus will automatically reload the certificate/key files if they change.

6. all control plane operators
7. kube-controller-manager
8. etcd
These are the ones I've updated already and we'll be able to get more.
Copy link
Contributor

Choose a reason for hiding this comment

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

All monitoring components (except Prometheus, Alertmanager and Thanos querier) also use kube-rbac-proxy to secure their metrics endpoint. The 3 others got scraped via oauth-proxy (because historical reasons I guess).

6. A participating metrics scraper target, can read the in-cluster client cert CA bundle and create an authenticator
using it.
The default delegated authentication stack does this and on openshift, the configmap is exposed to system:authenticated.
7. A servicemonitor grows a way to specify using this client cert.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what you mean here. The service monitor allows to specify a client certificate/key either by file path in the Prometheus container or reference to a secret's key.

https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md#tlsconfig

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean here. The service monitor allows to specify a client certificate/key either by file path in the Prometheus container or reference to a secret's key.

That appears to be a reference to a secret in the namespace containing the target. If that were the case, then every target would have to have client cert credentials for scanning /metrics. We want to avoid this level of permissions for every component.

Is is possible to specify a client-cert that exists in the metric scraping namespace?

Copy link
Contributor

Choose a reason for hiding this comment

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

The requirement is that the secret exists in the same namespace as the service monitor. The other option today would be to use the certFile and keyFile files but it assumes that the target knows the location of these files in the Prometheus container. It's not worse than what we do now for bearer tokens and CA but it's deprecated and we'll probably remove it eventually.

Copy link
Contributor

Choose a reason for hiding this comment

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

And I'm not saying that we can't evolve things upstream :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other option today would be to use the certFile and keyFile files but it assumes that the target knows the location of these files in the Prometheus container.

This option is more reasonable for the effort we're trying to complete here. Having every namespace with the power to scrape all metrics is not desireable.

Copy link
Contributor

@s-urbaniak s-urbaniak May 19, 2021

Choose a reason for hiding this comment

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

The requirement is that the secret exists in the same namespace as the service monitor.

@simonpasquier i think referencing the secret via reference rather than file would be desirable. Do I understand correctly you think about losening the restriction in prometheus-operator to be able to reference a secret from a different namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The requirement is that the secret exists in the same namespace as the service monitor.

@simonpasquier i think referencing the secret via reference rather than file would be desirable. Do I understand correctly you think about losening the restriction in prometheus-operator to be able to reference a secret from a different namespace?

I think an ideal way would be to say, "send me a client cert that identifies you as the scraper" without being able to specify exactly what. The exact mechanism can be niggled later. For the purposes of OCP, we can even hardcode it as a default since key material never leaves the scraper pod.

@s-urbaniak
Copy link
Contributor

@deads2k i think we reached consensus generally 👍 do you mind to address the nits above so we can merge? I think we can add specifics as a follow-up?

4. The monitoring operator keeps these credentials up to date (the signer is only good for 30 days or so).
5. The metrics scraper mounts the client cert/key pair and ensures that it is hot-reloadable or builds a process suicider.
There is library-go code to suicide on file changes.
6. A participating metrics scraper target, can read the in-cluster client cert CA bundle and create an authenticator
Copy link
Contributor

Choose a reason for hiding this comment

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

is it expected for the participating metrics scraper target to be able to hot-reload the client cert CA as well? (asking, as kube-rbac-proxy doesn't right now)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it expected for the participating metrics scraper target to be able to hot-reload the client cert CA as well? (asking, as kube-rbac-proxy doesn't right now)

Highly recommended. The delegating authenticator provided by kube in k8s.io/apiserver (vendoraable package) does this.

Copy link
Contributor

Choose a reason for hiding this comment

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

i was about to ask if there's things to reuse, thanks!

@s-urbaniak
Copy link
Contributor

@deads2k how about we wrap this enhancement up? i guess we reached consensus generally 🎉

@simonpasquier
Copy link
Contributor

/approve

@deads2k deads2k added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 3, 2021
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jun 3, 2021

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: simonpasquier

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment


1. The monitoriting operator creates a client cert/key pair valid against the kube-apiserver.
There is a CSR signer name specifically for this.
2. The kube-controller-manager operator runs an approver that allows monitoring operator SA to get signed certificate for
Copy link
Contributor

Choose a reason for hiding this comment

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

We concretely ended up to be in the KCM https://github.com/openshift/cluster-policy-controller sidecar.

@s-urbaniak
Copy link
Contributor

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jun 17, 2021
@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

7 similar comments
@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

20 similar comments
@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Jul 6, 2021
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 6, 2021

New changes are detected. LGTM label has been removed.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 6, 2021

@deads2k: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
ci/prow/markdownlint 7888a17 link /test markdownlint

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants