|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from abc import ABC |
| 19 | +from typing import Dict |
| 20 | + |
| 21 | +from gravitino.api.credential.credential import Credential |
| 22 | +from gravitino.utils.precondition import Precondition |
| 23 | + |
| 24 | + |
| 25 | +class OSSTokenCredential(Credential, ABC): |
| 26 | + """Represents OSS token credential.""" |
| 27 | + |
| 28 | + OSS_TOKEN_CREDENTIAL_TYPE: str = "oss-token" |
| 29 | + _GRAVITINO_OSS_SESSION_ACCESS_KEY_ID: str = "oss-access-key-id" |
| 30 | + _GRAVITINO_OSS_SESSION_SECRET_ACCESS_KEY: str = "oss-secret-access-key" |
| 31 | + _GRAVITINO_OSS_TOKEN: str = "oss-security-token" |
| 32 | + |
| 33 | + def __init__(self, credential_info: Dict[str, str], expire_time_in_ms: int): |
| 34 | + self._access_key_id = credential_info[self._GRAVITINO_OSS_SESSION_ACCESS_KEY_ID] |
| 35 | + self._secret_access_key = credential_info[ |
| 36 | + self._GRAVITINO_OSS_SESSION_SECRET_ACCESS_KEY |
| 37 | + ] |
| 38 | + self._security_token = credential_info[self._GRAVITINO_OSS_TOKEN] |
| 39 | + self._expire_time_in_ms = expire_time_in_ms |
| 40 | + Precondition.check_string_not_empty( |
| 41 | + self._access_key_id, "The OSS access key ID should not be empty" |
| 42 | + ) |
| 43 | + Precondition.check_string_not_empty( |
| 44 | + self._secret_access_key, "The OSS secret access key should not be empty" |
| 45 | + ) |
| 46 | + Precondition.check_string_not_empty( |
| 47 | + self._security_token, "The OSS security token should not be empty" |
| 48 | + ) |
| 49 | + Precondition.check_argument( |
| 50 | + self._expire_time_in_ms > 0, |
| 51 | + "The expiration time of OSS token credential should be greater than 0", |
| 52 | + ) |
| 53 | + |
| 54 | + def credential_type(self) -> str: |
| 55 | + """The type of the credential. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + the type of the credential. |
| 59 | + """ |
| 60 | + return self.OSS_TOKEN_CREDENTIAL_TYPE |
| 61 | + |
| 62 | + def expire_time_in_ms(self) -> int: |
| 63 | + """Returns the expiration time of the credential in milliseconds since |
| 64 | + the epoch, 0 means it will never expire. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + The expiration time of the credential. |
| 68 | + """ |
| 69 | + return self._expire_time_in_ms |
| 70 | + |
| 71 | + def credential_info(self) -> Dict[str, str]: |
| 72 | + """The credential information. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + The credential information. |
| 76 | + """ |
| 77 | + return { |
| 78 | + self._GRAVITINO_OSS_TOKEN: self._security_token, |
| 79 | + self._GRAVITINO_OSS_SESSION_ACCESS_KEY_ID: self._access_key_id, |
| 80 | + self._GRAVITINO_OSS_SESSION_SECRET_ACCESS_KEY: self._secret_access_key, |
| 81 | + } |
| 82 | + |
| 83 | + def access_key_id(self) -> str: |
| 84 | + """The OSS access key id. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + The OSS access key id. |
| 88 | + """ |
| 89 | + return self._access_key_id |
| 90 | + |
| 91 | + def secret_access_key(self) -> str: |
| 92 | + """The OSS secret access key. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + The OSS secret access key. |
| 96 | + """ |
| 97 | + return self._secret_access_key |
| 98 | + |
| 99 | + def security_token(self) -> str: |
| 100 | + """The OSS security token. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + The OSS security token. |
| 104 | + """ |
| 105 | + return self._security_token |
0 commit comments