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

[auth] add an OIDC auth reader #18920

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/18920.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an OIDC auth token reader
42 changes: 41 additions & 1 deletion datadog_checks_base/datadog_checks/base/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from copy import deepcopy
from io import open
from ipaddress import ip_address, ip_network
from urllib.parse import quote, urlparse, urlunparse
from urllib.parse import quote, urlparse, urlunparse, urljoin

import requests
import requests_unixsocket
Expand Down Expand Up @@ -923,6 +923,45 @@ def read(self, **request):

return self._token

class AuthTokenOIDCReader(object):
def __init__(self, config):
_url = config.get('url')
if not isinstance(_url, str):
raise ConfigurationError('The `url` setting of `auth_token` reader must be a string')
elif not _url:
raise ConfigurationError('The `url` setting of `auth_token` reader is required')

_role = config.get('role')
if not isinstance(_role, str):
raise ConfigurationError('The `role` setting of `auth_token` reader must be a string')
elif not _role:
raise ConfigurationError('The `role` setting of `auth_token` reader is required')

self._headers = config.get('headers') or {}
if not isinstance(self._headers, dict):
raise ConfigurationError('The `headers` setting of `auth_token` reader must be a dict')

self._token_url = urljoin(_url+"/", _role)

self._token = None
self._expiration = None

def read(self, **request):
if self._token is None or get_timestamp() >= self._expiration or 'error' in request:
resp = requests.get(self._token_url, headers=self._headers)
resp.raise_for_status()
try:
data = resp.json()['data']
except requests.exceptions.JSONDecodeError:
raise Exception("OIDC reader failed to parse token endpoint response")
except KeyError:
raise Exception("OIDC reader an invalid response")

self._token = data['token']
self._expiration = get_timestamp() + data['ttl']

return self._token


class AuthTokenHeaderWriter(object):
DEFAULT_PLACEHOLDER = '<TOKEN>'
Expand Down Expand Up @@ -958,6 +997,7 @@ def write(self, token, **request):
'file': AuthTokenFileReader,
'oauth': AuthTokenOAuthReader,
'dcos_auth': DCOSAuthTokenReader,
'oidc': AuthTokenOIDCReader,
}
AUTH_TOKEN_WRITERS = {'header': AuthTokenHeaderWriter}

Expand Down
47 changes: 47 additions & 0 deletions datadog_checks_base/tests/base/utils/http/test_authtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,53 @@ def test_basic_auth_not_boolean(self):
):
RequestsWrapper(instance, init_config)

class TestAuthTokenOIDCReaderCreation:
def test_url_missing(self):
instance = {'auth_token': {'reader': {'type': 'oidc'}, 'writer': {'type': 'header'}}}
init_config = {}

with pytest.raises(ConfigurationError, match='^The `url` setting of `auth_token` reader is required$'):
RequestsWrapper(instance, init_config)

def test_url_not_string(self):
instance = {'auth_token': {'reader': {'type': 'oidc', 'url': {}}, 'writer': {'type': 'header'}}}
init_config = {}

with pytest.raises(ConfigurationError, match='^The `url` setting of `auth_token` reader must be a string$'):
RequestsWrapper(instance, init_config)

def test_role_missing(self):
instance = {'auth_token': {'reader': {'type': 'oidc', 'url': 'foo'}, 'writer': {'type': 'header'}}}
init_config = {}

with pytest.raises(ConfigurationError, match='^The `role` setting of `auth_token` reader is required$'):
RequestsWrapper(instance, init_config)

def test_role_not_string(self):
instance = {
'auth_token': {'reader': {'type': 'oauth', 'url': 'foo', 'role': {}}, 'writer': {'type': 'header'}}
}
init_config = {}

with pytest.raises(
ConfigurationError, match='^The `role` setting of `auth_token` reader must be a string$'
):
RequestsWrapper(instance, init_config)

def test_headers_not_string(self):
instance = {
'auth_token': {
'reader': {'type': 'oauth', 'url': 'foo', 'role': 'bar', 'headers': ['header: value']},
'writer': {'type': 'header'},
}
}
init_config = {}

with pytest.raises(
ConfigurationError, match='^The `headers` setting of `auth_token` reader must be a dict$'
):
RequestsWrapper(instance, init_config)


class TestAuthTokenDCOSReaderCreation:
def test_login_url_missing(self):
Expand Down
1 change: 1 addition & 0 deletions http_check/changelog.d/18920.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an OIDC auth token reader
6 changes: 6 additions & 0 deletions http_check/datadog_checks/http_check/data/conf.yaml.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## All options defined here are available to all instances.

Check failure on line 1 in http_check/datadog_checks/http_check/data/conf.yaml.example

View workflow job for this annotation

GitHub Actions / run / Validate

File `conf.yaml.example` is not in sync, run "ddev validate config http_check -s"
#
init_config:

Expand Down Expand Up @@ -393,6 +393,12 @@
## options:
## audience: https://example.com
## scope: read:example
## - type: oidc
## url (required): The full OIDC token provider endpoint
## e.g. http://127.0.0.1:8200/v1/identity/oidc/token/
## role (required): The role against which to generate the token
## headers: Additional headers to include in the request
##
##
## The available writers are:
##
Expand Down
Loading