Skip to content

Commit 53d10bb

Browse files
authored
[HLRC] Support for role mapper expression dsl (#33745)
This commit adds support for role mapping expression dsl. Functionally it is similar to what we have on the server side except for the rule evaluation which is not required on the client. The role mapper expression can either be field expression or composite expression of one or more expressions. Role mapper expression parser is used to parse JSON DSL to list of expressions. This forms the base for role mapping APIs (get, post/put and delete)
1 parent a15b1b9 commit 53d10bb

File tree

10 files changed

+874
-0
lines changed

10 files changed

+874
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.support.expressiondsl;
21+
22+
import org.elasticsearch.common.xcontent.ToXContentObject;
23+
24+
/**
25+
* Implementations of this interface represent an expression used for user role mapping
26+
* that can later be resolved to a boolean value.
27+
*/
28+
public interface RoleMapperExpression extends ToXContentObject {
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.support.expressiondsl.expressions;
21+
22+
import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
/**
28+
* An expression that evaluates to <code>true</code> if-and-only-if all its children
29+
* evaluate to <code>true</code>.
30+
* An <em>all</em> expression with no children is always <code>true</code>.
31+
*/
32+
public final class AllRoleMapperExpression extends CompositeRoleMapperExpression {
33+
34+
private AllRoleMapperExpression(String name, RoleMapperExpression[] elements) {
35+
super(name, elements);
36+
}
37+
38+
public static Builder builder() {
39+
return new Builder();
40+
}
41+
42+
public static final class Builder {
43+
private List<RoleMapperExpression> elements = new ArrayList<>();
44+
45+
public Builder addExpression(final RoleMapperExpression expression) {
46+
assert expression != null : "expression cannot be null";
47+
elements.add(expression);
48+
return this;
49+
}
50+
51+
public AllRoleMapperExpression build() {
52+
return new AllRoleMapperExpression(CompositeType.ALL.getName(), elements.toArray(new RoleMapperExpression[0]));
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.support.expressiondsl.expressions;
21+
22+
import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
/**
28+
* An expression that evaluates to <code>true</code> if at least one of its children
29+
* evaluate to <code>true</code>.
30+
* An <em>any</em> expression with no children is never <code>true</code>.
31+
*/
32+
public final class AnyRoleMapperExpression extends CompositeRoleMapperExpression {
33+
34+
private AnyRoleMapperExpression(String name, RoleMapperExpression[] elements) {
35+
super(name, elements);
36+
}
37+
38+
public static Builder builder() {
39+
return new Builder();
40+
}
41+
42+
public static final class Builder {
43+
private List<RoleMapperExpression> elements = new ArrayList<>();
44+
45+
public Builder addExpression(final RoleMapperExpression expression) {
46+
assert expression != null : "expression cannot be null";
47+
elements.add(expression);
48+
return this;
49+
}
50+
51+
public AnyRoleMapperExpression build() {
52+
return new AnyRoleMapperExpression(CompositeType.ANY.getName(), elements.toArray(new RoleMapperExpression[0]));
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.support.expressiondsl.expressions;
21+
22+
import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
25+
import java.io.IOException;
26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Objects;
30+
31+
/**
32+
* Expression of role mapper expressions which can be combined by operators like AND, OR
33+
* <p>
34+
* Expression builder example:
35+
* <pre>
36+
* {@code
37+
* final RoleMapperExpression allExpression = AllRoleMapperExpression.builder()
38+
.addExpression(AnyRoleMapperExpression.builder()
39+
.addExpression(FieldRoleMapperExpression.ofUsername("[email protected]"))
40+
.addExpression(FieldRoleMapperExpression.ofUsername("[email protected]"))
41+
.build())
42+
.addExpression(FieldRoleMapperExpression.ofMetadata("metadata.location", "AMER"))
43+
.addExpression(new ExceptRoleMapperExpression(FieldRoleMapperExpression.ofUsername("[email protected]")))
44+
.build();
45+
* }
46+
* </pre>
47+
*/
48+
public abstract class CompositeRoleMapperExpression implements RoleMapperExpression {
49+
private final String name;
50+
private final List<RoleMapperExpression> elements;
51+
52+
CompositeRoleMapperExpression(final String name, final RoleMapperExpression... elements) {
53+
assert name != null : "field name cannot be null";
54+
assert elements != null : "at least one field expression is required";
55+
this.name = name;
56+
this.elements = Collections.unmodifiableList(Arrays.asList(elements));
57+
}
58+
59+
public String getName() {
60+
return this.getName();
61+
}
62+
63+
public List<RoleMapperExpression> getElements() {
64+
return elements;
65+
}
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) {
70+
return true;
71+
}
72+
if (o == null || getClass() != o.getClass()) {
73+
return false;
74+
}
75+
76+
final CompositeRoleMapperExpression that = (CompositeRoleMapperExpression) o;
77+
if (Objects.equals(this.getName(), that.getName()) == false) {
78+
return false;
79+
}
80+
return Objects.equals(this.getElements(), that.getElements());
81+
}
82+
83+
@Override
84+
public int hashCode() {
85+
return Objects.hash(name, elements);
86+
}
87+
88+
@Override
89+
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
90+
builder.startObject();
91+
builder.startArray(name);
92+
for (RoleMapperExpression e : elements) {
93+
e.toXContent(builder, params);
94+
}
95+
builder.endArray();
96+
return builder.endObject();
97+
}
98+
99+
}
100+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.support.expressiondsl.expressions;
21+
22+
import org.elasticsearch.common.ParseField;
23+
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
public enum CompositeType {
29+
30+
ANY("any"), ALL("all"), EXCEPT("except");
31+
32+
private static Map<String, CompositeType> nameToType = Collections.unmodifiableMap(initialize());
33+
private ParseField field;
34+
35+
CompositeType(String name) {
36+
this.field = new ParseField(name);
37+
}
38+
39+
public String getName() {
40+
return field.getPreferredName();
41+
}
42+
43+
public ParseField getParseField() {
44+
return field;
45+
}
46+
47+
public static CompositeType fromName(String name) {
48+
return nameToType.get(name);
49+
}
50+
51+
private static Map<String, CompositeType> initialize() {
52+
Map<String, CompositeType> map = new HashMap<>();
53+
for (CompositeType field : values()) {
54+
map.put(field.getName(), field);
55+
}
56+
return map;
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.support.expressiondsl.expressions;
21+
22+
import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
25+
import java.io.IOException;
26+
27+
/**
28+
* A negating expression. That is, this expression evaluates to <code>true</code> if-and-only-if
29+
* its delegate expression evaluate to <code>false</code>.
30+
* Syntactically, <em>except</em> expressions are intended to be children of <em>all</em>
31+
* expressions ({@link AllRoleMapperExpression}).
32+
*/
33+
public final class ExceptRoleMapperExpression extends CompositeRoleMapperExpression {
34+
35+
public ExceptRoleMapperExpression(final RoleMapperExpression expression) {
36+
super(CompositeType.EXCEPT.getName(), expression);
37+
}
38+
39+
@Override
40+
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
41+
builder.startObject();
42+
builder.field(CompositeType.EXCEPT.getName());
43+
builder.value(getElements().get(0));
44+
return builder.endObject();
45+
}
46+
47+
}

0 commit comments

Comments
 (0)