Skip to content

Commit db53b9e

Browse files
committed
- #5796 silence printing full config when config validation fails
1 parent 74d6c07 commit db53b9e

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

airbyte-cdk/.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"workbench.colorCustomizations": {
3+
"activityBar.background": "#1E128E",
4+
"titleBar.activeBackground": "#2A19C7",
5+
"titleBar.activeForeground": "#FDFDFF"
6+
},
7+
"python.formatting.provider": "black"
8+
}

airbyte-cdk/python/airbyte_cdk/sources/utils/schema_helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def check_config_against_spec_or_exit(config: Mapping[str, Any], spec: Connector
142142
try:
143143
validate(instance=config, schema=spec_schema)
144144
except ValidationError as validation_error:
145-
raise Exception("Config validation error: " + validation_error.message)
145+
logger.error("Config validation error: " + validation_error.message)
146+
raise SystemExit(1) from None
146147

147148

148149
class InternalConfig(BaseModel):
@@ -159,7 +160,8 @@ def split_config(config: Mapping[str, Any]) -> Tuple[dict, InternalConfig]:
159160
Break config map object into 2 instances: first is a dict with user defined
160161
configuration and second is internal config that contains private keys for
161162
acceptance test configuration.
162-
:param config - Dict object that has been loaded from config file.
163+
:param
164+
config - Dict object that has been loaded from config file.
163165
:return tuple of user defined config dict with filtered out internal
164166
parameters and SAT internal config object.
165167
"""

airbyte-cdk/python/unit_tests/sources/utils/test_schema_helpers.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@
2727
import os
2828
import shutil
2929
import sys
30+
import traceback
3031
from collections.abc import Mapping
3132
from pathlib import Path
3233

33-
from airbyte_cdk.sources.utils.schema_helpers import ResourceSchemaLoader
34+
from airbyte_cdk.logger import AirbyteLogger
35+
from airbyte_cdk.models.airbyte_protocol import ConnectorSpecification
36+
from airbyte_cdk.sources.utils.schema_helpers import ResourceSchemaLoader, check_config_against_spec_or_exit
3437
from pytest import fixture
38+
from pytest import raises as pytest_raises
39+
40+
logger = AirbyteLogger()
41+
3542

3643
MODULE = sys.modules[__name__]
3744
MODULE_NAME = MODULE.__name__.split(".")[0]
@@ -53,6 +60,38 @@ def create_schema(name: str, content: Mapping):
5360
f.write(json.dumps(content))
5461

5562

63+
@fixture
64+
def spec_object():
65+
spec = {
66+
"connectionSpecification": {
67+
"$schema": "http://json-schema.org/draft-07/schema#",
68+
"type": "object",
69+
"required": ["api_token"],
70+
"additionalProperties": False,
71+
"properties": {
72+
"api_token": {"title": "API Token", "type": "string"},
73+
},
74+
},
75+
}
76+
yield ConnectorSpecification.parse_obj(spec)
77+
78+
79+
def test_check_config_against_spec_or_exit_does_not_print_schema(capsys, spec_object):
80+
config = {"super_secret_token": "really_a_secret"}
81+
with pytest_raises(SystemExit) as ex_info:
82+
check_config_against_spec_or_exit(config, spec_object, logger)
83+
exc = ex_info.value
84+
traceback.print_exception(type(exc), exc, exc.__traceback__)
85+
out, err = capsys.readouterr()
86+
assert "really_a_secret" not in out + err
87+
88+
89+
def test_should_not_fail_validation_for_valid_config(spec_object):
90+
config = {"api_token": "something"}
91+
check_config_against_spec_or_exit(config, spec_object, logger)
92+
assert True, "should pass validation with valid config"
93+
94+
5695
class TestResourceSchemaLoader:
5796
# Test that a simple schema is loaded correctly
5897
@staticmethod

airbyte-cdk/python/unit_tests/test_entrypoint.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def config_mock(mocker, request):
152152
],
153153
indirect=["config_mock"],
154154
)
155-
def test_config_validate(entrypoint: AirbyteEntrypoint, mocker, config_mock, schema, config_valid):
155+
def test_config_validate(entrypoint: AirbyteEntrypoint, mocker, config_mock, schema, config_valid, capsys):
156156
parsed_args = Namespace(command="check", config="config_path")
157157
check_value = AirbyteConnectionStatus(status=Status.SUCCEEDED)
158158
mocker.patch.object(MockSource, "check", return_value=check_value)
@@ -161,9 +161,10 @@ def test_config_validate(entrypoint: AirbyteEntrypoint, mocker, config_mock, sch
161161
messages = list(entrypoint.run(parsed_args))
162162
assert [_wrap_message(check_value)] == messages
163163
else:
164-
with pytest.raises(Exception) as ex_info:
164+
with pytest.raises(SystemExit):
165165
list(entrypoint.run(parsed_args))
166-
assert "Config validation error:" in str(ex_info.value)
166+
out, err = capsys.readouterr()
167+
assert "Config validation error:" in out + err
167168

168169

169170
def test_run_check(entrypoint: AirbyteEntrypoint, mocker, spec_mock, config_mock):

0 commit comments

Comments
 (0)