コード例 #1
0
ファイル: luks.py プロジェクト: balamurugana/openlmi-scripts
def open_luks(ns, fmt, name, passphrase):
    """
    Open encrypted LUKS format and expose it as a clear-text block device.

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to open.
    :type name: string
    :param name: Requested name of the clear-text block device. It will be
            available as /dev/mapper/<name>.
    :type passphrase: string
    :param passphrase: Password to open the encrypted data.
    :rtype: LMIInstance/LMI_LUKSStorageExtent
    :returns: The block device with clear-text data.
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.SyncOpenEncryptionFormat(
            Format=fmt,
            ElementName=name,
            Passphrase=passphrase)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot open LUKS format: %s." % err)
        values = service.OpenEncryptionFormat.OpenEncryptionFormatValues
        raise LmiFailed("Cannot open LUKS format: %s."
                % (values.value_name(ret),))

    opened = outparams['Extent'].to_instance()
    LOG().info("Opened LUKS on %s as %s", fmt.ElementName, opened.Name)
    return opened
コード例 #2
0
ファイル: luks.py プロジェクト: balamurugana/openlmi-scripts
def add_luks_passphrase(ns, fmt, passphrase, new_passphrase):
    """
    Adds new password to LUKS format. Each format can have up to 8 separate
    passwords and any of them can be used to open(decrypt) the format.

    Any existing passphrase must be provided to add a new one. This proves
    the caller is authorized to add new passphrase (because it already knows
    one) and also this 'old' passphrase is used to retrieve encryption keys.
    This 'old' passphrase is not removed nor replaced when adding new
    passphrase!

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to modify.
    :type passphrase: string
    :param passphrase: Existing LUKS passphrase.
    :type new_passphrase: string
    :param new_passphrase: New passphrase to add to the format.
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.AddPassphrase(
            Format=fmt,
            Passphrase=passphrase,
            NewPassphrase=new_passphrase)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot add new passphrase: %s." % err)
        values = service.AddPassphrase.AddPassphraseValues
        raise LmiFailed("Cannot add new passphrase: %s."
                % (values.value_name(ret),))
    LOG().info("Added passphrase to %s", fmt.ElementName)
コード例 #3
0
def add_luks_passphrase(ns, fmt, passphrase, new_passphrase):
    """
    Adds new password to LUKS format. Each format can have up to 8 separate
    passwords and any of them can be used to open(decrypt) the format.

    Any existing passphrase must be provided to add a new one. This proves
    the caller is authorized to add new passphrase (because it already knows
    one) and also this 'old' passphrase is used to retrieve encryption keys.
    This 'old' passphrase is not removed nor replaced when adding new
    passphrase!

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to modify.
    :type passphrase: string
    :param passphrase: Existing LUKS passphrase.
    :type new_passphrase: string
    :param new_passphrase: New passphrase to add to the format.
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.AddPassphrase(Format=fmt,
                                                  Passphrase=passphrase,
                                                  NewPassphrase=new_passphrase)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot add new passphrase: %s." % err)
        values = service.AddPassphrase.AddPassphraseValues
        raise LmiFailed("Cannot add new passphrase: %s." %
                        (values.value_name(ret), ))
    LOG().info("Added passphrase to %s", fmt.ElementName)
コード例 #4
0
def open_luks(ns, fmt, name, passphrase):
    """
    Open encrypted LUKS format and expose it as a clear-text block device.

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to open.
    :type name: string
    :param name: Requested name of the clear-text block device. It will be
            available as /dev/mapper/<name>.
    :type passphrase: string
    :param passphrase: Password to open the encrypted data.
    :rtype: LMIInstance/LMI_LUKSStorageExtent
    :returns: The block device with clear-text data.
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams,
     err) = service.SyncOpenEncryptionFormat(Format=fmt,
                                             ElementName=name,
                                             Passphrase=passphrase)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot open LUKS format: %s." % err)
        values = service.OpenEncryptionFormat.OpenEncryptionFormatValues
        raise LmiFailed("Cannot open LUKS format: %s." %
                        (values.value_name(ret), ))

    opened = outparams['Extent'].to_instance()
    LOG().info("Opened LUKS on %s as %s", fmt.ElementName, opened.Name)
    return opened
コード例 #5
0
ファイル: show.py プロジェクト: jsynacek/openlmi-scripts
def format_show(ns, fmt, human_friendly):
    """
    Display description of data on the device.

    :type fmt: LMIInstance/LMI_DataFormat or string
    :param fmt: Format to show.
    """
    fmt = fs.str2format(ns, fmt)
    yield ("Data Format", fmt.FormatTypeDescription)
    if "UUID" in fmt.properties() and fmt.UUID:
        yield ("UUID", fmt.UUID)
コード例 #6
0
def format_show(ns, fmt, human_friendly):
    """
    Display description of data on the device.

    :type fmt: LMIInstance/LMI_DataFormat or string
    :param fmt: Format to show.
    """
    fmt = fs.str2format(ns, fmt)
    yield ("Data Format", fmt.FormatTypeDescription)
    if "UUID" in fmt.properties() and fmt.UUID:
        yield ("UUID", fmt.UUID)
コード例 #7
0
ファイル: luks.py プロジェクト: jsynacek/openlmi-scripts
def close_luks(ns, fmt):
    """
    Closes clear-text block device previously opened by open_luks().

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to close.
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.SyncCloseEncryptionFormat(Format=fmt)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot close LUKS format: %s." % err)
        values = service.CloseEncryptionFormat.CloseEncryptionFormatValues
        raise LmiFailed("Cannot close LUKS format: %s." % (values.value_name(ret),))
コード例 #8
0
ファイル: luks.py プロジェクト: balamurugana/openlmi-scripts
def get_passphrase_count(ns, fmt):
    """
    Each LUKS format can have up to 8 passphrases. Any of these passphrases can
    be used to decrypt the format and create clear-text device.

    This function returns number of passphrases in given LUKS format.

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to inspect.
    :rtype: int
    :returns: Number of used passphrases.
    """

    fmt = fs.str2format(ns, fmt)
    count = reduce(lambda a, b: a + b, fmt.SlotStatus)
    return count
コード例 #9
0
def get_passphrase_count(ns, fmt):
    """
    Each LUKS format can have up to 8 passphrases. Any of these passphrases can
    be used to decrypt the format and create clear-text device.

    This function returns number of passphrases in given LUKS format.

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to inspect.
    :rtype: int
    :returns: Number of used passphrases.
    """

    fmt = fs.str2format(ns, fmt)
    count = reduce(lambda a, b: a + b, fmt.SlotStatus)
    return count
コード例 #10
0
ファイル: luks.py プロジェクト: jsynacek/openlmi-scripts
def get_luks_device(ns, fmt):
    """
    Return clear-text device for given LUKS format. The format must be already
    opened by open_luks().

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to inspect.
    :rtype: LMIInstance/LMI_LUKSStorageExtent
    :returns: Block device with clear-text data or None, if the LUKS format is
            not open.
    """

    fmt = fs.str2format(ns, fmt)
    crypttext_device = fmt.first_associator(AssocClass="LMI_ResidesOnExtent", Role="Dependent")
    device = crypttext_device.first_associator(AssocClass="LMI_LUKSBasedOn", Role="Antecedent")
    return device
コード例 #11
0
ファイル: luks.py プロジェクト: jsynacek/openlmi-scripts
def close_luks(ns, fmt):
    """
    Closes clear-text block device previously opened by open_luks().

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to close.
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.SyncCloseEncryptionFormat(Format=fmt)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot close LUKS format: %s." % err)
        values = service.CloseEncryptionFormat.CloseEncryptionFormatValues
        raise LmiFailed("Cannot close LUKS format: %s." %
                        (values.value_name(ret), ))
コード例 #12
0
ファイル: luks.py プロジェクト: jsynacek/openlmi-scripts
def delete_luks_passphrase(ns, fmt, passphrase):
    """
    Delete passphrase from LUKS format.

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to modify.
    :type passphrase: string
    :param passphrase: The passphrase to remove
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.DeletePassphrase(Format=fmt, Passphrase=passphrase)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot delete passphrase: %s." % err)
        values = service.DeletePassphrase.DeletePassphraseValues
        raise LmiFailed("Cannot delete passphrase: %s." % (values.value_name(ret),))
コード例 #13
0
def get_luks_device(ns, fmt):
    """
    Return clear-text device for given LUKS format. The format must be already
    opened by open_luks().

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to inspect.
    :rtype: LMIInstance/LMI_LUKSStorageExtent
    :returns: Block device with clear-text data or None, if the LUKS format is
            not open.
    """

    fmt = fs.str2format(ns, fmt)
    crypttext_device = fmt.first_associator(AssocClass="LMI_ResidesOnExtent",
                                            Role="Dependent")
    device = crypttext_device.first_associator(AssocClass="LMI_LUKSBasedOn",
                                               Role="Antecedent")
    return device
コード例 #14
0
ファイル: luks.py プロジェクト: jsynacek/openlmi-scripts
def delete_luks_passphrase(ns, fmt, passphrase):
    """
    Delete passphrase from LUKS format.

    :type fmt: LMIInstance/LMI_EncryptionFormat or string
    :param fmt: The LUKS format to modify.
    :type passphrase: string
    :param passphrase: The passphrase to remove
    """
    fmt = fs.str2format(ns, fmt)
    service = ns.LMI_ExtentEncryptionConfigurationService.first_instance()
    (ret, outparams, err) = service.DeletePassphrase(Format=fmt,
                                                     Passphrase=passphrase)
    if ret != 0:
        if err:
            raise LmiFailed("Cannot delete passphrase: %s." % err)
        values = service.DeletePassphrase.DeletePassphraseValues
        raise LmiFailed("Cannot delete passphrase: %s." %
                        (values.value_name(ret), ))