Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 30 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
</scm>

<properties>
<jenkins.version>1.580.3</jenkins.version>
<jenkins.version>1.596.1</jenkins.version>
<java.level>6</java.level>
<workflow.version>1.7</workflow.version>
</properties>

<repositories>
Expand All @@ -55,6 +56,11 @@
<artifactId>credentials</artifactId>
<version>1.22</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.10-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>authentication-tokens</artifactId>
Expand All @@ -71,6 +77,29 @@
<artifactId>icon-shim</artifactId>
<version>1.0.3</version>
</dependency>

<!-- for Pipeline-based unit tests -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted in a later version. You can probably delete this dep?

<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jenkinsci.plugins.docker.commons.credentials;

import java.io.IOException;

import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
import org.jenkinsci.plugins.credentialsbinding.impl.AbstractOnDiskBinding;
import org.jenkinsci.plugins.docker.commons.Messages;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.FilePath;

public class DockerServerCredentialsBinding extends AbstractOnDiskBinding<DockerServerCredentials> {

@DataBoundConstructor
public DockerServerCredentialsBinding(String variable, String credentialsId) {
super(variable, credentialsId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be possible to extend MultiBinding directly (using UnbindableDir), which would enable you to supply a default value for variable by turning it into a @DataBoundSetter. (In the current inheritance chain this is impossible since the field is final.) Would enable Pipeline syntax to be simply

withCredentials([dockerCert(credentialsId: 'docker-client-cert')]) {
  sh 'docker push'
}

Not sure if it is worth the effort to make that refactoring, just FYI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but, well, I will pass. Lazy me...

I've pushed fixes for your other review comments.

}

@Override
protected Class<DockerServerCredentials> type() {
return DockerServerCredentials.class;
}

@Override
protected FilePath write(DockerServerCredentials credentials, FilePath dir) throws IOException, InterruptedException {
FilePath clientKey = dir.child("key.pem");
clientKey.write(credentials.getClientKey(), null);
clientKey.chmod(0600);

FilePath clientCert = dir.child("cert.pem");
clientCert.write(credentials.getClientCertificate(), null);
clientCert.chmod(0600);

FilePath serverCACert = dir.child("ca.pem");
serverCACert.write(credentials.getServerCaCertificate(), null);
serverCACert.chmod(0600);

return dir;
}

@Extension(optional = true)
public static class DescriptorImpl extends BindingDescriptor<DockerServerCredentials> {

@Override
protected Class<DockerServerCredentials> type() {
return DockerServerCredentials.class;
}

@Override
public String getDisplayName() {
return Messages.DockerServerCredentialsBinding_DisplayName();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DockerServerDomainSpecification.DisplayName=Docker Server Credentials
DockerFingerprintAction.DisplayName=Docker Fingerprints
DockerServerCredentialsBinding.DisplayName=Docker client certificate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add trailing newline

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License

Copyright 2013 jglick.

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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:c="/lib/credentials">
<f:entry title="${%Variable}" field="variable">
<f:textbox default="DOCKER_CERT_PATH"/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Name of an environment variable to be set during the build.<br>
Its value will be the absolute path of the directory where the <code>{ca,cert,key}.pem</code> files will be created.<br>
You probably want to call this variable <code>DOCKER_CERT_PATH</code>, which will be undestood by the docker client binary.<br>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* 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.
*/

package org.jenkinsci.plugins.docker.commons.credentials;

import java.util.Collections;

import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.credentialsbinding.impl.BindingStep;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerDomainSpecification;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.StepConfigTester;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import hudson.Functions;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainSpecification;

import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.*;

public class DockerServerCredentialsBindingTest {

@Rule
public RestartableJenkinsRule story = new RestartableJenkinsRule();

@Test
public void configRoundTrip() throws Exception {
story.addStep(new Statement() {
@SuppressWarnings("rawtypes")
@Override
public void evaluate() throws Throwable {
CredentialsStore store = CredentialsProvider.lookupStores(story.j.getInstance()).iterator().next();
assertThat(store, instanceOf(SystemCredentialsProvider.StoreImpl.class));
Domain domain = new Domain("docker", "A domain for docker credentials",
Collections.<DomainSpecification> singletonList(new DockerServerDomainSpecification()));
DockerServerCredentials c = new DockerServerCredentials(CredentialsScope.GLOBAL,
"docker-client-cert", "desc", "clientKey", "clientCertificate", "serverCaCertificate");
store.addDomain(domain, c);
BindingStep s = new StepConfigTester(story.j)
.configRoundTrip(new BindingStep(Collections.<MultiBinding> singletonList(
new DockerServerCredentialsBinding("DOCKER_CERT_PATH", "docker-client-cert"))));
story.j.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(
new DockerServerCredentialsBinding("DOCKER_CERT_PATH", "docker-client-cert")));
}
});
}

@Test
public void basics() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
DockerServerCredentials c = new DockerServerCredentials(CredentialsScope.GLOBAL,
"docker-client-cert", "desc", "clientKey", "clientCertificate", "serverCaCertificate");
CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c);
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
final String shellStep1, shellStep2;
if (Functions.isWindows()) {
shellStep1 = ""
+ " bat '''\n"
+ " REM check existence of the credentials dir\n"
+ " if not exist %DOCKER_CERT_PATH% exit /B 1\n"
+ "\n"
+ " REM check existence of the certificate files\n"
+ " if not exist %DOCKER_CERT_PATH%\\\\key.pem exit /B 1\n"
+ " if not exist %DOCKER_CERT_PATH%\\\\cert.pem exit /B 1\n"
+ " if not exist %DOCKER_CERT_PATH%\\\\ca.pem exit /B 1\n"
+ "\n"
+ " REM keep location of the certificate dir for the next step\n"
+ " echo %DOCKER_CERT_PATH% > cert-path\n"
+ " '''\n";
shellStep2 = ""
+ " bat '''\n"
+ " REM get path of the certificate directory\n"
+ " if not exist cert-path exit /B 1\n"
+ " set /p cert_path=<cert-path\n"
+ "\n"
+ " REM check it has been deleted\n"
+ " if exist %cert_path% exit /B 1\n"
+ " '''\n";
} else {
shellStep1 = ""
+ " sh '''\n"
+ " set -e -x\n"
+ "\n"
+ " # check permissions on the credentials dir and its parent\n"
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH\") = 700 ]\n"
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH\"/..) = 700 ]\n"
+ "\n"
+ " # check permissions and content of the certificate files\n"
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH/key.pem\") = 600 ]\n"
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH/cert.pem\") = 600 ]\n"
+ " [ $(stat -c %a \"$DOCKER_CERT_PATH/ca.pem\") = 600 ]\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW this stuff could be put into src/test/resources/ and copied prior to running the build using TheClass.class.getResource[AsStream] and Node.getWorkspaceFor. Might be more readable that way.

+ " [ $(stat -c %s \"$DOCKER_CERT_PATH/key.pem\") = 9 ]\n"
+ " [ $(stat -c %s \"$DOCKER_CERT_PATH/cert.pem\") = 17 ]\n"
+ " [ $(stat -c %s \"$DOCKER_CERT_PATH/ca.pem\") = 19 ]\n"
+ "\n"
+ " # keep location of the certificate dir for the next step\n"
+ " echo \"$DOCKER_CERT_PATH\" > cert-path\n"
+ " '''\n";
shellStep2 = ""
+ " sh '''\n"
+ " set -e -x\n"
+ "\n"
+ " # get path of the certificate directory\n"
+ " cert_path=$(cat cert-path)\n"
+ "\n"
+ " # check it was where we would expect it to be\n"
+ " echo \"$cert_path\" | grep -q \"/workspace/p@tmp/secretFiles/[-0-9a-f]\\\\{36\\\\}$\"\n"
+ "\n"
+ " # check it has been deleted\n"
+ " if [ -e \"$cert_path\" ] ; then\n"
+ " echo \"$cert_path still exists!!!\" >&2\n"
+ " exit 1\n"
+ " fi\n"
+ " '''\n";
}
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " withCredentials([[$class: 'DockerServerCredentialsBinding',\n"
+ " variable: 'DOCKER_CERT_PATH',\n"
+ " credentialsId: 'docker-client-cert']]) {\n"
+ " semaphore 'basics'\n"
+ "\n"
+ shellStep1
+ " }\n"
+ "\n"
+ shellStep2
+ "}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("basics/1", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
assertNotNull(p);
WorkflowRun b = p.getBuildByNumber(1);
assertNotNull(b);
SemaphoreStep.success("basics/1", null);
story.j.waitForCompletion(b);
story.j.assertBuildStatusSuccess(b);
}
});
}

}