15
15
from docker_registry .core import compat
16
16
from docker_registry .core import lru
17
17
18
- # import gevent.monkey
19
- # gevent.monkey.patch_all()
20
-
21
18
import logging
19
+ import os
20
+ import re
21
+ import time
22
22
23
+ import boto .exception
23
24
import boto .s3
24
25
import boto .s3 .connection
25
26
import boto .s3 .key
26
27
27
28
logger = logging .getLogger (__name__ )
28
29
29
30
31
+ class Cloudfront ():
32
+ def __init__ (self , awsaccess , awssecret , base , keyid , privatekey ):
33
+ boto .connect_cloudfront (
34
+ awsaccess ,
35
+ awssecret
36
+ )
37
+ host = re .compile ('^https?://([^/]+)' ).findall (base )
38
+ self .dist = boto .cloudfront .distribution .Distribution (domain_name = host )
39
+ self .base = base
40
+ self .keyid = keyid
41
+ self .privatekey = privatekey
42
+ try :
43
+ self .privatekey = open (privatekey ).read ()
44
+ except Exception :
45
+ logger .debug ('Passed private key is not readable. Assume string.' )
46
+
47
+ def sign (self , url , expire_time = 0 ):
48
+ path = os .path .join (self .base , url )
49
+ if expire_time :
50
+ expire_time = time .time () + expire_time
51
+ return self .dist .create_signed_url (
52
+ path ,
53
+ self .keyid ,
54
+ private_key_string = self .privatekey ,
55
+ expire_time = int (expire_time )
56
+ )
57
+
58
+ def pub (self , path ):
59
+ return os .path .join (self .base , path )
60
+
61
+
30
62
class Storage (coreboto .Base ):
31
63
32
64
def __init__ (self , path , config ):
@@ -48,6 +80,16 @@ def makeConnection(self):
48
80
** kwargs )
49
81
logger .warn ("No S3 region specified, using boto default region, " +
50
82
"this may affect performance and stability." )
83
+ # Connect cloudfront if we are required to
84
+ if self ._config .cloudfront :
85
+ self .signer = Cloudfront (
86
+ self ._config .s3_access_key ,
87
+ self ._config .s3_secret_key ,
88
+ self ._config .cloudfront ['base' ],
89
+ self ._config .cloudfront ['keyid' ],
90
+ self ._config .cloudfront ['keysecret' ]
91
+ ).sign
92
+
51
93
return boto .s3 .connection .S3Connection (
52
94
self ._config .s3_access_key ,
53
95
self ._config .s3_secret_key ,
@@ -82,16 +124,22 @@ def stream_write(self, path, fp):
82
124
mp .upload_part_from_file (io , num_part )
83
125
num_part += 1
84
126
io .close ()
85
- except IOError :
86
- pass
127
+ except IOError as e :
128
+ raise e
87
129
mp .complete_upload ()
88
130
89
131
def content_redirect_url (self , path ):
90
132
path = self ._init_path (path )
91
133
key = self .makeKey (path )
92
134
if not key .exists ():
93
135
raise IOError ('No such key: \' {0}\' ' .format (path ))
94
- return key .generate_url (
95
- expires_in = 1200 ,
96
- method = 'GET' ,
97
- query_auth = True )
136
+
137
+ # No cloudfront? Sign to the bucket
138
+ if not self .signer :
139
+ return key .generate_url (
140
+ expires_in = 1200 ,
141
+ method = 'GET' ,
142
+ query_auth = True )
143
+
144
+ # Have cloudfront? Sign it
145
+ return self .signer (path , expire_time = 60 )
0 commit comments