-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PersonalUserManagerTest.java
281 lines (249 loc) · 9.22 KB
/
PersonalUserManagerTest.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.server.security.enterprise.auth;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.IOException;
import java.time.Clock;
import java.util.Set;
import org.neo4j.internal.kernel.api.security.AuthSubject;
import org.neo4j.internal.kernel.api.security.SecurityContext;
import org.neo4j.kernel.api.exceptions.InvalidArgumentsException;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.security.User;
import org.neo4j.logging.Log;
import org.neo4j.server.security.auth.BasicPasswordPolicy;
import org.neo4j.server.security.auth.InMemoryUserRepository;
import org.neo4j.server.security.auth.RateLimitedAuthenticationStrategy;
import org.neo4j.server.security.enterprise.log.SecurityLog;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
public class PersonalUserManagerTest
{
@Rule
public ExpectedException expectedException = ExpectedException.none();
private PersonalUserManager userManager;
private EvilUserManager evilUserManager;
private Log log;
@Test
public void shouldHandleFailureToCreateUser() throws Exception
{
// Given
evilUserManager.setFailNextCall();
//Expect
expectedException.expect( IOException.class );
expectedException.expectMessage( "newUserException" );
// When
userManager.newUser( "hewhoshallnotbenamed", "avada kedavra", false );
verify( log ).error( withSubject( SecurityContext.AUTH_DISABLED.subject(), "tried to create user `%s`: %s" ),
"hewhoshallnotbenamed", "newUserException" );
}
@Before
public void setup()
{
evilUserManager = new EvilUserManager(
new InternalFlatFileRealm( new InMemoryUserRepository(), new InMemoryRoleRepository(),
new BasicPasswordPolicy(), new RateLimitedAuthenticationStrategy( Clock.systemUTC(), Config.defaults() ),
new InternalFlatFileRealmIT.TestJobScheduler(), new InMemoryUserRepository(),
new InMemoryUserRepository() ) );
log = spy( Log.class );
userManager = new PersonalUserManager( evilUserManager, AuthSubject.AUTH_DISABLED, new SecurityLog( log ), true );
}
private String withSubject( AuthSubject subject, String msg )
{
return "[" + subject.username() + "] " + msg;
}
private class EvilUserManager implements EnterpriseUserManager
{
private boolean failNextCall;
private EnterpriseUserManager delegate;
EvilUserManager( EnterpriseUserManager delegate )
{
this.delegate = delegate;
}
void setFailNextCall()
{
failNextCall = true;
}
@Override
public User newUser( String username, String password, boolean changeRequired )
throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "newUserException" );
}
return delegate.newUser( username, password, changeRequired );
}
@Override
public boolean deleteUser( String username ) throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "deleteUserException" );
}
return delegate.deleteUser( username );
}
@Override
public User getUser( String username ) throws InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new InvalidArgumentsException( "getUserException" );
}
return delegate.getUser( username );
}
@Override
public User silentlyGetUser( String username )
{
return delegate.silentlyGetUser( username );
}
@Override
public void setUserPassword( String username, String password, boolean requirePasswordChange )
throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "setUserPasswordException" );
}
delegate.setUserPassword( username, password, requirePasswordChange );
}
@Override
public Set<String> getAllUsernames()
{
return delegate.getAllUsernames();
}
@Override
public void suspendUser( String username ) throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "suspendUserException" );
}
delegate.suspendUser( username );
}
@Override
public void activateUser( String username, boolean requirePasswordChange )
throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "activateUserException" );
}
delegate.activateUser( username, requirePasswordChange );
}
@Override
public void newRole( String roleName, String... usernames ) throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "newRoleException" );
}
delegate.newRole( roleName, usernames );
}
@Override
public boolean deleteRole( String roleName ) throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "deleteRoleException" );
}
return delegate.deleteRole( roleName );
}
@Override
public void assertRoleExists( String roleName ) throws InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new InvalidArgumentsException( "getRoleException" );
}
delegate.assertRoleExists( roleName );
}
@Override
public void addRoleToUser( String roleName, String username ) throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "addRoleToUserException" );
}
delegate.addRoleToUser( roleName, username );
}
@Override
public void removeRoleFromUser( String roleName, String username ) throws IOException, InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new IOException( "removeRoleFromUserException" );
}
delegate.removeRoleFromUser( roleName, username );
}
@Override
public Set<String> getAllRoleNames()
{
return delegate.getAllRoleNames();
}
@Override
public Set<String> getRoleNamesForUser( String username ) throws InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new InvalidArgumentsException( "getRoleNamesForUserException" );
}
return delegate.getRoleNamesForUser( username );
}
@Override
public Set<String> silentlyGetRoleNamesForUser( String username )
{
return delegate.silentlyGetRoleNamesForUser( username );
}
@Override
public Set<String> getUsernamesForRole( String roleName ) throws InvalidArgumentsException
{
if ( failNextCall )
{
failNextCall = false;
throw new InvalidArgumentsException( "getUsernamesForRoleException" );
}
return delegate.getUsernamesForRole( roleName );
}
@Override
public Set<String> silentlyGetUsernamesForRole( String roleName )
{
return delegate.silentlyGetUsernamesForRole( roleName );
}
}
}