def test_mocked_castellan_keymanager_bad_user_domain_name( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Bad user domain name with mocks. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return invalid Barbican configuration parameters. ''' kms_conf = dict(TEST_KMS_KEYMASTER_CONF) kms_conf['user_domain_name'] = (TEST_KMS_INVALID_USER_DOMAIN_NAME) mock_readconf.return_value = kms_conf ''' Verify that an exception is raised by the mocked function. ''' try: self.app = kms_keymaster.KmsKeyMaster( self.swift, TEST_PROXYSERVER_CONF_EXTERNAL_KEYMASTER_CONF) raise Exception('Success even though api_version invalid') except Unauthorized as e: self.assertEqual(e.http_status, 401) except Exception: print("Unexpected error: %s" % sys.exc_info()[0]) raise
def test_mocked_castellan_keymanager_none_key( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Nonexistent key. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return invalid Barbican configuration parameters. ''' kms_conf = dict(TEST_KMS_KEYMASTER_CONF) kms_conf['key_id'] = TEST_KMS_NONE_KEY_ID mock_readconf.return_value = kms_conf ''' Verify that an exception is raised by the mocked function. ''' try: self.app = kms_keymaster.KmsKeyMaster( self.swift, TEST_PROXYSERVER_CONF_EXTERNAL_KEYMASTER_CONF) raise Exception('Success even though None key returned') except ValueError: pass except Exception: print("Unexpected error: %s" % sys.exc_info()[0]) raise
def test_mocked_castellan_keymanager_config_file_and_params( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Both external config file and config parameters specified. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return invalid Barbican configuration parameters. ''' kms_conf = dict(TEST_KMS_KEYMASTER_CONF) kms_conf['keymaster_config_path'] = ('PATH_TO_KEYMASTER_CONFIG_FILE') mock_readconf.return_value = kms_conf ''' Verify that an exception is raised by the mocked function. ''' try: self.app = kms_keymaster.KmsKeyMaster(self.swift, kms_conf) raise Exception('Success even though config invalid') except Exception as e: expected_message = ('keymaster_config_path is set, but there are ' 'other config options specified:') self.assertTrue( e.message.startswith(expected_message), "Error message does not start with '%s'" % expected_message)
def test_mocked_castellan_keymanager_bad_auth_url( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Bad kms auth_url. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return invalid Barbican configuration parameters. ''' kms_conf = dict(TEST_KMS_KEYMASTER_CONF) kms_conf['auth_endpoint'] = TEST_KMS_NON_BARBICAN_URL mock_readconf.return_value = kms_conf ''' Verify that an exception is raised by the mocked function. ''' try: self.app = kms_keymaster.KmsKeyMaster( self.swift, TEST_PROXYSERVER_CONF_EXTERNAL_KEYMASTER_CONF) raise Exception('Success even though auth_url invalid') except DiscoveryFailure: pass except Exception: print("Unexpected error: %s" % sys.exc_info()[0]) raise
def test_mocked_castellan_keymanager_invalid_key_id( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Invalid key ID. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return invalid Barbican configuration parameters. ''' kms_conf = dict(TEST_KMS_KEYMASTER_CONF) kms_conf['key_id'] = TEST_KMS_INVALID_KEY_ID mock_readconf.return_value = kms_conf ''' Verify that an exception is raised by the mocked function. ''' try: self.app = kms_keymaster.KmsKeyMaster( self.swift, TEST_PROXYSERVER_CONF_EXTERNAL_KEYMASTER_CONF) raise Exception('Success even though key id invalid') except ValueError as e: self.assertEqual(e.message, ERR_MESSAGE_SECRET_INCORRECTLY_SPECIFIED) except Exception: print("Unexpected error: %s" % sys.exc_info()[0]) raise
def test_mocked_castellan_keymanager_nonexistent_key_id( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Nonexistent key. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return invalid Barbican configuration parameters. ''' kms_conf = dict(TEST_KMS_KEYMASTER_CONF) kms_conf['key_id'] = TEST_KMS_NONEXISTENT_KEY_ID mock_readconf.return_value = kms_conf ''' Verify that an exception is raised by the mocked function. ''' try: self.app = kms_keymaster.KmsKeyMaster( self.swift, TEST_PROXYSERVER_CONF_EXTERNAL_KEYMASTER_CONF) raise Exception('Success even though key id invalid') except Exception as e: expected_message = ('Key not found, uuid: ' + TEST_KMS_NONEXISTENT_KEY_ID) self.assertEqual(e.message, expected_message)
def test_get_root_secret_multiple_keys( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, ): config = dict(TEST_KMS_KEYMASTER_CONF) config.update({ 'key_id_foo': 'foo-valid_kms_key_id-123456', 'key_id_bar': 'bar-valid_kms_key_id-123456', 'active_root_secret_id': 'foo' }) # Set side_effect functions. mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) # Return valid Barbican configuration parameters. mock_readconf.return_value = config self.app = kms_keymaster.KmsKeyMaster(self.swift, config) expected_secrets = { None: b'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv', 'foo': b'ffffffffffffffffffffffffffffffff', 'bar': b'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' } self.assertDictEqual(self.app._root_secrets, expected_secrets) self.assertEqual(self.app.active_secret_id, 'foo')
def test_mocked_castellan_keymanager(self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config, mock_keystone_password): # Successful call with finer grained mocks. mock_keystone_password.side_effect = ( mock_keystone_password_side_effect) ''' Set side_effect functions. ''' mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) ''' Return valid Barbican configuration parameters. ''' mock_readconf.return_value = TEST_KMS_KEYMASTER_CONF ''' Verify that no exceptions are raised by the mocked functions. ''' try: self.app = kms_keymaster.KmsKeyMaster(self.swift, TEST_KMS_KEYMASTER_CONF) except Exception: print("Unexpected error: %s" % sys.exc_info()[0]) raise
def test_app_exception_v3(self, mock_get_root_secret_from_kms, mock_readconf): mock_get_root_secret_from_kms.return_value = (base64.b64encode(b'x' * 32)) mock_readconf.return_value = TEST_KMS_KEYMASTER_CONF app = kms_keymaster.KmsKeyMaster(FakeAppThatExcepts(), TEST_KMS_KEYMASTER_CONF) req = Request.blank('/', environ={'REQUEST_METHOD': 'PUT'}) start_response, _ = capture_start_response() self.assertRaises(Exception, app, req.environ, start_response)
def test_get_root_secret(self, mock_get_root_secret_from_kms): # Successful call with coarse _get_root_secret_from_kms() mock. mock_get_root_secret_from_kms.return_value = (base64.b64encode(b'x' * 32)) # Provide valid Barbican configuration parameters in proxy-server # config. self.app = kms_keymaster.KmsKeyMaster(self.swift, TEST_KMS_KEYMASTER_CONF) # Verify that _get_root_secret_from_kms() was called with the # correct parameters. mock_get_root_secret_from_kms.assert_called_with( TEST_KMS_KEYMASTER_CONF)
def test_get_root_secret_from_external_file( self, mock_get_root_secret_from_kms, mock_readconf): # Return valid Barbican configuration parameters. mock_readconf.return_value = TEST_KMS_KEYMASTER_CONF # Successful call with coarse _get_root_secret_from_kms() mock. mock_get_root_secret_from_kms.return_value = ( base64.b64encode(b'x' * 32)) # Point to external config in proxy-server config. self.app = kms_keymaster.KmsKeyMaster( self.swift, TEST_PROXYSERVER_CONF_EXTERNAL_KEYMASTER_CONF) # Verify that _get_root_secret_from_kms() was called with the # correct parameters. mock_get_root_secret_from_kms.assert_called_with( TEST_KMS_KEYMASTER_CONF ) self.assertEqual(mock_readconf.mock_calls, [ mock.call('PATH_TO_KEYMASTER_CONFIG_FILE', 'kms_keymaster')])
def test_get_root_secret(self, mock_get_root_secret_from_kms, mock_readconf): # Successful call with coarse _get_root_secret_from_kms() mock. mock_get_root_secret_from_kms.return_value = (base64.b64encode(b'x' * 32)) ''' Return valid Barbican configuration parameters. ''' mock_readconf.return_value = TEST_KMS_KEYMASTER_CONF ''' Verify that keys are derived correctly by the keymaster. ''' self.app = kms_keymaster.KmsKeyMaster(self.swift, TEST_KMS_KEYMASTER_CONF) ''' Verify that _get_root_secret_from_kms() was called with the correct parameters. ''' mock_get_root_secret_from_kms.assert_called_with( TEST_KMS_KEYMASTER_CONF)
def test_get_root_secret_legacy_key_id( self, mock_castellan_key_manager, mock_readconf, mock_castellan_options, mock_oslo_config): # Set side_effect functions. mock_castellan_key_manager.API.side_effect = ( mock_castellan_api_side_effect) mock_castellan_options.set_defaults.side_effect = ( mock_options_set_defaults_side_effect) mock_oslo_config.ConfigOpts.side_effect = ( mock_config_opts_side_effect) # Return valid Barbican configuration parameters. mock_readconf.return_value = TEST_KMS_KEYMASTER_CONF self.app = kms_keymaster.KmsKeyMaster(self.swift, TEST_KMS_KEYMASTER_CONF) expected_secrets = {None: b'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv'} self.assertDictEqual(self.app._root_secrets, expected_secrets) self.assertIsNone(self.app.active_secret_id)