Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMConnectionProvider.java: getAuthenticationHolder(): fix check for Bot user name… #211

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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
Loading