Skip to content

Commit

Permalink
Merge pull request Aiven-Open#554 from aiven/jjaakola-aiven-return-40…
Browse files Browse the repository at this point in the history
…4-for-unknown-subject-in-config-endpoints

fix: return 404 for config actions on unknown subjects
  • Loading branch information
tvainika authored Mar 2, 2023
2 parents 59f20aa + f0406b1 commit 88bee3a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
10 changes: 7 additions & 3 deletions karapace/in_memory_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ def insert_subject(self, *, subject: Subject) -> None:
self.subjects.setdefault(subject, SubjectData())

def get_subject_compatibility(self, *, subject: Subject) -> Optional[str]:
return self.subjects[subject].compatibility
if subject in self.subjects:
return self.subjects[subject].compatibility
return None

def delete_subject_compatibility(self, *, subject: Subject) -> None:
self.subjects[subject].compatibility = None
if subject in self.subjects:
self.subjects[subject].compatibility = None

def set_subject_compatibility(self, *, subject: Subject, compatibility: str) -> None:
self.subjects[subject].compatibility = compatibility
if subject in self.subjects:
self.subjects[subject].compatibility = compatibility

def find_schema(self, *, schema_id: SchemaId) -> Optional[TypedSchema]:
return self.schemas[schema_id]
Expand Down
11 changes: 10 additions & 1 deletion karapace/schema_registry_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,17 @@ async def config_subject_get(

# Config for a subject can exist without schemas so no need to check for their existence
assert self.schema_registry.schema_reader, "KarapaceSchemaRegistry not initialized. Missing call to _init"
compatibility = self.schema_registry.database.get_subject_compatibility(subject=subject)
if self.schema_registry.database.find_subject(subject=subject) is None:
self.r(
body={
"error_code": SchemaErrorCodes.SUBJECT_NOT_FOUND.value,
"message": SchemaErrorMessages.SUBJECT_NOT_FOUND_FMT.value.format(subject=subject),
},
content_type=content_type,
status=HTTPStatus.NOT_FOUND,
)

compatibility = self.schema_registry.database.get_subject_compatibility(subject=subject)
default_to_global = request.query.get("defaultToGlobal", "false").lower() == "true"
if not compatibility and default_to_global:
compatibility = self.schema_registry.compatibility
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,19 @@ async def test_schema_version_number_existing_schema(registry_async_client: Clie
assert schema_id_3 > schema_id_2


@pytest.mark.parametrize("trail", ["", "/"])
async def test_get_config_unknown_subject(registry_async_client: Client, trail: str) -> None:
res = await registry_async_client.get(f"config/unknown-subject{trail}")
assert res.status_code == 404, f"{res} - Should return 404 for unknown subject"

# Set global config, see that unknown subject is still returns correct 404 and does not fallback to global config
res = await registry_async_client.put(f"config{trail}", json={"compatibility": "FULL"})
assert res.status_code == 200

res = await registry_async_client.get(f"config/unknown-subject{trail}")
assert res.status_code == 404, f"{res} - Should return 404 for unknown subject also when global config set"


@pytest.mark.parametrize("trail", ["", "/"])
async def test_config(registry_async_client: Client, trail: str) -> None:
subject_name_factory = create_subject_name_factory(f"test_config-{trail}")
Expand Down

0 comments on commit 88bee3a

Please sign in to comment.