Skip to content

Commit dc48b4b

Browse files
authored
Add attribute count to SamlAttribute toString (#131173)
Sometimes SAML IdPs send what _should_ be a list of values as a single comma-separated string. That is, we expect something using SAML's multi-valued attribute feature: <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri" Name="http://idp.example.org/attributes/groups" FriendlyName="groups"> <saml:AttributeValue>engineering</saml:AttributeValue> <saml:AttributeValue>elasticsearch-admins</saml:AttributeValue> <saml:AttributeValue>employees</saml:AttributeValue> </saml:Attribute> but we get <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri" Name="http://idp.example.org/attributes/groups" FriendlyName="groups"> <saml:AttributeValue>engineering,elasticsearch-admins,employees</saml:AttributeValue> </saml:Attribute> In order to help detect these cases, this commit changes the `toString()` on `SamlAttribute` to include the length (e.g. `(len=1)`) at the end Relates: #84379, #102769
1 parent 13aceaa commit dc48b4b

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

docs/changelog/131173.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 131173
2+
summary: Add attribute count to `SamlAttribute` `toString`
3+
area: Authentication
4+
type: enhancement
5+
issues: []

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAttributes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ static class SamlAttribute {
9292

9393
@Override
9494
public String toString() {
95+
StringBuilder str = new StringBuilder();
9596
if (Strings.isNullOrEmpty(friendlyName)) {
96-
return name + '=' + values;
97+
str.append(name);
9798
} else {
98-
return friendlyName + '(' + name + ")=" + values;
99+
str.append(friendlyName).append('(').append(name).append(')');
99100
}
101+
str.append("=").append(values).append("(len=").append(values.size()).append(')');
102+
return str.toString();
100103
}
101104
}
102105

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.security.authc.saml;
9+
10+
import org.hamcrest.Matchers;
11+
import org.opensaml.saml.saml2.core.NameID;
12+
13+
import java.util.List;
14+
15+
public class SamlAttributesTests extends SamlTestCase {
16+
17+
public void testToString() {
18+
final String nameFormat = randomFrom(NameID.TRANSIENT, NameID.PERSISTENT, NameID.EMAIL);
19+
final String nameId = randomIdentifier();
20+
final String session = randomAlphaOfLength(16);
21+
final SamlAttributes attributes = new SamlAttributes(
22+
new SamlNameId(nameFormat, nameId, null, null, null),
23+
session,
24+
List.of(
25+
new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.1", null, List.of("peter.ng")),
26+
new SamlAttributes.SamlAttribute("urn:oid:2.5.4.3", "name", List.of("Peter Ng")),
27+
new SamlAttributes.SamlAttribute(
28+
"urn:oid:1.3.6.1.4.1.5923.1.5.1.1",
29+
"groups",
30+
List.of("employees", "engineering", "managers")
31+
)
32+
)
33+
);
34+
assertThat(
35+
attributes.toString(),
36+
Matchers.equalTo(
37+
"SamlAttributes("
38+
+ ("NameId(" + nameFormat + ")=" + nameId)
39+
+ ")["
40+
+ session
41+
+ "]{["
42+
+ "urn:oid:0.9.2342.19200300.100.1.1=[peter.ng](len=1)"
43+
+ ", "
44+
+ "name(urn:oid:2.5.4.3)=[Peter Ng](len=1)"
45+
+ ", "
46+
+ "groups(urn:oid:1.3.6.1.4.1.5923.1.5.1.1)=[employees, engineering, managers](len=3)"
47+
+ "]}"
48+
)
49+
);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)