-
Notifications
You must be signed in to change notification settings - Fork 9
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
test: add integration testing for az iot ops secretsync
#475
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ecadab5
make some secretsync
vilit1 42046ff
forgot to yaml
vilit1 091706d
meh
vilit1 b96095c
move tox setup before login
vilit1 5c7ccf9
action
vilit1 798480f
pylint
vilit1 26814fc
Merge branch 'dev' into pipeline_int_tests
vilit1 edab5a7
fix up tests
vilit1 0decdb8
pdb
vilit1 f580ce0
fixtox
vilit1 625d833
add retry to identity check
vilit1 f5ef854
I guess I will use warnings for debug
vilit1 84f8c66
freaking specify false
vilit1 45e3095
principals them ids
vilit1 5313abf
freaking pruge
vilit1 642816f
Merge branch 'dev' into pipeline_int_tests
vilit1 1a40a70
pr commetns
vilit1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
azext_edge/tests/edge/orchestration/test_secretsync_int.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
# coding=utf-8 | ||
# ---------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License file in the project root for license information. | ||
# ---------------------------------------------------------------------------------------------- | ||
|
||
import pytest | ||
from knack.log import get_logger | ||
from time import sleep | ||
from typing import Optional | ||
|
||
from azure.cli.core.azclierror import CLIInternalError | ||
from ...generators import generate_random_string | ||
from ...helpers import run | ||
|
||
logger = get_logger(__name__) | ||
ROLE_MAX_RETRIES = 5 | ||
ROLE_RETRY_INTERVAL = 15 | ||
|
||
|
||
@pytest.fixture | ||
def secretsync_int_setup(settings, tracked_resources): | ||
from ...settings import EnvironmentVariables | ||
|
||
settings.add_to_config(EnvironmentVariables.rg.value) | ||
settings.add_to_config(EnvironmentVariables.instance.value) | ||
settings.add_to_config(EnvironmentVariables.kv.value) | ||
settings.add_to_config(EnvironmentVariables.user_assigned_mi_id.value) | ||
settings.add_to_config(EnvironmentVariables.sp_object_id.value) | ||
|
||
if not all([settings.env.azext_edge_instance, settings.env.azext_edge_rg]): | ||
raise AssertionError( | ||
f"Cannot run secretsync tests without an instance and resource group. Current settings:\n {settings}" | ||
) | ||
if not any([settings.env.azext_edge_kv, settings.env.azext_edge_sp_object_id]): | ||
pytest.skip( | ||
"Cannot run secretsync tests without a keyvault id or a object id. Object Id is needed to add " | ||
"'Key Vault Secrets Officer' to a newly created key vault." | ||
) | ||
|
||
kv_id = settings.env.azext_edge_kv | ||
kv_name = None | ||
if not kv_id: | ||
kv_name = "spc" + generate_random_string(size=6) | ||
kv_id = run(f"az keyvault create -n {kv_name} -g {settings.env.azext_edge_rg}")["id"] | ||
# add "Key Vault Secrets Officer" role | ||
run( | ||
"az role assignment create --role b86a8fe4-44ce-4948-aee5-eccb2c155cd7 " | ||
f"--assignee {settings.env.azext_edge_sp_object_id} --scope {kv_id}" | ||
) | ||
|
||
mi_id = settings.env.azext_edge_user_assigned_mi_id | ||
if not mi_id: | ||
mi_id = run( | ||
f"az identity create -n {'spc' + generate_random_string(size=6)} -g {settings.env.azext_edge_rg}" | ||
)["id"] | ||
tracked_resources.append(mi_id) | ||
|
||
instance_name = settings.env.azext_edge_instance | ||
resource_group = settings.env.azext_edge_rg | ||
# list to track initial result if there is something | ||
initial_list_result = run(f"az iot ops secretsync list -n {instance_name} -g {resource_group}") | ||
if initial_list_result: | ||
run(f"az iot ops secretsync disable -n {instance_name} -g {resource_group} -y") | ||
|
||
yield { | ||
"resourceGroup": resource_group, | ||
"instanceName": instance_name, | ||
"keyvaultId": kv_id, | ||
"userAssignedId": mi_id, | ||
} | ||
|
||
# note that you need to purge the kv too... | ||
if kv_name: | ||
try: | ||
run(f"az keyvault delete -n {kv_name} -g {settings.env.azext_edge_rg}") | ||
# sometimes it takes a bit to get the deleted list to update | ||
sleep(ROLE_RETRY_INTERVAL) | ||
run(f"az keyvault purge -n {kv_name}") | ||
except CLIInternalError as e: | ||
logger.error(f"Failed to delete the keyvault {kv_name} properly. {e.error_msg}") | ||
|
||
# if it was enabled before, reenable | ||
if initial_list_result: | ||
kv_name = initial_list_result[0]["properties"]["keyvaultName"] | ||
mi_client_id = initial_list_result[0]["properties"]["clientId"] | ||
spc_name = initial_list_result[0]["name"] | ||
try: | ||
kv_id = run(f"az keyvault show -n {kv_name}")["id"] | ||
mi_id = run(f"az identity list --query \"[?clientId=='{mi_client_id}']\"")[0]["id"] | ||
# if the role assignments were applied, they should still exist | ||
# TODO: phase 2 - direct cluster connection for --self-hosted-issuer | ||
run( | ||
f"az iot ops secretsync enable -n {instance_name} -g {resource_group} " | ||
f"--mi-user-assigned {mi_id} --kv-resource-id {kv_id} --spc {spc_name} --skip-ra" | ||
) | ||
except (CLIInternalError, IndexError): | ||
logger.error("Could not reenable secretsync correctly.") | ||
|
||
|
||
@pytest.mark.rpsaas | ||
@pytest.mark.require_wlif_setup | ||
def test_secretsync(secretsync_int_setup): | ||
resource_group = secretsync_int_setup["resourceGroup"] | ||
instance_name = secretsync_int_setup["instanceName"] | ||
kv_id = secretsync_int_setup["keyvaultId"] | ||
mi_id = secretsync_int_setup["userAssignedId"] | ||
use_self_hosted_issuer = False | ||
|
||
extended_loc = run(f"az iot ops show -g {resource_group} -n {instance_name}")["extendedLocation"]["name"] | ||
mi_client_id = run(f"az identity show --ids {mi_id}")["clientId"] | ||
mi_principal_id = run(f"az identity show --ids {mi_id}")["principalId"] | ||
expected_result = { | ||
"extended_location": extended_loc, | ||
"resource_group": resource_group, | ||
"kv_name": kv_id.rsplit("/", maxsplit=1)[-1], | ||
"mi_client_id": mi_client_id, | ||
} | ||
|
||
initial_role_list = [ | ||
role["roleDefinitionName"] for role in _get_role_list(kv_id, mi_client_id) | ||
] | ||
|
||
# enable with skip ra + check if test can be run | ||
try: | ||
enable_result = run( | ||
f"az iot ops secretsync enable -n {instance_name} -g {resource_group} " | ||
f"--mi-user-assigned {mi_id} --kv-resource-id {kv_id} --skip-ra" | ||
) | ||
except CLIInternalError as e: | ||
if "not enabled as an oidc issuer or for workload identity federation." in e.error_msg: | ||
pytest.skip("Cluster is not enabled for secretsync.") | ||
elif "No issuerUrl is available." in e.error_msg: | ||
use_self_hosted_issuer = True | ||
enable_result = run( | ||
f"az iot ops secretsync enable -n {instance_name} -g {resource_group} " | ||
f"--mi-user-assigned {mi_id} --kv-resource-id {kv_id} --skip-ra --self-hosted-issuer" | ||
) | ||
else: | ||
raise e | ||
_assert_secret_sync_class( | ||
result=enable_result, | ||
**expected_result | ||
) | ||
_assert_role_assignments( | ||
initial_assignment_names=initial_role_list, | ||
kv_id=kv_id, | ||
mi_principal_id=mi_principal_id | ||
) | ||
|
||
# list | ||
list_result = run(f"az iot ops secretsync list -n {instance_name} -g {resource_group}") | ||
assert len(list_result) == 1 | ||
_assert_secret_sync_class( | ||
result=list_result[0], | ||
**expected_result | ||
) | ||
|
||
# disable | ||
run(f"az iot ops secretsync disable -n {instance_name} -g {resource_group} -y") | ||
|
||
# second enable with custom name | ||
spc_name = generate_random_string(force_lower=True) | ||
enable_result = run( | ||
f"az iot ops secretsync enable -n {instance_name} -g {resource_group} " | ||
f"--mi-user-assigned {mi_id} --kv-resource-id {kv_id} --spc {spc_name} " | ||
f"--skip-ra false {'--self-hosted-issuer' if use_self_hosted_issuer else ''} " | ||
) | ||
# TODO: phase 2 - direct cluster connection for --self-hosted-issuer | ||
_assert_secret_sync_class( | ||
result=enable_result, | ||
spc_name=spc_name, | ||
**expected_result | ||
) | ||
_assert_role_assignments( | ||
initial_assignment_names=initial_role_list, | ||
kv_id=kv_id, | ||
mi_principal_id=mi_principal_id, | ||
expected_secretsync_roles=True | ||
) | ||
|
||
# disable | ||
run(f"az iot ops secretsync disable -n {instance_name} -g {resource_group} -y") | ||
|
||
|
||
def _assert_secret_sync_class( | ||
result: dict, | ||
extended_location: str, | ||
resource_group: str, | ||
kv_name: str, | ||
mi_client_id: str, | ||
spc_name: Optional[str] = None, | ||
): | ||
assert result["extendedLocation"]["name"] == extended_location | ||
assert result["resourceGroup"] == resource_group | ||
if spc_name: | ||
assert result["name"] == spc_name | ||
else: | ||
assert result["name"].startswith("spc-ops-") | ||
|
||
assert result["properties"]["keyvaultName"] == kv_name | ||
assert result["properties"]["clientId"] == mi_client_id | ||
|
||
|
||
def _assert_role_assignments( | ||
initial_assignment_names: list, | ||
kv_id: str, | ||
mi_principal_id: str, | ||
expected_secretsync_roles: bool = False | ||
): | ||
tries = 0 | ||
while tries < ROLE_MAX_RETRIES: | ||
try: | ||
current_assignment_names = [ | ||
role["roleDefinitionName"] for role in run( | ||
f"az role assignment list --scope {kv_id} --assignee {mi_principal_id}" | ||
) | ||
] | ||
if expected_secretsync_roles: | ||
assert "Key Vault Secrets User" in current_assignment_names | ||
assert "Key Vault Reader" in current_assignment_names | ||
else: | ||
# role could have been applied before - so just make sure nothing new was applied | ||
difference_roles = set(current_assignment_names).difference(set(initial_assignment_names)) | ||
assert not difference_roles | ||
return | ||
except AssertionError as e: | ||
tries += 1 | ||
sleep(ROLE_RETRY_INTERVAL) | ||
if tries == ROLE_MAX_RETRIES: | ||
raise e | ||
|
||
|
||
def _get_role_list( | ||
kv_id: str, | ||
mi_client_id: str | ||
): | ||
tries = 0 | ||
while tries < ROLE_MAX_RETRIES: | ||
try: | ||
return run(f"az role assignment list --scope {kv_id} --assignee {mi_client_id}") | ||
except CLIInternalError: | ||
tries += 1 | ||
sleep(ROLE_RETRY_INTERVAL) | ||
|
||
raise AssertionError("Failed to create user assigned identity. Please retry with a given identity.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this variable passed into / set in a pipeline somewhere, or how is it configured?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no...
pipeline testing currently makes a new mi...
do I need to add in a pass through...
can I not...