Skip to content

Commit

Permalink
Merge pull request #11 from scoheb/ssl-cert
Browse files Browse the repository at this point in the history
Update to support SSL certificate authentication.
  • Loading branch information
scoheb committed Jan 13, 2017
2 parents 6414885 + 33207db commit 0076054
Show file tree
Hide file tree
Showing 26 changed files with 694 additions and 170 deletions.
Expand Up @@ -129,6 +129,11 @@ private void startTriggerThread() {
stopTriggerThread();
JMSMessagingProvider provider = GlobalCIConfiguration.get()
.getProvider(providerName);
if (provider == null) {
log.log(Level.SEVERE, "Failed to locate JMSMessagingProvider with name "
+ providerName + ". You must update the job configuration. Trigger not started.");
return;
}
CITriggerThread trigger = new CITriggerThread(provider, job
.getFullName(), selector);
trigger.setName("CIBuildTrigger-" + job.getFullName() + "-" + provider.getClass().getSimpleName());
Expand Down
Expand Up @@ -115,8 +115,15 @@ public Integer getTimeout() {

public String waitforCIMessage(Run<?, ?> build, Launcher launcher, TaskListener listener) {
GlobalCIConfiguration config = GlobalCIConfiguration.get();
JMSMessagingProvider provider = config.getProvider(providerName);
if (provider == null) {
listener.error("Failed to locate JMSMessagingProvider with name "
+ providerName + ". You must update the job configuration.");
return null;
}

JMSMessagingWorker worker =
config.getProvider(getProviderName()).createWorker(build
provider.createWorker(build
.getParent().getName());
return worker.waitForMessage(build, selector, variable, timeout);
}
Expand Down
@@ -1,24 +1,27 @@
package com.redhat.jenkins.plugins.ci;

import com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingProvider;
import com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider;
import hudson.Extension;
import hudson.model.Failure;
import hudson.util.Secret;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import hudson.model.Failure;
import hudson.util.Secret;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

import com.redhat.jenkins.plugins.ci.authentication.activemq.UsernameAuthenticationMethod;
import com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingProvider;
import com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider;

/*
* The MIT License
*
Expand Down Expand Up @@ -89,7 +92,7 @@ protected Object readResolve() {
if (getProvider(DEFAULT_PROVIDER)==null) {
log.info("There is no default Message Provider.");
configs.add(new ActiveMqMessagingProvider(DEFAULT_PROVIDER,
broker, topic, user, password));
broker, topic, new UsernameAuthenticationMethod(user, password)));
log.info("Added default Message Provider using deprecated configuration.");
setMigrationInProgress(true);
save();
Expand All @@ -98,17 +101,28 @@ protected Object readResolve() {
}
}
}
// Examine providers
if (configs != null) {
for (JMSMessagingProvider config: configs) {
if (config instanceof ActiveMqMessagingProvider) {
ActiveMqMessagingProvider aconfig = (ActiveMqMessagingProvider) config;
if (aconfig.IsMigrationInProgress()) {
log.info("Migration in progress for ActiveMqMessagingProvider " + aconfig.getName());
save();
}
}
}
}
return this;
}

@SuppressWarnings("unused")
@DataBoundSetter
public void setConfigs(List<JMSMessagingProvider> configs) {
this.configs = configs;
}

public List<JMSMessagingProvider> getConfigs() {
return Collections.unmodifiableList(configs);
return Collections.unmodifiableList(this.configs);
}

public boolean addMessageProvider(JMSMessagingProvider provider) {
Expand All @@ -121,7 +135,7 @@ public boolean addMessageProvider(JMSMessagingProvider provider) {
}

public JMSMessagingProvider getProvider(String name) {
for (JMSMessagingProvider provider: configs) {
for (JMSMessagingProvider provider: getConfigs()) {
if (provider.getName().equals(name)) {
return provider;
}
Expand Down
@@ -0,0 +1,58 @@
package com.redhat.jenkins.plugins.ci.authentication;

import hudson.ExtensionList;
import hudson.model.Describable;
import hudson.model.Descriptor;

import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;

/*
* The MIT License
*
* Copyright (c) Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public abstract class AuthenticationMethod implements Describable<AuthenticationMethod>, Serializable {

private static final long serialVersionUID = -6077120270692721571L;
private transient static final Logger log = Logger.getLogger(AuthenticationMethod.class.getName());

public abstract static class AuthenticationMethodDescriptor extends Descriptor<AuthenticationMethod> {
public static ExtensionList<AuthenticationMethodDescriptor> all() {
return Jenkins.getInstance().getExtensionList(AuthenticationMethodDescriptor.class);
}

public static boolean isValidURL(String url) {
try {
new URI(url);
} catch (URISyntaxException e) {
log.log(Level.SEVERE, "URISyntaxException, returning false.");
return false;
}
return true;
}
}
}
@@ -0,0 +1,35 @@
package com.redhat.jenkins.plugins.ci.authentication.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.redhat.jenkins.plugins.ci.authentication.AuthenticationMethod;

/*
* The MIT License
*
* Copyright (c) Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public abstract class ActiveMQAuthenticationMethod extends AuthenticationMethod {

private static final long serialVersionUID = -6077120270692721571L;

public abstract ActiveMQConnectionFactory getConnectionFactory(String broker);
}
@@ -0,0 +1,162 @@
package com.redhat.jenkins.plugins.ci.authentication.activemq;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import hudson.util.Secret;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jms.Connection;
import javax.jms.Session;
import javax.servlet.ServletException;

import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

import org.apache.activemq.ActiveMQSslConnectionFactory;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import com.redhat.jenkins.plugins.ci.Messages;
import com.redhat.jenkins.plugins.ci.authentication.AuthenticationMethod;

/*
* The MIT License
*
* Copyright (c) Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public class SSLCertificateAuthenticationMethod extends ActiveMQAuthenticationMethod {
private static final long serialVersionUID = -5934219869726669459L;
private transient static final Logger log = Logger.getLogger(SSLCertificateAuthenticationMethod.class.getName());

private String keystore;
private Secret keypwd = Secret.fromString("");
private String truststore;
private Secret trustpwd = Secret.fromString("");

@DataBoundConstructor
public SSLCertificateAuthenticationMethod(String keystore, Secret keypwd, String truststore, Secret trustpwd) {
this.setKeystore(keystore);
this.setKeypwd(keypwd);
this.setTruststore(truststore);
this.setTrustpwd(trustpwd);
}

public String getKeystore() {
return keystore;
}

public void setKeystore(String keystore) {
this.keystore = keystore;
}

public Secret getKeypwd() {
return keypwd;
}

public void setKeypwd(Secret password) {
this.keypwd = password;
}

public String getTruststore() {
return truststore;
}

public void setTruststore(String truststore) {
this.truststore = truststore;
}

public Secret getTrustpwd() {
return trustpwd;
}

public void setTrustpwd(Secret trustpwd) {
this.trustpwd = trustpwd;
}

@Override
public ActiveMQSslConnectionFactory getConnectionFactory(String broker) {
try {
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory(broker);
connectionFactory.setKeyStore(getKeystore());
connectionFactory.setKeyStorePassword(Secret.toString(getKeypwd()));
connectionFactory.setTrustStore(getTruststore());
connectionFactory.setTrustStorePassword(Secret.toString(getTrustpwd()));
return connectionFactory;
} catch (Exception e) {
log.log(Level.SEVERE, "Unhandled exception creating connection factory.", e);;
}
return null;
}

@Override
public Descriptor<AuthenticationMethod> getDescriptor() {
return Jenkins.getInstance().getDescriptorByType(SSLCertificateAuthenticationMethodDescriptor.class);
}

@Extension
public static class SSLCertificateAuthenticationMethodDescriptor extends AuthenticationMethodDescriptor {

@Override
public String getDisplayName() {
return "SSL certificate";
}

@Override
public SSLCertificateAuthenticationMethod newInstance(StaplerRequest sr, JSONObject jo) {
return new SSLCertificateAuthenticationMethod(jo.getString("keystore"), Secret.fromString(jo.getString("keypwd")),
jo.getString("truststore"), Secret.fromString(jo.getString("trustpwd")));
}

public String getConfigPage() {
return "sslcert.jelly";
}

public FormValidation doTestConnection(@QueryParameter("broker") String broker,
@QueryParameter("keystore") String keystore,
@QueryParameter("keypwd") String keypwd,
@QueryParameter("truststore") String truststore,
@QueryParameter("trustpwd") String trustpwd) throws ServletException {
broker = StringUtils.strip(StringUtils.stripToNull(broker), "/");
if (broker != null && isValidURL(broker)) {
try {
SSLCertificateAuthenticationMethod sam = new SSLCertificateAuthenticationMethod(keystore, Secret.fromString(keypwd), truststore, Secret.fromString(trustpwd));
ActiveMQSslConnectionFactory connectionFactory = sam.getConnectionFactory(broker);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
session.close();
connection.close();
return FormValidation.ok(Messages.SuccessBrokerConnect(broker));
} catch (Exception e) {
log.log(Level.SEVERE, "Unhandled exception in SSLCertificateAuthenticationMethod.doTestConnection: ", e);
return FormValidation.error(Messages.Error() + ": " + e);
}
} else {
return FormValidation.error(Messages.InvalidURI());
}
}
}
}

0 comments on commit 0076054

Please sign in to comment.