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

[IoT] Change certificate loading to encode to b64 strings by default #23140

Merged
merged 2 commits into from
Jul 25, 2022
Merged
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
5 changes: 1 addition & 4 deletions src/azure-cli/azure/cli/command_modules/iot/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ def open_certificate(certificate_path):
if certificate_path.endswith('.pem') or certificate_path.endswith('.cer'):
with open(certificate_path, "rb") as cert_file:
certificate = cert_file.read()
try:
certificate = certificate.decode("utf-8")
except UnicodeError:
certificate = base64.b64encode(certificate).decode("utf-8")
certificate = base64.b64encode(certificate).decode("utf-8")
else:
raise ValueError("Certificate file type must be either '.pem' or '.cer'.")
# Remove trailing white space from the certificate content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ def _create_test_cert(cert_file, key_file, subject, valid_days, serial_number):
f.write(key_dump)


def _delete_test_cert(cert_file, key_file, verification_file):
if exists(cert_file) and exists(key_file):
os.remove(cert_file)
os.remove(key_file)
if exists(verification_file):
os.remove(verification_file)
def _create_fake_chain_cert(cert_file, chain_file):
# get the contents of a certificate
certificate = ""
with open(cert_file, "rb") as c:
certificate = c.read()
certificate = certificate.decode("utf-8")

# write it twice to a file to create a chain cert
with open(chain_file, "wt", encoding="utf-8") as f:
f.write(certificate)
f.write("\n")
f.write(certificate)


def _delete_test_cert(cert_files):
for cert_file in cert_files:
if exists(cert_file):
os.remove(cert_file)


def _create_verification_cert(cert_file, key_file, verification_file, nonce, valid_days, serial_number):
Expand Down
Loading