-
Notifications
You must be signed in to change notification settings - Fork 140
/
VaultStringCredentialImpl.java
86 lines (68 loc) · 2.89 KB
/
VaultStringCredentialImpl.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
package com.datapipe.jenkins.vault.credentials.common;
import com.cloudbees.plugins.credentials.CredentialsScope;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
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;
public class VaultStringCredentialImpl extends AbstractVaultBaseStandardCredentials implements VaultStringCredential {
public static final String DEFAULT_VAULT_KEY = "secret";
private static final long serialVersionUID = 1L;
private String vaultKey;
@DataBoundConstructor
public VaultStringCredentialImpl(CredentialsScope scope, String id, String description) {
super(scope, id, description);
}
@NonNull
public String getVaultKey() {
return vaultKey;
}
@DataBoundSetter
public void setVaultKey(String vaultKey) {
this.vaultKey = defaultIfBlank(vaultKey, DEFAULT_VAULT_KEY);
}
@NonNull
@Override
public Secret getSecret() {
String k = defaultIfBlank(vaultKey, DEFAULT_VAULT_KEY);
String s = getVaultSecretKeyValue(k);
return Secret.fromString(s);
}
@Extension
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
@Override
public String getDisplayName() {
return "Vault Secret Text Credential";
}
public FormValidation doTestConnection(
@AncestorInPath ItemGroup<Item> context,
@QueryParameter("path") String path,
@QueryParameter("vaultKey") String vaultKey,
@QueryParameter("prefixPath") String prefixPath,
@QueryParameter("namespace") String namespace,
@QueryParameter("engineVersion") Integer engineVersion) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
try {
getVaultSecretKey(path, defaultIfBlank(vaultKey, DEFAULT_VAULT_KEY), prefixPath, namespace, engineVersion, context);
} catch (Exception e) {
return FormValidation.error("FAILED to retrieve Vault secret: \n" + e);
}
return FormValidation
.ok("Successfully retrieved secret by key " + vaultKey);
}
@SuppressWarnings("unused") // used by stapler
public ListBoxModel doFillEngineVersionItems(@AncestorInPath Item context) {
return engineVersions(context);
}
}
}