Repository navigation
Conversation
| // 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"` |
There was a problem hiding this comment.
Naming for FilesystemAccessThroughServiceMonitors is up for discussion. This is merely to make the pull request itself easier to understand.
brancz
left a comment
There was a problem hiding this comment.
just a first pass, generally the strategy looks good to me
| mon.Name, ep.BearerTokenSecret.Name, mon.Namespace, | ||
| ) | ||
| } | ||
| secrets[fmt.Sprintf("serviceMonitor/%s/%s/%d", mon.Namespace, mon.Name, i)] = BasicAuthCredentials{username: "", password: token} |
There was a problem hiding this comment.
reusing BasicAuthCredentials here seems a little odd, maybe having a second map here would be more suitable?
There was a problem hiding this comment.
Right. I am moving it into a separate function to differentiate between basic auth and bearer tokens.
|
|
||
| // If denied by Prometheus spec, filter out all service monitors that access | ||
| // the file system. | ||
| if p.Spec.FilesystemAccessThroughServiceMonitors.Deny { |
There was a problem hiding this comment.
this should likely be recorded in the ServiceMonitor's status (I realize this doesn't exist today)
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
as a follow up this will need to be pushed to a public repo
40e4db4 to
a0c2372
Compare
fba6479 to
ae70c4c
Compare
a4ba3fd to
6223a6d
Compare
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.
fa39215 to
4b41b62
Compare
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 |
There was a problem hiding this comment.
I have not yet figured out how to tell our generators to ignore this.
|
@metalmatze @brancz @paulfantom @squat @s-urbaniak does one of you have some time for another review for this pull request? |
|
@mxinden this just cosmetic, doesn't have any impact on the change itself. |
|
@elisiano thanks for proof reading. This originates from our dependency on https://github.com/kubernetes/kubernetes/blob/7a8e11c16d131a92e3d7c3847a33a4d29522b3ef/staging/src/k8s.io/api/core/v1/types.go#L1097. Would you like to propose a patch on https://github.com/kubernetes/kubernetes? |
|
@mxinden okay I will bring it up there, thanks. |
| Interval: "30s", | ||
| Scheme: "https", | ||
| TLSConfig: &monitoringv1.TLSConfig{ | ||
| InsecureSkipVerify: true, |
There was a problem hiding this comment.
Is this still validating authorities?
There was a problem hiding this comment.
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?
|
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", |
There was a problem hiding this comment.
this might be good to put into a const? Not sure if referenced multiple times.
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
This might be a good candidate for a const too 😊
There was a problem hiding this comment.
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 namebar, key namebaz=>foo_xy_bar_baz - namespace
foo, secret namexy_bar, key namebaz=>foo_xy_bar_baz
There was a problem hiding this comment.
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.
| if p.Spec.ArbitraryFSAccessThroughSMs.Deny { | ||
| for namespaceAndName, sm := range res { | ||
| for _, endpoint := range sm.Spec.Endpoints { | ||
| if endpoint.BearerTokenFile != "" { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That is a good idea. I moved it into a separate function also deduplicating the logging calls.
| if endpoint.BearerTokenFile != "" { | ||
| delete(res, namespaceAndName) | ||
| level.Warn(c.logger).Log( | ||
| "msg", fmt.Sprintf( |
There was a problem hiding this comment.
Having printf in structured logging is an "anti pattern". Mabye we can rephrase and give the ServiceMonitor name as a key value pair
|
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. |
Reference secrets instead of local files (cont. #2524)
|
Closing this as we already implemented it with #2716. |
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:
Instead of referencing files on the Prometheus container file system (e.g.
Bearer tokens, TLS configs, ...), have users reference Kubernetes secrets in
service monitors.
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.