forked from secure-systems-lab/securesystemslib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VaultSigner.import_ already works for ed25519 keys, and can be tested via tox -e local-vault TODO: - sign - from_priv_key_uri - test on CI Signed-off-by: Lukas Puehringer <[email protected]>
- Loading branch information
Showing
4 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Signer implementation for HashiCorp Vault (Transit secrets engine)""" | ||
|
||
from typing import Tuple | ||
from base64 import b64decode | ||
|
||
from securesystemslib.exceptions import UnsupportedLibraryError | ||
from securesystemslib.signer._key import Key, SSlibKey | ||
from securesystemslib.signer._signer import SecretsHandler, Signature, Signer | ||
from securesystemslib.signer._utils import compute_default_keyid | ||
|
||
|
||
VAULT_IMPORT_ERROR = None | ||
try: | ||
import hvac | ||
from cryptography.hazmat.primitives.asymmetric.ed25519 import ( | ||
Ed25519PublicKey, | ||
) | ||
|
||
except ImportError: | ||
VAULT_IMPORT_ERROR = "Signing with HashiCorp Vault requires hvac and cryptography." | ||
|
||
|
||
class VaultSigner(Signer): | ||
"""HashiCorp Vault Signer (Transit secrets engine) """ | ||
SCHEME = "hv" | ||
|
||
@classmethod | ||
def import_(cls, hv_key_name: str) -> Tuple[str, Key]: | ||
"""Load key and signer details from vault. | ||
Supported keytypes: | ||
* ed25519 | ||
""" | ||
if VAULT_IMPORT_ERROR: | ||
raise UnsupportedLibraryError(VAULT_IMPORT_ERROR) | ||
|
||
client = hvac.Client() | ||
|
||
resp = client.secrets.transit.read_key(hv_key_name) | ||
|
||
# Extract "newest" key from response | ||
pub_b64 = sorted(resp["data"]["keys"].items())[-1][1]["public_key"] | ||
pub_raw = b64decode(pub_b64) | ||
pub_crypto = Ed25519PublicKey.from_public_bytes(pub_raw) | ||
|
||
pub = SSlibKey.from_crypto(pub_crypto) | ||
uri = f"{VaultSigner.SCHEME}:{hv_key_name}" | ||
|
||
return uri, pub | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Test AWSSigner | ||
""" | ||
|
||
import unittest | ||
|
||
from securesystemslib.signer import VaultSigner | ||
|
||
|
||
class TestVaultSigner(unittest.TestCase): | ||
"""Test VaultSigner""" | ||
|
||
def test_vault_import_sign_verify(self): | ||
# Test full signer flow with vault | ||
# - see tests/scripts/init-vault.sh for how keys are created | ||
# - see tox.ini for how credentials etc. are passed via env vars | ||
keys_and_schemes = [ | ||
("test-key-ed25519", "ed25519") | ||
] | ||
for hv_key_name, scheme in keys_and_schemes: | ||
# Test import | ||
uri, public_key = VaultSigner.import_(hv_key_name) | ||
self.assertEqual(uri, f"{VaultSigner.SCHEME}:{hv_key_name}") | ||
self.assertEqual(scheme, public_key.scheme) | ||
|
||
# # Test load | ||
# signer = Signer.from_priv_key_uri(uri, public_key) | ||
# self.assertIsInstance(signer, VaultSigner) | ||
|
||
# # Test sign and verify | ||
# signature = signer.sign(b"DATA") | ||
# self.assertIsNone( | ||
# public_key.verify_signature(signature, b"DATA") | ||
# ) | ||
# with self.assertRaises(UnverifiedSignatureError): | ||
# public_key.verify_signature(signature, b"NOT DATA") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters