Skip to content

Commit

Permalink
SHIBUI-2268
Browse files Browse the repository at this point in the history
load properties to database from csv configuration on startup


Former-commit-id: b24b378
  • Loading branch information
chasegawa committed Aug 17, 2022
1 parent 0f77826 commit 4683ce1
Show file tree
Hide file tree
Showing 9 changed files with 813 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package edu.internet2.tier.shibboleth.admin.ui.service

import com.opencsv.CSVReader
import edu.internet2.tier.shibboleth.admin.ui.domain.ShibConfigurationProperty
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.event.ApplicationStartedEvent
import org.springframework.context.event.EventListener
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource
import org.springframework.stereotype.Component

import javax.transaction.Transactional

@Component
@Slf4j
class ShibPropertiesBootstrap {
@Autowired
private ShibConfigurationService service

ShibPropertiesBootstrap(ShibConfigurationService service) {
this.service = service
}

@Transactional
@EventListener
void bootstrapUsersAndRoles(ApplicationStartedEvent e) {
log.info("Ensuring base Shibboleth properties configuration has loaded")

Resource resource = new ClassPathResource('shib_configuration_prop.csv')
final HashMap<String, ShibConfigurationProperty> propertiesMap = new HashMap<>()

// Read in the defaults in the configuration file
new CSVReader(new InputStreamReader(resource.inputStream)).each { fields ->
def (resource_id,category,config_file,description,idp_version,module,module_version,note,default_value,property_name,property_type,selection_items,property_value) = fields
ShibConfigurationProperty prop = new ShibConfigurationProperty().with {
it.resourceId = resource_id
it.category = category
it.configFile = config_file
it.description = description
it.idpVersion = idp_version
it.module = module
it.moduleVersion = module_version
it.note = note
it.defaultValue = default_value
it.description = description
it.propertyName = property_name
def pt = property_type
it.setPropertyType(pt)
it.selectionItems = selection_items
// we shouldn't have property values coming in from the config...
it
}
propertiesMap.put(prop.getPropertyName(), prop)
}

// If we already have the property in the db, ignore the configuration setup for that property
service.getExistingPropertyNames().each {
propertiesMap.remove(it)
}

// Save anything that's left
if (propertiesMap.size() > 0) {
log.info("Saving/loading [" + propertiesMap.size() + "] properties to the database")
service.addAll(propertiesMap.values())
}

log.info("COMPLETED: ensuring base Shibboleth properties configuration has loaded")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import edu.internet2.tier.shibboleth.admin.ui.domain.IRelyingPartyOverrideProperty;
import edu.internet2.tier.shibboleth.admin.ui.domain.RelyingPartyOverrideProperty;
import edu.internet2.tier.shibboleth.admin.ui.domain.ShibConfigurationProperty;
import edu.internet2.tier.shibboleth.admin.ui.service.CustomEntityAttributesDefinitionService;
import edu.internet2.tier.shibboleth.admin.ui.service.events.CustomEntityAttributeDefinitionChangeEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

@Configuration
@ConfigurationProperties(prefix = "custom")
public class CustomPropertiesConfiguration implements ApplicationListener<CustomEntityAttributeDefinitionChangeEvent> {
Expand All @@ -28,6 +27,8 @@ public class CustomPropertiesConfiguration implements ApplicationListener<Custom

private List<RelyingPartyOverrideProperty> overridesFromConfigFile = new ArrayList<>();

private List<ShibConfigurationProperty> shibprops = new ArrayList<>();

private void buildRelyingPartyOverrides() {
// Start over with a clean map and get the CustomEntityAttributesDefinitions from the DB
HashMap<String, IRelyingPartyOverrideProperty> reloaded = new HashMap<>();
Expand Down Expand Up @@ -68,6 +69,7 @@ public void onApplicationEvent(CustomEntityAttributeDefinitionChangeEvent arg0)
public void postConstruct() {
// Make sure we have the right data
buildRelyingPartyOverrides();
updateShibPropsDatabase();
}

public void setAttributes(List<? extends Map<String, String>> attributes) {
Expand All @@ -85,4 +87,7 @@ public void setCeadService(CustomEntityAttributesDefinitionService ceadService)
public void setOverrides(List<RelyingPartyOverrideProperty> overridesFromConfigFile) {
this.overridesFromConfigFile = overridesFromConfigFile;
}
}

private void updateShibPropsDatabase() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ShibConfigurationProperty {
@Column(name = "config_file", nullable = false)
String configFile;

@Column(name = "default_value", nullable = false)
@Column(name = "default_value")
String defaultValue;

@Column(name = "description")
Expand All @@ -46,8 +46,16 @@ public class ShibConfigurationProperty {
@Column(name = "property_type", nullable = false)
PropertyType propertyType;

@Column(name = "property_value", nullable = false)
@Column(name = "property_value")
String propertyValue;

@Column(name = "selection_items")
String selectionItems;

public void setPropertyType(String val) {
this.propertyType = PropertyType.valueOf(val);
}

}

enum PropertyType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.internet2.tier.shibboleth.admin.ui.repository;

import edu.internet2.tier.shibboleth.admin.ui.domain.ShibConfigurationProperty;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* Repository to manage {@link ShibConfigurationProperty} instances.
*/
public interface ShibConfigurationRepository extends JpaRepository<ShibConfigurationProperty, String> {
@Query(value = "select property_name from shib_configuration_prop", nativeQuery = true)
List<String> getPropertyNames();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CustomEntityAttributesDefinitionServiceImpl implements CustomEntity
private ApplicationEventPublisher applicationEventPublisher;

@Autowired
EntityManager entityManager;
EntityManager entityManager; // Why is this here - it isn't used

@Autowired
private CustomEntityAttributeDefinitionRepository repository;
Expand Down Expand Up @@ -53,4 +53,4 @@ public List<CustomEntityAttributeDefinition> getAllDefinitions() {
private void notifyListeners() {
applicationEventPublisher.publishEvent(new CustomEntityAttributeDefinitionChangeEvent(this));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.internet2.tier.shibboleth.admin.ui.service;

import edu.internet2.tier.shibboleth.admin.ui.domain.ShibConfigurationProperty;

import java.util.Collection;
import java.util.List;

public interface ShibConfigurationService {
void addAll(Collection<ShibConfigurationProperty> newProperties);

List<String> getExistingPropertyNames();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.internet2.tier.shibboleth.admin.ui.service;

import edu.internet2.tier.shibboleth.admin.ui.domain.ShibConfigurationProperty;
import edu.internet2.tier.shibboleth.admin.ui.repository.ShibConfigurationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class ShibConfigurationServiceImpl implements ShibConfigurationService {
@Autowired
private ShibConfigurationRepository repository;

@Override
public void addAll(Collection<ShibConfigurationProperty> newProperties) {
repository.saveAll(newProperties);
}

@Override
public List<String> getExistingPropertyNames() {
return repository.getPropertyNames();
}
}
15 changes: 14 additions & 1 deletion backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,17 @@ custom:
displayType: boolean
helpText: tooltip.ignore-request-signatures
attributeName: http://shibboleth.net/ns/profiles/ignoreRequestSignatures
attributeFriendlyName: ignoreRequestSignatures
attributeFriendlyName: ignoreRequestSignatures
shibprops:
- category: asd # required
configFile: kj # required
defaultValue: foo
description: blak
idpVersion: 4.1 # required
module: h
moduleVersion: 1
note: nnn
propertyName: dddd # required
propertyType: dddd # required as one of: BOOLEAN, DURATION, INTEGER, SELECTION_LIST, SPRING_BEAN_ID, STRING
propertyValue: dddd
selectionItems: dddd,dddd # required if propertyType is SELECTION_LIST - comma seperated values
Loading

0 comments on commit 4683ce1

Please sign in to comment.