/
SamlIDPRefreshMockMvcTests.java
438 lines (367 loc) · 22 KB
/
SamlIDPRefreshMockMvcTests.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2014] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/
package org.cloudfoundry.identity.uaa.login.saml;
import org.cloudfoundry.identity.uaa.event.EntityDeletedEvent;
import org.cloudfoundry.identity.uaa.provider.SamlIdentityProviderDefinition;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneConfiguration;
import org.cloudfoundry.identity.uaa.zone.SamlConfig;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.mock.InjectedMockContextTest;
import org.cloudfoundry.identity.uaa.mock.util.MockMvcUtils;
import org.cloudfoundry.identity.uaa.test.UaaTestAccounts;
import org.cloudfoundry.identity.uaa.util.SetServerNameRequestPostProcessor;
import org.cloudfoundry.identity.uaa.provider.IdentityProvider;
import org.cloudfoundry.identity.uaa.zone.IdentityProviderProvisioning;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneProvisioning;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
import org.springframework.security.saml.metadata.ExtendedMetadataDelegate;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.http.MediaType.TEXT_HTML;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
/**
* This test ensures that UAA instances properly refresh the SAML providers from the database.
*/
public class SamlIDPRefreshMockMvcTests extends InjectedMockContextTest {
private static final String DEFAULT_SIMPLE_SAML_METADATA = String.format(MockMvcUtils.IDP_META_DATA, "http://simplesamlphp.cfapps.io/saml2/idp/metadata.php");
private UaaTestAccounts testAccounts;
private JdbcTemplate jdbcTemplate;
private IdentityProviderProvisioning providerProvisioning;
private ZoneAwareMetadataManager zoneAwareMetadataManager;
private IdentityZoneProvisioning zoneProvisioning;
private SamlIdentityProviderConfigurator configurator;
@Before
public void setUpContext() throws Exception {
SecurityContextHolder.clearContext();
testAccounts = UaaTestAccounts.standard(null);
jdbcTemplate = getWebApplicationContext().getBean(JdbcTemplate.class);
providerProvisioning = getWebApplicationContext().getBean(IdentityProviderProvisioning.class);
zoneAwareMetadataManager = getWebApplicationContext().getBean(ZoneAwareMetadataManager.class);
zoneProvisioning = getWebApplicationContext().getBean(IdentityZoneProvisioning.class);
configurator = getWebApplicationContext().getBean(SamlIdentityProviderConfigurator.class);
//ensure that we don't fire the listener, we want to test the DB refresh
getWebApplicationContext().getBean(ProviderChangedListener.class).setMetadataManager(null);
cleanSamlProviders();
}
@After
public void cleanSamlProviders() throws Exception {
jdbcTemplate.update("UPDATE identity_provider SET active=? WHERE type=?", false, OriginKeys.SAML);
for (SamlIdentityProviderDefinition definition : configurator.getIdentityProviderDefinitions()) {
configurator.removeIdentityProviderDefinition(definition);
}
for (SamlIdentityProviderDefinition definition : configurator.getIdentityProviderDefinitions()) {
configurator.removeIdentityProviderDefinition(definition);
}
assertEquals(0, configurator.getIdentityProviderDefinitions().size());
for (IdentityZone zone : zoneProvisioning.retrieveAll()) {
for (ExtendedMetadataDelegate metadata : zoneAwareMetadataManager.getManager(zone).getAvailableProviders()) {
String hostedSPName = zoneAwareMetadataManager.getManager(zone).getHostedSPName();
if (metadata.getExtendedMetadata(hostedSPName)==null ||
!metadata.getExtendedMetadata(hostedSPName).isLocal()) {
zoneAwareMetadataManager.getManager(zone).removeMetadataProvider(metadata);
}
}
zoneAwareMetadataManager.getManager(zone).setRefreshRequired(true);
getMockMvc().perform(post("/saml/metadata").with(new SetServerNameRequestPostProcessor(zone.getSubdomain() + ".localhost")));
//all we have left is the local provider
assertEquals(1, zoneAwareMetadataManager.getManager(zone).getAvailableProviders().size());
}
jdbcTemplate.update("delete from identity_provider where type=?", OriginKeys.SAML);
SecurityContextHolder.clearContext();
IdentityZoneHolder.clear();
}
@Test
public void testFallbackIDP_shows_Error_Message_Instead_Of_Default() throws Exception {
String nonExistentIDPDiscovery = "/saml/discovery?returnIDParam=idp&entityID=cloudfoundry-saml-login&idp=NON-EXISTENT-ALIAS&isPassive=true";
getMockMvc().perform(get(nonExistentIDPDiscovery))
.andExpect(status().isFound())
.andExpect(redirectedUrl("/login?error=idp_not_found"));
nonExistentIDPDiscovery = "/saml/discovery?returnIDParam=idp&entityID=cloudfoundry-saml-login&idp=NON-EXISTENT-ALIAS&isPassive=false";
getMockMvc().perform(get(nonExistentIDPDiscovery))
.andExpect(status().isFound())
.andExpect(redirectedUrl("/login?error=idp_not_found"));
}
@Test
public void testThatDBAddedXMLProviderShowsOnLoginPage() throws Exception {
addXmlProviderToDatabase();
}
@Test
public void testThatDBDeletedXMLProviderDoesNotShowOnLoginPage() throws Exception {
IdentityProvider<SamlIdentityProviderDefinition> provider = addXmlProviderToDatabase();
SamlIdentityProviderDefinition definition = provider.getConfig();
//delete from DB
EntityDeletedEvent event = new EntityDeletedEvent(provider);
getWebApplicationContext().publishEvent(event);
//verify that provider is deleted
assertEquals(0, getWebApplicationContext().getBean(JdbcTemplate.class).queryForInt("select count(*) from identity_provider where id=?", provider.getId()));
//issue a timer
zoneAwareMetadataManager.refreshAllProviders();
//ensure that it the link doesn't show up
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").doesNotExist());
//and provider should be gone
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
}
protected IdentityProvider<SamlIdentityProviderDefinition> addXmlProviderToDatabase() throws Exception {
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
IdentityProvider<SamlIdentityProviderDefinition> provider = createSamlProvider(DEFAULT_SIMPLE_SAML_METADATA, "simplesamlphp", "Log in with Simple Saml PHP Config");
SamlIdentityProviderDefinition definition = provider.getConfig();
//ensure that the listener was not the one who created the provider
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
//ensure that we have an actual SAML provider created
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
return provider;
}
@Test
public void test_Reject_Duplicate_Alias_and_Duplicate_Entity_ID() throws Exception {
IdentityProvider<SamlIdentityProviderDefinition> provider = addXmlProviderToDatabase();
//
try {
createSamlProvider(DEFAULT_SIMPLE_SAML_METADATA, "simplesamlphp", "Log in with Simple Saml PHP Config");
fail("Should not be able to create a duplicate provider using alias 'simplesamlphp'");
} catch (Exception e) {
//expected
}
//adding another SAML provider - this one has the same entityID
provider = createSamlProvider(DEFAULT_SIMPLE_SAML_METADATA, "simplesamlphp2", "Log in with Simple Saml PHP Config 2");
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
zoneAwareMetadataManager.refreshAllProviders();
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
SamlIdentityProviderDefinition definition = provider.getConfig();
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").doesNotExist());
}
@Test
public void testThatDBXMLDisabledProvider() throws Exception {
IdentityProvider<SamlIdentityProviderDefinition> provider = addXmlProviderToDatabase();
provider.setActive(false);
provider = providerProvisioning.update(provider);
SamlIdentityProviderDefinition definition = provider.getConfig();
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
//ensure that we have an actual SAML provider created
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").doesNotExist());
}
@Test
public void testThatDBAddedFileProviderShowsOnLoginPage() throws Exception {
IdentityProvider<SamlIdentityProviderDefinition> provider = addXmlProviderToDatabase();
SamlIdentityProviderDefinition definition = provider.getConfig();
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
}
@Test
public void testThatDBAddedUrlProviderShowsOnLoginPage() throws Exception {
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
IdentityProvider<SamlIdentityProviderDefinition> provider = createSamlProvider("http://simplesamlphp.cfapps.io/saml2/idp/metadata.php", "simplesamlphp", "Log in with Simple Saml PHP URL");
SamlIdentityProviderDefinition definition = provider.getConfig();
//ensure that the listener was not the one who created the provider
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
//ensure that we have an actual SAML provider created
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
}
@Test
public void testThatDBFileUrlProvider() throws Exception {
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
IdentityProvider<SamlIdentityProviderDefinition> provider = createSamlProvider("http://simplesamlphp.cfapps.io/saml2/idp/metadata.php", "simplesamlphpurl", "Log in with Simple Saml PHP URL");
SamlIdentityProviderDefinition definition = provider.getConfig();
//ensure that the listener was not the one who created the provider
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
//ensure that we have an actual SAML provider created
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
provider.setActive(false);
provider = providerProvisioning.update(provider);
definition = provider.getConfig();
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
//ensure that we have an actual SAML provider created
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").doesNotExist());
}
@Test
public void testThatDifferentMetadataLocationsShowsOnLoginPage() throws Exception {
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
IdentityProvider<SamlIdentityProviderDefinition> provider = createSamlProvider(DEFAULT_SIMPLE_SAML_METADATA, "simplesamlphp", "Log in with Simple Saml PHP Config");
SamlIdentityProviderDefinition definition = provider.getConfig();
//ensure that the listener was not the one who created the provider
assertEquals(1, zoneAwareMetadataManager.getAvailableProviders().size());
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
assertThat(zoneAwareMetadataManager.getAvailableProviders().get(1).getDelegate(), Matchers.instanceOf(ComparableProvider.class));
assertThat(zoneAwareMetadataManager.getAvailableProviders().get(1).getDelegate(), Matchers.instanceOf(ConfigMetadataProvider.class));
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
//change from XML content to a URL provider
definition.setMetaDataLocation("http://simplesamlphp.cfapps.io/saml2/idp/metadata.php");
provider.setConfig(definition);
providerProvisioning.update(provider);
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
assertThat(zoneAwareMetadataManager.getAvailableProviders().get(1).getDelegate(), Matchers.instanceOf(ComparableProvider.class));
assertThat(zoneAwareMetadataManager.getAvailableProviders().get(1).getDelegate(), Matchers.instanceOf(ConfigMetadataProvider.class));
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
//change from URL content to a File provider
definition.setMetaDataLocation(DEFAULT_SIMPLE_SAML_METADATA);
provider.setConfig(definition);
providerProvisioning.update(provider);
//this simulates what the timer does
zoneAwareMetadataManager.refreshAllProviders();
assertEquals(2, zoneAwareMetadataManager.getAvailableProviders().size());
assertThat(zoneAwareMetadataManager.getAvailableProviders().get(1).getDelegate(), Matchers.instanceOf(ComparableProvider.class));
assertThat(zoneAwareMetadataManager.getAvailableProviders().get(1).getDelegate(), Matchers.instanceOf(ConfigMetadataProvider.class));
//ensure that it exists in the link
getMockMvc().perform(get("/login").accept(TEXT_HTML))
.andExpect(status().isOk())
.andExpect(xpath("//a[text()='" + definition.getLinkText() + "']").exists());
}
@Test
public void metadataInZoneGeneratesCorrectId() throws Exception {
String zone1Name = "zone1";
String zone2Name = "zone2";
IdentityZone zone1 = new IdentityZone();
zone1.setName(zone1Name);
zone1.setSubdomain(zone1Name);
zone1.setId(zone1Name);
zone1 = zoneProvisioning.create(zone1);
IdentityZone zone2 = new IdentityZone();
zone2.setName(zone2Name);
zone2.setSubdomain(zone2Name);
zone2.setId(zone2Name);
zone2 = zoneProvisioning.create(zone2);
getMockMvc().perform(
get("/saml/metadata")
.with(new SetServerNameRequestPostProcessor(zone1.getSubdomain() + ".localhost")))
.andExpect(status().isOk())
.andExpect(content().string(containsString("ID=\"zone1.cloudfoundry-saml-login\" entityID=\"zone1.cloudfoundry-saml-login\"")));
getMockMvc().perform(
get("/saml/metadata")
.with(new SetServerNameRequestPostProcessor(zone2.getSubdomain() + ".localhost")))
.andExpect(status().isOk())
.andExpect(content().string(containsString("ID=\"zone2.cloudfoundry-saml-login\" entityID=\"zone2.cloudfoundry-saml-login\"")));
}
@Test
public void test_zone_saml_properties() throws Exception {
String zone1Name = new RandomValueStringGenerator().generate();
String zone2Name = new RandomValueStringGenerator().generate();
SamlConfig config1 = new SamlConfig();
config1. setWantAssertionSigned(true);
config1. setRequestSigned(true);
IdentityZoneConfiguration zoneConfig1 = new IdentityZoneConfiguration(null);
zoneConfig1.setSamlConfig(config1);
IdentityZone zone1 = new IdentityZone();
zone1.setName(zone1Name);
zone1.setSubdomain(zone1Name);
zone1.setId(zone1Name);
zone1.setConfig(zoneConfig1);
zone1 = zoneProvisioning.create(zone1);
SamlConfig config2 = new SamlConfig();
config2. setWantAssertionSigned(false);
config2. setRequestSigned(false);
IdentityZoneConfiguration zoneConfig2 = new IdentityZoneConfiguration(null);
zoneConfig2.setSamlConfig(config2);
IdentityZone zone2 = new IdentityZone();
zone2.setName(zone2Name);
zone2.setSubdomain(zone2Name);
zone2.setId(zone2Name);
zone2.setConfig(zoneConfig2);
zone2 = zoneProvisioning.create(zone2);
ZoneAwareMetadataGenerator generator = getWebApplicationContext().getBean(ZoneAwareMetadataGenerator.class);
IdentityZoneHolder.set(zone1);
assertTrue(generator.isRequestSigned());
assertTrue(generator.isWantAssertionSigned());
assertTrue(generator.generateMetadata().getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol").getWantAssertionsSigned());
assertTrue(generator.generateMetadata().getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol").isAuthnRequestsSigned());
IdentityZoneHolder.set(zone2);
assertFalse(generator.isRequestSigned());
assertFalse(generator.isWantAssertionSigned());
assertFalse(generator.generateMetadata().getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol").getWantAssertionsSigned());
assertFalse(generator.generateMetadata().getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol").isAuthnRequestsSigned());
}
public IdentityProvider<SamlIdentityProviderDefinition> createSamlProvider(String metadata, String alias, String linkText) {
SamlIdentityProviderDefinition definition = createSimplePHPSamlIDP(IdentityZone.getUaa().getId(), metadata, alias, linkText);
IdentityProvider provider = new IdentityProvider();
provider.setActive(true);
provider.setConfig(definition);
provider.setIdentityZoneId(IdentityZone.getUaa().getId());
provider.setOriginKey(alias);
provider.setName("DB Added SAML Provider");
provider.setType(OriginKeys.SAML);
provider = providerProvisioning.create(provider);
return provider;
}
public SamlIdentityProviderDefinition createSimplePHPSamlIDP(String zoneId, String metaData, String alias, String linkText) {
SamlIdentityProviderDefinition def = new SamlIdentityProviderDefinition();
def.setZoneId(zoneId);
def.setMetaDataLocation(metaData);
def.setNameID("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
def.setAssertionConsumerIndex(0);
def.setMetadataTrustCheck(false);
def.setShowSamlLink(true);
def.setIdpEntityAlias(alias);
def.setLinkText(linkText);
return def;
}
}