Skip to content

Commit fdb6714

Browse files
WIP: DockerServerCredentialsBinding
1 parent 9a91805 commit fdb6714

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
<classifier>tests</classifier>
7878
<scope>test</scope>
7979
</dependency>
80+
<dependency>
81+
<groupId>org.jenkins-ci.plugins</groupId>
82+
<artifactId>docker-commons</artifactId>
83+
<version>1.0</version>
84+
<optional>true</optional>
85+
</dependency>
8086
<dependency>
8187
<groupId>org.jenkins-ci.plugins.workflow</groupId>
8288
<artifactId>workflow-support</artifactId>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.jenkinsci.plugins.credentialsbinding.impl;
2+
3+
import java.io.IOException;
4+
import java.util.UUID;
5+
6+
import org.jenkinsci.plugins.credentialsbinding.Binding;
7+
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
8+
import org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials;
9+
import org.kohsuke.stapler.DataBoundConstructor;
10+
11+
import hudson.Extension;
12+
import hudson.FilePath;
13+
import hudson.Launcher;
14+
import hudson.model.TaskListener;
15+
import hudson.model.Run;
16+
17+
import static org.jenkinsci.plugins.credentialsbinding.impl.TempDirUtils.*;
18+
19+
public class DockerServerCredentialsBinding extends
20+
Binding<DockerServerCredentials> {
21+
22+
@DataBoundConstructor
23+
public DockerServerCredentialsBinding(String variable, String credentialsId) {
24+
super(variable, credentialsId);
25+
}
26+
27+
@Override
28+
protected Class<DockerServerCredentials> type() {
29+
return DockerServerCredentials.class;
30+
}
31+
32+
@Override
33+
public SingleEnvironment bindSingle(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener)
34+
throws IOException, InterruptedException {
35+
DockerServerCredentials credentials = getCredentials(build);
36+
FilePath secrets = secretsDir(workspace);
37+
String dirName = UUID.randomUUID().toString();
38+
final FilePath dir = secrets.child(dirName);
39+
dir.mkdirs();
40+
secrets.chmod(0700);
41+
dir.chmod(0700);
42+
43+
FilePath clientKey = dir.child("key.pem");
44+
clientKey.write(credentials.getClientKey(), null);
45+
clientKey.chmod(0600);
46+
47+
FilePath clientCert = dir.child("cert.pem");
48+
clientCert.write(credentials.getClientCertificate(), null);
49+
clientCert.chmod(0600);
50+
51+
FilePath serverCACert = dir.child("ca.pem");
52+
serverCACert.write(credentials.getServerCaCertificate(), null);
53+
serverCACert.chmod(0600);
54+
55+
return new SingleEnvironment(dir.getRemote(), new UnbinderImpl(dirName));
56+
}
57+
58+
@Extension
59+
public static class DescriptorImpl extends
60+
BindingDescriptor<DockerServerCredentials> {
61+
62+
@Override
63+
protected Class<DockerServerCredentials> type() {
64+
return DockerServerCredentials.class;
65+
}
66+
67+
@Override
68+
public String getDisplayName() {
69+
return Messages
70+
.DockerServerCredentialsBinding_docker_client_certificate();
71+
}
72+
73+
}
74+
75+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
The MIT License
4+
5+
Copyright 2013 jglick.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
-->
25+
<?jelly escape-by-default='true'?>
26+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:c="/lib/credentials">
27+
<f:entry title="${%Variable}" field="variable">
28+
<f:textbox default="DOCKER_CERT_PATH"/>
29+
</f:entry>
30+
</j:jelly>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
Name of an environment variable to be set during the build.<br>
3+
Its value will be the absolute path of the directory where the <code>{ca,cert,key}.pem</code> files will be created.<br>
4+
You probably want to call this variable <code>DOCKER_CERT_PATH</code>, which will be undestood by the docker client binary.<br>
5+
</div>

src/main/resources/org/jenkinsci/plugins/credentialsbinding/impl/Messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ StringBinding.secret_text=Secret text
44
UsernamePasswordBinding.username_and_password=Username and password (conjoined)
55
UsernamePasswordMultiBinding.username_and_password=Username and password (separated)
66
ZipFileBinding.secret_zip_file=Secret ZIP file
7+
DockerServerCredentialsBinding.docker_client_certificate=Docker client certificate

0 commit comments

Comments
 (0)