|
21 | 21 | logger = logging.getLogger(__name__) |
22 | 22 |
|
23 | 23 | class ManagedIdentity(UserDict): |
| 24 | + """Feed an instance of this class to :class:`msal.ManagedIdentityClient` |
| 25 | + to acquire token for the specified managed identity. |
| 26 | + """ |
24 | 27 | # The key names used in config dict |
25 | | - ID_TYPE = "ManagedIdentityIdType" |
| 28 | + ID_TYPE = "ManagedIdentityIdType" # Contains keyword ManagedIdentity so its json equivalent will be more readable |
26 | 29 | ID = "Id" |
27 | | - def __init__(self, identifier=None, id_type=None): |
28 | | - super(ManagedIdentity, self).__init__({ |
29 | | - self.ID_TYPE: id_type, |
30 | | - self.ID: identifier, |
31 | | - }) |
32 | | - |
33 | 30 |
|
34 | | -class UserAssignedManagedIdentity(ManagedIdentity): |
35 | | - """Feed an instance of this class to :class:`msal.ManagedIdentityClient` |
36 | | - to acquire token for user-assigned managed identity. |
37 | | - """ |
| 31 | + # Valid values for key ID_TYPE |
38 | 32 | CLIENT_ID = "ClientId" |
39 | 33 | RESOURCE_ID = "ResourceId" |
40 | 34 | OBJECT_ID = "ObjectId" |
| 35 | + SYSTEM_ASSIGNED = "SystemAssigned" |
| 36 | + |
41 | 37 | _types_mapping = { # Maps type name in configuration to type name on wire |
42 | 38 | CLIENT_ID: "client_id", |
43 | 39 | RESOURCE_ID: "mi_res_id", |
44 | 40 | OBJECT_ID: "object_id", |
45 | 41 | } |
46 | | - def __init__(self, identifier, id_type): |
47 | | - """Do not use this contructor. Use the following factory methods instead.""" |
48 | | - if id_type not in self._types_mapping: |
49 | | - raise ValueError("id_type only accepts one of: {}".format( |
50 | | - list(self._types_mapping))) |
51 | | - super(UserAssignedManagedIdentity, self).__init__( |
52 | | - identifier=identifier, |
53 | | - id_type=id_type, |
54 | | - ) |
55 | 42 |
|
56 | 43 | @classmethod |
57 | | - def from_client_id(cls, identifier): |
58 | | - """Construct a UserAssignedManagedIdentity instance from a client id. |
| 44 | + def system_assigned(cls): |
| 45 | + """Construct a system-assigned managed identity. |
59 | 46 |
|
60 | | - The outcome will be equivalent to:: |
| 47 | + The outcome is equivalent to:: |
| 48 | +
|
| 49 | + {"ManagedIdentityIdType": "SystemAssigned", "Id": None} |
| 50 | + """ |
| 51 | + return ManagedIdentity(id_type=cls.SYSTEM_ASSIGNED) |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def is_system_assigned(cls, unknown): |
| 55 | + return isinstance(unknown, dict) and unknown.get(cls.ID_TYPE) == cls.SYSTEM_ASSIGNED |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def user_assigned_client_id(cls, identifier): |
| 59 | + """Construct a ``ManagedIdentity`` instance from a user-assigned client id. |
| 60 | +
|
| 61 | + The outcome is equivalent to:: |
61 | 62 |
|
62 | 63 | {"ManagedIdentityIdType": "ClientId", "Id": "foo"} |
63 | 64 | """ |
64 | | - return UserAssignedManagedIdentity(identifier, cls.CLIENT_ID) |
| 65 | + return ManagedIdentity(identifier=identifier, id_type=cls.CLIENT_ID) |
65 | 66 |
|
66 | 67 | @classmethod |
67 | | - def from_resource_id(cls, identifier): |
68 | | - """Construct a UserAssignedManagedIdentity instance from a resource id. |
| 68 | + def user_assigned_resource_id(cls, identifier): |
| 69 | + """Construct a ``ManagedIdentity`` instance from a user-assigned resource id. |
69 | 70 |
|
70 | | - The outcome will be equivalent to:: |
| 71 | + The outcome is equivalent to:: |
71 | 72 |
|
72 | 73 | {"ManagedIdentityIdType": "ResourceId", "Id": "foo"} |
73 | 74 | """ |
74 | | - return UserAssignedManagedIdentity(identifier, cls.RESOURCE_ID) |
| 75 | + return ManagedIdentity(identifier=identifier, id_type=cls.RESOURCE_ID) |
75 | 76 |
|
76 | 77 | @classmethod |
77 | | - def from_object_id(cls, identifier): |
78 | | - """Construct a UserAssignedManagedIdentity instance from an object id. |
| 78 | + def user_assigned_object_id(cls, identifier): |
| 79 | + """Construct a ManagedIdentity instance from a user-assigned object id. |
79 | 80 |
|
80 | 81 | The outcome will be equivalent to:: |
81 | 82 |
|
82 | 83 | {"ManagedIdentityIdType": "ObjectId", "Id": "foo"} |
83 | 84 | """ |
84 | | - return UserAssignedManagedIdentity(identifier, cls.OBJECT_ID) |
85 | | - |
86 | | - |
87 | | -class SystemAssignedManagedIdentity(ManagedIdentity): |
88 | | - """Feed an instance of this class to :class:`msal.ManagedIdentityClient` |
89 | | - to acquire token for system-assigned managed identity. |
| 85 | + return ManagedIdentity(identifier=identifier, id_type=cls.OBJECT_ID) |
90 | 86 |
|
91 | | - By design, an instance of this class is equivalent to:: |
92 | | -
|
93 | | - {"ManagedIdentityIdType": "SystemAssignedManagedIdentity", "Id": None} |
94 | | - """ |
95 | | - def __init__(self): |
96 | | - super(SystemAssignedManagedIdentity, self).__init__( |
97 | | - id_type="SystemAssignedManagedIdentity", # As of this writing, |
98 | | - # It can be any value other than |
99 | | - # UserAssignedManagedIdentity._types_mapping's key names |
100 | | - ) |
| 87 | + def __init__(self, identifier=None, id_type=None): |
| 88 | + # Undocumented. Use other class methods instead. |
| 89 | + super(ManagedIdentity, self).__init__({ |
| 90 | + self.ID_TYPE: id_type, |
| 91 | + self.ID: identifier, |
| 92 | + }) |
101 | 93 |
|
102 | 94 |
|
103 | 95 | def _scope_to_resource(scope): # This is an experimental reasonable-effort approach |
@@ -136,7 +128,7 @@ def _obtain_token(http_client, managed_identity, resource): |
136 | 128 |
|
137 | 129 |
|
138 | 130 | def _adjust_param(params, managed_identity): |
139 | | - id_name = UserAssignedManagedIdentity._types_mapping.get( |
| 131 | + id_name = ManagedIdentity._types_mapping.get( |
140 | 132 | managed_identity.get(ManagedIdentity.ID_TYPE)) |
141 | 133 | if id_name: |
142 | 134 | params[id_name] = managed_identity[ManagedIdentity.ID] |
|
0 commit comments