Skip to content

Commit 28cc929

Browse files
authored
improve the error message if unresolved user attrs are in DLS (#6305)
Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com>
1 parent 69c5180 commit 28cc929

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/integrationTest/java/org/opensearch/security/privileges/dlsfls/DocumentPrivilegesTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
import static org.hamcrest.MatcherAssert.assertThat;
6969
import static org.hamcrest.Matchers.containsString;
70+
import static org.hamcrest.Matchers.equalTo;
7071
import static org.opensearch.security.util.MockIndexMetadataBuilder.dataStreams;
7172
import static org.opensearch.security.util.MockIndexMetadataBuilder.indices;
7273
import static org.junit.Assert.assertEquals;
@@ -386,6 +387,10 @@ public void queryPatternTemplate() throws Exception {
386387
if (index == index_a1) {
387388
if (userSpec.roles.contains("dls_role_1") && userSpec.roles.contains("dls_role_2")) {
388389
assertNotNull(exception);
390+
assertThat(
391+
exception.getMessage(),
392+
equalTo("DLS query references undefined user attributes: [attr.attr_a]. Available attributes are: [none]")
393+
);
389394
}
390395
}
391396
} else if (userSpec.roles.contains("non_dls_role") && dfmEmptyOverridesAll) {

src/main/java/org/opensearch/security/privileges/dlsfls/DocumentPrivileges.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,17 @@ RenderedDlsQuery evaluate(PrivilegesEvaluationContext context) throws Privileges
168168
if (UserAttributes.needsAttributeSubstitution(effectiveQueryString)) {
169169
List<String> unresolved = UserAttributes.findUnresolvedAttributes(effectiveQueryString);
170170
Set<String> available = context.getUser().getCustomAttributesMap().keySet();
171-
String availableStr = available.size() > MAX_ATTRIBUTES_IN_ERROR_MESSAGE
172-
? available.stream().limit(MAX_ATTRIBUTES_IN_ERROR_MESSAGE).collect(Collectors.joining(", "))
171+
String availableStr;
172+
if (available.size() > MAX_ATTRIBUTES_IN_ERROR_MESSAGE) {
173+
availableStr = available.stream().limit(MAX_ATTRIBUTES_IN_ERROR_MESSAGE).collect(Collectors.joining(", "))
173174
+ " ... and "
174175
+ (available.size() - MAX_ATTRIBUTES_IN_ERROR_MESSAGE)
175-
+ " more"
176-
: String.join(", ", available);
176+
+ " more";
177+
} else if (!available.isEmpty()) {
178+
availableStr = String.join(", ", available);
179+
} else {
180+
availableStr = "[none]";
181+
}
177182
throw new PrivilegesEvaluationException(
178183
"DLS query references undefined user attributes: " + unresolved + ". Available attributes are: " + availableStr,
179184
new OpenSearchSecurityException("User attribute substitution failed")

0 commit comments

Comments
 (0)