Skip to content

Commit 520c56c

Browse files
committed
Added templatizing info and patch file to package
1 parent 2b49fab commit 520c56c

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

.openshift/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Django Template for OpenShift
2+
3+
## Template App Information
4+
Product: Django
5+
Version: 1.4
6+
Source: https://github.com/django/django.git
7+
Commit: 2591fb8d4c0246f68b79554976c012039df75359
8+
9+
## Maintenance
10+
This folder contains a diff file that includes the changes made to the
11+
stock Django app in order to make it OpenShift-Template-ready. If
12+
you are a maintainer tasked with updating the Django template, you
13+
may be able to use this patch file on the updated Django code to
14+
automatically reapply these changes.
15+
16+
Here are the steps involved:
17+
18+
1. Under the 'wsgi' directory, apply any patches required to update the 'openshift' Django app.
19+
2. From the template root directory, run 'git apply --check .openshift/template.patch' to test for patching problems.
20+
3. Next run 'git am --signoff < .openshift/template.patch' to apply the patch to the template.
21+
22+
If this process succeeds, then the changes have been automatically
23+
applied. Otherwise it may be necessary to manually apply the
24+
changes. If the base package has changed enough, you may need to
25+
re-audit the base code and generate a new patch file.

.openshift/template.patch

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Add this upstream repo
4343
git remote add upstream -m master git://github.com/openshift/django-example.git
4444
git pull -s recursive -X theirs upstream master
4545

46-
Set your Django admin password
46+
Set your Django admin password. (Django must be installed on your dev system for this to work; 'sudo yum install Django' will do this for Fedora and RHEL)
4747

4848
cd wsgi/openshift
4949
./manage.py changepassword admin

0 commit comments

Comments
 (0)