Skip to content

Commit

Permalink
Merge pull request #211 from jimklimov/JENKINS-72590
Browse files Browse the repository at this point in the history
IMConnectionProvider.java: getAuthenticationHolder(): fix check for Bot user name…
  • Loading branch information
jimklimov authored Jan 23, 2024
2 parents b93c351 + 438d0ca commit fc1db63
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
18 changes: 16 additions & 2 deletions src/main/java/hudson/plugins/im/IMConnectionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.acegisecurity.userdetails.UsernameNotFoundException;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
* Abstract implementation of a provider of {@link IMConnection}s.
Expand Down Expand Up @@ -132,9 +135,20 @@ public Authentication getAuthentication() {
return null;
}

User u = User.get(descriptor.getHudsonUserName());
if (descriptor.getHudsonUserName().isBlank() && !(Jenkins.getInstance().isUseSecurity())) {
return null;
}

return u.impersonate();
try {
User u = User.get(descriptor.getHudsonUserName());
return u.impersonate();
} catch (UsernameNotFoundException ue) {
if (descriptor.getHudsonUserName().isBlank()) {
throw new UsernameNotFoundException(
"No local Jenkins user name is configured for instant messaging to act as");
}
throw ue;

Check warning on line 150 in src/main/java/hudson/plugins/im/IMConnectionProvider.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 138-150 are not covered by tests
}
}
};
}
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/hudson/plugins/im/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import jenkins.model.Jenkins;
import jenkins.security.NotReallyRoleSensitiveCallable;
import org.acegisecurity.userdetails.UsernameNotFoundException;

/**
* Instant messaging bot.
Expand Down Expand Up @@ -162,15 +163,20 @@ public void onMessage(final IMMessage msg) {
final BotCommand command = this.cmdsAndAliases.get(cmd);
if (command != null) {
if (isAuthenticationNeeded()) {
ACL.impersonate(this.authentication.getAuthentication(), new NotReallyRoleSensitiveCallable<Void, IMException>() {
private static final long serialVersionUID = 1L;

@Override
public Void call() throws IMException {
command.executeCommand(Bot.this, chat, msg, s, args);
return null;
}
});
try {
ACL.impersonate(this.authentication.getAuthentication(), new NotReallyRoleSensitiveCallable<Void, IMException>() {
private static final long serialVersionUID = 1L;

@Override
public Void call() throws IMException {
command.executeCommand(Bot.this, chat, msg, s, args);
return null;
}
});
} catch (UsernameNotFoundException ue) {
this.chat.sendMessage(s.getNickname() + " This bot is not authorized to execute commands at this time, sorry!");
throw ue;
}

Check warning on line 179 in src/main/java/hudson/plugins/im/bot/Bot.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 176-179 are not covered by tests
} else {
command.executeCommand(Bot.this, chat, msg, s, args);
}
Expand Down

0 comments on commit fc1db63

Please sign in to comment.