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

feat: Use credentials plugin for managing authentication credentials #325

Merged
merged 6 commits into from
Nov 16, 2021
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
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
</dependency>

<!-- workflow stuff -->
<dependency>
Expand Down Expand Up @@ -240,11 +244,6 @@
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,25 +483,10 @@ boolean sendMail(ExtendedEmailPublisherContext context) {
+ mailAccount.getAddress()
+ " has invalid SMTP server");
}
} else if (!mailAccount.isSmtpAuthValid()) {
context.getListener()
.getLogger()
.println("Mail account has invalid SMTP authentication settings");
if (mailAccount.isDefaultAccount()) {
debug(
context.getListener().getLogger(),
"Default account has invalid SMTP authentication settings");
} else {
debug(
context.getListener().getLogger(),
"Additional account "
+ mailAccount.getAddress()
+ " has invalid SMTP authentication settings");
}
}
return false;
}
Session session = getDescriptor().createSession(mailAccount);
Session session = getDescriptor().createSession(mailAccount, context);
if (session == null) {
context.getListener().getLogger().println("Could not create session");
return false;
Expand Down Expand Up @@ -843,12 +828,6 @@ MailAccount getMailAccount(ExtendedEmailPublisherContext context) throws Address
"Ignoring additional account "
+ addAccount.getAddress()
+ " with invalid SMTP server");
} else if (!addAccount.isSmtpAuthValid()) {
debug(
context.getListener().getLogger(),
"Ignoring additional account "
+ addAccount.getAddress()
+ " with invalid SMTP authentication");
}
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package hudson.plugins.emailext;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.plugins.emailext.plugins.EmailTriggerDescriptor;
import hudson.plugins.emailext.plugins.trigger.FailureTrigger;
import hudson.security.Permission;
Expand All @@ -23,6 +27,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -173,10 +178,19 @@ public final class ExtendedEmailPublisherDescriptor extends BuildStepDescriptor<
private transient Secret smtpAuthPassword;
private transient boolean useSsl = false;

private transient Function<MailAccount, Authenticator> authenticatorProvider = acc -> new Authenticator() {
private transient BiFunction<MailAccount, Run<?,?>, Authenticator> authenticatorProvider = (acc, run) -> new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(acc.getSmtpUsername(), Secret.toString(acc.getSmtpPassword()));
StandardUsernamePasswordCredentials c = CredentialsProvider.findCredentialById(acc.getCredentialsId(),
StandardUsernamePasswordCredentials.class,
run,
(DomainRequirement) null);

if(c == null) {
return null;
}

return new PasswordAuthentication(c.getUsername(), Secret.toString(c.getPassword()));
}
};

Expand Down Expand Up @@ -281,7 +295,7 @@ public void setDefaultSuffix(String defaultSuffix) {
}

@Restricted(NoExternalUse.class)
Session createSession(MailAccount acc) {
Session createSession(MailAccount acc, ExtendedEmailPublisherContext context) {
final String SMTP_PORT_PROPERTY = "mail.smtp.port";
final String SMTP_SOCKETFACTORY_PORT_PROPERTY = "mail.smtp.socketFactory.port";

Expand Down Expand Up @@ -333,7 +347,7 @@ Session createSession(MailAccount acc) {
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
}
if (!StringUtils.isBlank(acc.getSmtpUsername())) {
if (!StringUtils.isBlank(acc.getCredentialsId())) {
props.put("mail.smtp.auth", "true");
}

Expand All @@ -350,14 +364,14 @@ Session createSession(MailAccount acc) {
LOGGER.log(Level.WARNING, "Parameters parse fail.", e);
}

return Session.getInstance(props, getAuthenticator(acc));
return Session.getInstance(props, getAuthenticator(acc, context));
}

private Authenticator getAuthenticator(final MailAccount acc) {
if (acc == null || StringUtils.isBlank(acc.getSmtpUsername())) {
private Authenticator getAuthenticator(final MailAccount acc, final ExtendedEmailPublisherContext context) {
if (acc == null || StringUtils.isBlank(acc.getCredentialsId())) {
return null;
}
return authenticatorProvider.apply(acc);
return authenticatorProvider.apply(acc, context.getRun());
}

public String getHudsonUrl() {
Expand Down Expand Up @@ -819,11 +833,11 @@ public Permission getRequiredGlobalConfigPagePermission() {
return Jenkins.MANAGE;
}

Function<MailAccount, Authenticator> getAuthenticatorProvider() {
BiFunction<MailAccount, Run<?, ?>, Authenticator> getAuthenticatorProvider() {
return authenticatorProvider;
}

void setAuthenticatorProvider(Function<MailAccount, Authenticator> authenticatorProvider) {
void setAuthenticatorProvider(BiFunction<MailAccount, Run<?,?>, Authenticator> authenticatorProvider) {
this.authenticatorProvider = authenticatorProvider;
}
}
Loading