Skip to content

Commit 3c6a34d

Browse files
committed
Require SSlibKey in relevant signers
Strictly require SSlibKey at runtime in from_priv_key_uri, to allow narrowing the key type in the signer constructor. Narrow key type to allow using key type-specific methods. Signed-off-by: Lukas Puehringer <[email protected]>
1 parent 0b6e018 commit 3c6a34d

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

securesystemslib/signer/_aws_signer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class AWSSigner(Signer):
7474
"rsa-pkcs1v15-sha512": "RSASSA_PKCS1_V1_5_SHA_512",
7575
}
7676

77-
def __init__(self, aws_key_id: str, public_key: Key):
77+
def __init__(self, aws_key_id: str, public_key: SSlibKey):
7878
if AWS_IMPORT_ERROR:
7979
raise UnsupportedLibraryError(AWS_IMPORT_ERROR)
8080

@@ -84,7 +84,7 @@ def __init__(self, aws_key_id: str, public_key: Key):
8484
self.aws_algo = self.aws_algos[self.public_key.scheme]
8585

8686
@property
87-
def public_key(self) -> Key:
87+
def public_key(self) -> SSlibKey:
8888
return self._public_key
8989

9090
@classmethod
@@ -94,6 +94,9 @@ def from_priv_key_uri(
9494
public_key: Key,
9595
secrets_handler: SecretsHandler | None = None,
9696
) -> AWSSigner:
97+
if not isinstance(public_key, SSlibKey):
98+
raise ValueError(f"Expected SSlibKey for {priv_key_uri}")
99+
97100
uri = parse.urlparse(priv_key_uri)
98101

99102
if uri.scheme != cls.SCHEME:
@@ -121,7 +124,7 @@ def _get_keytype_for_scheme(scheme: str) -> str:
121124
@classmethod
122125
def import_(
123126
cls, aws_key_id: str, local_scheme: str | None = None
124-
) -> tuple[str, Key]:
127+
) -> tuple[str, SSlibKey]:
125128
"""Loads a key and signer details from AWS KMS.
126129
127130
Returns the private key uri and the public key. This method should only
@@ -133,7 +136,7 @@ def import_(
133136
Defaults to 'rsassa-pss-sha256' if not provided and RSA.
134137
135138
Returns:
136-
Tuple[str, Key]: A tuple where the first element is a string
139+
Tuple[str, SSlibKey]: A tuple where the first element is a string
137140
representing the private key URI, and the second element is an
138141
instance of the public key.
139142

securesystemslib/signer/_azure_signer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class AzureSigner(Signer):
6666

6767
SCHEME = "azurekms"
6868

69-
def __init__(self, az_key_uri: str, public_key: Key):
69+
def __init__(self, az_key_uri: str, public_key: SSlibKey):
7070
if AZURE_IMPORT_ERROR:
7171
raise UnsupportedLibraryError(AZURE_IMPORT_ERROR)
7272

@@ -86,7 +86,7 @@ def __init__(self, az_key_uri: str, public_key: Key):
8686
self._public_key = public_key
8787

8888
@property
89-
def public_key(self) -> Key:
89+
def public_key(self) -> SSlibKey:
9090
return self._public_key
9191

9292
@staticmethod
@@ -129,7 +129,7 @@ def _create_crypto_client(
129129
raise e
130130

131131
@staticmethod
132-
def _get_signature_algorithm(public_key: Key) -> SignatureAlgorithm:
132+
def _get_signature_algorithm(public_key: SSlibKey) -> SignatureAlgorithm:
133133
"""Return SignatureAlgorithm after parsing the public key"""
134134
if public_key.keytype != "ecdsa":
135135
logger.info("only EC keys are supported for now")
@@ -183,6 +183,9 @@ def from_priv_key_uri(
183183
public_key: Key,
184184
secrets_handler: SecretsHandler | None = None,
185185
) -> AzureSigner:
186+
if not isinstance(public_key, SSlibKey):
187+
raise ValueError(f"Expected SSlibKey for {priv_key_uri}")
188+
186189
uri = parse.urlparse(priv_key_uri)
187190

188191
if uri.scheme != cls.SCHEME:
@@ -192,7 +195,7 @@ def from_priv_key_uri(
192195
return cls(az_key_uri, public_key)
193196

194197
@classmethod
195-
def import_(cls, az_vault_name: str, az_key_name: str) -> tuple[str, Key]:
198+
def import_(cls, az_vault_name: str, az_key_name: str) -> tuple[str, SSlibKey]:
196199
"""Load key and signer details from KMS
197200
198201
Returns the private key uri and the public key. This method should only

securesystemslib/signer/_crypto_signer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __init__(
187187
self._public_key = public_key
188188

189189
@property
190-
def public_key(self) -> Key:
190+
def public_key(self) -> SSlibKey:
191191
return self._public_key
192192

193193
@property

securesystemslib/signer/_gcp_signer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class GCPSigner(Signer):
5656

5757
SCHEME = "gcpkms"
5858

59-
def __init__(self, gcp_keyid: str, public_key: Key):
59+
def __init__(self, gcp_keyid: str, public_key: SSlibKey):
6060
if GCP_IMPORT_ERROR:
6161
raise exceptions.UnsupportedLibraryError(GCP_IMPORT_ERROR)
6262

@@ -66,7 +66,7 @@ def __init__(self, gcp_keyid: str, public_key: Key):
6666
self.client = kms.KeyManagementServiceClient()
6767

6868
@property
69-
def public_key(self) -> Key:
69+
def public_key(self) -> SSlibKey:
7070
return self._public_key
7171

7272
@classmethod
@@ -76,6 +76,9 @@ def from_priv_key_uri(
7676
public_key: Key,
7777
secrets_handler: SecretsHandler | None = None,
7878
) -> GCPSigner:
79+
if not isinstance(public_key, SSlibKey):
80+
raise ValueError(f"Expected SSlibKey for {priv_key_uri}")
81+
7982
uri = parse.urlparse(priv_key_uri)
8083

8184
if uri.scheme != cls.SCHEME:
@@ -84,7 +87,7 @@ def from_priv_key_uri(
8487
return cls(uri.path, public_key)
8588

8689
@classmethod
87-
def import_(cls, gcp_keyid: str) -> tuple[str, Key]:
90+
def import_(cls, gcp_keyid: str) -> tuple[str, SSlibKey]:
8891
"""Load key and signer details from KMS
8992
9093
Returns the private key uri and the public key. This method should only

securesystemslib/signer/_hsm_signer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def __init__(
128128
self,
129129
hsm_keyid: int,
130130
token_filter: dict[str, str],
131-
public_key: Key,
131+
public_key: SSlibKey,
132132
pin_handler: SecretsHandler,
133133
):
134134
if CRYPTO_IMPORT_ERROR:
@@ -149,7 +149,7 @@ def __init__(
149149
self.pin_handler = pin_handler
150150

151151
@property
152-
def public_key(self) -> Key:
152+
def public_key(self) -> SSlibKey:
153153
return self._public_key
154154

155155
@staticmethod

securesystemslib/signer/_vault_signer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class VaultSigner(Signer):
4040

4141
SCHEME = "hv"
4242

43-
def __init__(self, hv_key_name: str, public_key: Key, hv_key_version: int):
43+
def __init__(self, hv_key_name: str, public_key: SSlibKey, hv_key_version: int):
4444
if VAULT_IMPORT_ERROR:
4545
raise UnsupportedLibraryError(VAULT_IMPORT_ERROR)
4646

@@ -76,7 +76,7 @@ def sign(self, payload: bytes) -> Signature:
7676
return Signature(self.public_key.keyid, sig)
7777

7878
@property
79-
def public_key(self) -> Key:
79+
def public_key(self) -> SSlibKey:
8080
return self._public_key
8181

8282
@classmethod
@@ -86,6 +86,9 @@ def from_priv_key_uri(
8686
public_key: Key,
8787
secrets_handler: SecretsHandler | None = None,
8888
) -> VaultSigner:
89+
if not isinstance(public_key, SSlibKey):
90+
raise ValueError(f"Expected SSlibKey for {priv_key_uri}")
91+
8992
uri = parse.urlparse(priv_key_uri)
9093

9194
if uri.scheme != cls.SCHEME:
@@ -96,7 +99,7 @@ def from_priv_key_uri(
9699
return cls(name, public_key, int(version))
97100

98101
@classmethod
99-
def import_(cls, hv_key_name: str) -> tuple[str, Key]:
102+
def import_(cls, hv_key_name: str) -> tuple[str, SSlibKey]:
100103
"""Load key and signer details from HashiCorp Vault.
101104
102105
If multiple keys exist in the vault under the passed name, only the

0 commit comments

Comments
 (0)