Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/azure-cli/azure/cli/command_modules/role/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ def load_arguments(self, _):
with self.argument_context(item) as c:
# general credential arguments
c.argument('years', type=int, default=None, arg_group='Credential',
help='Number of years for which the credentials will be valid. Default: 1 year')
help='Number of years for which the credentials will be valid. Each year contains 365 days.'
'Leap years are not considered. Default: 1 year')
c.argument('append', action='store_true', arg_group='Credential',
help='Append the new credential instead of overwriting.')
c.argument('end_date', default=None, arg_group='Credential',
Expand Down
11 changes: 7 additions & 4 deletions src/azure-cli/azure/cli/command_modules/role/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import uuid

import dateutil.parser
from dateutil.relativedelta import relativedelta
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although dateutil.relativedelta.relativedelta also supports days, dateutil is third-party:

https://docs.python.org/3/library/datetime.html

Package dateutil
Third-party library with expanded time zone and parsing support.

Third-party library dependencies should be as less as possible.

from knack.log import get_logger
from knack.util import CLIError, todict
from msrestazure.azure_exceptions import CloudError
Expand Down Expand Up @@ -1174,7 +1173,7 @@ def create_service_principal_for_rbac(
existing_sps = list(graph_client.service_principal_list(filter=query_exp))

app_start_date = datetime.datetime.now(datetime.timezone.utc)
app_end_date = app_start_date + relativedelta(years=years or 1)
app_end_date = app_start_date + datetime.timedelta(days=_years_to_days(years))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use years or 1 as the fallback logic already happens at L1162:


use_cert = False
public_cert_string = None
Expand Down Expand Up @@ -1659,7 +1658,7 @@ def _build_key_credentials(key_value=None, key_type=None, key_usage=None,
start_date = dateutil.parser.parse(start_date)

if not end_date:
end_date = start_date + relativedelta(years=1) - relativedelta(hours=24)
end_date = start_date + datetime.timedelta(days=_years_to_days(1))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of - relativedelta(hours=24) is to address #13769 (comment)

elif isinstance(end_date, str):
end_date = dateutil.parser.parse(end_date)

Expand Down Expand Up @@ -1705,7 +1704,7 @@ def _reset_credential(cmd, graph_object, add_password_func, remove_password_func
raise CLIError('usage error: --years | --end-date')
if end_date is None:
years = years or 1
app_end_date = app_start_date + relativedelta(years=years)
app_end_date = app_start_date + datetime.timedelta(days=_years_to_days(years))
else:
app_end_date = dateutil.parser.parse(end_date)
if app_end_date.tzinfo is None:
Expand Down Expand Up @@ -2001,3 +2000,7 @@ def _get_member_groups(get_member_group_func, identifier, security_enabled_only)
"securityEnabledOnly": security_enabled_only
}
return get_member_group_func(identifier, body)


def _years_to_days(years):
return years * 365
Loading