Skip to content

Commit

Permalink
Use openstack proxy for kms service (#127)
Browse files Browse the repository at this point in the history
Use openstack proxy for kms service

Switch kms and obs to using openstack proxy instead of openstack_sdk proxy

Reviewed-by: None <None>
Reviewed-by: Artem Goncharov <[email protected]>
  • Loading branch information
Polina-Gubina authored Apr 12, 2021
1 parent 1502247 commit c06b329
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 65 deletions.
2 changes: 1 addition & 1 deletion doc/source/sdk/guides/kms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Encrypt Datakey
This interface is used to encrypt a KMS data encryption key.

.. literalinclude:: ../examples/kms/encrypt_datakey.py
:lines: 16-27
:lines: 16-25

Decrypt Datakey
^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion examples/kms/create_datakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
conn = openstack.connect(cloud='otc')

dek = conn.kms.create_datakey(
key_id='cmk_id',
cmk='cmk_id',
datakey_length='512'
)
print(dek)
2 changes: 1 addition & 1 deletion examples/kms/create_datakey_without_plaintext.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
conn = openstack.connect(cloud='otc')

dek = conn.kms.create_datakey_wo_plain(
key_id='cmk_id',
cmk='cmk_id',
datakey_length='512'
)
print(dek)
2 changes: 1 addition & 1 deletion examples/kms/decrypt_datakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
conn = openstack.connect(cloud='otc')

data = conn.kms.decrypt_datakey(
key_id='cmk_id',
cmk='cmk_id',
cypher_text='64_bit_cypher',
datakey_cypher_length='64'
)
Expand Down
4 changes: 1 addition & 3 deletions examples/kms/encrypt_datakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
conn = openstack.connect(cloud='otc')

data = conn.kms.encrypt_datakey(
key_id='cmk_id',
plain_text='64_bit_key',
datakey_plain_length='64'
datakey='datakey_id'
)
print(data)
4 changes: 2 additions & 2 deletions otcextensions/sdk/kms/kms_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
# under the License.
from openstack import service_description

from otcextensions.sdk.kms.v1 import _proxy
from otcextensions.sdk.kms.v1 import _proxy as _proxy_v1


class KmsService(service_description.ServiceDescription):
"""The KMS service."""

supported_versions = {
'1': _proxy.Proxy
'1': _proxy_v1.Proxy
}
20 changes: 8 additions & 12 deletions otcextensions/sdk/kms/v1/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,37 @@
from openstack import utils

from otcextensions.i18n import _
from otcextensions.sdk import sdk_resource

_logger = _log.setup_logging('openstack')


class Resource(sdk_resource.Resource):
class Resource(resource.Resource):
base_path = '/kms'

#: Error code when create a secret key
error_code = resource.Body('error_code')
#: Error message when create a secret key
error_msg = resource.Body('error_msg')

def create(self, session, prepend_key=True, requires_id=True,
endpoint_override=None, headers=None, uri=None):
def create(self, session, prepend_key=False, uri=None,
requires_id=True, **params):
if not self.allow_create:
raise exceptions.MethodNotSupported(self, "create")

session = self._get_session(session)

request = self._prepare_request(requires_id=False,
prepend_key=prepend_key)
prepend_key=prepend_key
)
# PATH is different
if uri:
request.url = uri
elif self.create_path:
request.url = self.create_path

req_args = self._prepare_override_args(
endpoint_override=endpoint_override,
request_headers=request.headers,
additional_headers=headers)

response = session.post(request.url,
json=request.body, **req_args)
json=request.body, headers=request.headers,
params=params)

if response.status_code == 400:
body = response.json()
Expand All @@ -69,7 +65,7 @@ def create(self, session, prepend_key=True, requires_id=True,
return self

def _action(self, session, url_part, body):
"""Preform actions given the message body.
"""Perform actions given the message body.
"""
url = utils.urljoin(self.base_path, url_part)
response = session.post(
Expand Down
13 changes: 10 additions & 3 deletions otcextensions/sdk/kms/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from otcextensions.sdk import sdk_proxy
from openstack import proxy
from otcextensions.sdk.kms.v1 import data_key as _data_key
from otcextensions.sdk.kms.v1 import key as _key
from otcextensions.sdk.kms.v1 import misc as _misc


class Proxy(sdk_proxy.Proxy):
class Proxy(proxy.Proxy):

skip_discovery = True

def __init__(self, session, *args, **kwargs):
super(Proxy, self).__init__(session=session, *args, **kwargs)
self.additional_headers = {
'Content-Type': 'application/json',
'X-Language': 'en-us'
}

# ======== CMK Keys ========
def keys(self, **query):
"""List all master keys.
Expand Down Expand Up @@ -233,7 +240,7 @@ def get_instance_number(self):
:class:`~otcextensions.sdk.kms.v1.key.InstanceNumber`
"""
instance_num_obj = _misc.InstanceNumber()
return instance_num_obj.get(self)
return instance_num_obj.fetch(self)

def quotas(self):
"""List quota resources for KMS service
Expand Down
10 changes: 5 additions & 5 deletions otcextensions/sdk/kms/v1/data_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DataKey(_base.Resource):

allow_create = True
allow_update = True
allow_fetch = False

# Properties
#: Secret key (CMK) ID
Expand Down Expand Up @@ -60,12 +61,11 @@ class DataKey(_base.Resource):
#: *Type:str*
cipher_text = resource.Body('cipher_text')

def create_wo_plain(self, session, prepend_key=True, requires_id=True,
endpoint_override=None, headers=None):
def create_wo_plain(self, session, prepend_key=True, requires_id=True):
return super(DataKey, self).create(
session, prepend_key=prepend_key, requires_id=requires_id,
endpoint_override=endpoint_override, headers=headers,
uri='/kms/create-datakey-without-plaintext')
session, prepend_key=prepend_key,
uri='/kms/create-datakey-without-plaintext',
requires_id=requires_id)

def encrypt(self, session):
"""Encrypt DEK
Expand Down
24 changes: 10 additions & 14 deletions otcextensions/sdk/kms/v1/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class Key(_base.Resource):
resource_key = 'key_info'

allow_list = True
allow_get = True
allow_create = True
allow_update = False
allow_fetch = True

# Properties
#: Secret key ID
Expand Down Expand Up @@ -55,8 +56,8 @@ class Key(_base.Resource):
#: Error message when create a secret key
error_msg = resource.Body('error_msg')

def get(self, session, error_message=None, requires_id=True,
endpoint_override=None, headers=None):
def fetch(self, session, requires_id=None,
base_path=None, error_message=None):
if not self.allow_get:
raise exceptions.MethodNotSupported(self, "get")
url = self.get_path
Expand Down Expand Up @@ -93,8 +94,7 @@ def cancel_deletion(self, session):
)

@classmethod
def list(cls, session, paginated=True,
endpoint_override=None, headers=None, **kwargs):
def list(cls, session, paginated=True, base_path=None, **kwargs):

if not cls.allow_list:
raise exceptions.MethodNotSupported(cls, "list")
Expand All @@ -108,25 +108,21 @@ def list(cls, session, paginated=True,
body = {}
limit = None
if 'limit' in kwargs:
limit = kwargs['limit']
limit = kwargs.pop('limit')
body['limit'] = limit
if 'marker' in kwargs:
body['marker'] = kwargs['marker']
body['marker'] = kwargs.pop('marker')
if 'key_state' in kwargs:
body['key_state'] = kwargs['key_state']
body['key_state'] = kwargs.pop('key_state')
if 'sequence' in kwargs:
body['sequence'] = kwargs['sequence']
body['sequence'] = kwargs.pop('sequence')

total_yielded = 0
while uri:

session = cls._get_session(session)

args = cls._prepare_override_args(
endpoint_override=endpoint_override,
additional_headers=headers)

response = session.post(uri, json=body, **args)
response = session.post(uri, json=body, **kwargs)

data = response.json()

Expand Down
8 changes: 5 additions & 3 deletions otcextensions/sdk/kms/v1/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ class Random(_base.Resource):
create_path = '/kms/gen-random'

allow_create = True
allow_get = False

# Properties
#: Random data length
#: *Type:str*
random_data_length = resource.Body('random_data_length', type=int)
#: Random data content

#: *Type:str*
random_data = resource.Body('random_data')


class InstanceNumber(_base.Resource):

base_path = 'kms/user-instances'
allow_get = True
allow_fetch = True
# Properties
#: Instance number
#: *Type: int*
instance_num = resource.Body('instance_num', type=int)

def get(self, session):
return super(InstanceNumber, self).get(session, requires_id=False)
def fetch(self, session):
return super(InstanceNumber, self).fetch(session, requires_id=False)


class Quota(_base.Resource):
Expand Down
5 changes: 3 additions & 2 deletions otcextensions/tests/functional/sdk/kms/v1/test_data_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def setUp(self):
)

def tearDown(self):
super(TestDataKey, self).tearDown()
try:
if self.cmk:
key = self.cmk
if key.id:
self.conn.kms.schedule_key_deletion(key, 7)
except exceptions.SDKException as e:
_logger.warning('Got exception during clearing resources %s'
% e.message)
self.warning = _logger.warning('Got exception during '
'clearing resources %s' % e.message)

def test_dek(self):

Expand Down
3 changes: 2 additions & 1 deletion otcextensions/tests/functional/sdk/kms/v1/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class TestKey(base.BaseFunctionalTest):
KEY_ALIAS = 'sdk_test_key'
KEY_ALIAS = 'sdkk_test_key'
cmks = []

def setUp(self):
Expand All @@ -40,6 +40,7 @@ def tearDown(self):
except exceptions.SDKException as e:
_logger.warning('Got exception during clearing resources %s'
% e.message)
super(TestKey, self).tearDown()

def test_list(self):
self.keys = list(self.conn.kms.keys())
Expand Down
2 changes: 1 addition & 1 deletion otcextensions/tests/unit/sdk/kms/v1/test_data_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_basic(self):
self.assertEqual('/kms/create-datakey', sot.create_path)
self.assertFalse(sot.allow_list)
self.assertTrue(sot.allow_create)
self.assertFalse(sot.allow_get)
self.assertFalse(sot.allow_fetch)
self.assertTrue(sot.allow_update)
self.assertFalse(sot.allow_delete)

Expand Down
8 changes: 4 additions & 4 deletions otcextensions/tests/unit/sdk/kms/v1/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def test_basic(self):
self.assertEqual('/kms/list-keys', sot.list_path)
self.assertTrue(sot.allow_list)
self.assertTrue(sot.allow_create)
self.assertTrue(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertTrue(sot.allow_fetch)
self.assertFalse(sot.allow_commit)
self.assertFalse(sot.allow_delete)

def test_make_it(self):
Expand Down Expand Up @@ -164,7 +164,7 @@ def test_list(self):
self.assertEqual(expected_list, result)

def test_get(self):
sot = _key.Key.existing(
sot = _key.Key(
id=EXAMPLE['key_id'])
mock_response = mock.Mock()
mock_response.status_code = 200
Expand All @@ -174,7 +174,7 @@ def test_get(self):

self.sess.post.return_value = mock_response

result = sot.get(self.sess)
result = sot.fetch(self.sess)

self.sess.post.assert_called_once_with(
'/kms/describe-key',
Expand Down
12 changes: 7 additions & 5 deletions otcextensions/tests/unit/sdk/kms/v1/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def test_basic(self):
'/kms/gen-random', sot.create_path)
self.assertFalse(sot.allow_list)
self.assertTrue(sot.allow_create)
self.assertFalse(sot.allow_get)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_fetch)
self.assertFalse(sot.allow_commit)
self.assertFalse(sot.allow_delete)

def test_make_it(self):
Expand Down Expand Up @@ -80,7 +80,7 @@ class TestInstanceNum(base.TestCase):
def setUp(self):
super(TestInstanceNum, self).setUp()
self.sess = mock.Mock(spec=adapter.Adapter)
# self.sess.get = mock.Mock()
self.sess.default_microversion = None
self.sess.get = mock.Mock()
self.sot = misc.InstanceNumber()

Expand All @@ -95,10 +95,12 @@ def test_get(self):
self.sess.get.return_value = mock_response

sot = misc.InstanceNumber()
result = sot.get(self.sess)
result = sot.fetch(self.sess)

self.sess.get.assert_called_once_with(
'kms/user-instances'
'kms/user-instances',
microversion=None,
params={}
)

self.assertEqual(sot, result)
Expand Down
Loading

0 comments on commit c06b329

Please sign in to comment.