Ejemplo n.º 1
0
 def _get_kv(self, key):
     scope = self._scope
     LOG.debug('Lookup system kv: scope: %s and key: %s', scope, key)
     kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
     if kvp:
         LOG.debug('Got value %s from datastore.', kvp.value)
     return kvp.value if kvp else ''
Ejemplo n.º 2
0
def get_key(key=None, user=None, scope=None, decrypt=False):
    """Retrieve key from KVP store
    """
    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.')

    if not user:
        user = UserDB(cfg.CONF.system_user.user)

    scope, key_id = _derive_scope_and_key(key, user, scope)

    scope = get_datastore_full_scope(scope)

    LOG.debug('get_key scope: %s', scope)

    _validate_scope(scope=scope)

    is_admin = rbac_utils.user_is_admin(user_db=user)

    # User needs to be either admin or requesting item for itself
    _validate_decrypt_query_parameter(decrypt=decrypt,
                                      scope=scope,
                                      is_admin=is_admin,
                                      user=user)

    value = KeyValuePair.get_by_scope_and_name(scope, key_id)

    if value:
        return deserialize_key_value(value.value, decrypt)

    return None
 def _get_kv(self, key):
     scope = self._scope
     LOG.debug('Lookup system kv: scope: %s and key: %s', scope, key)
     kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
     if kvp:
         LOG.debug('Got value %s from datastore.', kvp.value)
     return kvp.value if kvp else ''
Ejemplo n.º 4
0
    def _get_kv(self, key):
        scope = self._scope

        try:
            kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
        except StackStormDBObjectNotFoundError:
            kvp = None

        return kvp.value if kvp else ""
Ejemplo n.º 5
0
    def _get_kv(self, key):
        scope = self._scope

        try:
            kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
        except StackStormDBObjectNotFoundError:
            kvp = None

        return kvp.value if kvp else ''
Ejemplo n.º 6
0
    def _get_kv(self, key):
        scope = self._scope
        LOG.debug("Lookup system kv: scope: %s and key: %s", scope, key)

        try:
            kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
        except StackStormDBObjectNotFoundError:
            kvp = None

        if kvp:
            LOG.debug("Got value %s from datastore.", kvp.value)
        return kvp.value if kvp else ""
Ejemplo n.º 7
0
    def _get_kv(self, key):
        scope = self._scope
        LOG.debug('Lookup system kv: scope: %s and key: %s', scope, key)

        try:
            kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
        except StackStormDBObjectNotFoundError:
            kvp = None

        if kvp:
            LOG.debug('Got value %s from datastore.', kvp.value)
        return kvp.value if kvp else ''
Ejemplo n.º 8
0
Archivo: config.py Proyecto: zwunix/st2
def set_datastore_value_for_config_key(pack_name,
                                       key_name,
                                       value,
                                       secret=False,
                                       user=None):
    """
    Set config value in the datastore.

    This function takes care of correctly encoding the key name, serializing the
    value, etc.

    :param pack_name: Pack name.
    :type pack_name: ``str``

    :param key_name: Config key name.
    :type key_name: ``str``

    :param secret: True if this value is a secret.
    :type secret: ``bool``

    :param user: Optional username if working on a user-scoped config item.
    :type user: ``str``

    :rtype: :class:`KeyValuePairDB`
    """

    if user:
        scope = FULL_USER_SCOPE
    else:
        scope = FULL_SYSTEM_SCOPE

    name = get_key_reference(scope=scope, name=key_name, user=user)

    kvp_api = KeyValuePairAPI(name=name,
                              value=value,
                              scope=scope,
                              secret=secret)
    kvp_db = KeyValuePairAPI.to_model(kvp_api)

    # TODO: Obtain a lock
    try:
        existing_kvp_db = KeyValuePair.get_by_scope_and_name(scope=scope,
                                                             name=name)
    except StackStormDBObjectNotFoundError:
        existing_kvp_db = None

    if existing_kvp_db:
        kvp_db.id = existing_kvp_db.id

    kvp_db = KeyValuePair.add_or_update(kvp_db)
    return kvp_db
Ejemplo n.º 9
0
def get_key(key=None, user_db=None, scope=None, decrypt=False):
    """
    Retrieve key from KVP store
    """
    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.')

    if not user_db:
        # Use system user
        user_db = UserDB(cfg.CONF.system_user.user)

    scope, key_id = _derive_scope_and_key(key=key,
                                          user=user_db.name,
                                          scope=scope)
    scope = get_datastore_full_scope(scope)

    LOG.debug('get_key key_id: %s, scope: %s, user: %s, decrypt: %s' %
              (key_id, scope, str(user_db.name), decrypt))

    _validate_scope(scope=scope)

    rbac_utils = get_rbac_backend().get_utils_class()
    is_admin = rbac_utils.user_is_admin(user_db=user_db)

    # User needs to be either admin or requesting item for itself
    _validate_decrypt_query_parameter(decrypt=decrypt,
                                      scope=scope,
                                      is_admin=is_admin,
                                      user_db=user_db)

    # Get the key value pair by scope and name.
    kvp = KeyValuePair.get_by_scope_and_name(scope, key_id)

    # Decrypt in deserialize_key_value cannot handle NoneType.
    if kvp.value is None:
        return kvp.value

    return deserialize_key_value(kvp.value, decrypt)
Ejemplo n.º 10
0
Archivo: config.py Proyecto: Bala96/st2
def set_datastore_value_for_config_key(pack_name, key_name, value, secret=False, user=None):
    """
    Set config value in the datastore.

    This function takes care of correctly encoding the key name, serializing the
    value, etc.

    :param pack_name: Pack name.
    :type pack_name: ``str``

    :param key_name: Config key name.
    :type key_name: ``str``

    :param secret: True if this value is a secret.
    :type secret: ``bool``

    :param user: Optional username if working on a user-scoped config item.
    :type user: ``str``

    :rtype: :class:`KeyValuePairDB`
    """

    if user:
        scope = USER_SCOPE
    else:
        scope = SYSTEM_SCOPE

    name = get_key_reference(scope=scope, name=key_name, user=user)

    kvp_api = KeyValuePairAPI(name=name, value=value, scope=scope, secret=secret)
    kvp_db = KeyValuePairAPI.to_model(kvp_api)

    # TODO: Obtain a lock
    existing_kvp_db = KeyValuePair.get_by_scope_and_name(scope=scope, name=name)
    if existing_kvp_db:
        kvp_db.id = existing_kvp_db.id

    kvp_db = KeyValuePair.add_or_update(kvp_db)
    return kvp_db
Ejemplo n.º 11
0
def get_key(key=None, user_db=None, scope=None, decrypt=False):
    """
    Retrieve key from KVP store
    """
    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.')

    if not user_db:
        # Use system user
        user_db = UserDB(cfg.CONF.system_user.user)

    scope, key_id = _derive_scope_and_key(key=key, user=user_db.name, scope=scope)
    scope = get_datastore_full_scope(scope)

    LOG.debug('get_key key_id: %s, scope: %s, user: %s, decrypt: %s' % (key_id, scope,
                                                                        str(user_db.name),
                                                                        decrypt))

    _validate_scope(scope=scope)

    rbac_utils = get_rbac_backend().get_utils_class()
    is_admin = rbac_utils.user_is_admin(user_db=user_db)

    # User needs to be either admin or requesting item for itself
    _validate_decrypt_query_parameter(decrypt=decrypt, scope=scope, is_admin=is_admin,
                                      user_db=user_db)

    # Get the key value pair by scope and name.
    kvp = KeyValuePair.get_by_scope_and_name(scope, key_id)

    # Decrypt in deserialize_key_value cannot handle NoneType.
    if kvp.value is None:
        return kvp.value

    return deserialize_key_value(kvp.value, decrypt)
Ejemplo n.º 12
0
 def _get_kv(self, key):
     scope = self._scope
     kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
     return kvp.value if kvp else ''
Ejemplo n.º 13
0
 def _get_kv(self, key):
     scope = self._scope
     kvp = KeyValuePair.get_by_scope_and_name(scope=scope, name=key)
     return kvp.value if kvp else ''