|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.cloud.exceptions import NotFound |
| 16 | +from google.cloud._helpers import _rfc3339_to_datetime |
| 17 | + |
| 18 | + |
| 19 | +class HMACKeyMetadata(object): |
| 20 | + """Metadata about an HMAC service account key withn Cloud Storage. |
| 21 | +
|
| 22 | + :type client: :class:`~google.cloud.stoage.client.Client` |
| 23 | + :param client: client associated with the key metadata. |
| 24 | +
|
| 25 | + :type access_id: str |
| 26 | + :param access_id: (Optional) unique ID of an existing key. |
| 27 | +
|
| 28 | + :type project_id: str |
| 29 | + :param project_id: (Optional) project ID of an existing key. |
| 30 | + Defaults to client's project. |
| 31 | + """ |
| 32 | + |
| 33 | + ACTIVE_STATE = "ACTIVE" |
| 34 | + """Key is active, and may be used to sign requests.""" |
| 35 | + INACTIVE_STATE = "INACTIVE" |
| 36 | + """Key is inactive, and may not be used to sign requests. |
| 37 | +
|
| 38 | + It can be re-activated via :meth:`update`. |
| 39 | + """ |
| 40 | + DELETED_STATE = "DELETED" |
| 41 | + """Key is deleted. It cannot be re-activated.""" |
| 42 | + |
| 43 | + _SETTABLE_STATES = (ACTIVE_STATE, INACTIVE_STATE) |
| 44 | + |
| 45 | + def __init__(self, client, access_id=None, project_id=None): |
| 46 | + self._client = client |
| 47 | + self._properties = {} |
| 48 | + |
| 49 | + if access_id is not None: |
| 50 | + self._properties["accessId"] = access_id |
| 51 | + |
| 52 | + if project_id is not None: |
| 53 | + self._properties["projectId"] = project_id |
| 54 | + |
| 55 | + def __eq__(self, other): |
| 56 | + if not isinstance(other, self.__class__): |
| 57 | + return NotImplemented |
| 58 | + |
| 59 | + return self._client == other._client and self.access_id == other.access_id |
| 60 | + |
| 61 | + def __hash__(self): |
| 62 | + return hash(self._client) + hash(self.access_id) |
| 63 | + |
| 64 | + @property |
| 65 | + def access_id(self): |
| 66 | + """ID of the key. |
| 67 | +
|
| 68 | + :rtype: str or None |
| 69 | + :returns: unique identifier of the key within a project. |
| 70 | + """ |
| 71 | + return self._properties.get("accessId") |
| 72 | + |
| 73 | + @property |
| 74 | + def etag(self): |
| 75 | + """ETag identifying the version of the key metadata. |
| 76 | +
|
| 77 | + :rtype: str or None |
| 78 | + :returns: ETag for the version of the key's metadata. |
| 79 | + """ |
| 80 | + return self._properties.get("etag") |
| 81 | + |
| 82 | + @property |
| 83 | + def project(self): |
| 84 | + """Project ID associated with the key. |
| 85 | +
|
| 86 | + :rtype: str or None |
| 87 | + :returns: project identfier for the key. |
| 88 | + """ |
| 89 | + return self._properties.get("projectId") |
| 90 | + |
| 91 | + @property |
| 92 | + def service_account_email(self): |
| 93 | + """Service account e-mail address associated with the key. |
| 94 | +
|
| 95 | + :rtype: str or None |
| 96 | + :returns: e-mail address for the service account which created the key. |
| 97 | + """ |
| 98 | + return self._properties.get("serviceAccountEmail") |
| 99 | + |
| 100 | + @property |
| 101 | + def state(self): |
| 102 | + """Get / set key's state. |
| 103 | +
|
| 104 | + One of: |
| 105 | + - ``ACTIVE`` |
| 106 | + - ``INACTIVE`` |
| 107 | + - ``DELETED`` |
| 108 | +
|
| 109 | + :rtype: str or None |
| 110 | + :returns: key's current state. |
| 111 | + """ |
| 112 | + return self._properties.get("state") |
| 113 | + |
| 114 | + @state.setter |
| 115 | + def state(self, value): |
| 116 | + if value not in self._SETTABLE_STATES: |
| 117 | + raise ValueError( |
| 118 | + "State may only be set to one of: {}".format( |
| 119 | + ", ".join(self._SETTABLE_STATES) |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + self._properties["state"] = value |
| 124 | + |
| 125 | + @property |
| 126 | + def time_created(self): |
| 127 | + """Retrieve the timestamp at which the HMAC key was created. |
| 128 | +
|
| 129 | + :rtype: :class:`datetime.datetime` or ``NoneType`` |
| 130 | + :returns: Datetime object parsed from RFC3339 valid timestamp, or |
| 131 | + ``None`` if the bucket's resource has not been loaded |
| 132 | + from the server. |
| 133 | + """ |
| 134 | + value = self._properties.get("timeCreated") |
| 135 | + if value is not None: |
| 136 | + return _rfc3339_to_datetime(value) |
| 137 | + |
| 138 | + @property |
| 139 | + def updated(self): |
| 140 | + """Retrieve the timestamp at which the HMAC key was created. |
| 141 | +
|
| 142 | + :rtype: :class:`datetime.datetime` or ``NoneType`` |
| 143 | + :returns: Datetime object parsed from RFC3339 valid timestamp, or |
| 144 | + ``None`` if the bucket's resource has not been loaded |
| 145 | + from the server. |
| 146 | + """ |
| 147 | + value = self._properties.get("updated") |
| 148 | + if value is not None: |
| 149 | + return _rfc3339_to_datetime(value) |
| 150 | + |
| 151 | + @property |
| 152 | + def path(self): |
| 153 | + """Resource path for the metadata's key.""" |
| 154 | + |
| 155 | + if self.access_id is None: |
| 156 | + raise ValueError("No 'access_id' set.") |
| 157 | + |
| 158 | + project = self.project |
| 159 | + if project is None: |
| 160 | + project = self._client.project |
| 161 | + |
| 162 | + return "/projects/{}/hmacKeys/{}".format(project, self.access_id) |
| 163 | + |
| 164 | + def exists(self): |
| 165 | + """Determine whether or not the key for this metadata exists. |
| 166 | +
|
| 167 | + :rtype: bool |
| 168 | + :returns: True if the key exists in Cloud Storage. |
| 169 | + """ |
| 170 | + try: |
| 171 | + self._client._connection.api_request(method="GET", path=self.path) |
| 172 | + except NotFound: |
| 173 | + return False |
| 174 | + else: |
| 175 | + return True |
| 176 | + |
| 177 | + def reload(self): |
| 178 | + """Reload properties from Cloud Storage. |
| 179 | +
|
| 180 | + :raises :class:`~google.api_core.exceptions.NotFound`: |
| 181 | + if the key does not exist on the back-end. |
| 182 | + """ |
| 183 | + self._properties = self._client._connection.api_request( |
| 184 | + method="GET", path=self.path |
| 185 | + ) |
| 186 | + |
| 187 | + def update(self): |
| 188 | + """Save writable properties to Cloud Storage. |
| 189 | +
|
| 190 | + :raises :class:`~google.api_core.exceptions.NotFound`: |
| 191 | + if the key does not exist on the back-end. |
| 192 | + """ |
| 193 | + payload = {"state": self.state} |
| 194 | + self._properties = self._client._connection.api_request( |
| 195 | + method="PUT", path=self.path, data=payload |
| 196 | + ) |
| 197 | + |
| 198 | + def delete(self): |
| 199 | + """Delete the key from Cloud Storage. |
| 200 | +
|
| 201 | + :raises :class:`~google.api_core.exceptions.NotFound`: |
| 202 | + if the key does not exist on the back-end. |
| 203 | + """ |
| 204 | + if self.state != self.INACTIVE_STATE: |
| 205 | + raise ValueError("Cannot delete key if not in 'INACTIVE' state.") |
| 206 | + |
| 207 | + self._client._connection.api_request(method="DELETE", path=self.path) |
0 commit comments