-
Notifications
You must be signed in to change notification settings - Fork 88
/
KeycloakAuthorizer.java
247 lines (217 loc) · 8.82 KB
/
KeycloakAuthorizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2017-2023, Strimzi authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.strimzi.kafka.oauth.server.authorizer;
import io.strimzi.kafka.oauth.common.ConfigException;
import org.apache.kafka.common.Endpoint;
import org.apache.kafka.common.Uuid;
import org.apache.kafka.common.acl.AclBinding;
import org.apache.kafka.common.acl.AclBindingFilter;
import org.apache.kafka.common.acl.AclOperation;
import org.apache.kafka.common.resource.ResourceType;
import org.apache.kafka.metadata.authorizer.AclMutator;
import org.apache.kafka.metadata.authorizer.ClusterMetadataAuthorizer;
import org.apache.kafka.metadata.authorizer.StandardAcl;
import org.apache.kafka.metadata.authorizer.StandardAuthorizer;
import org.apache.kafka.server.authorizer.AclCreateResult;
import org.apache.kafka.server.authorizer.AclDeleteResult;
import org.apache.kafka.server.authorizer.Action;
import org.apache.kafka.server.authorizer.AuthorizableRequestContext;
import org.apache.kafka.server.authorizer.AuthorizationResult;
import org.apache.kafka.server.authorizer.AuthorizerServerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicInteger;
/**
* An authorizer using Keycloak Authorization Services that supports KRaft mode and Zookeeper mode.
* <p>
* In KRaft mode multiple instances of this class can be instantiated, and each needs its own instance of <code>StandardAuthorizer</code> for
* delegating authorization to Kafka ACL implementation.
* <p>
* This authorizer auto-detects whether it runs in KRaft mode or Zookeeper mode, and automatically sets up appropriate Kafka ACL delegation classes.
* All authorization logic is delegated to <code>KeycloakRBACAuthorizer</code> of which a single instance is created and shared between all
* instances of this class.
* <p>
* To install this authorizer in Kafka, specify the following in your 'server.properties':
* </p>
* <pre>
* authorizer.class.name=io.strimzi.kafka.oauth.server.authorizer.KeycloakAuthorizer
* principal.builder.class=io.strimzi.kafka.oauth.server.OAuthKafkaPrincipalBuilder
* </pre>
* <p>
* Configuration options are the same as for {@link KeycloakRBACAuthorizer}.
*/
@SuppressWarnings("deprecation")
public class KeycloakAuthorizer implements ClusterMetadataAuthorizer {
private static final Logger log = LoggerFactory.getLogger(KeycloakAuthorizer.class);
/**
* A counter used to generate an instance number for each instance of this class
*/
private final static AtomicInteger INSTANCE_NUMBER_COUNTER = new AtomicInteger(1);
/**
* An instance number used in {@link #toString()} method, to easily track the number of instances of this class
*/
private final int instanceNumber = INSTANCE_NUMBER_COUNTER.getAndIncrement();
private StandardAuthorizer delegate;
private KeycloakRBACAuthorizer singleton;
@Override
public void configure(Map<String, ?> configs) {
Configuration configuration = new Configuration(configs);
// There is one singleton to which authorize() calls are delegated
singleton = KeycloakAuthorizerService.getInstance();
if (singleton == null) {
singleton = new KeycloakRBACAuthorizer(this);
singleton.configure(configs);
KeycloakAuthorizerService.setInstance(singleton);
} else if (!configuration.equals(singleton.getConfiguration())) {
throw new ConfigException("Only one authorizer configuration per JVM is supported");
}
if (configuration.isDelegateToKafkaACL() && configuration.isKRaft()) {
delegate = instantiateStandardAuthorizer();
delegate.configure(configs);
}
if (log.isDebugEnabled()) {
log.debug("Configured " + this + " using " + singleton);
}
}
private StandardAuthorizer instantiateStandardAuthorizer() {
try {
log.debug("Using StandardAuthorizer (KRaft based) as a delegate");
return new StandardAuthorizer();
} catch (Exception e) {
throw new ConfigException("KRaft mode detected ('process.roles' configured), but failed to instantiate org.apache.kafka.metadata.authorizer.StandardAuthorizer", e);
}
}
@Override
public Map<Endpoint, ? extends CompletionStage<Void>> start(AuthorizerServerInfo serverInfo) {
if (delegate != null) {
return delegate.start(serverInfo);
}
return singleton.start(serverInfo);
}
@Override
public void setAclMutator(AclMutator aclMutator) {
if (delegate != null) {
delegate.setAclMutator(aclMutator);
}
}
@Override
public AclMutator aclMutatorOrException() {
if (delegate != null) {
return delegate.aclMutatorOrException();
}
throw new IllegalStateException("KeycloakAuthorizer has not been properly configured");
}
@Override
public void completeInitialLoad() {
if (delegate != null) {
delegate.completeInitialLoad();
}
}
@Override
public void completeInitialLoad(Exception e) {
if (e != null) {
e.printStackTrace();
}
if (delegate != null) {
delegate.completeInitialLoad(e);
}
}
@Override
public void loadSnapshot(Map<Uuid, StandardAcl> acls) {
if (delegate != null) {
delegate.loadSnapshot(acls);
}
}
@Override
public void addAcl(Uuid id, StandardAcl acl) {
if (delegate != null) {
delegate.addAcl(id, acl);
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
@Override
public void removeAcl(Uuid id) {
if (delegate != null) {
delegate.removeAcl(id);
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
@Override
public Iterable<AclBinding> acls(AclBindingFilter filter) {
if (delegate != null) {
return delegate.acls(filter);
} else if (singleton != null) {
return singleton.acls(filter);
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
@Override
public List<? extends CompletionStage<AclCreateResult>> createAcls(AuthorizableRequestContext requestContext, List<AclBinding> aclBindings) {
if (delegate != null) {
return delegate.createAcls(requestContext, aclBindings);
} else if (singleton != null) {
return singleton.createAcls(requestContext, aclBindings);
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
@Override
public List<? extends CompletionStage<AclDeleteResult>> deleteAcls(AuthorizableRequestContext requestContext, List<AclBindingFilter> aclBindingFilters) {
if (delegate != null) {
return delegate.deleteAcls(requestContext, aclBindingFilters);
} else if (singleton != null) {
return singleton.deleteAcls(requestContext, aclBindingFilters);
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
@Override
public int aclCount() {
if (delegate != null) {
return delegate.aclCount();
} else if (singleton != null) {
return singleton.aclCount();
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
public AuthorizationResult authorizeByResourceType(AuthorizableRequestContext requestContext, AclOperation op, ResourceType resourceType) {
if (delegate != null) {
return delegate.authorizeByResourceType(requestContext, op, resourceType);
} else if (singleton != null) {
return singleton.authorizeByResourceType(requestContext, op, resourceType);
} else {
throw new UnsupportedOperationException("ACL delegation not enabled");
}
}
@Override
public List<AuthorizationResult> authorize(AuthorizableRequestContext requestContext, List<Action> actions) {
if (delegate != null) {
return singleton.authorize(delegate, requestContext, actions);
} else {
return singleton.authorize(requestContext, actions);
}
}
@Override
public void close() throws IOException {
if (singleton != null) {
singleton.close();
}
if (delegate != null) {
delegate.close();
}
}
@Override
public String toString() {
return KeycloakAuthorizer.class.getSimpleName() + "@" + instanceNumber;
}
}