Skip to content

Commit

Permalink
Add runtime configuration validation (DataDog#8896)
Browse files Browse the repository at this point in the history
* Sync config models

* re-sync

* re-sync

* Update tox.ini

* try
  • Loading branch information
ofek authored Nov 8, 2021
1 parent 1cd174e commit 5740da0
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from .instance import InstanceConfig
from .shared import SharedConfig


class ConfigMixin:
_config_model_instance: InstanceConfig
_config_model_shared: SharedConfig

@property
def config(self) -> InstanceConfig:
return self._config_model_instance

@property
def shared_config(self) -> SharedConfig:
return self._config_model_shared
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from datadog_checks.base.utils.models.fields import get_default_field_value


def shared_proxy(field, value):
return get_default_field_value(field, value)


def shared_service(field, value):
return get_default_field_value(field, value)


def shared_skip_proxy(field, value):
return False


def shared_timeout(field, value):
return 10


def instance_allow_redirects(field, value):
return True


def instance_auth_token(field, value):
return get_default_field_value(field, value)


def instance_auth_type(field, value):
return 'basic'


def instance_aws_host(field, value):
return get_default_field_value(field, value)


def instance_aws_region(field, value):
return get_default_field_value(field, value)


def instance_aws_service(field, value):
return get_default_field_value(field, value)


def instance_connect_timeout(field, value):
return get_default_field_value(field, value)


def instance_disable_generic_tags(field, value):
return False


def instance_empty_default_hostname(field, value):
return False


def instance_event_filter(field, value):
return get_default_field_value(field, value)


def instance_extra_headers(field, value):
return get_default_field_value(field, value)


def instance_headers(field, value):
return get_default_field_value(field, value)


def instance_kerberos_auth(field, value):
return 'disabled'


def instance_kerberos_cache(field, value):
return get_default_field_value(field, value)


def instance_kerberos_delegate(field, value):
return False


def instance_kerberos_force_initiate(field, value):
return False


def instance_kerberos_hostname(field, value):
return get_default_field_value(field, value)


def instance_kerberos_keytab(field, value):
return get_default_field_value(field, value)


def instance_kerberos_principal(field, value):
return get_default_field_value(field, value)


def instance_log_requests(field, value):
return False


def instance_min_collection_interval(field, value):
return 15


def instance_ntlm_domain(field, value):
return get_default_field_value(field, value)


def instance_password(field, value):
return get_default_field_value(field, value)


def instance_persist_connections(field, value):
return False


def instance_proxy(field, value):
return get_default_field_value(field, value)


def instance_read_timeout(field, value):
return get_default_field_value(field, value)


def instance_request_size(field, value):
return 16


def instance_results_per_page(field, value):
return 100


def instance_service(field, value):
return get_default_field_value(field, value)


def instance_skip_proxy(field, value):
return False


def instance_tags(field, value):
return get_default_field_value(field, value)


def instance_timeout(field, value):
return 10


def instance_tls_ca_cert(field, value):
return get_default_field_value(field, value)


def instance_tls_cert(field, value):
return get_default_field_value(field, value)


def instance_tls_ignore_warning(field, value):
return False


def instance_tls_private_key(field, value):
return get_default_field_value(field, value)


def instance_tls_use_host_header(field, value):
return False


def instance_tls_verify(field, value):
return True


def instance_use_legacy_auth_encoding(field, value):
return True


def instance_username(field, value):
return get_default_field_value(field, value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import annotations

from typing import Any, Mapping, Optional, Sequence

from pydantic import BaseModel, Field, root_validator, validator

from datadog_checks.base.utils.functions import identity
from datadog_checks.base.utils.models import validation

from . import defaults, validators


class AuthToken(BaseModel):
class Config:
allow_mutation = False

reader: Optional[Mapping[str, Any]]
writer: Optional[Mapping[str, Any]]


class Proxy(BaseModel):
class Config:
allow_mutation = False

http: Optional[str]
https: Optional[str]
no_proxy: Optional[Sequence[str]]


class InstanceConfig(BaseModel):
class Config:
allow_mutation = False

allow_redirects: Optional[bool]
api_url: str
auth_token: Optional[AuthToken]
auth_type: Optional[str]
aws_host: Optional[str]
aws_region: Optional[str]
aws_service: Optional[str]
client_id: str
client_secret: str
connect_timeout: Optional[float]
disable_generic_tags: Optional[bool]
empty_default_hostname: Optional[bool]
event_filter: Optional[Sequence[str]]
extra_headers: Optional[Mapping[str, Any]]
headers: Optional[Mapping[str, Any]]
kerberos_auth: Optional[str]
kerberos_cache: Optional[str]
kerberos_delegate: Optional[bool]
kerberos_force_initiate: Optional[bool]
kerberos_hostname: Optional[str]
kerberos_keytab: Optional[str]
kerberos_principal: Optional[str]
log_requests: Optional[bool]
min_collection_interval: Optional[float]
ntlm_domain: Optional[str]
password: Optional[str]
persist_connections: Optional[bool]
proxy: Optional[Proxy]
read_timeout: Optional[float]
request_size: Optional[float]
results_per_page: Optional[int] = Field(None, le=5000.0)
service: Optional[str]
skip_proxy: Optional[bool]
tags: Optional[Sequence[str]]
timeout: Optional[float]
tls_ca_cert: Optional[str]
tls_cert: Optional[str]
tls_ignore_warning: Optional[bool]
tls_private_key: Optional[str]
tls_use_host_header: Optional[bool]
tls_verify: Optional[bool]
use_legacy_auth_encoding: Optional[bool]
username: Optional[str]

@root_validator(pre=True)
def _initial_validation(cls, values):
return validation.core.initialize_config(getattr(validators, 'initialize_instance', identity)(values))

@validator('*', pre=True, always=True)
def _ensure_defaults(cls, v, field):
if v is not None or field.required:
return v

return getattr(defaults, f'instance_{field.name}')(field, v)

@validator('*')
def _run_validations(cls, v, field):
if not v:
return v

return getattr(validators, f'instance_{field.name}', identity)(v, field=field)

@root_validator(pre=False)
def _final_validation(cls, values):
return validation.core.finalize_config(getattr(validators, 'finalize_instance', identity)(values))
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from __future__ import annotations

from typing import Optional, Sequence

from pydantic import BaseModel, root_validator, validator

from datadog_checks.base.utils.functions import identity
from datadog_checks.base.utils.models import validation

from . import defaults, validators


class Proxy(BaseModel):
class Config:
allow_mutation = False

http: Optional[str]
https: Optional[str]
no_proxy: Optional[Sequence[str]]


class SharedConfig(BaseModel):
class Config:
allow_mutation = False

proxy: Optional[Proxy]
service: Optional[str]
skip_proxy: Optional[bool]
timeout: Optional[float]

@root_validator(pre=True)
def _initial_validation(cls, values):
return validation.core.initialize_config(getattr(validators, 'initialize_shared', identity)(values))

@validator('*', pre=True, always=True)
def _ensure_defaults(cls, v, field):
if v is not None or field.required:
return v

return getattr(defaults, f'shared_{field.name}')(field, v)

@validator('*')
def _run_validations(cls, v, field):
if not v:
return v

return getattr(validators, f'shared_{field.name}', identity)(v, field=field)

@root_validator(pre=False)
def _final_validation(cls, values):
return validation.core.finalize_config(getattr(validators, 'finalize_shared', identity)(values))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# (C) Datadog, Inc. 2021-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
2 changes: 2 additions & 0 deletions cloud_foundry_api/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dd_mypy_args =
--non-interactive
datadog_checks/
tests/
--exclude
'.*/config_models/.*\.py$'
dd_mypy_deps =
types-mock==0.1.5
usedevelop = true
Expand Down

0 comments on commit 5740da0

Please sign in to comment.