Skip to content

[WIP] pkg/prometheus: Reference secrets instead of local files - #2524

Closed
mxinden wants to merge 8 commits into
prometheus-operator:masterfrom
mxinden:bearer-secret
Closed

mxinden wants to merge 8 commits into
prometheus-operator:masterfrom
mxinden:bearer-secret

Conversation

@mxinden

@mxinden mxinden commented Apr 1, 2019 •

Copy link
Copy Markdown
Contributor

A service monitor can specify a BearerTokenFile in an endpoint referencing a
concrete file on the Promethus container file system. A malicious user could
create a service monitor selecting arbitrary secret files in the Prometheus
container. Those secrets would then be send with a scrape request by Prometheus
to a malicious target.

To prevent the above possible exploit I suggest the following:

  1. Instead of referencing files on the Prometheus container file system (e.g.
    Bearer tokens, TLS configs, ...), have users reference Kubernetes secrets in
    service monitors.

  2. Add an option to the Prometheus custom resource to disable all file
    references through service monitors.


With this pull request I would like to open up a discussion, it is in a
work-in-progress state. What do you all think of the suggestion above?

To do:

  • Add option to disable file reference from service monitors accessing files
    on the Prometheus container file system.

  • Add option to configure BearerToken in service monitors via secret
    references next to current file references.

  • Add option to configure TLS in service monitors via secret references next
    to current file references.

Comment thread pkg/apis/monitoring/v1/types.go Outdated
// FilesystemAccessThroughServiceMonitors configures whether configuration
// based on a service monitor can access assets on the file system of the
// Prometheus container e.g. bearer tokens.
FilesystemAccessThroughServiceMonitors FilesystemAccessThroughServiceMonitorsConfig `json:"filesystemAccessThroughServiceMonitors"`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Naming for FilesystemAccessThroughServiceMonitors is up for discussion. This is merely to make the pull request itself easier to understand.

@brancz brancz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just a first pass, generally the strategy looks good to me

Comment thread pkg/prometheus/operator.go Outdated
mon.Name, ep.BearerTokenSecret.Name, mon.Namespace,
)
}
secrets[fmt.Sprintf("serviceMonitor/%s/%s/%d", mon.Namespace, mon.Name, i)] = BasicAuthCredentials{username: "", password: token}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reusing BasicAuthCredentials here seems a little odd, maybe having a second map here would be more suitable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right. I am moving it into a separate function to differentiate between basic auth and bearer tokens.

Comment thread pkg/prometheus/operator.go Outdated

// If denied by Prometheus spec, filter out all service monitors that access
// the file system.
if p.Spec.FilesystemAccessThroughServiceMonitors.Deny {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should likely be recorded in the ServiceMonitor's status (I realize this doesn't exist today)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reporting status back into a service monitor seems to be not specific to this pull request. Thus I suggest achieving the above in a separate pull request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds good to me.

containers:
- name: example-app
image: quay.io/coreos/basic-auth-test-app:0.1.0
image: quay.io/mxinden/basic-auth-test-app:0.2.0-bearer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as a follow up this will need to be pushed to a public repo

@mxinden
mxinden force-pushed the bearer-secret branch 4 times, most recently from 40e4db4 to a0c2372 Compare April 10, 2019 14:51
@mxinden
mxinden force-pushed the bearer-secret branch 3 times, most recently from fba6479 to ae70c4c Compare April 26, 2019 16:48
@brancz brancz mentioned this pull request Apr 29, 2019
@mxinden
mxinden force-pushed the bearer-secret branch 2 times, most recently from a4ba3fd to 6223a6d Compare May 2, 2019 15:42
mxinden added 2 commits May 3, 2019 10:28
To configure a bearer token users could only specify a file path in the
service monitor, pointing to a bearer token file in the Prometheus
container. This enables hostile users, being able to configure a service
monitor and controlling the scrape target, to retrieve arbitrary files
in the Prometheus container.

In cases where users can not be trusted, this patch adds an option to
disallow the above file path specification and replaces it by a secret
reference. This secret has to be in the same namespace as the service
monitor, shrinking the attack vector.
… monitors

ArbitraryFSAccessThroughSMsConfig enables users to configure, whether
a service monitor selected by the Prometheus instance is allowed to use
arbitrary files on the file system of the Prometheus container. This is
the case when e.g. a service monitor specifies a BearerTokenFile in an
endpoint. A malicious user could create a service monitor
selecting arbitrary secret files in the Prometheus container. Those
secrets would then be send with a scrape request by Prometheus to a
malicious target. Denying the above would prevent the attack, users can
instead use the BearerTokenSecret field.
@mxinden
mxinden force-pushed the bearer-secret branch 2 times, most recently from fa39215 to 4b41b62 Compare May 3, 2019 10:34
mxinden added 2 commits May 3, 2019 19:49
Before retrieving TLS assets from Kubernetes secrets for a given service
monitor, make sure the user did not specify both file and secret
reference, e.g. both `CAFile` and `CASecret`.
Given that the basic-auth-test-app not only supports basic auth, but
also bearer token as well as tls authentication, this patch renames the
app to a more generic name.

// TLSConfigValidationError is returned by TLSConfig.Validate() on semantically
// invalid tls configurations.
// +k8s:openapi-gen=false

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have not yet figured out how to tell our generators to ignore this.

@mxinden

mxinden commented May 3, 2019

Copy link
Copy Markdown
Contributor Author

@metalmatze @brancz @paulfantom @squat @s-urbaniak does one of you have some time for another review for this pull request?

@elisiano

elisiano commented May 3, 2019

Copy link
Copy Markdown
Contributor

@mxinden this just cosmetic, doesn't have any impact on the change itself.
I see there are several description strings like: "...Secret or it's key...". I'm wondering if that should be "...Secret or its key..." (without apostrophe)

@mxinden

mxinden commented May 6, 2019

Copy link
Copy Markdown
Contributor Author

@elisiano

elisiano commented May 6, 2019

Copy link
Copy Markdown
Contributor

@mxinden okay I will bring it up there, thanks.

Interval: "30s",
Scheme: "https",
TLSConfig: &monitoringv1.TLSConfig{
InsecureSkipVerify: true,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this still validating authorities?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The test does assure, that Prometheus sends properly signed requests to the sample app. It does not ensure, that the responses are properly signed by the sample app.

While the latter check would be a great addition, the test is already valuable, given the first check.

I guess we would need to generate certificates on the fly to ensure, that the domain name in the certificates is correct. Any idea how we could get around this?

@metalmatze

Copy link
Copy Markdown
Member

What happens if keys inside Secrets change? We need some way to reconcile or at least fail?!

The Prometheus custom resource has the option to disable arbitrary
filesystem access configured through service monitors. This commit adds
an end-to-end test for this option in combination with the TLS
configuration via files or secret references in service monitors.
token, err := getCredFromSecret(
sClient,
ep.BearerTokenSecret,
"bearertoken",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this might be good to put into a const? Not sure if referenced multiple times.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As far as I can tell, getCredFromSecret is using this string only ever to log properly. What do you think of instead of passing it in, adding the info later on by wrapping the error? That should make the function call a lot easier on many call sites.


// TODO: Namespacing via underscores seems rather hacky.
assets[fmt.Sprintf(
"%v_%v_%v",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This might be a good candidate for a const too 😊

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right. Maybe we can even combine this with promcfg.go/addTLStoYaml to keep the two in sync. Do you have a good alternative to the hacky _ namespacing? That might turn into a security concern given that the following scenarios both return the same path:

  • namespace foo_xy, secret name bar, key name baz => foo_xy_bar_baz
  • namespace foo, secret name xy_bar, key name baz => foo_xy_bar_baz

@elisiano elisiano Jun 3, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As the docs suggest:

Note that the name of your namespace must be a DNS compatible label.

Same applies for secrets names.

Given this limitation I think it should be OK to leave the underscore as delimiter (given there cannot be ns/secret objects containing them). The keys could though.

$ kubectl create ns some_ns
The Namespace "some_ns" is invalid: metadata.name: Invalid value: "some_ns": a DNS-1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')

$ kubectl create secret generic some_secret --from-literal=foo=bar
The Secret "some_secret" is invalid: metadata.name: Invalid value: "some_secret": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')

I think it should be safe for usage in this case.

Comment thread pkg/prometheus/operator.go Outdated
if p.Spec.ArbitraryFSAccessThroughSMs.Deny {
for namespaceAndName, sm := range res {
for _, endpoint := range sm.Spec.Endpoints {
if endpoint.BearerTokenFile != "" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe it would become more readable if we would move everything within this inner for loop into a separate func where we could use early returns instead of continue. No hard feelings though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is a good idea. I moved it into a separate function also deduplicating the logging calls.

Comment thread pkg/prometheus/operator.go Outdated
if endpoint.BearerTokenFile != "" {
delete(res, namespaceAndName)
level.Warn(c.logger).Log(
"msg", fmt.Sprintf(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Having printf in structured logging is an "anti pattern". Mabye we can rephrase and give the ServiceMonitor name as a key value pair

@spinus

spinus commented Jun 24, 2019

Copy link
Copy Markdown

that would be so useful! I'm not very good in go but could help testing, let me know if I can help pushing that.

s-urbaniak added a commit that referenced this pull request Oct 9, 2019
Reference secrets instead of local files (cont. #2524)
@paulfantom

Copy link
Copy Markdown
Member

Closing this as we already implemented it with #2716.

@paulfantom paulfantom closed this Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants