def st2kv_(context, key, **kwargs): if not isinstance(key, six.string_types): raise TypeError('Given key is not typeof string.') decrypt = kwargs.get('decrypt', False) if not isinstance(decrypt, bool): raise TypeError('Decrypt parameter is not typeof bool.') try: username = context['__vars']['st2']['user'] except KeyError: raise KeyError('Could not get user from context.') try: user_db = auth_db_access.User.get(username) except Exception as e: raise Exception('Failed to retrieve User object for user "%s" % (username)' % (six.text_type(e))) has_default = 'default' in kwargs default_value = kwargs.get('default') try: return kvp_util.get_key(key=key, user_db=user_db, decrypt=decrypt) except db_exc.StackStormDBObjectNotFoundError as e: if not has_default: raise exc.ExpressionEvaluationException(str(e)) else: return default_value except Exception as e: raise exc.ExpressionEvaluationException(str(e))
def st2kv_(context, key, decrypt=False): if not isinstance(key, six.string_types): raise TypeError('Given key is not typeof string.') if not isinstance(decrypt, bool): raise TypeError('Decrypt parameter is not typeof bool.') try: username = context['__vars']['st2']['user'] except KeyError: raise KeyError('Could not get user from context.') try: user_db = auth_db_access.User.get(username) except Exception as e: raise Exception( 'Failed to retrieve User object for user "%s" % (username)' % (str(e))) kvp = kvp_util.get_key(key=key, user_db=user_db, decrypt=decrypt) if not kvp: raise exc.ExpressionEvaluationException( 'Key %s does not exist in StackStorm datastore.' % key) return kvp
def st2kv_(context, key, decrypt=False): if not isinstance(key, six.string_types): raise TypeError('Given key is not typeof string.') if not isinstance(decrypt, bool): raise TypeError('Decrypt parameter is not typeof bool.') try: username = context['__vars']['st2']['user'] except KeyError: raise KeyError('Could not get user from context.') try: user_db = auth_db_access.User.get(username) except Exception as e: raise Exception('Failed to retrieve User object for user "%s" % (username)' % (six.text_type(e))) kvp = kvp_util.get_key(key=key, user_db=user_db, decrypt=decrypt) if not kvp: raise exc.ExpressionEvaluationException( 'Key %s does not exist in StackStorm datastore.' % key ) return kvp
def st2kv_(context, key, decrypt=False): if not isinstance(key, six.string_types): raise TypeError('Given key is not typeof string.') if not isinstance(decrypt, bool): raise TypeError('Decrypt parameter is not typeof bool.') try: user = context['__vars']['st2']['user'] except KeyError: raise KeyError('Could not get user from context.') LOG.debug('ST2KV Decrypt: %s', decrypt) return get_key(key=key, user=user, decrypt=decrypt)
def test_get_key(self, deseralize_key_value, KeyValuePair): key, value = ('Lindsay', 'Lohan') decrypt = False KeyValuePair.get_by_scope_and_name().value = value deseralize_key_value.return_value = value result = kv_utl.get_key(key=key, user_db=auth_db.UserDB(name=USER), decrypt=decrypt) self.assertEqual(result, value) KeyValuePair.get_by_scope_and_name.assert_called_with( FULL_USER_SCOPE, 'stanley:%s' % key) deseralize_key_value.assert_called_once_with(value, decrypt)