Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a2f6b6c
Initial updates to use track 2 Hub GA SDK
c-ryan-k Mar 3, 2021
45cadfb
WIP initial user-assigned identity functionality
c-ryan-k Mar 16, 2021
2acf8e6
Added routing endpoint identity and added some preliminary test code
c-ryan-k Mar 17, 2021
b613397
Updated identity code with enums and minor logic updates
c-ryan-k Mar 18, 2021
b3208a4
More identity updates, role and scope assignment
c-ryan-k Mar 18, 2021
ee48812
Minor fixes to identity functions and return values
c-ryan-k Mar 19, 2021
ee807a0
Linting fixes
c-ryan-k Mar 19, 2021
ea84c66
WIP testing updates
c-ryan-k Mar 19, 2021
44117af
Merge branch 'azure-dev' into hub_track2
c-ryan-k Mar 26, 2021
fa2dbb8
Help/Param updates and minor tweaks/fixes
c-ryan-k Mar 30, 2021
691950d
Test updates, help updates, polling updates
c-ryan-k Mar 30, 2021
8fc4f2e
Test recording updates
c-ryan-k Mar 31, 2021
1282c05
test updates
c-ryan-k Apr 6, 2021
0a76443
minor fix in consumer_group_create, test updates
c-ryan-k Apr 6, 2021
5060def
Initial updates to use track 2 Hub GA SDK
c-ryan-k Mar 3, 2021
14fa27d
WIP initial user-assigned identity functionality
c-ryan-k Mar 16, 2021
bc5e641
Added routing endpoint identity and added some preliminary test code
c-ryan-k Mar 17, 2021
8779d9a
Updated identity code with enums and minor logic updates
c-ryan-k Mar 18, 2021
889f42c
More identity updates, role and scope assignment
c-ryan-k Mar 18, 2021
90342f9
Minor fixes to identity functions and return values
c-ryan-k Mar 19, 2021
9b25700
Linting fixes
c-ryan-k Mar 19, 2021
850ab5e
WIP testing updates
c-ryan-k Mar 19, 2021
a65084e
Help/Param updates and minor tweaks/fixes
c-ryan-k Mar 30, 2021
c92c45f
Test updates, help updates, polling updates
c-ryan-k Mar 30, 2021
5f5e1e2
Test recording updates
c-ryan-k Mar 31, 2021
df59fbf
test updates
c-ryan-k Apr 6, 2021
e6308da
minor fix in consumer_group_create, test updates
c-ryan-k Apr 6, 2021
47e700a
Merge branch 'hub_track2' of https://github.com/c-ryan-k/azure-cli in…
c-ryan-k Apr 12, 2021
0214f61
Test updates and new recordings
c-ryan-k Apr 12, 2021
3ea6fc4
Merge branch 'azure-dev' into hub_track2
c-ryan-k Apr 20, 2021
a29fe37
Fix for ARM issue - user identity object must be empty upon removal o…
c-ryan-k Apr 27, 2021
1a0ceeb
Updates to use stable multiapi SDK (2021-03-03) with backfill for dev…
c-ryan-k Apr 28, 2021
d1b5f0d
Merge branch 'dev' into hub_track2
c-ryan-k Apr 28, 2021
ef103b3
Help/Param string updates
c-ryan-k Apr 28, 2021
fb759e6
Version fix for new SDK (2021-03-31)
c-ryan-k Apr 29, 2021
bdc3f5a
Parameter updates
c-ryan-k May 11, 2021
a599f8b
Test and recording updates
c-ryan-k May 11, 2021
cd59e5a
Certificate create/update fixes and test updates
c-ryan-k May 11, 2021
4e8d6a3
Add DeviceConnectionStateEvents as a routing source type
c-ryan-k May 13, 2021
0ab2ae5
RoutingSource test updates
c-ryan-k May 14, 2021
20992a8
SDK version update to 2.0.0
c-ryan-k May 14, 2021
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
Prev Previous commit
Next Next commit
Updated identity code with enums and minor logic updates
  • Loading branch information
c-ryan-k committed Mar 18, 2021
commit b6133978bd9e1fd373f9d91ef3cca08e6e4e9087
1 change: 1 addition & 0 deletions src/azure-cli/azure/cli/command_modules/iot/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

certificate_help = """For a detailed explanation of CA certificates in Azure IoT Hub,
see https://docs.microsoft.com/azure/iot-hub/iot-hub-x509ca-overview """
SYSTEM_IDENTITY = '[system]'
45 changes: 28 additions & 17 deletions src/azure-cli/azure/cli/command_modules/iot/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from azure.mgmt.iothub.models import (IotHubSku,
AccessRights,
ArmIdentity,
CertificateBodyDescription,
CertificateVerificationDescription,
CloudToDeviceProperties,
Expand Down Expand Up @@ -49,7 +50,8 @@
from azure.mgmt.iotcentral.models import (AppSkuInfo,
App)

from azure.cli.command_modules.iot.shared import EndpointType, EncodingFormat, RenewKeyType, AuthenticationType
from azure.cli.command_modules.iot.shared import EndpointType, EncodingFormat, RenewKeyType, AuthenticationType, IdentityType
from azure.cli.command_modules.iot._constants import SYSTEM_IDENTITY
from ._client_factory import resource_service_factory
from ._utils import open_certificate, generate_key

Expand Down Expand Up @@ -458,14 +460,15 @@ def iot_hub_create(cmd, client, hub_name, resource_group_name, location=None,
properties=properties,
tags=tags)
if identities:
user_identities = [identity for identity in identities if identity != '[system]']
hub_description.identity = ArmIdentity()
user_identities = [identity for identity in identities if identity != SYSTEM_IDENTITY]
for identity in user_identities:
hub_description.identity.user_assigned_identities[identity] = {}

if '[system]' in identities:
hub_description.identity.type = "SystemAssigned, UserAssigned" if hub_description.identity.user_assigned_identities else "SystemAssigned"
if SYSTEM_IDENTITY in identities:
hub_description.identity.type = IdentityType.SystemAssignedUserAssigned if hub_description.identity.user_assigned_identities else IdentityType.SystemAssigned
else:
hub_description.identity.type = "UserAssigned"
hub_description.identity.type = IdentityType.UserAssigned

return client.iot_hub_resource.begin_create_or_update(resource_group_name, hub_name, hub_description)

Expand Down Expand Up @@ -636,16 +639,20 @@ def iot_hub_identity_assign(cmd, client, hub_name, identities, role=None, scopes
hub = iot_hub_get(cmd, client, hub_name, resource_group_name)

# if assigning a [system] identity, use role and scopes to update it after
user_identities = [identity for identity in identities if identity != '[system]']
user_identities = [identity for identity in identities if identity != SYSTEM_IDENTITY]
for identity in user_identities:
hub.identity.user_assigned_identities[identity] = {}

if '[system]' in identities or 'SystemAssigned' in hub.identity.type:
hub.identity.type = "SystemAssigned, UserAssigned" if hub.identity.user_assigned_identities else "SystemAssigned"
if SYSTEM_IDENTITY in identities or hub.identity.type in [IdentityType.SystemAssignedUserAssigned, IdentityType.SystemAssigned]:
hub.identity.type = IdentityType.SystemAssignedUserAssigned if hub.identity.user_assigned_identities else IdentityType.SystemAssigned
else:
hub.identity.type = "UserAssigned" if hub.identity.user_assigned_identities else "None"
hub.identity.type = IdentityType.UserAssigned if hub.identity.user_assigned_identities else IdentityType.NoIdentity

if '[system]' in identities:
# user_assigned_identities must be 'None', not '{}' for SystemAssigned only
if hub.identity.type == IdentityType.SystemAssigned:
hub.identity.user_assigned_identities = None

if SYSTEM_IDENTITY in identities:
if role and scopes:
# update hub
hub = client.iot_hub_resource.begin_create_or_update(resource_group_name, hub_name, hub, {'IF-MATCH': hub.etag})
Expand All @@ -670,13 +677,13 @@ def iot_hub_identity_remove(cmd, client, hub_name, identities, resource_group_na
hub_identity = hub.identity

# if identity is '[system]', turn off system managed identity
if '[system]' in identities:
if 'SystemAssigned' not in hub_identity.type:
if SYSTEM_IDENTITY in identities:
if hub_identity.type not in [IdentityType.SystemAssigned, IdentityType.SystemAssignedUserAssigned]:
raise CLIError('Hub {} is not currently using a System-assigned Identity'.format(hub_name))
hub_identity.type = "UserAssigned" if 'UserAssigned' in hub.identity.type else "None"
hub_identity.type = IdentityType.UserAssigned if hub.identity.type in [IdentityType.UserAssigned, IdentityType.SystemAssignedUserAssigned] else IdentityType.NoIdentity

# separate user identities from system identity
user_identities = [identity for identity in identities if identity != '[system]']
user_identities = [identity for identity in identities if identity != SYSTEM_IDENTITY]

# loop through user_identities to remove
for identity in user_identities:
Expand All @@ -685,10 +692,14 @@ def iot_hub_identity_remove(cmd, client, hub_name, identities, resource_group_na
del hub_identity.user_assigned_identities[identity]

# assign identity type correctly
if 'SystemAssigned' in hub_identity.type:
hub_identity.type = 'SystemAssigned, UserAssigned' if hub_identity.user_assigned_identities else 'SystemAssigned'
if hub_identity.type in [IdentityType.SystemAssigned, IdentityType.SystemAssignedUserAssigned]:
hub_identity.type = IdentityType.SystemAssignedUserAssigned if hub_identity.user_assigned_identities else IdentityType.SystemAssigned
else:
hub_identity.type = 'UserAssigned' if hub_identity.user_assigned_identities else 'None'
hub_identity.type = IdentityType.UserAssigned if hub_identity.user_assigned_identities else IdentityType.NoIdentity

# user_assigned_identities must be 'None', not '{}' for SystemAssigned only
if hub_identity.type == IdentityType.SystemAssigned:
hub_identity.user_assigned_identities = None

hub.identity = hub_identity
return client.iot_hub_resource.begin_create_or_update(resource_group_name, hub_name, hub, {'IF-MATCH': hub.etag})
Expand Down
11 changes: 11 additions & 0 deletions src/azure-cli/azure/cli/command_modules/iot/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ class AuthenticationType(Enum):
"""
KeyBased = 'keybased'
IdentityBased = 'identitybased'


# pylint: disable=too-few-public-methods
class IdentityType(Enum):
"""
Type of managed identity for the IoT Hub.
"""
SystemAssigned = 'SystemAssigned'
SystemAssignedUserAssigned = 'SystemAssigned,UserAssigned'
UserAssigned = 'UserAssigned'
NoIdentity = 'None'