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

Add runtime configuration validation #8912

Merged
merged 1 commit into from
Apr 1, 2021
Merged
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
18 changes: 18 additions & 0 deletions envoy/datadog_checks/envoy/config_models/__init__.py
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
172 changes: 172 additions & 0 deletions envoy/datadog_checks/envoy/config_models/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# (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_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_cache_metrics(field, value):
return True


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


def instance_empty_default_hostname(field, value):
return False


def instance_excluded_metrics(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_included_metrics(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_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)
97 changes: 97 additions & 0 deletions envoy/datadog_checks/envoy/config_models/instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# (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, 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

auth_token: Optional[AuthToken]
auth_type: Optional[str]
aws_host: Optional[str]
aws_region: Optional[str]
aws_service: Optional[str]
cache_metrics: Optional[bool]
connect_timeout: Optional[float]
empty_default_hostname: Optional[bool]
excluded_metrics: Optional[Sequence[str]]
extra_headers: Optional[Mapping[str, Any]]
headers: Optional[Mapping[str, Any]]
included_metrics: Optional[Sequence[str]]
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]
service: Optional[str]
skip_proxy: Optional[bool]
stats_url: str
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))
54 changes: 54 additions & 0 deletions envoy/datadog_checks/envoy/config_models/shared.py
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))
3 changes: 3 additions & 0 deletions envoy/datadog_checks/envoy/config_models/validators.py
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)