-
Notifications
You must be signed in to change notification settings - Fork 140
/
VaultCertificateCredentialsImpl.java
172 lines (148 loc) · 6.55 KB
/
VaultCertificateCredentialsImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.datapipe.jenkins.vault.credentials.common;
import com.cloudbees.plugins.credentials.CredentialsScope;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import static com.datapipe.jenkins.vault.configuration.VaultConfiguration.engineVersions;
import static com.datapipe.jenkins.vault.credentials.common.VaultHelper.getVaultSecretKey;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
@SuppressWarnings("ALL")
public class VaultCertificateCredentialsImpl extends AbstractVaultBaseStandardCredentials implements
VaultCertificateCredentials {
private static final Logger LOGGER = Logger.getLogger(VaultCertificateCredentialsImpl.class.getName());
private static final long serialVersionUID = 1L;
private String keyStoreKey;
private String passwordKey;
@DataBoundConstructor
public VaultCertificateCredentialsImpl(CredentialsScope scope, String id, String description) {
super(scope, id, description);
}
@NonNull
public String getKeyStoreKeyKey() {
return keyStoreKey;
}
@DataBoundSetter
public void setKeyStoreKey(String keyStoreKey) {
this.keyStoreKey = defaultIfBlank(keyStoreKey, DescriptorImpl.DEFAULT_KEYSTORE_KEY);
}
@NonNull
public String getPasswordKey() {
return passwordKey;
}
@DataBoundSetter
public void setPasswordKey(String passwordKey) {
this.passwordKey = defaultIfBlank(passwordKey, DescriptorImpl.DEFAULT_PASSWORD_KEY);
}
@NonNull
@Override
public KeyStore getKeyStore() {
String secretKey = defaultIfBlank(keyStoreKey, DescriptorImpl.DEFAULT_KEYSTORE_KEY);
String base64KeyStore = getVaultSecretKeyValue(secretKey);
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance("PKCS12");
} catch (KeyStoreException e) {
throw new IllegalStateException("PKCS12 is a keystore type per the JLS spec", e);
}
try {
keyStore.load(new ByteArrayInputStream(Base64.getDecoder().decode(unwrap(base64KeyStore))), toCharArray(getPassword()));
} catch (CertificateException | NoSuchAlgorithmException | IOException e) {
LogRecord lr = new LogRecord(Level.WARNING, "Credentials ID {0}: Could not load keystore from Vault");
lr.setParameters(new Object[] { getId() });
lr.setThrown(e);
LOGGER.log(lr);
}
return keyStore;
}
@NonNull
@Override
public Secret getPassword() {
String secretKey = defaultIfBlank(passwordKey, DescriptorImpl.DEFAULT_PASSWORD_KEY);
String secret = getVaultSecretKeyValue(secretKey);
return Secret.fromString(secret);
}
@Override
public void write(FilePath keyStoreFile) throws IOException {
try {
getKeyStore().store(keyStoreFile.write(), toCharArray(getPassword()));
} catch (KeyStoreException | CertificateException | InterruptedException | NoSuchAlgorithmException e) {
LogRecord lr = new LogRecord(Level.WARNING, "Credentials ID {0}: Could not write keystore to file");
lr.setParameters(new Object[] { getId() });
lr.setThrown(e);
LOGGER.log(lr);
}
}
/**
* Helper to convert a {@link Secret} password into a {@code char[]}
*
* @param password the password.
* @return a {@code char[]} containing the password or {@code null}
*/
@CheckForNull
private static char[] toCharArray(@NonNull Secret password) {
String plainText = Util.fixEmpty(password.getPlainText());
return plainText == null ? null : plainText.toCharArray();
}
@CheckForNull
private static String unwrap(@NonNull String wrapped) {
return wrapped == null ? null : Pattern.compile("\\r?\\n").matcher(wrapped).replaceAll("");
}
@Extension
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
public static final String DEFAULT_KEYSTORE_KEY = "keystore";
public static final String DEFAULT_PASSWORD_KEY = "password";
@Override
public String getDisplayName() {
return "Vault Certificate Credential";
}
public FormValidation doTestConnection(
@AncestorInPath ItemGroup<Item> context,
@QueryParameter("path") String path,
@QueryParameter("keyStoreKey") String keyStoreKey,
@QueryParameter("passwordKey") String passwordKey,
@QueryParameter("prefixPath") String prefixPath,
@QueryParameter("namespace") String namespace,
@QueryParameter("engineVersion") Integer engineVersion) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
try {
getVaultSecretKey(path, defaultIfBlank(keyStoreKey, DEFAULT_KEYSTORE_KEY), prefixPath, namespace, engineVersion, context);
} catch (Exception e) {
return FormValidation.error("FAILED to retrieve keyStore key: \n" + e);
}
try {
getVaultSecretKey(path, defaultIfBlank(passwordKey, DEFAULT_PASSWORD_KEY), prefixPath, namespace, engineVersion, context);
} catch (Exception e) {
return FormValidation.error("FAILED to retrieve password key: \n" + e);
}
return FormValidation
.ok("Successfully retrieved keyStore and the password");
}
@SuppressWarnings("unused") // used by stapler
public ListBoxModel doFillEngineVersionItems(@AncestorInPath Item context) {
return engineVersions(context);
}
}
}