|
| 1 | +From 2b49faba38b8ceb8abe639b5f7ec022a59f47ce0 Mon Sep 17 00:00:00 2001 |
| 2 | +From: "N. Harrison Ripps" < [email protected]> |
| 3 | +Date: Mon, 23 Jul 2012 11:05:13 -0400 |
| 4 | +Subject: [PATCH] Added changes to templatize the quick start. |
| 5 | + |
| 6 | +--- |
| 7 | + wsgi/openshift/openshiftlibs.py | 81 +++++++++++++++++++++++++++++++++++++++ |
| 8 | + wsgi/openshift/settings.py | 14 ++++++- |
| 9 | + 2 files changed, 93 insertions(+), 2 deletions(-) |
| 10 | + create mode 100644 wsgi/openshift/openshiftlibs.py |
| 11 | + |
| 12 | +diff --git a/wsgi/openshift/openshiftlibs.py b/wsgi/openshift/openshiftlibs.py |
| 13 | +new file mode 100644 |
| 14 | +index 0000000..a11e0e5 |
| 15 | +--- /dev/null |
| 16 | ++++ b/wsgi/openshift/openshiftlibs.py |
| 17 | +@@ -0,0 +1,81 @@ |
| 18 | ++#!/usr/bin/env python |
| 19 | ++import hashlib, inspect, os, random, sys |
| 20 | ++ |
| 21 | ++# Gets the secret token provided by OpenShift |
| 22 | ++# or generates one (this is slightly less secure, but good enough for now) |
| 23 | ++def get_openshift_secret_token(): |
| 24 | ++ token = os.getenv('OPENSHIFT_SECRET_TOKEN') |
| 25 | ++ name = os.getenv('OPENSHIFT_APP_NAME') |
| 26 | ++ uuid = os.getenv('OPENSHIFT_APP_UUID') |
| 27 | ++ if token is not None: |
| 28 | ++ return token |
| 29 | ++ elif (name is not None and uuid is not None): |
| 30 | ++ return hashlib.sha256(name + '-' + uuid).hexdigest() |
| 31 | ++ return None |
| 32 | ++ |
| 33 | ++# Loop through all provided variables and generate secure versions |
| 34 | ++# If not running on OpenShift, returns defaults and logs an error message |
| 35 | ++# |
| 36 | ++# This function calls secure_function and passes an array of: |
| 37 | ++# { |
| 38 | ++# 'hash': generated sha hash, |
| 39 | ++# 'variable': name of variable, |
| 40 | ++# 'original': original value |
| 41 | ++# } |
| 42 | ++def openshift_secure(default_keys, secure_function = 'make_secure_key'): |
| 43 | ++ # Attempts to get secret token |
| 44 | ++ my_token = get_openshift_secret_token() |
| 45 | ++ |
| 46 | ++ # Only generate random values if on OpenShift |
| 47 | ++ my_list = default_keys |
| 48 | ++ |
| 49 | ++ if my_token is not None: |
| 50 | ++ # Loop over each default_key and set the new value |
| 51 | ++ for key, value in default_keys.iteritems(): |
| 52 | ++ # Create hash out of token and this key's name |
| 53 | ++ sha = hashlib.sha256(my_token + '-' + key).hexdigest() |
| 54 | ++ # Pass a dictionary so we can add stuff without breaking existing calls |
| 55 | ++ vals = { 'hash': sha, 'variable': key, 'original': value } |
| 56 | ++ # Call user specified function or just return hash |
| 57 | ++ my_list[key] = sha |
| 58 | ++ if secure_function is not None: |
| 59 | ++ # Pick through the global and local scopes to find the function. |
| 60 | ++ possibles = globals().copy() |
| 61 | ++ possibles.update(locals()) |
| 62 | ++ supplied_function = possibles.get(secure_function) |
| 63 | ++ if not supplied_function: |
| 64 | ++ raise Exception("Cannot find supplied security function") |
| 65 | ++ else: |
| 66 | ++ my_list[key] = supplied_function(vals) |
| 67 | ++ else: |
| 68 | ++ calling_file = inspect.stack()[1][1] |
| 69 | ++ if os.getenv('OPENSHIFT_REPO_DIR'): |
| 70 | ++ base = os.getenv('OPENSHIFT_REPO_DIR') |
| 71 | ++ calling_file.replace(base,'') |
| 72 | ++ sys.stderr.write("OPENSHIFT WARNING: Using default values for secure variables, please manually modify in " + calling_file + "\n") |
| 73 | ++ |
| 74 | ++ return my_list |
| 75 | ++ |
| 76 | ++ |
| 77 | ++# This function transforms default keys into per-deployment random keys; |
| 78 | ++def make_secure_key(key_info): |
| 79 | ++ hashcode = key_info['hash'] |
| 80 | ++ key = key_info['variable'] |
| 81 | ++ original = key_info['original'] |
| 82 | ++ |
| 83 | ++ chars = '0123456789abcdef' |
| 84 | ++ |
| 85 | ++ # Use the hash to seed the RNG |
| 86 | ++ random.seed(int("0x" + hashcode[:8], 0)) |
| 87 | ++ |
| 88 | ++ # Create a random string the same length as the default |
| 89 | ++ rand_key = '' |
| 90 | ++ for _ in range(len(original)): |
| 91 | ++ rand_pos = random.randint(0,len(chars)) |
| 92 | ++ rand_key += chars[rand_pos:(rand_pos+1)] |
| 93 | ++ |
| 94 | ++ # Reset the RNG |
| 95 | ++ random.seed() |
| 96 | ++ |
| 97 | ++ # Set the value |
| 98 | ++ return rand_key |
| 99 | +diff --git a/wsgi/openshift/settings.py b/wsgi/openshift/settings.py |
| 100 | +index 842669e..2f44079 100644 |
| 101 | +--- a/wsgi/openshift/settings.py |
| 102 | ++++ b/wsgi/openshift/settings.py |
| 103 | +@@ -1,6 +1,6 @@ |
| 104 | + # -*- coding: utf-8 -*- |
| 105 | + # Django settings for openshift project. |
| 106 | +-import os |
| 107 | ++import imp, os |
| 108 | + |
| 109 | + # a setting to determine whether we are running on OpenShift |
| 110 | + ON_OPENSHIFT = False |
| 111 | +@@ -104,8 +104,18 @@ STATICFILES_FINDERS = ( |
| 112 | + #'django.contrib.staticfiles.finders.DefaultStorageFinder', |
| 113 | + ) |
| 114 | + |
| 115 | ++# Make a dictionary of default keys |
| 116 | ++default_keys = { 'SECRET_KEY': 'vm4rl5*ymb@2&d_(gc$gb-^twq9w(u69hi--%$5xrh!xk(t%hw' } |
| 117 | ++ |
| 118 | ++# Replace default keys with dynamic values if we are in OpenShift |
| 119 | ++use_keys = default_keys |
| 120 | ++if ON_OPENSHIFT: |
| 121 | ++ imp.find_module('openshiftlibs') |
| 122 | ++ import openshiftlibs |
| 123 | ++ use_keys = openshiftlibs.openshift_secure(default_keys) |
| 124 | ++ |
| 125 | + # Make this unique, and don't share it with anybody. |
| 126 | +-SECRET_KEY = 'vm4rl5*ymb@2&d_(gc$gb-^twq9w(u69hi--%$5xrh!xk(t%hw' |
| 127 | ++SECRET_KEY = use_keys['SECRET_KEY'] |
| 128 | + |
| 129 | + # List of callables that know how to import templates from various sources. |
| 130 | + TEMPLATE_LOADERS = ( |
| 131 | +-- |
| 132 | +1.7.5.4 |
| 133 | + |
0 commit comments