JdbcSamlServiceProviderProvisioning implements SamlServiceProviderProvisioning, SamlServiceProviderDeletable { @Override public SamlServiceProvider create(final SamlServiceProvider serviceProvider, final String zoneId) { validate(serviceProvider); final String id = UUID.randomUUID().toString(); try { jdbcTemplate.update(CREATE_SERVICE_PROVIDER_SQL, ps -> { int pos = 1; ps.setString(pos++, id); ps.setInt(pos++, serviceProvider.getVersion()); ps.setTimestamp(pos++, new Timestamp(System.currentTimeMillis())); ps.setTimestamp(pos++, new Timestamp(System.currentTimeMillis())); ps.setString(pos++, serviceProvider.getName()); ps.setString(pos++, serviceProvider.getEntityId()); ps.setString(pos++, JsonUtils.writeValueAsString(serviceProvider.getConfig())); ps.setString(pos++, zoneId); ps.setBoolean(pos++, serviceProvider.isActive()); }); } catch (DuplicateKeyException e) { throw new SamlSpAlreadyExistsException(e.getMostSpecificCause().getMessage()); } return retrieve(id, zoneId); } JdbcSamlServiceProviderProvisioning(JdbcTemplate jdbcTemplate); @Override SamlServiceProvider retrieve(String id, String zoneId); @Override void delete(String id, String zoneId); @Override int deleteByEntityId(String entityId, String zoneId); @Override int deleteByIdentityZone(String zoneId); @Override List<SamlServiceProvider> retrieveActive(String zoneId); @Override List<SamlServiceProvider> retrieveAll(boolean activeOnly, String zoneId); @Override SamlServiceProvider retrieveByEntityId(String entityId, String zoneId); @Override SamlServiceProvider create(final SamlServiceProvider serviceProvider, final String zoneId); @Override SamlServiceProvider update(final SamlServiceProvider serviceProvider, String zoneId); @Override Logger getLogger(); static final String SERVICE_PROVIDER_FIELDS; static final String CREATE_SERVICE_PROVIDER_SQL; static final String DELETE_SERVICE_PROVIDER_SQL; static final String DELETE_SERVICE_PROVIDER_BY_ENTITY_ID_SQL; static final String DELETE_SERVICE_PROVIDER_BY_ZONE_SQL; static final String SERVICE_PROVIDERS_QUERY; static final String ACTIVE_SERVICE_PROVIDERS_QUERY; static final String SERVICE_PROVIDER_UPDATE_FIELDS; static final String UPDATE_SERVICE_PROVIDER_SQL; static final String SERVICE_PROVIDER_BY_ID_QUERY; static final String SERVICE_PROVIDER_BY_ENTITY_ID_QUERY; }
@Test void createSamlServiceProviderInOtherZone() { IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(), "myzone"); SamlServiceProvider sp = createSamlServiceProvider(zone.getId()); SamlServiceProvider createdSp = db.create(sp, sp.getIdentityZoneId()); Map<String, Object> rawCreatedSp = jdbcTemplate.queryForMap("select * from service_provider where id = ?", createdSp.getId()); assertEquals(sp.getName(), createdSp.getName()); assertEquals(sp.getConfig(), createdSp.getConfig()); assertEquals(sp.getName(), rawCreatedSp.get("name")); assertEquals(sp.getConfig(), JsonUtils.readValue((String) rawCreatedSp.get("config"), SamlServiceProviderDefinition.class)); assertEquals(zone.getId(), rawCreatedSp.get("identity_zone_id")); }
@Test void createSamlServiceProviderWithSameEntityIdInDefaultZone() { String zoneId = IdentityZone.getUaaZoneId(); SamlServiceProvider sp = createSamlServiceProvider(zoneId); db.create(sp, sp.getIdentityZoneId()); assertThrows(SamlSpAlreadyExistsException.class, () -> db.create(sp, sp.getIdentityZoneId())); }
@Test void createSamlServiceProviderWithSameEntityIdInOtherZone() { IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(), "myzone"); SamlServiceProvider sp = createSamlServiceProvider(zone.getId()); db.create(sp, sp.getIdentityZoneId()); assertThrows(SamlSpAlreadyExistsException.class, () -> db.create(sp, sp.getIdentityZoneId())); }
@Test void createSamlServiceProviderWithSameEntityIdInDifferentZones() { String zoneId = IdentityZone.getUaaZoneId(); SamlServiceProvider sp = createSamlServiceProvider(zoneId); db.create(sp, sp.getIdentityZoneId()); IdentityZone zone = MultitenancyFixture.identityZone(UUID.randomUUID().toString(), "myzone"); zoneId = zone.getId(); sp.setIdentityZoneId(zoneId); db.create(sp, sp.getIdentityZoneId()); }
@Test void deleteSamlServiceProvidersInUaaZone() { String zoneId = IdentityZone.getUaaZoneId(); SamlServiceProvider sp = createSamlServiceProvider(zoneId); SamlServiceProvider createdSp = db.create(sp, sp.getIdentityZoneId()); assertNotNull(createdSp); assertThat(jdbcTemplate.queryForObject("select count(*) from service_provider where identity_zone_id=?", new Object[]{IdentityZone.getUaaZoneId()}, Integer.class), is(1)); db.onApplicationEvent(new EntityDeletedEvent<>(createdSp, mock(Authentication.class), zoneId)); assertThat(jdbcTemplate.queryForObject("select count(*) from service_provider where identity_zone_id=?", new Object[]{zoneId}, Integer.class), is(0)); }
@Test void deleteSamlServiceProvidersInOtherZone(RandomStringGetter zoneId) { IdentityZone zone = MultitenancyFixture.identityZone(zoneId.get(), zoneId.get()); SamlServiceProvider sp = createSamlServiceProvider(zoneId.get()); SamlServiceProvider createdSp = db.create(sp, sp.getIdentityZoneId()); assertNotNull(createdSp); assertThat(jdbcTemplate.queryForObject("select count(*) from service_provider where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class), is(1)); db.onApplicationEvent(new EntityDeletedEvent<>(createdSp, mock(Authentication.class), zone.getId())); assertThat(jdbcTemplate.queryForObject("select count(*) from identity_provider where identity_zone_id=?", new Object[]{zone.getId()}, Integer.class), is(0)); }