~ubuntu-branches/ubuntu/saucy/glance/saucy-proposed

« back to all changes in this revision

Viewing changes to glance/db/sqlalchemy/migrate_repo/versions/017_quote_encrypted_swift_credentials.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-05-31 08:17:08 UTC
  • mfrom: (1.1.52)
  • Revision ID: package-import@ubuntu.com-20130531081708-3un9mp81xo3d6geh
Tags: 1:2013.2~b1-0ubuntu1
* New upstream release.
* debian/control: Add python-openssl as a build-depends. 
* debian/patches/fix-nosetests-path.patch: No longer needed.
* debian/rules: Temporarily disable tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
Fixes bug #1081043
30
30
"""
31
 
 
32
31
import types
 
32
import urllib
33
33
import urlparse
34
34
 
35
35
from oslo.config import cfg
112
112
    """
113
113
    if not uri:
114
114
        return
115
 
    location = glance.store.swift.StoreLocation({})
116
 
    if to_quoted:
117
 
        # The legacy parse_uri doesn't unquote credentials
118
 
        location.parse_uri = types.MethodType(legacy_parse_uri, location)
119
 
    else:
120
 
        # The legacy _get_credstring doesn't quote credentials
121
 
        location._get_credstring = types.MethodType(legacy__get_credstring,
122
 
                                                    location)
123
 
    decrypted_uri = None
124
115
    try:
125
116
        decrypted_uri = decrypt_location(uri)
126
117
    #NOTE (ameade): If a uri is not encrypted or incorrectly encoded then we
128
119
    except (TypeError, ValueError) as e:
129
120
        raise exception.Invalid(str(e))
130
121
 
131
 
    location.parse_uri(decrypted_uri)
132
 
    return encrypt_location(location.get_uri())
133
 
 
134
 
 
135
 
def legacy__get_credstring(self):
136
 
    if self.user:
137
 
        return '%s:%s@' % (self.user, self.key)
138
 
    return ''
139
 
 
140
 
 
141
 
def legacy_parse_uri(self, uri):
 
122
    return legacy_parse_uri(decrypted_uri, to_quoted)
 
123
 
 
124
 
 
125
def legacy_parse_uri(uri, to_quote):
142
126
    """
143
127
    Parse URLs. This method fixes an issue where credentials specified
144
128
    in the URL are interpreted differently in Python 2.6.1+ than prior
146
130
    Swift URIs have where a username can contain a ':', like so:
147
131
 
148
132
        swift://account:user:pass@authurl.com/container/obj
 
133
 
 
134
    If to_quoted is True, the uri is assumed to have credentials that
 
135
    have not been quoted, and the resulting uri will contain quoted
 
136
    credentials.
 
137
 
 
138
    If to_quoted is False, the uri is assumed to have credentials that
 
139
    have been quoted, and the resulting uri will contain credentials
 
140
    that have not been quoted.
149
141
    """
150
142
    # Make sure that URIs that contain multiple schemes, such as:
151
143
    # swift://user:pass@http://authurl.com/v1/container/obj
163
155
 
164
156
    pieces = urlparse.urlparse(uri)
165
157
    assert pieces.scheme in ('swift', 'swift+http', 'swift+https')
166
 
    self.scheme = pieces.scheme
 
158
    scheme = pieces.scheme
167
159
    netloc = pieces.netloc
168
160
    path = pieces.path.lstrip('/')
169
161
    if netloc != '':
187
179
        # User can be account:user, in which case cred_parts[0:2] will be
188
180
        # the account and user. Combine them into a single username of
189
181
        # account:user
190
 
        if len(cred_parts) == 1:
191
 
            reason = (_("Badly formed credentials '%(creds)s' in Swift "
192
 
                        "URI") % locals())
193
 
            LOG.error(reason)
194
 
            raise exception.BadStoreUri()
195
 
        elif len(cred_parts) == 3:
196
 
            user = ':'.join(cred_parts[0:2])
 
182
        if to_quote:
 
183
            if len(cred_parts) == 1:
 
184
                reason = (_("Badly formed credentials '%(creds)s' in Swift "
 
185
                            "URI") % locals())
 
186
                LOG.error(reason)
 
187
                raise exception.BadStoreUri()
 
188
            elif len(cred_parts) == 3:
 
189
                user = ':'.join(cred_parts[0:2])
 
190
            else:
 
191
                user = cred_parts[0]
 
192
            key = cred_parts[-1]
 
193
            user = user
 
194
            key = key
197
195
        else:
198
 
            user = cred_parts[0]
199
 
        key = cred_parts[-1]
200
 
        self.user = user
201
 
        self.key = key
 
196
            if len(cred_parts) != 2:
 
197
                reason = (_("Badly formed credentials in Swift URI."))
 
198
                LOG.debug(reason)
 
199
                raise exception.BadStoreUri()
 
200
            user, key = cred_parts
 
201
            user = urllib.unquote(user)
 
202
            key = urllib.unquote(key)
202
203
    else:
203
 
        self.user = None
 
204
        user = None
 
205
        key = None
204
206
    path_parts = path.split('/')
205
207
    try:
206
 
        self.obj = path_parts.pop()
207
 
        self.container = path_parts.pop()
 
208
        obj = path_parts.pop()
 
209
        container = path_parts.pop()
208
210
        if not netloc.startswith('http'):
209
211
            # push hostname back into the remaining to build full authurl
210
212
            path_parts.insert(0, netloc)
211
 
            self.auth_or_store_url = '/'.join(path_parts)
 
213
            auth_or_store_url = '/'.join(path_parts)
212
214
    except IndexError:
213
215
        reason = _("Badly formed S3 URI: %s") % uri
214
216
        LOG.error(message=reason)
215
217
        raise exception.BadStoreUri()
 
218
 
 
219
    if auth_or_store_url.startswith('http://'):
 
220
        auth_or_store_url = auth_or_store_url[len('http://'):]
 
221
    elif auth_or_store_url.startswith('https://'):
 
222
        auth_or_store_url = auth_or_store_url[len('https://'):]
 
223
 
 
224
    credstring = ''
 
225
    if user and key:
 
226
        if to_quote:
 
227
            quote_user = urllib.quote(user)
 
228
            quote_key = urllib.quote(key)
 
229
        else:
 
230
            quote_user = user
 
231
            quote_key = key
 
232
        credstring = '%s:%s@' % (quote_user, quote_key)
 
233
 
 
234
    auth_or_store_url = auth_or_store_url.strip('/')
 
235
    container = container.strip('/')
 
236
    obj = obj.strip('/')
 
237
 
 
238
    uri = '%s://%s%s/%s/%s' % (scheme, credstring, auth_or_store_url,
 
239
                               container, obj)
 
240
    return encrypt_location(uri)