0

I have a code base consisting of multiple scripts and many functions. Everything is controlled from one master script (master.txt, let's say).

I have a lot of "global" configuration files, for example the URL, login, and password of the webservice I'm communicating with (this changes between test and production), and some tuning parameters for an optimization algorithm hidden deep inside the other scripts.

I want to configure these parameters often, and preferably easily. What is the best way to do this?

So far, I thought of the following options:

  • A global variable parameters defined at the beginning, that stores all these options and is accessed from within the deep functions
  • A variable parameters that is defined at the beginning, and passed down as an additional function argument in each function call
  • A text file such as parameters.yaml that is read every time a function needs to access a value from it.

Currently, I am using the first option, but it doesn't feel right, since the functions are accessing data that is not passed to them. On the other hand, the second option makes every function call unnecessarily complex by adding an additional argument.

Surely I'm not the first guy with this problem and there exists a standard solution for this, right?

  • 1
    I would advise you to hash your password information on program start and discard the actual password value so you aren't keeping it in memory, just as a precaution, so that there is no way to get that password information unless you had access to the server computer directly. Word to the wise. – Neil Oct 18 '17 at 13:05
  • I do not thing passing a function the data it needs qualifies as unnecesary complexity, actually I would say it is the standard solution. Whether it is the best for you depend on a number of things like scripting language, details of the corrent architecture, constraints... – Goyo Oct 18 '17 at 13:16
2

An external file like your third option is the general best practice for managing configuration information that is only known at run time and needs to be changed based on environment. Also rather than reading the file directly in each place values are needed it would be better to create a getConfigValue function that takes the name of the value you need and returns the value if one exists in the file. From here you could either call getConfigValue in each place you need a value, or preferably in your master you call it to get the needed values for a function call and provide them as parameters to the functions that need them. You could pass a parameter object if there are several values, but the important part is to only populate it with what the underlying function need.

You could take this a step further as well and have parameters.yaml reference secrets.yaml that holds passwords or other sensitive values that don't change (or change infrequently) in an encrypted state. getConfigValue would handle going to secrets.yaml and encrypting the value. There are several reasons for doing it this way:

  • so sensitive information can be excluded from version control systems
  • a different person can manage the encryption of values so developers don't need to know the values and can still accomplish their work
  • the values only have to be stored on the machines that need them
  • anyone with file system access can't just view the file in a useful state

What you don't want to do is create a single parameter object that holds every possible config value and pass that around everywhere, this creates greater risk for potentially exposing information you don't want exposed because you are keeping values in memory longer than you need to and allowing any function that can access the parameter object to access any value you can unintentionally alter or reveal sensitive values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.