Skip to content

Commit 68ffb81

Browse files
committed
Merge pull request #16 from takinbo/master
bug fix for #15 implementing a different calling method with the S3 API
2 parents 8dc864b + f030354 commit 68ffb81

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

keychain/app.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import collections
21
import base64
32
import uuid
43
import hashlib
@@ -8,6 +7,7 @@
87
import requests
98
import boto
109
import boto.s3.key
10+
from boto.s3.connection import OrdinaryCallingFormat
1111

1212
from flask import Flask
1313
from flask import request
@@ -23,21 +23,25 @@
2323

2424
s3 = None
2525

26+
2627
def s3key(email, name):
2728
global s3
2829
if not s3:
29-
s3 = boto.connect_s3()
30-
k = boto.s3.key.Key(s3.lookup(bucket_name))
30+
s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat())
31+
k = boto.s3.key.Key(
32+
s3.get_bucket(bucket_name))
3133
k.key = '{}.{}'.format(email, name)
3234
return k
3335

36+
3437
def s3keys(email):
3538
global s3
3639
if not s3:
37-
s3 = boto.connect_s3()
40+
s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat())
3841
b = s3.get_bucket(bucket_name)
3942
return b.list(prefix=email)
4043

44+
4145
def lookup_key(email, name=None):
4246
name = name or 'default'
4347
k = s3key(email, name)
@@ -46,60 +50,74 @@ def lookup_key(email, name=None):
4650
except:
4751
return None
4852

53+
4954
def lookup_keys(email):
5055
keys = s3keys(email)
5156
try:
5257
return [k.get_contents_as_string() for k in keys]
53-
except Exception, e:
58+
except Exception:
5459
return None
5560

61+
5662
def upload_key(email, name, key):
5763
k = s3key(email, name)
5864
k.set_contents_from_string(key.strip())
5965

66+
6067
def delete_key(email, name):
6168
k = s3key(email, name)
6269
k.delete()
6370

71+
6472
def fingerprint(keystring):
6573
key = base64.b64decode(keystring.split(' ')[1])
6674
fp = hashlib.md5(key).hexdigest()
67-
return ':'.join(a+b for a,b in zip(fp[::2], fp[1::2]))
75+
return ':'.join(a+b for a, b in zip(fp[::2], fp[1::2]))
76+
6877

6978
def confirm_key_upload(email, keyname, key):
7079
token = str(uuid.uuid4())
7180
pending_actions[token] = (upload_key, (email, keyname, key))
7281
schedule_action_expiration(token)
7382
send_confirmation('upload', token, email)
7483

84+
7585
def confirm_key_delete(email, keyname):
7686
token = str(uuid.uuid4())
7787
pending_actions[token] = (delete_key, (email, keyname))
7888
schedule_action_expiration(token)
7989
send_confirmation('delete', token, email)
8090

91+
8192
def schedule_action_expiration(token):
82-
eventlet.spawn_after(action_expiry, lambda: pending_actions.pop(token, None))
93+
eventlet.spawn_after(action_expiry, lambda: pending_actions.pop(
94+
token, None))
95+
8396

8497
def send_confirmation(action, token, email):
8598
if 'SENDGRID_USERNAME' in os.environ:
86-
requests.post("https://sendgrid.com/api/mail.send.json",
99+
requests.post(
100+
"https://sendgrid.com/api/mail.send.json",
87101
data={
88-
'api_user':os.environ.get('SENDGRID_USERNAME'),
89-
'api_key':os.environ.get('SENDGRID_PASSWORD'),
90-
'to':email,
91-
'subject':"Keychain.io {} Confirmation".format(action.capitalize()),
92-
'from':"robot@keychain.io",
93-
'text':"Click this link to confirm {}:\n{}{}/confirm/{}".format(
94-
action, request.url_root, email, token)})
102+
'api_user': os.environ.get('SENDGRID_USERNAME'),
103+
'api_key': os.environ.get('SENDGRID_PASSWORD'),
104+
'to': email,
105+
'subject': "Keychain.io {} Confirmation".format(
106+
action.capitalize()),
107+
'from': "robot@keychain.io",
108+
'text': "Click this link to confirm {}:\n{}{}/confirm/{}"
109+
.format(action, request.url_root, email, token)
110+
})
95111
else:
96112
print("Email to {} for {}: {}{}/confirm/{}".format(
97113
email, action, request.url_root, email, token))
98114

115+
99116
@app.route('/')
100117
def index():
101118
return redirect("http://github.com/progrium/keychain.io")
102119

120+
103121
@app.route('/<email>/confirm/<token>')
104122
def confirm_action(email, token):
105123
if token not in pending_actions:
@@ -109,32 +127,39 @@ def confirm_action(email, token):
109127
action[0](*action[1])
110128
return "Action completed\n"
111129

130+
112131
@app.route('/<email>', methods=['GET', 'PUT', 'DELETE'])
113132
def default_key(email):
114133
return named_key(email, 'default')
115134

135+
116136
@app.route('/<email>/upload')
117137
def default_upload(email):
118138
return named_key_action(email, 'default', 'upload')
119139

140+
120141
@app.route('/<email>/install')
121142
def default_install(email):
122143
return named_key_action(email, 'default', 'install')
123144

145+
124146
@app.route('/<email>/fingerprint')
125147
def default_fingerprint(email):
126148
return named_key_action(email, 'default', 'fingerprint')
127149

150+
128151
@app.route('/<email>/all')
129152
def all_keys(email):
130153
keys_ = lookup_keys(email)
131154
return "{0}\n".format('\n'.join(keys_))
132155

156+
133157
@app.route('/<email>/all/install')
134158
def all_install(email):
135159
keys_ = lookup_keys(email)
136160
return render_template('install.sh', keys=keys_)
137161

162+
138163
@app.route('/<email>/<keyname>', methods=['GET', 'PUT', 'DELETE'])
139164
def named_key(email, keyname):
140165
if request.method == 'PUT':
@@ -160,6 +185,7 @@ def named_key(email, keyname):
160185
else:
161186
return "Key not found\n", 404
162187

188+
163189
@app.route('/<email>/<keyname>/<action>')
164190
def named_key_action(email, keyname, action):
165191
if action == 'fingerprint':
@@ -172,14 +198,13 @@ def named_key_action(email, keyname, action):
172198
elif action == 'upload':
173199
keypath = request.args.get('keypath', '')
174200
url_root = request.url_root
175-
return render_template('upload.sh', email=email,
176-
keyname=keyname, keypath=keypath, url_root=url_root)
201+
return render_template(
202+
'upload.sh', email=email,
203+
keyname=keyname, keypath=keypath, url_root=url_root)
177204

178205
elif action == 'install':
179206
key = lookup_key(email, keyname)
180207
if key:
181208
return render_template('install.sh', keys=[key])
182209
else:
183210
return 'echo "No key to install."'
184-
185-

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Flask==0.9
22
requests==0.14.1
3-
boto==2.6.0
4-
eventlet==0.9.17
3+
boto==2.34.0
4+
eventlet==0.9.17

0 commit comments

Comments
 (0)