-
Notifications
You must be signed in to change notification settings - Fork 234
/
CredentialsProviderTest.java
307 lines (255 loc) · 17.7 KB
/
CredentialsProviderTest.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* The MIT License
*
* Copyright (c) 2011-2012, CloudBees, Inc., Stephen Connolly.
*
* 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 com.cloudbees.plugins.credentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.impl.DummyCredentials;
import com.cloudbees.plugins.credentials.impl.DummyLegacyCredentials;
import hudson.model.Descriptor;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Node;
import hudson.slaves.NodeProperty;
import hudson.model.User;
import hudson.slaves.DumbSlave;
import hudson.slaves.JNLPLauncher;
import hudson.slaves.RetentionStrategy;
import hudson.security.ACL;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class CredentialsProviderTest {
@Rule
public JenkinsRule r = new JenkinsRule();
@Test
public void testNoCredentialsUntilWeAddSome() throws Exception {
FreeStyleProject project = r.createFreeStyleProject();
assertTrue(CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
SystemCredentialsProvider.getInstance().getCredentials().add(
new DummyCredentials(CredentialsScope.SYSTEM, "foo", "bar"));
assertFalse(CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, ACL.SYSTEM).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.ANONYMOUS).isEmpty());
assertFalse("null auth -> ACL.SYSTEM",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Authentication) null).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.get()).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (ItemGroup) null).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, project).isEmpty());
SystemCredentialsProvider.getInstance().getCredentials().add(
new DummyCredentials(CredentialsScope.GLOBAL, "manchu", "bar"));
assertFalse(CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, ACL.SYSTEM).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.ANONYMOUS).isEmpty());
assertFalse("null auth -> ACL.SYSTEM",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Authentication) null).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.get()).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (ItemGroup) null).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, project).isEmpty());
assertEquals(CredentialsProvider.lookupCredentials(DummyCredentials.class, project).size(), 1);
assertEquals(
CredentialsProvider.lookupCredentials(DummyCredentials.class, project).iterator().next().getUsername(),
"manchu");
}
@Test
public void testNoCredentialsUntilWeAddSomeViaStore() throws Exception {
FreeStyleProject project = r.createFreeStyleProject();
assertTrue(CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
CredentialsStore store = CredentialsProvider.lookupStores(Jenkins.get()).iterator().next();
store.addCredentials(Domain.global(), new DummyCredentials(CredentialsScope.SYSTEM, "foo", "bar"));
assertFalse(CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, ACL.SYSTEM).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.ANONYMOUS).isEmpty());
assertFalse("null auth -> ACL.SYSTEM",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Authentication) null).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.get()).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (ItemGroup) null).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, project).isEmpty());
store.addCredentials(Domain.global(), new DummyCredentials(CredentialsScope.GLOBAL, "manchu", "bar"));
assertFalse(CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, ACL.SYSTEM).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.ANONYMOUS).isEmpty());
assertFalse("null auth -> ACL.SYSTEM",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Authentication) null).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, Jenkins.get()).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null).isEmpty());
assertFalse("null item -> Root",
CredentialsProvider.lookupCredentials(DummyCredentials.class, (ItemGroup) null).isEmpty());
assertFalse(CredentialsProvider.lookupCredentials(DummyCredentials.class, project).isEmpty());
assertEquals(CredentialsProvider.lookupCredentials(DummyCredentials.class, project).size(), 1);
assertEquals(
CredentialsProvider.lookupCredentials(DummyCredentials.class, project).iterator().next().getUsername(),
"manchu");
}
@Test
public void testManageUserCredentials() throws IOException {
final User alice = User.get("alice");
DummyCredentials aliceCred1 = new DummyCredentials(CredentialsScope.USER, "aliceCred1", "pwd");
DummyCredentials aliceCred2 = new DummyCredentials(CredentialsScope.USER, "aliceCred2", "pwd");
DummyCredentials aliceCred3 = new DummyCredentials(CredentialsScope.USER, "aliceCred3", "pwd");
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
CredentialsStore userStore;
SecurityContext ctx = ACL.impersonate(alice.impersonate());
userStore = CredentialsProvider.lookupStores(alice).iterator().next();
userStore.addCredentials(Domain.global(), aliceCred1);
userStore.addCredentials(Domain.global(), aliceCred2);
assertEquals(2, CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null, alice.impersonate(), Collections.<DomainRequirement>emptyList()).size());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, Jenkins.ANONYMOUS, Collections.<DomainRequirement>emptyList()).isEmpty());
// Remove credentials
userStore.removeCredentials(Domain.global(), aliceCred2);
assertEquals(1, CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null, alice.impersonate(), Collections.<DomainRequirement>emptyList()).size());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).isEmpty());
assertTrue(CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, Jenkins.ANONYMOUS, Collections.<DomainRequirement>emptyList()).isEmpty());
// Update credentials
userStore.updateCredentials(Domain.global(), aliceCred1, aliceCred3);
assertEquals(1, CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null, alice.impersonate(), Collections.<DomainRequirement>emptyList()).size());
assertEquals(aliceCred3.getUsername(), CredentialsProvider.lookupCredentials(DummyCredentials.class, (Item) null, alice.impersonate(), Collections.<DomainRequirement>emptyList()).get(0).getUsername());
SecurityContextHolder.setContext(ctx);
}
@Test
public void testUpdateAndDeleteCredentials() throws IOException {
FreeStyleProject project = r.createFreeStyleProject();
DummyCredentials systemCred = new DummyCredentials(CredentialsScope.SYSTEM, "systemCred", "pwd");
DummyCredentials systemCred2 = new DummyCredentials(CredentialsScope.SYSTEM, "systemCred2", "pwd");
DummyCredentials globalCred = new DummyCredentials(CredentialsScope.GLOBAL, "globalCred", "pwd");
DummyCredentials modCredential = new DummyCredentials(CredentialsScope.GLOBAL, "modCredential", "pwd");
CredentialsStore store = CredentialsProvider.lookupStores(Jenkins.get()).iterator().next();
// Add credentials
store.addCredentials(Domain.global(), systemCred);
store.addCredentials(Domain.global(), systemCred2);
store.addCredentials(Domain.global(), globalCred);
assertEquals(3, CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).size());
assertEquals(1, CredentialsProvider.lookupCredentials(DummyCredentials.class, project, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).size());
assertEquals(globalCred.getUsername(), CredentialsProvider.lookupCredentials(DummyCredentials.class, project, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).get(0).getUsername());
// Update credentials
store.updateCredentials(Domain.global(), globalCred, modCredential);
assertEquals(3, CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).size());
assertEquals(1, CredentialsProvider.lookupCredentials(DummyCredentials.class, project, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).size());
assertEquals(modCredential.getUsername(), CredentialsProvider.lookupCredentials(DummyCredentials.class, project, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).get(0).getUsername());
// Remove credentials
store.removeCredentials(Domain.global(), systemCred2);
assertEquals(2, CredentialsProvider.lookupCredentials(DummyCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).size());
assertEquals(1, CredentialsProvider.lookupCredentials(DummyCredentials.class, project, ACL.SYSTEM, Collections.<DomainRequirement>emptyList()).size());
}
@Test
public void testHaveDummyCredentialsType() throws Exception {
assertTrue(!CredentialsProvider.allCredentialsDescriptors().isEmpty());
DummyCredentials.DescriptorImpl descriptor = null;
for (Descriptor<Credentials> d : CredentialsProvider.allCredentialsDescriptors()) {
if (d instanceof DummyCredentials.DescriptorImpl) {
descriptor = (DummyCredentials.DescriptorImpl) d;
break;
}
}
assertNotNull(descriptor);
assertNotNull(new DummyCredentials(CredentialsScope.SYSTEM, "foo", "bar").getDescriptor());
}
@Test
public void testLegacyCredentialMigration() throws Exception {
DummyLegacyCredentials legacyCredentials = new DummyLegacyCredentials(CredentialsScope.GLOBAL, "foo", "bar");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(legacyCredentials);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Credentials c = (Credentials) ois.readObject();
assertTrue("Resolved credentials are UsernamePasswordCredentials", c instanceof UsernamePasswordCredentials);
assertTrue("Resolved credentials are DummyCredentials", c instanceof DummyCredentials);
assertFalse("Resolved credentials are not DummyLegacyCredentials", c instanceof DummyLegacyCredentials);
assertTrue("No credentials currently", CredentialsProvider.lookupCredentials(Credentials.class).isEmpty());
SystemCredentialsProvider.getInstance().getCredentials().add(c);
final List<DummyLegacyCredentials> resolved =
CredentialsProvider.lookupCredentials(DummyLegacyCredentials.class);
assertFalse("Have resolved credentials", resolved.isEmpty());
DummyLegacyCredentials r = resolved.iterator().next();
assertEquals(legacyCredentials.getScope(), r.getScope());
assertEquals(legacyCredentials.getUsername(), r.getUsername());
assertEquals(legacyCredentials.getPassword(), r.getPassword());
}
@Test
public void testNodeCredentialFingerprintsAreRemovedForNonExistentNodes() throws Exception {
// Create dummy credentials to use
DummyCredentials globalCred = new DummyCredentials(CredentialsScope.GLOBAL, "globalCred", "pwd");
// Find how many times this credential has been currently tracked
int initialFingerprintSize = CredentialsProvider.getOrCreateFingerprintOf(globalCred).getFacets().size();
// Create a DumbSlave, this time don't add it to the model,
// it should not be recorded
DumbSlave nonAddedSlave = new DumbSlave("non-added-slave",
"dummy", "/home/test/slave", "1", Node.Mode.NORMAL, "remote",
new JNLPLauncher(),
RetentionStrategy.INSTANCE, Collections.<NodeProperty<?>>emptyList());
CredentialsProvider.track(nonAddedSlave, globalCred);
assertEquals(initialFingerprintSize, CredentialsProvider.getOrCreateFingerprintOf(globalCred).getFacets().size());
// Create a DumbSlave to use, and add it to the Jenkins model, this
// one should be recorded
DumbSlave addedSlave = new DumbSlave("added-slave",
"dummy", "/home/test/slave", "1", Node.Mode.NORMAL, "remote",
new JNLPLauncher(),
RetentionStrategy.INSTANCE, Collections.<NodeProperty<?>>emptyList());
Jenkins.get().addNode(addedSlave);
CredentialsProvider.track(addedSlave, globalCred);
assertEquals(initialFingerprintSize+1, CredentialsProvider.getOrCreateFingerprintOf(globalCred).getFacets().size());
// Track the usage of the credential for a second time, this should
// not increase the number of fingerprints further
CredentialsProvider.track(addedSlave, globalCred);
assertEquals(initialFingerprintSize+1, CredentialsProvider.getOrCreateFingerprintOf(globalCred).getFacets().size());
// Remove the added slave from Jenkins, and track the non-added slave
// to flush any mapped credentials for nodes that no longer exist.
Jenkins.get().removeNode(addedSlave);
CredentialsProvider.track(nonAddedSlave, globalCred);
assertEquals(initialFingerprintSize, CredentialsProvider.getOrCreateFingerprintOf(globalCred).getFacets().size());
}
}