Add utility functions for Swift TempURLs

blueprint swiftsignal-resource

Change-Id: I88c78b74dfb3c23cf74831bda3a9bdc69df6162a
This commit is contained in:
Jason Dunsmore 2014-07-02 13:57:54 -05:00
parent 6dd121c44d
commit f388b2e2a8
2 changed files with 178 additions and 0 deletions

View File

@ -11,11 +11,21 @@
# License for the specific language governing permissions and limitations
# under the License.
import hashlib
import random
import time
import urlparse
from swiftclient import client as sc
from swiftclient import exceptions
from swiftclient import utils as swiftclient_utils
from heat.engine.clients import client_plugin
IN_PROGRESS = 'in progress'
MAX_EPOCH = 2147483647
class SwiftClientPlugin(client_plugin.ClientPlugin):
@ -50,3 +60,56 @@ class SwiftClientPlugin(client_plugin.ClientPlugin):
def is_over_limit(self, ex):
return (isinstance(ex, exceptions.ClientException) and
ex.http_status == 413)
@staticmethod
def is_valid_temp_url_path(path):
'''Return True if path is a valid Swift TempURL path, False otherwise.
A Swift TempURL path must:
- Be five parts, ['', 'v1', 'account', 'container', 'object']
- Be a v1 request
- Have account, container, and object values
- Have an object value with more than just '/'s
:param path: The TempURL path
:type path: string
'''
parts = path.split('/', 4)
return bool(len(parts) == 5 and
not parts[0] and
parts[1] == 'v1' and
parts[2] and
parts[3] and
parts[4].strip('/'))
def get_temp_url(self, container_name, obj_name, timeout=None):
'''
Return a Swift TempURL.
'''
key_header = 'x-account-meta-temp-url-key'
if key_header in self.client().head_account():
key = self.client().head_account()[key_header]
else:
key = hashlib.sha224(str(random.getrandbits(256))).hexdigest()[:32]
self.client().post_account({key_header: key})
method = 'PUT'
path = '/v1/AUTH_%s/%s/%s' % (self.context.tenant_id, container_name,
obj_name)
if timeout is None:
timeout = MAX_EPOCH - 60 - time.time()
tempurl = swiftclient_utils.generate_temp_url(path, timeout, key,
method)
sw_url = urlparse.urlparse(self.client().url)
return '%s://%s%s' % (sw_url.scheme, sw_url.netloc, tempurl)
def get_signal_url(self, container_name, obj_name, timeout=None):
'''
Turn on object versioning so we can use a single TempURL for
multiple signals and return a Swift TempURL.
'''
self.client().put_container(
container_name, headers={'x-versions-location': container_name})
self.client().put_object(container_name, obj_name, IN_PROGRESS)
return self.get_temp_url(container_name, obj_name, timeout)

View File

@ -0,0 +1,115 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from testtools.matchers import MatchesRegex
from heat.engine.clients.os import swift
from heat.tests.common import HeatTestCase
from heat.tests import utils
class SwiftClientPluginTestCase(HeatTestCase):
def setUp(self):
super(SwiftClientPluginTestCase, self).setUp()
self.swift_client = mock.Mock()
con = utils.dummy_context()
c = con.clients
self.swift_plugin = c.client_plugin('swift')
self.swift_plugin._client = self.swift_client
class SwiftUtilsTests(SwiftClientPluginTestCase):
def test_is_valid_temp_url_path(self):
sc = swift.SwiftClientPlugin
valids = [
"/v1/AUTH_demo/c/o",
"/v1/AUTH_demo/c/o/",
"/v1/TEST_demo/c/o",
"/v1/AUTH_demo/c/pseudo_folder/o",
]
for url in valids:
self.assertTrue(sc.is_valid_temp_url_path(url))
invalids = [
"/v2/AUTH_demo/c/o",
"/v1/AUTH_demo/c//",
"/v1/AUTH_demo/c/",
"/AUTH_demo/c//",
"//AUTH_demo/c/o",
"//v1/AUTH_demo/c/o",
"/v1/AUTH_demo/o",
"/v1/AUTH_demo//o",
"/v1//c/o",
"/v1/c/o",
]
for url in invalids:
self.assertFalse(sc.is_valid_temp_url_path(url))
def test_get_temp_url(self):
self.swift_client.url = ("http://fake-host.com:8080/v1/"
"AUTH_test_tenant_id")
self.swift_client.head_account = mock.Mock(return_value={
'x-account-meta-temp-url-key': '123456'})
self.swift_client.post_account = mock.Mock()
container_name = '1234' # from stack.id
stack_name = 'test'
handle_name = 'foo'
obj_name = '%s-%s' % (stack_name, handle_name)
url = self.swift_plugin.get_temp_url(container_name, obj_name)
self.assertFalse(self.swift_client.post_account.called)
regexp = ("http://fake-host.com:8080/v1/AUTH_test_tenant_id/%s"
"/%s\?temp_url_sig=[0-9a-f]{40}&"
"temp_url_expires=[0-9]{10}" %
(container_name, obj_name))
self.assertThat(url, MatchesRegex(regexp))
timeout = int(url.split('=')[-1])
self.assertTrue(timeout < swift.MAX_EPOCH)
def test_get_temp_url_no_account_key(self):
self.swift_client.url = ("http://fake-host.com:8080/v1/"
"AUTH_test_tenant_id")
self.swift_client.head_account = mock.Mock(return_value={})
self.swift_client.post_account = mock.Mock()
self.assertFalse(self.swift_client.post_account.called)
container_name = '1234' # from stack.id
stack_name = 'test'
handle_name = 'foo'
obj_name = '%s-%s' % (stack_name, handle_name)
self.swift_plugin.get_temp_url(container_name, obj_name)
self.assertTrue(self.swift_client.post_account.called)
def test_get_signal_url(self):
self.swift_client.url = ("http://fake-host.com:8080/v1/"
"AUTH_test_tenant_id")
self.swift_client.head_account = mock.Mock(return_value={
'x-account-meta-temp-url-key': '123456'})
self.swift_client.post_account = mock.Mock()
container_name = '1234' # from stack.id
stack_name = 'test'
handle_name = 'foo'
obj_name = '%s-%s' % (stack_name, handle_name)
url = self.swift_plugin.get_signal_url(container_name, obj_name)
self.assertTrue(self.swift_client.put_container.called)
self.assertTrue(self.swift_client.put_object.called)
regexp = ("http://fake-host.com:8080/v1/AUTH_test_tenant_id/%s"
"/%s\?temp_url_sig=[0-9a-f]{40}&"
"temp_url_expires=[0-9]{10}" %
(container_name, obj_name))
self.assertThat(url, MatchesRegex(regexp))