-
Notifications
You must be signed in to change notification settings - Fork 41k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration property to export environment configuration properties to .properties or .yaml file #44289
Comments
(Risible?) ad hoc workaround: reporting property values in a custom bannerPossibly too much info (possibly also an easy target for derisive eyeball rolls, if that's your thing): here is an example of what I'm doing now in the absence of something like Sometimes, when I want to check the "last wins" (or "did it get found?") values of a few specific application properties (which might be spread across multiple property key prefixes), and I don't feel like enabling the e.g.
Using this workaround makes me half-wish for a read-only "property" (set by Spring Boot itself) that "contains" (when referenced, returns) a serialization of all (or, since I'm dreaming 😉, selected-by-wildcarded-pattern) environment property keys and their values (e.g. |
The proposed solution is intended primarily for application end usersTo dispel any possible confusion caused by my mention of Spring Boot Tools, I thought it was worth clarifying that the primary intended users of the proposed new While I imagine application developers and testers will also find such a property useful, my point is: the people who need this information don't necessarily have a Java IDE. They almost certainly won't have the application Java source. So, with apologies if I'm stating the bleeding obvious, and the possibility had occurred to nobody but me: the solution isn't adding more smarts to a Java IDE extension such as Spring Boot Tools. |
We discussed this today on our call and unfortunately we don't think it's feasible to implement the functionality your looking for in a way that will work with every application. We're also not convinced that there will be that much demand for such a feature (vs the amount of time it would take for us to implement it). You may be able to develop what you're looking for yourself in a way that will work with your own applications, if you accept certain limitations. Specifically, Spring's The other problem, as you've already identified, is that we don't have a way to convert I think your best option is to look at the existing code for the One more thing to be aware of, you can system environment variables and command line flags to change properties. So each run of the application may result in different exported properties. E.g. |
Thanks for at least considering this issue ("for: team-meeting"). I understand that it's been declined. I've converted the JSON from the
|
Hi @philwebb, Sorry, I stupidly missed your recent comment (thanks!) when I wrote my previous comment. (Maybe I forgot to refresh the page, or just didn't scroll up.)
Yeah, I didn't mention it in my previous comments, but I've been iterating over the
Understood. I was planning to (a) convert the Spring Boot configuration properties into an object with "atomic" (not composite, dotted) keys that can be passed to the SnakeYAML
Thanks for the tip. I'm already—by iterating over the property keys in the various property sources, omitting duplicate keys, and then getting the "active"/"last wins" property value from the Spring environment—getting the properties I need in
Understood. I really appreciate how Spring Boot loads properties from multiple property sources such as environment variables, Java system properties, and command-line arguments, not just multiple
I can't speak about "much"; I defer to your judgement on that, no question. However, there's "some" (not just me): the nearly-11-year-old Stack Overflow question "How to log the active configuration in a Spring Boot application?" has been viewed 83k times, and there are 11 answers. The page for that question also shows multiple related questions, most of which are asking essentially the same question. Thanks again for considering this issue and for your advice. And thanks for Spring Boot! |
Problem
Spring Boot's (impressively!) flexible externalization of properties, with multiple property sources, can raise questions such as, "Did the application merge the properties from the multiple sources as expected?" And, more specifically, "Did the properties from one source get overridden as expected by the properties from another source?"
Note: I acknowledge that the questions "Did the application get properties from all of the expected sources?" and, more specifically, "Did the application load all of the expected configuration files?" are already answered by the configuration property
logging.level.org.springframework.boot.context.config: trace
, as described in answers to the Stack Overflow question "Is there a way to get list of loaded properties file in SpringBoot application?".Proposed solution (enhancement)
A new configuration property to export the merged set of properties.
For example:
where the export format is inferred from the file extension, as opposed to, say, specified by a separate
spring.config.export.format
property.Example use case: specifying properties in multiple files
Suppose:
application.yaml
file that specifies properties for forwarding data to multiple locationsapplication.yaml
file is under source control management (SCM)To avoid merging issues when different teams update that single file, it would be useful to split the file into multiple files, one per location.
Spring Boot's flexible externalization of properties means that there are multiple different ways to do that; multiple possible solutions.
The proposed
spring.config.export
property offers an easy way to compare different solutions and verify that they produce the same configuration as the original single file.Related to configuration validation, but different
Questions about whether the configuration is as expected are closely related to, but distinct from, the question "Is the configuration valid"?
A set of configuration properties can be both (a) valid according to a set of rules that specify allowed structures and values and (b) not what you expect or intend.
Hence, I've created this separate issue, rather than adding a comment to issue #10030.
Why not use the existing
configprops
andenv
endpoints?Reasons:
setDefaultProperties()
in the applicationmain()
), export them, and then exit. I don't want the application to start a web server.env
endpoint shows all property values from all sources, with property keys reported using the same dot notation as.properties
files, but it doesn't report the merged ("last wins") property value. (Except, in my testing, for the value ofserver.port
, which the endpoint reports under the property namelocal.server.port
. I'm guessing this property is a special case; perhaps there are others.)configprops
endpoint does report the merged values, but parsing that structure into the equivalent YAML structure of an input configuration file seems like unnecessarily hard work. For example, given the single propertymanagement.endpoints.web.exposure.include: health,configprops,env
in an input YAML configuration file, theconfigprops
endpoint containscontexts.application.beans.management ... WebEndpointProperties.inputs.exposure.include
, whereinclude
is an array of three identical objects, where each object contains the same key/value"value": "health,configprops,env"
. For now, I've decided to look away.What exactly do I mean by "merge, export, and then exit"?
More specifically, I mean:
@Configuration
.spring.main.web-application-type=none
(don't start a web server).What I'm doing now: exporting to
.properties
, and then converting to.yaml
Thanks to more experienced colleagues, I'm already able to do most of this in experimental code. I code in other languages, but I'm a Java beginner.
I'm exporting to a
.properties
file, but not yet to a.yaml
file.I haven't yet invested time in, if you'll excuse the expression, "joining the dots" between the flattened, "compound" dot notation of the in-memory Spring Boot property keys and the nested structure of "atomic" property keys (including arrays, which might themselves contain nested structures) that I could pass to the SnakeYAML
dump()
method to write a.yaml
file.Instead, I'm using existing external tools to convert the
.properties
file to YAML. For example, the Environment Variable Generator website (Input: Spring Boot Properties; Output: Spring Boot YAML).Notes:
Environment Variable Generator website output nit: it wraps numeric property values in quotes, explicitly characterizing them as strings, not numbers. I'm using a JSON schema in Microsoft Visual Code editor to validate the YAML. If the schema defines a property as numeric, the quoted value gets reported as a validation error. I mention this because I'm hoping for a solution that is more type-aware: that exports values of numeric properties to YAML without enclosing quotes.
I'd like to use the VS Code Spring Boot Tools extension "Convert .properties to .yaml", but I currently can't get that to work.
Acknowledging that I'm being opportunistic, and that Stack Overflow is probably a better place to ask: if you feel like replying with Java code that converts in-memory Spring Boot configuration properties into an object that can be passed to the SnakeYAML
dump()
method to produce YAML with "atomic" (as opposed to "compound"; dotted) keys, please do! I'm aware of the GitHub project anubhavshukla/properties-to-yaml-converter, but I'm hoping for more concise code that begins with in-memory Spring Boot configuration properties and uses SnakeYAML for YAML serialization.Implementation details: possible additional sub-properties
Sanitization
Example:
spring.config.export.show-values: never|always
From the Spring Boot docs topic "Endpoints":
The same is true of this proposed new property.
Perhaps this new property should also have a corresponding
show-values
(sub-)property; although, perhaps the valuewhen-authorized
doesn't apply in this context.And perhaps the same
SanitizingFunction
should apply. I'm watching issue #39243 with interest.Only export properties from files
Example:
spring.config.export.only-properties-from-files: true|false
Exporting all properties can result in too much information.
As mentioned, one of the primary use cases for this new property is for users to confirm that the properties that they specified in multiple configuration files have been merged as they expected.
In that case, users are primarily interested in properties sourced from configuration files; properties from other sources, such as environment variables, are effectively noise.
only-properties-from-files: true
would only export properties from files; that is, property sources that matchinstanceof OriginTrackedMapPropertySource
.I'm applying this filter now in my experimental code, and the results are far more concise, and more useful for my purposes, than a comprehensive export of all properties, regardless of source.
A sub-property to filter exported property keys by regex patterns has occurred to me, but I think that's a lower priority.
Risk of exported
.properties
or.yaml
file being inadvertently used as input?Yes, it's a possibility. Maybe mention this in the docs for this property; for instance, recommend an export file name (and path) that Spring Boot won't automatically find and load.
The flipside: this property also offers an opportunity for condensing properties from multiple configuration files (back) into a single, functionally equivalent configuration file. That might be useful in some cases (to encapsulate environment variables, Java system properties, command-line arguments, multiple files, into a single resource).
The text was updated successfully, but these errors were encountered: