Skip to content

Commit

Permalink
[JENKINS-55305] Add user creation listener method
Browse files Browse the repository at this point in the history
Four new methods added to the SecurityListener class
to enable support for listening to Jenkins user-account
creation events.

[JENKINS-55305] Update SecurityListener class

[JENKINS-55307] Add new private-realm unit tests

Added unit tests to exercise the updated methods
of the HudsonPrivateSecurityRealm class.

[JENKINS-55307] Update realm user-creation methods

[JENKINS-55305] Remove usage of listener method

Updated the SecurityListener and HudsonPrivateSecurityRealm
classes, removing usage of the SecurityListener failedToCreateUser()
method, as no valid Jenkins use cases could be identified for the
method at this point in time.
  • Loading branch information
davidolorundare committed Jan 4, 2019
1 parent 554dd92 commit 74aa4db
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ public boolean isMailerPluginPresent() {
public User createAccount(String userName, String password) throws IOException {
User user = User.getById(userName, true);
user.addProperty(Details.fromPlainPassword(password));
SecurityListener.fireUserCreated(user.getId());
return user;
}

Expand Down
17 changes: 17 additions & 0 deletions core/src/main/java/jenkins/security/SecurityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ protected void failedToAuthenticate(@Nonnull String username){}
*/
protected void loggedIn(@Nonnull String username){}

/**
* @since TODO
*
* Fired when a new user account has been created.
*
* @param username the user
*/
protected void userCreated(@Nonnull String username) {}

/**
* Fired when a user has failed to log in.
* Would be called after {@link #failedToAuthenticate}.
Expand Down Expand Up @@ -98,6 +107,14 @@ public static void fireAuthenticated(@Nonnull UserDetails details) {
}
}

/** @since TODO */
public static void fireUserCreated(@Nonnull String username) {
LOGGER.log(Level.FINE, "new user created: {0}", username);
for (SecurityListener l : all()) {
l.userCreated(username);
}
}

/** @since 1.569 */
public static void fireFailedToAuthenticate(@Nonnull String username) {
LOGGER.log(Level.FINE, "failed to authenticate: {0}", username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,43 @@ public void selfRegistrationTriggerLoggedIn() throws Exception {
assertTrue(spySecurityListener.loggedInUsernames.get(0).equals("bob"));
}

@Issue("JENKINS-55307")
@Test
public void selfRegistrationTriggerUserCreation() throws Exception {
HudsonPrivateSecurityRealm securityRealm = new HudsonPrivateSecurityRealm(true, false, null);
j.jenkins.setSecurityRealm(securityRealm);
j.jenkins.setCrumbIssuer(null);

spySecurityListener.createdUsers.clear();
assertTrue(spySecurityListener.createdUsers.isEmpty());

selfRegistration("bob");
selfRegistration("charlie");
assertTrue(spySecurityListener.createdUsers.get(0).equals("bob"));
assertTrue(spySecurityListener.createdUsers.get(1).equals("charlie"));
}

@Issue("JENKINS-55307")
@Test
public void userCreationFromRealm() throws Exception {
HudsonPrivateSecurityRealm securityRealm = new HudsonPrivateSecurityRealm(false, false, null);
j.jenkins.setSecurityRealm(securityRealm);

spySecurityListener.createdUsers.clear();
assertTrue(spySecurityListener.createdUsers.isEmpty());

User u1 = securityRealm.createAccount("alice", "alicePassword");
u1.setFullName("Alice User");
u1.save();

User u2 = securityRealm.createAccount("debbie", "debbiePassword");
u2.setFullName("Debbie User");
u2.save();

assertTrue(spySecurityListener.createdUsers.get(0).equals("alice"));
assertTrue(spySecurityListener.createdUsers.get(1).equals("debbie"));
}

private void createFirstAccount(String login) throws Exception {
assertNull(User.getById(login, false));

Expand Down Expand Up @@ -366,11 +403,15 @@ private void selfRegistration(String login) throws Exception {
@TestExtension
public static class SpySecurityListenerImpl extends SecurityListener {
private List<String> loggedInUsernames = new ArrayList<>();
private List<String> createdUsers = new ArrayList<String>();

@Override
protected void loggedIn(@Nonnull String username) {
loggedInUsernames.add(username);
}

@Override
protected void userCreated(@Nonnull String username) { createdUsers.add(username); }
}

@Issue("SECURITY-786")
Expand Down

0 comments on commit 74aa4db

Please sign in to comment.