Skip to content

Commit 1009088

Browse files
committed
Add pluggable ACL variables via VarResolver SPI, migrate built-in
variables (#660)
1 parent 2aa3a05 commit 1009088

8 files changed

Lines changed: 1457 additions & 181 deletions

File tree

commons/src/main/java/org/restheart/security/AclVarsInterpolator.java

Lines changed: 159 additions & 181 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*-
2+
* ========================LICENSE_START=================================
3+
* restheart-commons
4+
* %%
5+
* Copyright (C) 2019 - 2026 SoftInstigate
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package org.restheart.security;
21+
22+
import org.restheart.configuration.ConfigurationException;
23+
24+
/**
25+
* Registry for programmatically contributing {@link VarResolver}s to {@link AclVarsInterpolator}.
26+
*
27+
* <h2>Example Usage</h2>
28+
* <pre>{@code
29+
* @Inject("acl-vars-registry")
30+
* private AclVarsRegistry vars;
31+
*
32+
* @OnInit
33+
* public void init() {
34+
* vars.register(new SubscriptionVarResolver());
35+
* }
36+
* }</pre>
37+
*
38+
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
39+
* @since 9.7.0
40+
* @see VarResolver
41+
*/
42+
public interface AclVarsRegistry {
43+
/**
44+
* Registers a {@link VarResolver}, making its variable usable in ACL {@code predicate}
45+
* strings and in {@code readFilter}/{@code mergeRequest} documents.
46+
*
47+
* <p>Must be called from {@code @OnInit}, before any request is processed. Built-in
48+
* resolvers ({@code @user}, {@code @request}, {@code @now}, ...) are registered before any
49+
* plugin's {@code @OnInit} can run and therefore always win a name collision.
50+
*
51+
* @param resolver the resolver to register; must not be {@code null}
52+
* @throws ConfigurationException if {@link VarResolver#name()} is already registered
53+
*/
54+
void register(VarResolver resolver) throws ConfigurationException;
55+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*-
2+
* ========================LICENSE_START=================================
3+
* restheart-commons
4+
* %%
5+
* Copyright (C) 2019 - 2026 SoftInstigate
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package org.restheart.security;
21+
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
import java.util.Optional;
25+
import java.util.Set;
26+
27+
import org.restheart.configuration.ConfigurationException;
28+
29+
/**
30+
* Default {@link AclVarsRegistry} implementation: a process-wide singleton, reachable both via
31+
* dependency injection (see {@link AclVarsRegistryProvider}, for plugins registering a
32+
* {@link VarResolver}) and via {@link #getInstance()} (for {@link AclVarsInterpolator}'s static
33+
* methods, which cannot receive an injected instance).
34+
*
35+
* <p>Built-in resolvers are registered in the constructor, i.e. at class-loading time — before
36+
* any plugin {@code @OnInit} can run — so they always win a name collision (see
37+
* {@link #register(VarResolver)}).
38+
*/
39+
public class AclVarsRegistryImpl implements AclVarsRegistry {
40+
private static final AclVarsRegistryImpl HOLDER = new AclVarsRegistryImpl();
41+
42+
private final Map<String, VarResolver> resolvers = new LinkedHashMap<>();
43+
44+
private AclVarsRegistryImpl() {
45+
BuiltInVarResolvers.all().forEach(resolver -> resolvers.put(resolver.name(), resolver));
46+
}
47+
48+
static AclVarsRegistryImpl getInstance() {
49+
return HOLDER;
50+
}
51+
52+
@Override
53+
public void register(VarResolver resolver) throws ConfigurationException {
54+
var name = resolver.name();
55+
56+
if (resolvers.containsKey(name)) {
57+
throw new ConfigurationException(
58+
"Cannot register VarResolver '" + name + "': a resolver with this name is already registered");
59+
}
60+
61+
resolvers.put(name, resolver);
62+
}
63+
64+
/**
65+
* Looks up a registered resolver by name, without the leading {@code @}.
66+
*/
67+
Optional<VarResolver> resolver(String name) {
68+
return Optional.ofNullable(resolvers.get(name));
69+
}
70+
71+
/**
72+
* All registered resolver names, without the leading {@code @}, built-in and custom —
73+
* in registration order (built-ins first).
74+
*/
75+
Set<String> names() {
76+
return resolvers.keySet();
77+
}
78+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* ========================LICENSE_START=================================
3+
* restheart-commons
4+
* %%
5+
* Copyright (C) 2019 - 2026 SoftInstigate
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package org.restheart.security;
21+
22+
import org.restheart.plugins.PluginRecord;
23+
import org.restheart.plugins.Provider;
24+
import org.restheart.plugins.RegisterPlugin;
25+
26+
/**
27+
* Provides the {@link AclVarsRegistry} for plugins to register a custom {@link VarResolver}.
28+
*
29+
* @see AclVarsRegistry
30+
*/
31+
@RegisterPlugin(name = "acl-vars-registry", description = "provides the AclVarsRegistry to register custom ACL variables (VarResolver)")
32+
public class AclVarsRegistryProvider implements Provider<AclVarsRegistry> {
33+
34+
@Override
35+
public AclVarsRegistry get(PluginRecord<?> caller) {
36+
return AclVarsRegistryImpl.getInstance();
37+
}
38+
}

0 commit comments

Comments
 (0)