-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PluginRealm.java
464 lines (416 loc) · 16.2 KB
/
PluginRealm.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.security.enterprise.auth.plugin;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import java.nio.file.Path;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.security.AuthExpirationException;
import org.neo4j.kernel.api.security.exception.InvalidAuthTokenException;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.internal.Version;
import org.neo4j.logging.Log;
import org.neo4j.server.security.enterprise.auth.PredefinedRolesBuilder;
import org.neo4j.server.security.enterprise.auth.SecureHasher;
import org.neo4j.server.security.enterprise.auth.ShiroAuthToken;
import org.neo4j.server.security.enterprise.auth.ShiroAuthorizationInfoProvider;
import org.neo4j.server.security.enterprise.auth.plugin.api.AuthToken;
import org.neo4j.server.security.enterprise.auth.plugin.api.AuthorizationExpired;
import org.neo4j.server.security.enterprise.auth.plugin.api.RealmOperations;
import org.neo4j.server.security.enterprise.auth.plugin.spi.AuthInfo;
import org.neo4j.server.security.enterprise.auth.plugin.spi.AuthPlugin;
import org.neo4j.server.security.enterprise.auth.plugin.spi.AuthenticationPlugin;
import org.neo4j.server.security.enterprise.auth.plugin.spi.AuthorizationPlugin;
import org.neo4j.server.security.enterprise.auth.plugin.spi.CustomCacheableAuthenticationInfo;
import org.neo4j.server.security.enterprise.auth.plugin.spi.RealmLifecycle;
import org.neo4j.server.security.enterprise.log.SecurityLog;
import static org.neo4j.server.security.enterprise.configuration.SecuritySettings.PLUGIN_REALM_NAME_PREFIX;
public class PluginRealm extends AuthorizingRealm implements RealmLifecycle, ShiroAuthorizationInfoProvider
{
private AuthenticationPlugin authenticationPlugin;
private AuthorizationPlugin authorizationPlugin;
private final Config config;
private AuthPlugin authPlugin;
private final Log log;
private final Clock clock;
private final SecureHasher secureHasher;
private RealmOperations realmOperations = new PluginRealmOperations();
public PluginRealm( Config config, SecurityLog securityLog, Clock clock, SecureHasher secureHasher )
{
this.config = config;
this.clock = clock;
this.secureHasher = secureHasher;
this.log = securityLog;
setCredentialsMatcher( new CredentialsMatcher() );
// Synchronize this default value with the javadoc for RealmOperations.setAuthenticationCachingEnabled
setAuthenticationCachingEnabled( false );
// Synchronize this default value with the javadoc for RealmOperations.setAuthorizationCachingEnabled
setAuthorizationCachingEnabled( true );
setRolePermissionResolver( PredefinedRolesBuilder.rolePermissionResolver );
}
public PluginRealm( AuthenticationPlugin authenticationPlugin, AuthorizationPlugin authorizationPlugin,
Config config, SecurityLog securityLog, Clock clock, SecureHasher secureHasher )
{
this( config, securityLog, clock, secureHasher );
this.authenticationPlugin = authenticationPlugin;
this.authorizationPlugin = authorizationPlugin;
resolvePluginName();
}
public PluginRealm( AuthPlugin authPlugin, Config config, SecurityLog securityLog, Clock clock,
SecureHasher secureHasher )
{
this( config, securityLog, clock, secureHasher );
this.authPlugin = authPlugin;
resolvePluginName();
}
private void resolvePluginName()
{
String pluginName = null;
if ( authPlugin != null )
{
pluginName = authPlugin.name();
}
else if ( authenticationPlugin != null )
{
pluginName = authenticationPlugin.name();
}
else if ( authorizationPlugin != null )
{
pluginName = authorizationPlugin.name();
}
if ( pluginName != null && !pluginName.isEmpty() )
{
setName( PLUGIN_REALM_NAME_PREFIX + pluginName );
}
// Otherwise we rely on the Shiro default generated name
}
private Collection<AuthorizationPlugin.PrincipalAndRealm> getPrincipalAndRealmCollection(
PrincipalCollection principalCollection
)
{
Collection<AuthorizationPlugin.PrincipalAndRealm> principalAndRealmCollection = new ArrayList<>();
for ( String realm : principalCollection.getRealmNames() )
{
for ( Object principal : principalCollection.fromRealm( realm ) )
{
principalAndRealmCollection.add( new AuthorizationPlugin.PrincipalAndRealm( principal, realm ) );
}
}
return principalAndRealmCollection;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals )
{
if ( authorizationPlugin != null )
{
org.neo4j.server.security.enterprise.auth.plugin.spi.AuthorizationInfo authorizationInfo;
try
{
authorizationInfo = authorizationPlugin.authorize( getPrincipalAndRealmCollection( principals ) );
}
catch ( AuthorizationExpired e )
{
throw new AuthExpirationException(
"Plugin '" + getName() + "' authorization info expired: " + e.getMessage() );
}
if ( authorizationInfo != null )
{
return PluginAuthorizationInfo.create( authorizationInfo );
}
}
else if ( authPlugin != null && !principals.fromRealm( getName() ).isEmpty() )
{
// The cached authorization info has expired.
// Since we do not have the subject's credentials we cannot perform a new
// authenticateAndAuthorize() to renew authorization info.
// Instead we need to fail with a special status, so that the client can react by re-authenticating.
throw new AuthExpirationException( "Plugin '" + getName() + "' authorization info expired." );
}
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token ) throws AuthenticationException
{
if ( token instanceof ShiroAuthToken )
{
try
{
AuthToken pluginAuthToken =
PluginApiAuthToken.createFromMap( ((ShiroAuthToken) token).getAuthTokenMap() );
if ( authPlugin != null )
{
AuthInfo authInfo = authPlugin.authenticateAndAuthorize( pluginAuthToken );
if ( authInfo != null )
{
PluginAuthInfo pluginAuthInfo =
PluginAuthInfo.createCacheable( authInfo, getName(), secureHasher );
cacheAuthorizationInfo( pluginAuthInfo );
return pluginAuthInfo;
}
}
else if ( authenticationPlugin != null )
{
org.neo4j.server.security.enterprise.auth.plugin.spi.AuthenticationInfo authenticationInfo =
authenticationPlugin.authenticate( pluginAuthToken );
if ( authenticationInfo != null )
{
return PluginAuthenticationInfo.createCacheable( authenticationInfo, getName(), secureHasher );
}
}
}
catch ( org.neo4j.server.security.enterprise.auth.plugin.api.AuthenticationException |
InvalidAuthTokenException e )
{
throw new AuthenticationException( e.getMessage(), e.getCause() );
}
}
return null;
}
private void cacheAuthorizationInfo( PluginAuthInfo authInfo )
{
// Use the existing authorizationCache in our base class
Cache<Object, AuthorizationInfo> authorizationCache = getAuthorizationCache();
Object key = getAuthorizationCacheKey( authInfo.getPrincipals() );
authorizationCache.put( key, authInfo );
}
@Override
protected Object getAuthorizationCacheKey( PrincipalCollection principals )
{
return getAvailablePrincipal( principals );
}
@Override
protected Object getAuthenticationCacheKey( AuthenticationToken token )
{
return token != null ? token.getPrincipal() : null;
}
@Override
public boolean supports( AuthenticationToken token )
{
return supportsSchemeAndRealm( token );
}
private boolean supportsSchemeAndRealm( AuthenticationToken token )
{
if ( token instanceof ShiroAuthToken )
{
ShiroAuthToken shiroAuthToken = (ShiroAuthToken) token;
return shiroAuthToken.supportsRealm( getName() );
}
return false;
}
@Override
public void initialize( RealmOperations ignore ) throws Throwable
{
if ( authenticationPlugin != null )
{
authenticationPlugin.initialize( this.realmOperations );
}
if ( authorizationPlugin != null && authorizationPlugin != authenticationPlugin )
{
authorizationPlugin.initialize( this.realmOperations );
}
if ( authPlugin != null )
{
authPlugin.initialize( this.realmOperations );
}
}
@Override
public void start() throws Throwable
{
if ( authenticationPlugin != null )
{
authenticationPlugin.start();
}
if ( authorizationPlugin != null && authorizationPlugin != authenticationPlugin )
{
authorizationPlugin.start();
}
if ( authPlugin != null )
{
authPlugin.start();
}
}
@Override
public void stop() throws Throwable
{
if ( authenticationPlugin != null )
{
authenticationPlugin.stop();
}
if ( authorizationPlugin != null && authorizationPlugin != authenticationPlugin )
{
authorizationPlugin.stop();
}
if ( authPlugin != null )
{
authPlugin.stop();
}
}
@Override
public void shutdown() throws Throwable
{
if ( authenticationPlugin != null )
{
authenticationPlugin.shutdown();
}
if ( authorizationPlugin != null && authorizationPlugin != authenticationPlugin )
{
authorizationPlugin.shutdown();
}
if ( authPlugin != null )
{
authPlugin.shutdown();
}
}
private static CustomCacheableAuthenticationInfo.CredentialsMatcher getCustomCredentialsMatcherIfPresent(
AuthenticationInfo info
)
{
if ( info instanceof CustomCredentialsMatcherSupplier )
{
return ((CustomCredentialsMatcherSupplier) info).getCredentialsMatcher();
}
return null;
}
@Override
public AuthorizationInfo getAuthorizationInfoSnapshot( PrincipalCollection principalCollection )
{
return getAuthorizationInfo( principalCollection );
}
private class CredentialsMatcher implements org.apache.shiro.authc.credential.CredentialsMatcher
{
@Override
public boolean doCredentialsMatch( AuthenticationToken token, AuthenticationInfo info )
{
CustomCacheableAuthenticationInfo.CredentialsMatcher
customCredentialsMatcher = getCustomCredentialsMatcherIfPresent( info );
if ( customCredentialsMatcher != null )
{
// Authentication info is originating from a CustomCacheableAuthenticationInfo
Map<String,Object> authToken = ((ShiroAuthToken) token).getAuthTokenMap();
try
{
AuthToken pluginApiAuthToken = PluginApiAuthToken.createFromMap( authToken );
return customCredentialsMatcher.doCredentialsMatch( pluginApiAuthToken );
}
catch ( InvalidAuthTokenException e )
{
throw new AuthenticationException( e.getMessage() );
}
}
else if ( info.getCredentials() != null )
{
// Authentication info is originating from a CacheableAuthenticationInfo or a CacheableAuthInfo
return secureHasher.getHashedCredentialsMatcher()
.doCredentialsMatch( PluginShiroAuthToken.of( token ), info );
}
else
{
// Authentication info is originating from an AuthenticationInfo or an AuthInfo
if ( PluginRealm.this.isAuthenticationCachingEnabled() )
{
log.error( "Authentication caching is enabled in plugin %s but it does not return " +
"cacheable credentials. This configuration is not secure.", getName() );
return false;
}
return true; // Always match if we do not cache credentials
}
}
}
private class PluginRealmOperations implements RealmOperations
{
private Log innerLog = new Log()
{
private String withPluginName( String msg )
{
return "{" + getName() + "} " + msg;
}
@Override
public void debug( String message )
{
log.debug( withPluginName( message ) );
}
@Override
public void info( String message )
{
log.info( withPluginName( message ) );
}
@Override
public void warn( String message )
{
log.warn( withPluginName( message ) );
}
@Override
public void error( String message )
{
log.error( withPluginName( message ) );
}
@Override
public boolean isDebugEnabled()
{
return log.isDebugEnabled();
}
};
@Override
public Path neo4jHome()
{
return config.get( GraphDatabaseSettings.neo4j_home ).getAbsoluteFile().toPath();
}
@Override
public Optional<Path> neo4jConfigFile()
{
return config.getConfigFile();
}
@Override
public String neo4jVersion()
{
return Version.getNeo4jVersion();
}
@Override
public Clock clock()
{
return clock;
}
@Override
public Log log()
{
return innerLog;
}
@Override
public void setAuthenticationCachingEnabled( boolean authenticationCachingEnabled )
{
PluginRealm.this.setAuthenticationCachingEnabled( authenticationCachingEnabled );
}
@Override
public void setAuthorizationCachingEnabled( boolean authorizationCachingEnabled )
{
PluginRealm.this.setAuthorizationCachingEnabled( authorizationCachingEnabled );
}
}
}