Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Add HSM support to Key Vault #1717

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ action_groups:
- azure.azcollection.azure_rm_ipgroup_info
- azure.azcollection.azure_rm_keyvault
- azure.azcollection.azure_rm_keyvault_info
- azure.azcollection.azure_rm_keyvaultsecuritydomain
- azure.azcollection.azure_rm_keyvaultkey
- azure.azcollection.azure_rm_keyvaultkey_info
- azure.azcollection.azure_rm_keyvaultsecret
Expand Down
61 changes: 61 additions & 0 deletions plugins/module_utils/security_domain_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------


import array
import base64
import hashlib
import secrets
import traceback

try:
from cryptography.hazmat.primitives.serialization import Encoding
HAS_CRYPTO = True
HAS_CRYPTO_EXC = None
except ImportError:
Encoding = None
HAS_CRYPTO = False
HAS_CRYPTO_EXC = traceback.format_exc()


class Utils:
@staticmethod
def is_little_endian():
a = array.array('H', [1]).tobytes()
# little endian: b'\x01\x00'
# big endian: b'\x00\x01'
return a[0] == 1

@staticmethod
def convert_to_uint16(b: bytearray):
ret = [0 for _ in range(len(b) // 2)]
for i in range(0, len(b), 2):
byte_order = 'little' if Utils.is_little_endian() else 'big'
ret[i // 2] = int.from_bytes(bytearray([b[i], b[i + 1]]), byteorder=byte_order, signed=False)
return ret

@staticmethod
def get_random(cb):
ret = bytearray()
for _ in range(cb):
ret.append(secrets.randbits(8))
return ret

@staticmethod
def get_SHA256_thumbprint(cert):
public_bytes = cert.public_bytes(Encoding.DER)
return hashlib.sha256(public_bytes).digest()

@staticmethod
def security_domain_b64_url_encode_for_x5c(s):
return base64.b64encode(s).decode('ascii')

@staticmethod
def security_domain_b64_url_encode(s):
return base64.b64encode(s).decode('ascii').strip('=').replace('+', '-').replace('/', '_')


if __name__ == '__main__':
print(Utils.convert_to_uint16(bytearray([40, 30, 20, 10])))
Loading