|
| 1 | +""" |
| 2 | +This module confirms that signing using Azure KMS keys works. |
| 3 | +
|
| 4 | +The purpose is to do a smoke test, not to exhaustively test every possible |
| 5 | +key and environment combination. |
| 6 | +
|
| 7 | +For Azure, the requirements to successfully test are: |
| 8 | +* Azure authentication details have to be available in the environment |
| 9 | +* The key defined in the test has to be available to the authenticated user |
| 10 | +
|
| 11 | +NOTE: the filename is purposefully check_ rather than test_ so that tests are |
| 12 | +only run when explicitly invoked. |
| 13 | +""" |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +from securesystemslib.exceptions import UnverifiedSignatureError |
| 18 | +from securesystemslib.signer import AzureSigner, Key, Signer |
| 19 | + |
| 20 | + |
| 21 | +class TestAzureKeys(unittest.TestCase): |
| 22 | + """Test that KMS keys can be used to sign.""" |
| 23 | + |
| 24 | + azure_pubkey = Key.from_dict( |
| 25 | + "8b4af6aec66518bc66718474aa15c8becd3286e8e2b958c497a60a828d591d04", |
| 26 | + { |
| 27 | + "keytype": "ecdsa", |
| 28 | + "scheme": "ecdsa-sha2-nistp256", |
| 29 | + "keyval": { |
| 30 | + "public": "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE95qxD+/kX6oCace7hrfChtz2IYGK\nHNBmUwtf3wXH0VEdLPWVoFgGITonvA7vxqYrF8ZzAeeZYNyEBbod7SEeaw==\n-----END PUBLIC KEY-----\n" |
| 31 | + }, |
| 32 | + }, |
| 33 | + ) |
| 34 | + azure_id = "azurekms://fsn-vault-1.vault.azure.net/keys/ec-key-1/b1089bbf068742d483970282f02090de" |
| 35 | + |
| 36 | + def test_azure_sign(self): |
| 37 | + """Test that Azure KMS key works for signing |
| 38 | +
|
| 39 | + Note that this test requires valid credentials available. |
| 40 | + """ |
| 41 | + |
| 42 | + data = "data".encode("utf-8") |
| 43 | + |
| 44 | + signer = Signer.from_priv_key_uri(self.azure_id, self.azure_pubkey) |
| 45 | + sig = signer.sign(data) |
| 46 | + |
| 47 | + print(sig.signature) |
| 48 | + |
| 49 | + self.azure_pubkey.verify_signature(sig, data) |
| 50 | + with self.assertRaises(UnverifiedSignatureError): |
| 51 | + self.azure_pubkey.verify_signature(sig, b"NOT DATA") |
| 52 | + |
| 53 | + def test_azure_import(self): |
| 54 | + """Test that Azure KMS key works for signing |
| 55 | +
|
| 56 | + Note that this test requires valid credentials available. |
| 57 | + """ |
| 58 | + |
| 59 | + uri, pubkey = AzureSigner.import_("fsn-vault-1", "ec-key-1") |
| 60 | + |
| 61 | + self.assertEqual(pubkey, self.azure_pubkey) |
| 62 | + self.assertEqual(uri, self.azure_id) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + unittest.main(verbosity=1, buffer=True) |
0 commit comments