ulisesbocchio / jasypt-spring-boot-samples

Sample apps using jasypt-spring-boot
132 stars 111 forks source link

Failes when property not defined #2

Closed edross15057 closed 6 years ago

edross15057 commented 7 years ago

in our environment, we - for development put all properties in our application-dev.properties file. in our upstream environments - we add a startup parameter (-Dsecurity.file=somefile). which has the encrypted properties

our configuration file for the non dev envionments has the following: @EncryptablePropertySource(value = "file:${security.file}", ignoreResourceNotFound = true)

this fails as security.file is not defined in our dev environments.

Assume the fix is: in EncryptablePropertySourceBeanFactoryPostProcessor

something like:

private PropertySource createPropertySource(AnnotationAttributes attributes, ConfigurableEnvironment environment, ResourceLoader resourceLoader, EncryptablePropertyResolver resolver, List loaders) throws Exception { String name = generateName(attributes.getString("name")); String[] locations = attributes.getStringArray("value"); boolean ignoreResourceNotFound = attributes.getBoolean("ignoreResourceNotFound"); CompositePropertySource compositePropertySource = new CompositePropertySource(name); Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required"); try{ for (String location : locations) { String resolvedLocation = environment.resolveRequiredPlaceholders(location); Resource resource = resourceLoader.getResource(resolvedLocation); if (!resource.exists()) { if (!ignoreResourceNotFound) { throw new IllegalStateException(String.format("Encryptable Property Source '%s' from location: %s Not Found", name, resolvedLocation)); } else { log.info("Ignoring NOT FOUND Encryptable Property Source '{}' from locations: {}", name, resolvedLocation); } } else { String actualName = name + "#" + resolvedLocation; loadPropertySource(loaders, resource, actualName) .ifPresent(compositePropertySource::addPropertySource); } } } catch (Exception e){ if(!ignoreResourceNotFound){ log.warn("the % is not defined - ignoring the property file",locations.toString()); throw e; } } return new EncryptableEnumerablePropertySourceWrapper<>(compositePropertySource, resolver); }

ulisesbocchio commented 7 years ago

Just define a default value for the variable to a non-existing file:

@encryptablepropertysource(value = "file:${security.file:/dev/null}", ignoreResourceNotFound = true)