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

Prevent adding empty renamed attribute #1322

Merged
merged 1 commit into from
Sep 10, 2024
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
23 changes: 23 additions & 0 deletions src/OpenConext/EngineBlock/Service/ReleaseAsEnforcer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ public function enforce(array $attributes, array $releaseAsOverrides)
{
foreach ($releaseAsOverrides as $oldAttributeName => $overrideValue) {
$newAttributeName = $overrideValue[0]['release_as'];
if (!array_key_exists($oldAttributeName, $attributes)) {
$this->logger->notice(
sprintf(
'Releasing "%s" as "%s" is not possible, "%s" is not in assertion',
$oldAttributeName,
$newAttributeName,
$oldAttributeName
)
);
continue;
}
if (is_null($attributes[$oldAttributeName])) {
mharte-ib marked this conversation as resolved.
Show resolved Hide resolved
$this->logger->warning(
sprintf(
'Releasing "%s" as "%s" is not possible, value for "%s" is null',
$oldAttributeName,
$newAttributeName,
$oldAttributeName
)
);
unset($attributes[$oldAttributeName]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thijskh in this testcase; edupersontargetedId is in the assertion but is set to be an empty array as value.

The enforcer now tests if the value is empty, and pops it out of the assertion. Is that expected behavior for Engine? I think, prior to this change. EB would allow this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. The problem only occurs when the attribute is completely unset. So I would limit the solution to that only.

continue;
}
$attributeValue = $attributes[$oldAttributeName];
unset($attributes[$oldAttributeName]);
$this->logger->notice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,20 @@ public function testEnforce($attributes, $releaseAsOverrides, $expectedResult, $

foreach ($releaseAsOverrides as $oldName => $override) {
$this->assertArrayNotHasKey($oldName, $result);
$this->assertArrayHasKey($override[0]['release_as'], $result);
}
}


/**
* @dataProvider enforceDataProviderWarnings
*/
public function testEnforceImpossible($attributes, $releaseAsOverrides, $expectedResult, $expectedLogMessage)
{
$this->logger->shouldReceive('warning')->with($expectedLogMessage);
$result = $this->enforcer->enforce($attributes, $releaseAsOverrides);
$this->assertEquals($expectedResult, $result);
}

public function enforceDataProvider()
{
return [
Expand Down Expand Up @@ -88,6 +98,34 @@ public function enforceDataProvider()
'Releasing attribute "urn:mace:dir:attribute-def:cn" as "ComonNaam" as specified in the release_as ARP setting'
]
],
'single attribute override, empty attribute value is allowed' => [
'attributes' => [
"urn:mace:dir:attribute-def:displayName" => ["Ad Doe"],
"urn:mace:dir:attribute-def:cn" => [],
"urn:mace:dir:attribute-def:sn" => ["Doe"],
"urn:mace:dir:attribute-def:givenName" => ["Ad"],
"urn:mace:dir:attribute-def:mail" => ["[email protected]"]
],
'releaseAsOverrides' => [
"urn:mace:dir:attribute-def:cn" => [
[
"value" => "*",
"release_as" => "ComonNaam",
"use_as_nameid" => false
]
]
],
'expectedResult' => [
"urn:mace:dir:attribute-def:displayName" => ["Ad Doe"],
"urn:mace:dir:attribute-def:sn" => ["Doe"],
"urn:mace:dir:attribute-def:givenName" => ["Ad"],
"urn:mace:dir:attribute-def:mail" => ["[email protected]"],
"ComonNaam" => []
],
'expectedLogMessages' => [
'Releasing attribute "urn:mace:dir:attribute-def:cn" as "ComonNaam" as specified in the release_as ARP setting'
]
],
'multiple attribute overrides' => [
'attributes' => [
"urn:mace:dir:attribute-def:displayName" => ["John Smith"],
Expand Down Expand Up @@ -139,6 +177,65 @@ public function enforceDataProvider()
'expectedLogMessages' => [
]
],
'targeted attribute not in assertion' => [
'attributes' => [
"urn:mace:dir:attribute-def:displayName" => ["Ad Doe"],
"urn:mace:dir:attribute-def:cn" => ["Ad Doe"],
"urn:mace:dir:attribute-def:sn" => ["Doe"],
"urn:mace:dir:attribute-def:givenName" => ["Ad"],
"urn:mace:dir:attribute-def:mail" => ["[email protected]"],
],
'releaseAsOverrides' => [
"urn:mace:dir:attribute-def:eduPersonTargetedId" => [
[
"value" => "*",
"release_as" => "UserName",
"use_as_nameid" => false
]
]
],
'expectedResult' => [
"urn:mace:dir:attribute-def:displayName" => ["Ad Doe"],
"urn:mace:dir:attribute-def:cn" => ["Ad Doe"],
"urn:mace:dir:attribute-def:sn" => ["Doe"],
"urn:mace:dir:attribute-def:givenName" => ["Ad"],
"urn:mace:dir:attribute-def:mail" => ["[email protected]"]
],
'expectedLogMessages' => ['Releasing "urn:mace:dir:attribute-def:eduPersonTargetedId" as "UserName" is not possible, "urn:mace:dir:attribute-def:eduPersonTargetedId" is not in assertion']
],
];
}

public function enforceDataProviderWarnings()
{
return [
'targeted attribute value is set to null in assertion' => [
'attributes' => [
"urn:mace:dir:attribute-def:displayName" => ["Ad Doe"],
"urn:mace:dir:attribute-def:cn" => ["Ad Doe"],
"urn:mace:dir:attribute-def:sn" => ["Doe"],
"urn:mace:dir:attribute-def:eduPersonTargetedId" => null,
"urn:mace:dir:attribute-def:givenName" => ["Ad"],
"urn:mace:dir:attribute-def:mail" => ["[email protected]"],
],
'releaseAsOverrides' => [
"urn:mace:dir:attribute-def:eduPersonTargetedId" => [
[
"value" => "*",
"release_as" => "UserName",
"use_as_nameid" => false
]
]
],
'expectedResult' => [
"urn:mace:dir:attribute-def:displayName" => ["Ad Doe"],
"urn:mace:dir:attribute-def:cn" => ["Ad Doe"],
"urn:mace:dir:attribute-def:sn" => ["Doe"],
"urn:mace:dir:attribute-def:givenName" => ["Ad"],
"urn:mace:dir:attribute-def:mail" => ["[email protected]"]
],
'expectedLogMessages' => 'Releasing "urn:mace:dir:attribute-def:eduPersonTargetedId" as "UserName" is not possible, value for "urn:mace:dir:attribute-def:eduPersonTargetedId" is null'
],
];
}
}