Merge "Reject invalid username and password"
This commit is contained in:
31
src/charm.py
31
src/charm.py
@@ -525,10 +525,37 @@ class CephISCSIGatewayCharmBase(
|
|||||||
rbd_pool_name = self.data_pool_name
|
rbd_pool_name = self.data_pool_name
|
||||||
return rbd_pool_name, ec_rbd_metadata_pool
|
return rbd_pool_name, ec_rbd_metadata_pool
|
||||||
|
|
||||||
|
def _validate_str(self, value, allowed, min_len, max_len, typ):
|
||||||
|
if any(s for s in value if s not in allowed):
|
||||||
|
raise ValueError('%s can only contain: %s' % (typ, allowed))
|
||||||
|
elif len(value) < min_len or len(value) > max_len:
|
||||||
|
raise ValueError('%s must be between %d and %d characters long' %
|
||||||
|
(typ, min_len, max_len))
|
||||||
|
|
||||||
|
def _validate_username(self, value):
|
||||||
|
self._validate_str(value, string.ascii_letters + string.digits +
|
||||||
|
'.@-_:', 8, 64, 'username')
|
||||||
|
|
||||||
|
def _validate_password(self, value):
|
||||||
|
self._validate_str(value, string.ascii_letters + string.digits +
|
||||||
|
'@-_/', 12, 16, 'password')
|
||||||
|
|
||||||
def on_create_target_action(self, event):
|
def on_create_target_action(self, event):
|
||||||
"""Create an iSCSI target."""
|
"""Create an iSCSI target."""
|
||||||
gw_client = gwcli_client.GatewayClient()
|
gw_client = gwcli_client.GatewayClient()
|
||||||
target = event.params.get('iqn', self.DEFAULT_TARGET)
|
target = event.params.get('iqn', self.DEFAULT_TARGET)
|
||||||
|
username = event.params['client-username']
|
||||||
|
passwd = event.params['client-password']
|
||||||
|
try:
|
||||||
|
self._validate_username(username)
|
||||||
|
self._validate_password(passwd)
|
||||||
|
except ValueError as exc:
|
||||||
|
logging.error(str(exc))
|
||||||
|
fail_str = 'invalid username or password: %s' % str(exc)
|
||||||
|
event.fail(fail_str)
|
||||||
|
event.set_results({'err': fail_str})
|
||||||
|
return
|
||||||
|
|
||||||
gateway_units = event.params.get(
|
gateway_units = event.params.get(
|
||||||
'gateway-units',
|
'gateway-units',
|
||||||
[u for u in self.peers.ready_peer_details.keys()])
|
[u for u in self.peers.ready_peer_details.keys()])
|
||||||
@@ -571,8 +598,8 @@ class CephISCSIGatewayCharmBase(
|
|||||||
gw_client.add_client_auth(
|
gw_client.add_client_auth(
|
||||||
target,
|
target,
|
||||||
event.params['client-initiatorname'],
|
event.params['client-initiatorname'],
|
||||||
event.params['client-username'],
|
username,
|
||||||
event.params['client-password'])
|
passwd)
|
||||||
gw_client.add_disk_to_client(
|
gw_client.add_disk_to_client(
|
||||||
target,
|
target,
|
||||||
event.params['client-initiatorname'],
|
event.params['client-initiatorname'],
|
||||||
|
@@ -237,7 +237,7 @@ class TestCephISCSIGatewayCharmBase(CharmTestCase):
|
|||||||
'image-size': '5G',
|
'image-size': '5G',
|
||||||
'client-initiatorname': 'client-initiator',
|
'client-initiatorname': 'client-initiator',
|
||||||
'client-username': 'myusername',
|
'client-username': 'myusername',
|
||||||
'client-password': 'mypassword'}
|
'client-password': 'mypassword123'}
|
||||||
self.harness.charm.on_create_target_action(action_event)
|
self.harness.charm.on_create_target_action(action_event)
|
||||||
self.gwc.add_gateway_to_target.assert_has_calls([
|
self.gwc.add_gateway_to_target.assert_has_calls([
|
||||||
call(
|
call(
|
||||||
@@ -260,7 +260,7 @@ class TestCephISCSIGatewayCharmBase(CharmTestCase):
|
|||||||
'iqn.mock.iscsi-gw:iscsi-igw',
|
'iqn.mock.iscsi-gw:iscsi-igw',
|
||||||
'client-initiator',
|
'client-initiator',
|
||||||
'myusername',
|
'myusername',
|
||||||
'mypassword')
|
'mypassword123')
|
||||||
self.gwc.add_disk_to_client.assert_called_once_with(
|
self.gwc.add_disk_to_client.assert_called_once_with(
|
||||||
'iqn.mock.iscsi-gw:iscsi-igw',
|
'iqn.mock.iscsi-gw:iscsi-igw',
|
||||||
'client-initiator',
|
'client-initiator',
|
||||||
@@ -283,7 +283,7 @@ class TestCephISCSIGatewayCharmBase(CharmTestCase):
|
|||||||
'image-size': '5G',
|
'image-size': '5G',
|
||||||
'client-initiatorname': 'client-initiator',
|
'client-initiatorname': 'client-initiator',
|
||||||
'client-username': 'myusername',
|
'client-username': 'myusername',
|
||||||
'client-password': 'mypassword'}
|
'client-password': 'mypassword123'}
|
||||||
self.harness.charm.on_create_target_action(action_event)
|
self.harness.charm.on_create_target_action(action_event)
|
||||||
self.subprocess.check_call.assert_called_once_with(
|
self.subprocess.check_call.assert_called_once_with(
|
||||||
[
|
[
|
||||||
@@ -315,7 +315,7 @@ class TestCephISCSIGatewayCharmBase(CharmTestCase):
|
|||||||
'iqn.mock.iscsi-gw:iscsi-igw',
|
'iqn.mock.iscsi-gw:iscsi-igw',
|
||||||
'client-initiator',
|
'client-initiator',
|
||||||
'myusername',
|
'myusername',
|
||||||
'mypassword')
|
'mypassword123')
|
||||||
self.gwc.add_disk_to_client.assert_called_once_with(
|
self.gwc.add_disk_to_client.assert_called_once_with(
|
||||||
'iqn.mock.iscsi-gw:iscsi-igw',
|
'iqn.mock.iscsi-gw:iscsi-igw',
|
||||||
'client-initiator',
|
'client-initiator',
|
||||||
|
Reference in New Issue
Block a user