Skip to content

Commit

Permalink
Update the default value of the bearer_token parameter (#10706)
Browse files Browse the repository at this point in the history
to send the bearer token only to secure https endpoints and not to clear text http endpoints.
  • Loading branch information
L3n41c committed Dec 29, 2021
1 parent ebf8c71 commit 82a3e20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,16 @@ def _get_setting(name, default):
# INTERNAL FEATURE, might be removed in future versions
config['_text_filter_blacklist'] = []

# Whether or not to use the service account bearer token for authentication
# if 'bearer_token_path' is not set, we use /var/run/secrets/kubernetes.io/serviceaccount/token
# Whether or not to use the service account bearer token for authentication.
# Can be explicitly set to true or false to send or not the bearer token.
# If set to the `tls_only` value, the bearer token will be sent only to https endpoints.
# If 'bearer_token_path' is not set, we use /var/run/secrets/kubernetes.io/serviceaccount/token
# as a default path to get the token.
config['bearer_token_auth'] = is_affirmative(
instance.get('bearer_token_auth', default_instance.get('bearer_token_auth', False))
)
bearer_token_auth = _get_setting('bearer_token_auth', False)
if bearer_token_auth == 'tls_only':
config['bearer_token_auth'] = config['prometheus_url'].startswith("https://")
else:
config['bearer_token_auth'] = is_affirmative(bearer_token_auth)

# Can be used to get a service account bearer token from files
# other than /var/run/secrets/kubernetes.io/serviceaccount/token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,19 @@ def test_bearer_token_not_found():
}
with pytest.raises(IOError):
OpenMetricsBaseCheck('prometheus_check', {}, {}, [instance])


def test_bearer_token_auto_http():
endpoint = "http://localhost:12345/metrics"
instance = {'prometheus_url': endpoint, 'namespace': 'default_namespace', 'bearer_token_auth': 'tls_only'}
with patch.object(OpenMetricsBaseCheck, 'KUBERNETES_TOKEN_PATH', os.path.join(FIXTURE_PATH, 'default_token')):
check = OpenMetricsBaseCheck('prometheus_check', {}, {}, [instance])
assert check.get_scraper_config(instance)['_bearer_token'] is None


def test_bearer_token_auto_https():
endpoint = "https://localhost:12345/metrics"
instance = {'prometheus_url': endpoint, 'namespace': 'default_namespace', 'bearer_token_auth': 'tls_only'}
with patch.object(OpenMetricsBaseCheck, 'KUBERNETES_TOKEN_PATH', os.path.join(FIXTURE_PATH, 'default_token')):
check = OpenMetricsBaseCheck('prometheus_check', {}, {}, [instance])
assert check.get_scraper_config(instance)['_bearer_token'] == 'my default token'

0 comments on commit 82a3e20

Please sign in to comment.