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

✨ Add TLS serving option for metrics #993

Closed

Conversation

Danil-Grigorev
Copy link
Member

@Danil-Grigorev Danil-Grigorev commented Jun 11, 2020

This PR introduces new option to the manager to pass a TLS certificate and key in a separate directory, which will be used to serve metrics endpoint over https://

  • MetricsCertDir is a location where the certificate files would be located. Those should be named tls.crt and tls.key. Non empty value in the variable enables secure metrics endpoint.

Fixes #979

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 11, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @Danil-Grigorev!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @Danil-Grigorev. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 11, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Danil-Grigorev
To complete the pull request process, please assign joelanford
You can assign the PR to them by writing /assign @joelanford in a comment when ready.

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

@k8s-ci-robot k8s-ci-robot requested review from alexeldeib and droot June 11, 2020 12:33
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 11, 2020
@Danil-Grigorev
Copy link
Member Author

/ok-to-test

@k8s-ci-robot
Copy link
Contributor

@Danil-Grigorev: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/ok-to-test

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.

@JoelSpeed
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 11, 2020
Comment on lines 42 to 90
const testCrt = `-----BEGIN CERTIFICATE-----
MIIC8zCCAdugAwIBAgIJAOBRXEjnBd8DMA0GCSqGSIb3DQEBBQUAMCQxIjAgBgNV
BAMTGWh0dHBzbG9jYWxob3N0NDQ0M21ldHJpY3MwHhcNMjAwNjExMTEzMDI3WhcN
MzAwNjA5MTEzMDI3WjAkMSIwIAYDVQQDExlodHRwc2xvY2FsaG9zdDQ0NDNtZXRy
aWNzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxY6bVSLkfdHlI9kl
4nAYTXCEdqEjVyrBQpwNeOkQ+E2hX2T28CBUPgWcHfD2wD85rVRh26Cs1ACifpIS
SJI5nSswBqkA+whzV1uIofcjbxGFlVBfeOEQnbuXpHb60ZzgCVH5/9XIlw5NiR7V
wfDzm9ZlNZzhCI8dGYFObop3fUfunmublU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++fZ
RR1wT84WmV94lDOC4AKHtCQXx4o9m4GVwFcNIzdtexxbdBYPY9FPpZlR/S/VC9Ef
73rEV46bie8TMDd0CmHBSpiDHDqgPyNwaYpttuoVFGNxw59/8OdeJQM=
-----END RSA PRIVATE KEY-----
`

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could generate a new key and certificate on the fly rather than storing one? I don't know how complex that would be, but I suspect this will flag up those tools that scan repositories for secrets as it is know

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved it to testdata directory, should be little more sane. Can't really see a good solution to generate it on the go, and judging from the examples, it should be ok: https://golang.org/src/crypto/tls/example_test.go

pkg/metrics/listener.go Outdated Show resolved Hide resolved
pkg/metrics/listener.go Outdated Show resolved Hide resolved
pkg/manager/manager.go Outdated Show resolved Hide resolved
pkg/manager/manager_test.go Outdated Show resolved Hide resolved
@Danil-Grigorev Danil-Grigorev force-pushed the secure-metrics branch 3 times, most recently from e8b2eaf to be3e6dd Compare June 11, 2020 20:07
@Danil-Grigorev Danil-Grigorev requested a review from JoelSpeed June 11, 2020 20:19
Copy link
Contributor

@djzager djzager left a comment

Choose a reason for hiding this comment

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

One super nitpick. Other than that this looks good to me.

@@ -31,8 +31,8 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"sigs.k8s.io/controller-runtime/pkg/internal/certwatcher"
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that imports are intentionally grouped together such that all sigs.k8s.io/controller-runtime would be separate.

@Danil-Grigorev Danil-Grigorev force-pushed the secure-metrics branch 2 times, most recently from e6c854e to 12f3eb3 Compare June 16, 2020 08:58
@Danil-Grigorev Danil-Grigorev requested a review from djzager June 16, 2020 08:58
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

Had another look, added a few more minor things

@@ -233,6 +235,24 @@ var _ = Describe("manger.Manager", func() {
Expect(listener.Close()).ToNot(HaveOccurred())
})

It("should create a TLS listener for the metrics if a valid address is provided", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps rename this one, as it creates a normal listener if a valid address is provided, we need certs to create the TLS listener, WDYT?


// NewSecureListener creates a new TCP listener over TLS bound to the given address.
func NewSecureListener(addr, certDir string) (net.Listener, error) {
ln, err := NewListener(addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

This error is unchecked

Suggested change
ln, err := NewListener(addr)
ln, err := NewListener(addr)
if err != nil {
return nil, err
}

}

go func() {
if err = certWatcher.Start(context.Background().Done()); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm normally not a fan of shadowing, but in this situation I'd be tempted to use a different variable since it's within a go routine, WDYT?

Suggested change
if err = certWatcher.Start(context.Background().Done()); err != nil {
if err := certWatcher.Start(context.Background().Done()); err != nil {

@Danil-Grigorev Danil-Grigorev force-pushed the secure-metrics branch 2 times, most recently from a0366e0 to 5c909a0 Compare June 16, 2020 09:24
@Danil-Grigorev Danil-Grigorev requested a review from JoelSpeed June 16, 2020 09:27
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

/lgtm

@@ -233,6 +235,24 @@ var _ = Describe("manger.Manager", func() {
Expect(listener.Close()).ToNot(HaveOccurred())
})

It("should create a TLS listener for the metrics if a certificates are provided and the address is correct", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit

Suggested change
It("should create a TLS listener for the metrics if a certificates are provided and the address is correct", func() {
It("should create a TLS listener for the metrics if certificates are provided and the address is correct", func() {

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 16, 2020
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 16, 2020
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 16, 2020
Copy link
Contributor

@djzager djzager left a comment

Choose a reason for hiding this comment

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

/lgtm

@joelanford
Copy link
Member

@JoelSpeed @Danil-Grigorev Instead of forcing callers into always having to get certs on disk in their image (albeit not that hard), WDYT of instead exposing a MetricsTLSConfig *tls.Config field to the manager Options struct?

The default nil value would result in the current behavior, but if it was non-nil, we would create a listener using tls.NewListener.

@joelanford
Copy link
Member

/hold

For above discussion.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 16, 2020
@DirectXMan12
Copy link
Contributor

Hey, so we generally wanted to sidestep this by using kube-rbac-proxy. I'm open to rediscussion, but the historical decision we made was largely to just let sidecar containers (rbac proxy, service mesh, etc) handle that.

@Danil-Grigorev
Copy link
Member Author

Closing in favour of #1007 to be exposed for the user modification. This PR introduces sample implementation for that on https://github.com/kubernetes-sigs/controller-runtime/pull/993/files#diff-652ee3c14d4f011345992fadfb31853eR66-R92

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for TLS listener on metrics
6 participants