Skip to content

Commit 1ade699

Browse files
authored
[Identity] Update live test config (#35019)
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent 95593f6 commit 1ade699

File tree

4 files changed

+66
-37
lines changed

4 files changed

+66
-37
lines changed

sdk/identity/azure-identity/tests/conftest.py

+40-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
import base64
56
import os
67
import sys
78

@@ -82,20 +83,31 @@ def live_service_principal():
8283
}
8384

8485

85-
def get_certificate_parameters(content, password_protected_content, password, extension):
86-
# type: (bytes, bytes, str, str) -> dict
86+
def get_certificate_parameters(content: bytes, extension: str) -> dict:
8787
current_directory = os.path.dirname(__file__)
8888
parameters = {
8989
"cert_bytes": content,
9090
"cert_path": os.path.join(current_directory, "certificate." + extension),
91+
}
92+
93+
try:
94+
with open(parameters["cert_path"], "wb") as f:
95+
f.write(parameters["cert_bytes"])
96+
except IOError as ex:
97+
pytest.skip("Failed to write a file: {}".format(ex))
98+
99+
return parameters
100+
101+
102+
def get_certificate_with_password_parameters(password_protected_content: bytes, password: str, extension: str) -> dict:
103+
current_directory = os.path.dirname(__file__)
104+
parameters = {
91105
"cert_with_password_bytes": password_protected_content,
92106
"cert_with_password_path": os.path.join(current_directory, "certificate-with-password." + extension),
93107
"password": password,
94108
}
95109

96110
try:
97-
with open(parameters["cert_path"], "wb") as f:
98-
f.write(parameters["cert_bytes"])
99111
with open(parameters["cert_with_password_path"], "wb") as f:
100112
f.write(parameters["cert_with_password_bytes"])
101113
except IOError as ex:
@@ -110,31 +122,45 @@ def live_pem_certificate(live_service_principal):
110122
password_protected_content = os.environ.get("PEM_CONTENT_PASSWORD_PROTECTED")
111123
password = os.environ.get("CERTIFICATE_PASSWORD")
112124

113-
if content and password_protected_content and password:
114-
parameters = get_certificate_parameters(
115-
content.encode("utf-8"), password_protected_content.encode("utf-8"), password, "pem"
125+
cert_info = {}
126+
127+
if content:
128+
content = content.replace("\\n", "\r\n")
129+
parameters = get_certificate_parameters(content.encode("utf-8"), "pem")
130+
cert_info.update(parameters)
131+
132+
if password_protected_content and password:
133+
parameters = get_certificate_with_password_parameters(
134+
password_protected_content.encode("utf-8"), password, "pem"
116135
)
117-
return dict(live_service_principal, **parameters)
136+
cert_info.update(parameters)
118137

138+
if cert_info:
139+
return dict(live_service_principal, **cert_info)
119140
pytest.skip("Missing PEM certificate configuration")
120141

121142

122143
@pytest.fixture()
123144
def live_pfx_certificate(live_service_principal):
124145
# PFX bytes arrive base64 encoded because Key Vault secrets have string values
125-
encoded_content = os.environ.get("PFX_CONTENT")
146+
encoded_content = os.environ.get("PFX_CONTENTS")
126147
encoded_password_protected_content = os.environ.get("PFX_CONTENT_PASSWORD_PROTECTED")
127148
password = os.environ.get("CERTIFICATE_PASSWORD")
128149

129-
if encoded_content and encoded_password_protected_content and password:
130-
import base64
150+
cert_info = {}
131151

152+
if encoded_content:
132153
content = base64.b64decode(encoded_content.encode("utf-8"))
133-
password_protected_content = base64.b64decode(encoded_password_protected_content.encode("utf-8"))
154+
parameters = get_certificate_parameters(content, "pfx")
155+
cert_info.update(parameters)
134156

135-
parameters = get_certificate_parameters(content, password_protected_content, password, "pfx")
136-
return dict(live_service_principal, **parameters)
157+
if encoded_password_protected_content and password:
158+
password_protected_content = base64.b64decode(encoded_password_protected_content.encode("utf-8"))
159+
parameters = get_certificate_with_password_parameters(password_protected_content, password, "pfx")
160+
cert_info.update(parameters)
137161

162+
if cert_info:
163+
return dict(live_service_principal, **cert_info)
138164
pytest.skip("Missing PFX certificate configuration")
139165

140166

sdk/identity/azure-identity/tests/test_live.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,22 @@ def test_certificate_credential(certificate_fixture, request):
3939
credential = CertificateCredential(tenant_id, client_id, cert["cert_path"])
4040
get_token(credential)
4141

42-
credential = CertificateCredential(tenant_id, client_id, cert["cert_with_password_path"], password=cert["password"])
43-
get_token(credential)
44-
4542
credential = CertificateCredential(tenant_id, client_id, certificate_data=cert["cert_bytes"])
46-
get_token(credential)
47-
48-
credential = CertificateCredential(
49-
tenant_id, client_id, certificate_data=cert["cert_with_password_bytes"], password=cert["password"]
50-
)
5143
token = get_token(credential, enable_cae=True)
5244
parsed_payload = get_token_payload_contents(token.token)
5345
assert "xms_cc" in parsed_payload and "CP1" in parsed_payload["xms_cc"]
5446

47+
if "password" in cert:
48+
credential = CertificateCredential(
49+
tenant_id, client_id, cert["cert_with_password_path"], password=cert["password"]
50+
)
51+
get_token(credential)
52+
53+
credential = CertificateCredential(
54+
tenant_id, client_id, certificate_data=cert["cert_with_password_bytes"], password=cert["password"]
55+
)
56+
get_token(credential)
57+
5558

5659
def test_client_secret_credential(live_service_principal):
5760
credential = ClientSecretCredential(

sdk/identity/azure-identity/tests/test_live_async.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@ async def test_certificate_credential(certificate_fixture, request):
3737
credential = CertificateCredential(tenant_id, client_id, cert["cert_path"])
3838
await get_token(credential)
3939

40-
credential = CertificateCredential(tenant_id, client_id, cert["cert_with_password_path"], password=cert["password"])
41-
await get_token(credential)
42-
4340
credential = CertificateCredential(tenant_id, client_id, certificate_data=cert["cert_bytes"])
44-
await get_token(credential)
45-
46-
credential = CertificateCredential(
47-
tenant_id, client_id, certificate_data=cert["cert_with_password_bytes"], password=cert["password"]
48-
)
4941
token = await get_token(credential, enable_cae=True)
5042
parsed_payload = get_token_payload_contents(token.token)
5143
assert "xms_cc" in parsed_payload and "CP1" in parsed_payload["xms_cc"]
5244

45+
if "password" in cert:
46+
credential = CertificateCredential(
47+
tenant_id, client_id, cert["cert_with_password_path"], password=cert["password"]
48+
)
49+
await get_token(credential)
50+
51+
credential = CertificateCredential(
52+
tenant_id, client_id, certificate_data=cert["cert_with_password_bytes"], password=cert["password"]
53+
)
54+
await get_token(credential, enable_cae=True)
55+
5356

5457
@pytest.mark.asyncio
5558
async def test_client_secret_credential(live_service_principal):

sdk/identity/tests.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ extends:
55
parameters:
66
ServiceDirectory: identity
77
EnvVars:
8-
AZURE_CLIENT_ID: $(python-identity-client-id)
9-
AZURE_CLIENT_SECRET: $(python-identity-client-secret)
10-
AZURE_TENANT_ID: $(aad-azure-sdk-test-tenant-id)
11-
CERTIFICATE_PASSWORD: $(python-identity-certificate-password)
8+
AZURE_CLIENT_ID: $(IDENTITY_SP_CLIENT_ID)
9+
AZURE_CLIENT_SECRET: $(IDENTITY_SP_CLIENT_SECRET)
10+
AZURE_TENANT_ID: $(IDENTITY_SP_TENANT_ID)
1211
PEM_CONTENT: $(python-identity-certificate)
13-
PEM_CONTENT_PASSWORD_PROTECTED: $(python-identity-certificate-with-password)
14-
PFX_CONTENT: $(python-identity-certificate-pfx)
15-
PFX_CONTENT_PASSWORD_PROTECTED: $(python-identity-certificate-with-password-pfx)
1612
AZURE_TEST_RUN_LIVE: true
1713
AZURE_SKIP_LIVE_RECORDING: 'True'
1814
CloudConfig:
1915
Public:
2016
SubscriptionConfigurations:
2117
- $(sub-config-azure-cloud-test-resources)
18+
- $(sub-config-identity-test-resources)
2219
${{ if contains(variables['Build.DefinitionName'], 'tests-weekly') }}:
2320
# Test Managed Identity integrations tests on weekly tests pipeline.
2421
AdditionalMatrixConfigs:

0 commit comments

Comments
 (0)