Skip to content

Commit c2674c1

Browse files
committed
sp-cert2
1 parent db505d1 commit c2674c1

File tree

5 files changed

+28
-40
lines changed

5 files changed

+28
-40
lines changed

src/azure-cli-core/azure/cli/core/auth/identity.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
"Select the account you want to log in with. "
3838
"For more information on login with Azure CLI, see https://go.microsoft.com/fwlink/?linkid=2271136")
3939

40-
PASSWORD_CERTIFICATE_WARNING = (
41-
"Passing the service principal certificate with `--password` is deprecated and will be removed "
42-
"by version 2.74. Please use `--certificate` instead.")
43-
4440
logger = get_logger(__name__)
4541

4642

@@ -196,7 +192,7 @@ def login_with_service_principal(self, client_id, credential, scopes):
196192
"""
197193
sp_auth = ServicePrincipalAuth.build_from_credential(self.tenant_id, client_id, credential)
198194
client_credential = sp_auth.get_msal_client_credential()
199-
cca = ConfidentialClientApplication(client_id, client_credential, **self._msal_app_kwargs)
195+
cca = ConfidentialClientApplication(client_id, client_credential=client_credential, **self._msal_app_kwargs)
200196
result = cca.acquire_token_for_client(scopes)
201197
check_result(result)
202198

@@ -307,7 +303,7 @@ def build_from_credential(cls, tenant_id, client_id, credential):
307303
return ServicePrincipalAuth(entry)
308304

309305
@classmethod
310-
def build_credential(cls, secret_or_certificate=None,
306+
def build_credential(cls, client_secret=None,
311307
certificate=None, use_cert_sn_issuer=None,
312308
client_assertion=None):
313309
"""Build credential from user input. The credential looks like below, but only one key can exist.
@@ -318,20 +314,12 @@ def build_credential(cls, secret_or_certificate=None,
318314
}
319315
"""
320316
entry = {}
321-
if certificate:
317+
if client_secret:
318+
entry[_CLIENT_SECRET] = client_secret
319+
elif certificate:
322320
entry[_CERTIFICATE] = os.path.expanduser(certificate)
323321
if use_cert_sn_issuer:
324322
entry[_USE_CERT_SN_ISSUER] = use_cert_sn_issuer
325-
elif secret_or_certificate:
326-
# TODO: Make secret_or_certificate secret only
327-
user_expanded = os.path.expanduser(secret_or_certificate)
328-
if os.path.isfile(user_expanded):
329-
logger.warning(PASSWORD_CERTIFICATE_WARNING)
330-
entry[_CERTIFICATE] = user_expanded
331-
if use_cert_sn_issuer:
332-
entry[_USE_CERT_SN_ISSUER] = use_cert_sn_issuer
333-
else:
334-
entry[_CLIENT_SECRET] = secret_or_certificate
335323
elif client_assertion:
336324
entry[_CLIENT_ASSERTION] = client_assertion
337325
return entry

src/azure-cli-core/azure/cli/core/auth/tests/test_identity.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,10 @@ def test_service_principal_auth_client_assertion(self):
264264
assert client_credential == {'client_assertion': 'test_jwt'}
265265

266266
def test_build_credential(self):
267-
# secret
268-
cred = ServicePrincipalAuth.build_credential(secret_or_certificate="test_secret")
267+
# client_secret
268+
cred = ServicePrincipalAuth.build_credential(client_secret="test_secret")
269269
assert cred == {"client_secret": "test_secret"}
270270

271-
# secret with '~', which is preserved as-is
272-
cred = ServicePrincipalAuth.build_credential(secret_or_certificate="~test_secret")
273-
assert cred == {"client_secret": "~test_secret"}
274-
275-
# certificate as password (deprecated)
276-
current_dir = os.path.dirname(os.path.realpath(__file__))
277-
test_cert_file = os.path.join(current_dir, 'sp_cert.pem')
278-
cred = ServicePrincipalAuth.build_credential(secret_or_certificate=test_cert_file)
279-
assert cred == {'certificate': test_cert_file}
280-
281271
# certificate
282272
current_dir = os.path.dirname(os.path.realpath(__file__))
283273
test_cert_file = os.path.join(current_dir, 'sp_cert.pem')
@@ -297,7 +287,7 @@ def test_build_credential(self):
297287
cred = ServicePrincipalAuth.build_credential(certificate=test_cert_file, use_cert_sn_issuer=True)
298288
assert cred == {'certificate': test_cert_file, 'use_cert_sn_issuer': True}
299289

300-
# client assertion
290+
# client_assertion
301291
cred = ServicePrincipalAuth.build_credential(client_assertion="test_jwt")
302292
assert cred == {"client_assertion": "test_jwt"}
303293

src/azure-cli-core/azure/cli/core/auth/util.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
AccessToken = namedtuple("AccessToken", ["token", "expires_on"])
1515

1616

17+
PASSWORD_CERTIFICATE_WARNING = (
18+
"The error may be caused by passing a service principal certificate with --password. "
19+
"Please note that --password no longer accepts a service principal certificate. "
20+
"To pass a service principal certificate, use --certificate instead.")
21+
22+
1723
def aad_error_handler(error, **kwargs):
1824
""" Handle the error from AAD server returned by ADAL or MSAL. """
1925

@@ -30,17 +36,21 @@ def aad_error_handler(error, **kwargs):
3036
"below, please mention the hostname '%s'", socket.gethostname())
3137

3238
error_description = error.get('error_description')
39+
error_codes = error.get('error_codes')
3340

3441
# Build recommendation message
35-
login_command = _generate_login_command(**kwargs)
36-
login_message = (
37-
# Cloud Shell uses IMDS-like interface for implicit login. If getting token/cert failed,
38-
# we let the user explicitly log in to AAD with MSAL.
39-
"Please explicitly log in with:\n{}" if error.get('error') == 'broker_error'
40-
else "Interactive authentication is needed. Please run:\n{}").format(login_command)
42+
if error_codes and 7000215 in error_codes:
43+
recommendation = PASSWORD_CERTIFICATE_WARNING
44+
else:
45+
login_command = _generate_login_command(**kwargs)
46+
recommendation = (
47+
# Cloud Shell uses IMDS-like interface for implicit login. If getting token/cert failed,
48+
# we let the user explicitly log in to AAD with MSAL.
49+
"Please explicitly log in with:\n{}" if error.get('error') == 'broker_error'
50+
else "Interactive authentication is needed. Please run:\n{}").format(login_command)
4151

4252
from azure.cli.core.azclierror import AuthenticationError
43-
raise AuthenticationError(error_description, msal_error=error, recommendation=login_message)
53+
raise AuthenticationError(error_description, msal_error=error, recommendation=recommendation)
4454

4555

4656
def _generate_login_command(scopes=None, claims=None):

src/azure-cli/azure/cli/command_modules/profile/_help.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
For more details, see https://go.microsoft.com/fwlink/?linkid=2276314
2222
2323
24-
[WARNING] Passing the service principal certificate with `--password` is deprecated and will be removed
25-
by version 2.74. Please use `--certificate` instead.
24+
[WARNING] `--password` no longer accepts a service principal certificate.
25+
Use `--certificate` to pass a service principal certificate.
2626
2727
2828
To log in with a service principal, specify --service-principal.

src/azure-cli/azure/cli/command_modules/profile/custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
159159
if service_principal:
160160
from azure.cli.core.auth.identity import ServicePrincipalAuth
161161
password = ServicePrincipalAuth.build_credential(
162-
secret_or_certificate=password,
162+
client_secret=password,
163163
certificate=certificate, use_cert_sn_issuer=use_cert_sn_issuer,
164164
client_assertion=client_assertion)
165165

0 commit comments

Comments
 (0)