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

{Role} ad sp create-for-rbac: Retry Service Principal creation for "must in the local tenant" error #17412

Merged
merged 3 commits into from
Apr 8, 2021
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
8 changes: 6 additions & 2 deletions src/azure-cli/azure/cli/command_modules/role/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,10 +1457,14 @@ def create_service_principal_for_rbac(
aad_sp = _create_service_principal(cmd.cli_ctx, app_id, resolve_app=False)
break
except Exception as ex: # pylint: disable=broad-except
err_msg = str(ex)
if retry_time < _RETRY_TIMES and (
' does not reference ' in str(ex) or ' does not exist ' in str(ex)):
' does not reference ' in err_msg or
' does not exist ' in err_msg or
'service principal being created must in the local tenant' in err_msg):
logger.warning("Creating service principal failed with error '%s'. Retrying: %s/%s",
err_msg, retry_time + 1, _RETRY_TIMES)
time.sleep(5)
logger.warning('Retrying service principal creation: %s/%s', retry_time + 1, _RETRY_TIMES)
else:
logger.warning(
"Creating service principal failed for appid '%s'. Trace followed:\n%s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,25 @@ def test_create_for_rbac_idempotent(self):
finally:
self.cmd('ad sp delete --id {name}')

@mock.patch('azure.cli.command_modules.role.custom._auth_client_factory')
@mock.patch('azure.cli.command_modules.role.custom._graph_client_factory')
@mock.patch('azure.cli.command_modules.role.custom._create_service_principal')
@mock.patch('azure.cli.command_modules.role.custom.create_application')
def test_create_for_rbac_retry(self, create_application_mock, create_service_principal_mock,
graph_client_factory_mock, auth_client_factory_mock):
graph_client_factory_mock.return_value.config.tenant_id = '00000001-0000-0000-0000-000000000000'
create_application_mock.return_value.app_id = '00000000-0000-0000-0000-000000000000'
create_service_principal_mock.side_effect = [
# Mock replication exceptions
Exception("The appId '00000000-0000-0000-0000-000000000000' of the service principal "
"does not reference a valid application object."),
Exception("When using this permission, the backing application of the service principal being "
"created must in the local tenant"),
# Success
mock.MagicMock()
]
self.cmd('ad sp create-for-rbac --skip-assignment')


class GraphGroupScenarioTest(ScenarioTest):

Expand Down