forked from cyberark/conjur-credentials-plugin
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConjurCredentialProvider.java
155 lines (125 loc) · 7.15 KB
/
ConjurCredentialProvider.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
package org.conjur.jenkins.credentials;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.CertificateCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import org.acegisecurity.Authentication;
import org.conjur.jenkins.configuration.GlobalConjurConfiguration;
import org.conjur.jenkins.conjursecrets.ConjurSecretCredentials;
import org.conjur.jenkins.conjursecrets.ConjurSecretUsernameCredentials;
import org.conjur.jenkins.conjursecrets.ConjurSecretUsernameSSHKeyCredentials;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.ModelObject;
import hudson.security.ACL;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
@Extension
public class ConjurCredentialProvider extends CredentialsProvider {
private static final Logger LOGGER = Logger.getLogger(ConjurCredentialProvider.class.getName());
private static final ConcurrentHashMap<String, Supplier<Collection<StandardCredentials>>> allCredentialSuppliers = new ConcurrentHashMap<String, Supplier<Collection<StandardCredentials>>> ();
private Supplier<Collection<StandardCredentials>> currentCredentialSupplier;
public <C extends Credentials> List<C> getCredentials(@Nonnull Class<C> type,
@Nullable ItemGroup itemGroup,
@Nullable Authentication authentication,
@Nonnull List<DomainRequirement> domainRequirements) {
LOGGER.log(Level.FINE, "getCredentials (1) type: " + type + " itemGroup: " + itemGroup);
return getCredentials(type, itemGroup, authentication);
}
@Override
@Nonnull
public <C extends Credentials> List<C> getCredentials(@Nonnull Class<C> type,
@Nonnull Item item,
@Nonnull Authentication authentication,
@Nonnull List<DomainRequirement> domainRequirements) {
LOGGER.log(Level.FINE, "getCredentials (2) type: " + type + " item: " + item);
return getCredentialsFromSupplier(type, item, authentication);
}
@Override
@Nonnull
public <C extends Credentials> List<C> getCredentials(@Nonnull Class<C> type,
ItemGroup itemGroup,
Authentication authentication) {
LOGGER.log(Level.FINE, "getCredentials (3) type: " + type + " itemGroup: " + itemGroup);
return getCredentialsFromSupplier(type, itemGroup, authentication);
}
private <C extends Credentials> List<C> getCredentialsFromSupplier(@Nonnull Class<C> type,
ModelObject context,
Authentication authentication) {
LOGGER.log(Level.FINE, "Type: " + type.getName() +
" authentication: " + authentication + " context: " + context.getDisplayName());
if (!type.isInstance(CertificateCredentials.class) && (
(type.isInstance(ConjurSecretCredentials.class) || type == ConjurSecretUsernameCredentials.class) ||
type.isAssignableFrom(ConjurSecretCredentials.class) ||
type.isAssignableFrom(ConjurSecretUsernameSSHKeyCredentials.class))) {
LOGGER.log(Level.FINE, "*****");
if (ACL.SYSTEM.equals(authentication)) {
Collection<StandardCredentials> allCredentials = Collections.emptyList();
LOGGER.log(Level.FINE, "**** getCredentials ConjurCredentialProvider: " + this.getId() + " : " + this );
LOGGER.log(Level.FINE, "Getting Credentials from ConjurCredentialProvider @ " + context.getClass().getName());
LOGGER.log(Level.FINE, "To Fetch credentials");
getStore(context);
if (currentCredentialSupplier != null) {
allCredentials = currentCredentialSupplier.get();
return allCredentials.stream()
.filter(c -> type.isAssignableFrom(c.getClass()))
// cast to keep generics happy even though we are assignable
.map(type::cast)
.collect(Collectors.toList());
}
}
}
return Collections.emptyList();
}
@Override
public ConjurCredentialStore getStore(ModelObject object) {
GlobalConjurConfiguration globalConfig = GlobalConfiguration.all().get(GlobalConjurConfiguration.class);
if (globalConfig == null || !globalConfig.getEnableJWKS() || !globalConfig.getEnableContextAwareCredentialStore()) {
LOGGER.log(Level.FINE, "No Conjur Credential Store (Content Aware)");
return null;
}
if (object == Jenkins.get()) {
return null;
}
ConjurCredentialStore store = null;
Supplier<Collection<StandardCredentials>> supplier = null;
if (object != null) {
String key = String.valueOf(object.hashCode());
if (ConjurCredentialStore.getAllStores().containsKey(key)) {
// LOGGER.log(Level.FINEST, "GetStore EXISTING ConjurCredentialProvider : " + object.getClass().getName() + ": " + object.toString() + " => " + object.hashCode());
store = ConjurCredentialStore.getAllStores().get(key);
} else {
// LOGGER.log(Level.FINEST, "GetStore NEW ConjurCredentialProvider : " + object.getClass().getName() + ": " + object.toString() + " => " + object.hashCode());
store = new ConjurCredentialStore(this, object);
supplier = memoizeWithExpiration(CredentialsSupplier.standard(object), Duration.ofSeconds(120));
ConjurCredentialStore.getAllStores().put(key, store);
allCredentialSuppliers.put(key, supplier);
}
currentCredentialSupplier = allCredentialSuppliers.get(key);
}
return store;
}
public static ConcurrentHashMap<String, Supplier<Collection<StandardCredentials>>> getAllCredentialSuppliers() {
return allCredentialSuppliers;
}
@Override
public String getIconClassName() {
return "icon-conjur-credentials-store";
}
public static <T> Supplier<T> memoizeWithExpiration(Supplier<T> base, Duration duration) {
return CustomSuppliers.memoizeWithExpiration(base, duration);
}
}