|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from typing import Any |
| 9 | + |
| 10 | + |
| 11 | +# pylint:disable=protected-access |
| 12 | + |
| 13 | + |
| 14 | +class KeyVaultPermission(object): |
| 15 | + """Role definition permissions. |
| 16 | +
|
| 17 | + :ivar list[str] actions: allowed actions |
| 18 | + :ivar list[str] not_actions: denied actions |
| 19 | + :ivar list[str] data_actions: allowed data actions |
| 20 | + :ivar list[str] not_data_actions: denied data actions |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, **kwargs): |
| 24 | + # type: (**Any) -> None |
| 25 | + self.actions = kwargs.get("actions") |
| 26 | + self.not_actions = kwargs.get("not_actions") |
| 27 | + self.data_actions = kwargs.get("data_actions") |
| 28 | + self.not_data_actions = kwargs.get("not_data_actions") |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def _from_generated(cls, permissions): |
| 32 | + return cls( |
| 33 | + actions=permissions.actions, |
| 34 | + not_actions=permissions.not_actions, |
| 35 | + data_actions=permissions.data_actions, |
| 36 | + not_data_actions=permissions.not_data_actions, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +class KeyVaultRoleAssignment(object): |
| 41 | + """Represents the assignment to a principal of a role over a scope""" |
| 42 | + |
| 43 | + def __init__(self, **kwargs): |
| 44 | + # type: (**Any) -> None |
| 45 | + self._assignment_id = kwargs.get("assignment_id") |
| 46 | + self._name = kwargs.get("name") |
| 47 | + self._properties = kwargs.get("properties") |
| 48 | + self._type = kwargs.get("assignment_type") |
| 49 | + |
| 50 | + def __repr__(self): |
| 51 | + # type: () -> str |
| 52 | + return "KeyVaultRoleAssignment<{}>".format(self._assignment_id) |
| 53 | + |
| 54 | + @property |
| 55 | + def assignment_id(self): |
| 56 | + # type: () -> str |
| 57 | + """unique identifier for this assignment""" |
| 58 | + return self._assignment_id |
| 59 | + |
| 60 | + @property |
| 61 | + def name(self): |
| 62 | + # type: () -> str |
| 63 | + """name of the assignment""" |
| 64 | + return self._name |
| 65 | + |
| 66 | + @property |
| 67 | + def principal_id(self): |
| 68 | + # type: () -> str |
| 69 | + """ID of the principal this assignment applies to. |
| 70 | +
|
| 71 | + This maps to the ID inside the Active Directory. It can point to a user, service principal, or security group. |
| 72 | + """ |
| 73 | + return self._properties.principal_id |
| 74 | + |
| 75 | + @property |
| 76 | + def role_definition_id(self): |
| 77 | + # type: () -> str |
| 78 | + """ID of the role's definition""" |
| 79 | + return self._properties.role_definition_id |
| 80 | + |
| 81 | + @property |
| 82 | + def scope(self): |
| 83 | + # type: () -> str |
| 84 | + """scope of the assignment""" |
| 85 | + return self._properties.scope |
| 86 | + |
| 87 | + @property |
| 88 | + def type(self): |
| 89 | + # type: () -> str |
| 90 | + """the type of this assignment""" |
| 91 | + return self._type |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def _from_generated(cls, role_assignment): |
| 95 | + return cls( |
| 96 | + assignment_id=role_assignment.id, |
| 97 | + name=role_assignment.name, |
| 98 | + assignment_type=role_assignment.type, |
| 99 | + properties=KeyVaultRoleAssignmentProperties._from_generated(role_assignment.properties), |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +class KeyVaultRoleAssignmentProperties(object): |
| 104 | + def __init__(self, **kwargs): |
| 105 | + # type: (**Any) -> None |
| 106 | + self.principal_id = kwargs.get("principal_id") |
| 107 | + self.role_definition_id = kwargs.get("role_definition_id") |
| 108 | + self.scope = kwargs.get("scope") |
| 109 | + |
| 110 | + def __repr__(self): |
| 111 | + # type: () -> str |
| 112 | + return "KeyVaultRoleAssignmentProperties(principal_id={}, role_definition_id={}, scope={})".format( |
| 113 | + self.principal_id, self.role_definition_id, self.scope |
| 114 | + )[:1024] |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def _from_generated(cls, role_assignment_properties): |
| 118 | + # the generated RoleAssignmentProperties and RoleAssignmentPropertiesWithScope |
| 119 | + # models differ only in that the latter has a "scope" attribute |
| 120 | + return cls( |
| 121 | + principal_id=role_assignment_properties.principal_id, |
| 122 | + role_definition_id=role_assignment_properties.role_definition_id, |
| 123 | + scope=getattr(role_assignment_properties, "scope", None), |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +class KeyVaultRoleDefinition(object): |
| 128 | + """Role definition. |
| 129 | +
|
| 130 | + :ivar str id: The role definition ID. |
| 131 | + :ivar str name: The role definition name. |
| 132 | + :ivar str type: The role definition type. |
| 133 | + :ivar str role_name: The role name. |
| 134 | + :ivar str description: The role definition description. |
| 135 | + :ivar str role_type: The role type. |
| 136 | + :ivar permissions: Role definition permissions. |
| 137 | + :vartype permissions: list[KeyVaultPermission] |
| 138 | + :ivar list[str] assignable_scopes: Role definition assignable scopes. |
| 139 | + """ |
| 140 | + |
| 141 | + def __init__(self, **kwargs): |
| 142 | + # type: (**Any) -> None |
| 143 | + self.id = kwargs.get("id") |
| 144 | + self.name = kwargs.get("name") |
| 145 | + self.role_name = kwargs.get("role_name") |
| 146 | + self.description = kwargs.get("description") |
| 147 | + self.role_type = kwargs.get("role_type") |
| 148 | + self.type = kwargs.get("type") |
| 149 | + self.permissions = kwargs.get("permissions") |
| 150 | + self.assignable_scopes = kwargs.get("assignable_scopes") |
| 151 | + |
| 152 | + def __repr__(self): |
| 153 | + # type: () -> str |
| 154 | + return "<KeyVaultRoleDefinition {}>".format(self.role_name)[:1024] |
| 155 | + |
| 156 | + @classmethod |
| 157 | + def _from_generated(cls, definition): |
| 158 | + return cls( |
| 159 | + assignable_scopes=definition.assignable_scopes, |
| 160 | + description=definition.description, |
| 161 | + id=definition.id, |
| 162 | + name=definition.name, |
| 163 | + permissions=[KeyVaultPermission._from_generated(p) for p in definition.permissions], |
| 164 | + role_name=definition.role_name, |
| 165 | + role_type=definition.role_type, |
| 166 | + type=definition.type, |
| 167 | + ) |
0 commit comments