Re: [extremeprogramming] About placing configuration defaults

"Jim Shore" <[email protected]> Tue, 19 Nov 2019 19:37:36 -0800
Newsgroups gmane.comp.programming.extreme-programming
Message-ID <[email protected]>
I’m assuming we’re talking about Ops-style configuration here, not command-line arguments.

In that case, I typically have one class, per major part of the system, that’s responsible for providing all the configuration information for that part of the system. That class sometimes delegates to other classes in order to load configuration data (such as an Environment wrapper, or a KeyVault wrapper, or a ConfigFile wrapper).

The Configuration class interprets the various sources of data it has to provide whole objects, rather than raw data, to its clients.

For example, my letscodejavascript.com website uses Auth0 for authentication. Auth0 has a bunch of configuration variables.

Configuration for the website is handled by the LcjsConfig class. LcjsConfig has a getAuth0Client() method. When getAuth0Client() is called, perhaps by an endpoint such as /auth/login, LcjsConfig gathers the configuration variables it needs and constructs an instance of Auth0Client. The endpoint then call a method such as auth0Client.validateLoginCode(), which is a wrapper around the underlying Auth0 REST call.

All sensitive data, such as Auth0 secrets, are stored in environment variables. LcjsConfig gets those variables from its instance of LcjsEnvironment. It calls methods like _lcjsEnvironment.getAuth0Secret(). LcjsEnvironment is responsible for knowing the correct environment variable names, such as LCJS_AUTH0_SECRET. Then LcjsEnvironment delegates to a more generic Environment class which is responsible for understanding how to pull data from environment variables.

So the flow is:  login endpoint —> lcjsConfig.getAuth0Client() —> lcjsEnvironment.getAuth0Secret() —> environment.get(“LCJS_AUTH0_SECRET”) —> return process.env[“LCJS_AUTH0_SECRET”]

(The reason for LcjsEnvironment and Environment are separated is that my code actually hosts multiple websites. Each has its own config logic.)

I don’t usually provide defaults, though. Instead, I program my systems to fail fast on startup when expected configuration data isn’t present. (Specifically, by calling lcjsEnvironment.selfCheck(), which throws an exception.) This way, if I’ve made a mistake, I learn about it right away, rather than having some part of the system fail in obscure ways later on.

That said, some “configuration" is present in the LcjsConfig class, but hardcoded. Most of my endpoint URLs fall into this category—they’re constants that need to be used by multiple endpoint handlers, but they'll never change without coding effort.

Cheers,
James

--
James Shore - The Art of Agile

voice: +1 503-267-5490
blog: http://jamesshore.com

> On Nov 19, 2019, at 6:28 AM, Luiz Esmiralha <[email protected]> wrote:
> 
> Hello XP people,
> 
> I am not sure if here is the right place to ask such a question, but I know there are lots of people here that can program circles around me, so I will give it a shot. If I am off-topic, well... sorry. :)
> 
> Anyways, I've been thinking about configurations and defaults. And I would like to know what are your thoughts on where to place defaults. Would you centralize them all in one single place (such as a Configuration class at the very edge of the system) or delegate each one to the components/classes actually using it?
> 
> My concrete case is (yet another) small test framework driven by a CLI. There is some configs such as a folder to read the specs from, api keys, URLs and so on. Some have sensible defaults, some don't. At the moment, the configuration defaults are decided by the args parser and passed on to a Configuration object that is used in every other place.
> 
> What would you do instead?
> 
> After writing it, the question seems so basic that it makes me blush a bit. But that's me: always wondering if someone knows a better way of doing what I do.
> 
> Thanks a lot,
> Esmiralha
> 


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#160245): https://groups.io/g/extremeprogramming/message/160245
Mute This Topic: https://groups.io/mt/60557252/2417047
Group Owner: [email protected]
Unsubscribe: https://groups.io/g/extremeprogramming/leave/4902963/619838065/xyzzy  [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-