-
Notifications
You must be signed in to change notification settings - Fork 79
credentials-binding implementation for Docker client certificates #54
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
Changes from 1 commit
09d48bd
332e920
6e992a4
9381589
a1449de
628b2a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be possible to extend withCredentials([dockerCert(credentialsId: 'docker-client-cert')]) {
sh 'docker push'
}Not sure if it is worth the effort to make that refactoring, just FYI. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
| 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> | ||
|
||
| </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" | ||
|
||
| + " [ $(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); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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?