Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the Pyramid web framework, how do I source sensitive settings into development.ini / production.ini from an external file?

I'd like to keep development.ini and production.ini under version control, but for security reason would not want the sqlalchemy.url connection string to be stored, as this would contain the username and password used for the database connection.

What's the canonical way, in Pyramid, of sourcing this setting from an additional external file?

Edit In addition to solution using the environment variable, I came up with this solution after asking around on #pyramid:

def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
# Read db password from config file outside of version control
secret_cfg = ConfigParser()
secret_cfg.read(settings['secrets'])
dbpass = secret_cfg.get("secrets", "dbpass")
settings['sqlalchemy.url'] = settings['connstr'] % (dbpass,)
like image 752
michel-slm Avatar asked May 08 '13 07:05

michel-slm


People also ask

What is Pyramid web framework?

Pyramid is a general, open source, web application development framework built in python. It allows python developer to create web applications with ease. Pyramid is backed by the enterprise knowledge Management System KARL (a George Soros project).

What is Pserve?

This command serves a web application that uses a PasteDeploy configuration file for the server and application. You can also include variable assignments like 'http_port=8080' and then use %(http_port)s in your config files.


2 Answers

I looked into this a lot and played with a lot of different approaches. However, Pyramid is so flexible, and the .ini config parser is so minimal in what it does for you, that there doesn't seem to be a de facto answer.

In my scenario, I tried having a production.example.ini in version control at first that got copied on the production server with the details filled in, but this got hairy, as updates to the example didn't get translated to the copy, and so the copy had to be re-created any time a change was made. Also, I started using Heroku, so files not in version control never made it into the deployment.

Then, there's the encrypted config approach. Which, I don't like the paradigm. Imagine a sysadmin being responsible for maintaining the production environment, but he or she is unable to change the location of a database or environment-specific setting without running it back through version control. It's really nice to have the separation between environment and code as much as possible so those changes can be made on the fly without version control revisions.

My ultimate solution was to have some values that looked like this:

[app:main]

sqlalchemy.url = ${SQLALCHEMY_URL}

Then, on the production server, I would set the environment variable SQLALCHEMY_URL to point to the database. This even allowed me to use the same configuration file for staging and production, which is nice.

In my Pyramid init, I just expanded the environment variable value using os.path.expandvars:

sqlalchemy_url = os.path.expandvars(settings.get('sqlalchemy.url'))
engine = create_engine(sqlalchemy_url)

And, if you want to get fancy with it and automatically replace all the environment variables in your settings dictionary, I made this little helper method for my projects:

def expandvars_dict(settings):
    """Expands all environment variables in a settings dictionary."""
    return dict((key, os.path.expandvars(value)) for
                key, value in settings.iteritems())

Use it like this in your main app entry point:

settings = expandvars_dict(settings)
like image 185
Alan Christopher Thomas Avatar answered Nov 15 '22 15:11

Alan Christopher Thomas


The whole point of the separate ini files in Pyramid is that you do not have to version control all of them and that they can contain different settings for different scenarios (development/production/testing). Your production.ini almost always should not be in the same VCS as your source code.

like image 26
Michael Merickel Avatar answered Nov 15 '22 13:11

Michael Merickel