Skip to content

Commit c98c151

Browse files
authored
[#5623] feat(python): supports credential API in python client (#5777)
### What changes were proposed in this pull request? supports credential API in python client ```python catalog = gravitino_client.load_catalog(catalog_name) catalog.as_fileset_catalog().support_credentials().get_credentials() fileset = catalog.as_fileset_catalog().load_fileset( NameIdentifier.of("schema", "fileset") ) credentials = fileset.support_credentials().get_credentials() ``` ### Why are the changes needed? Fix: #5623 ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? 1. add UT 2. setup a Gravitino server which returns credential, test with python client
1 parent 26a8b37 commit c98c151

25 files changed

+1325
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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, abstractmethod
19+
from typing import Dict
20+
21+
22+
class Credential(ABC):
23+
"""Represents the credential in Gravitino."""
24+
25+
@abstractmethod
26+
def credential_type(self) -> str:
27+
"""The type of the credential.
28+
29+
Returns:
30+
the type of the credential.
31+
"""
32+
pass
33+
34+
@abstractmethod
35+
def expire_time_in_ms(self) -> int:
36+
"""Returns the expiration time of the credential in milliseconds since
37+
the epoch, 0 means it will never expire.
38+
39+
Returns:
40+
The expiration time of the credential.
41+
"""
42+
pass
43+
44+
@abstractmethod
45+
def credential_info(self) -> Dict[str, str]:
46+
"""The credential information.
47+
48+
Returns:
49+
The credential information.
50+
"""
51+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 GCSTokenCredential(Credential, ABC):
26+
"""Represents the GCS token credential."""
27+
28+
GCS_TOKEN_CREDENTIAL_TYPE: str = "gcs-token"
29+
_GCS_TOKEN_NAME: str = "token"
30+
31+
_expire_time_in_ms: int = 0
32+
33+
def __init__(self, credential_info: Dict[str, str], expire_time_in_ms: int):
34+
self._token = credential_info[self._GCS_TOKEN_NAME]
35+
self._expire_time_in_ms = expire_time_in_ms
36+
Precondition.check_string_not_empty(
37+
self._token, "GCS token should not be empty"
38+
)
39+
Precondition.check_argument(
40+
self._expire_time_in_ms > 0,
41+
"The expiration time of GCS token credential should be greater than 0",
42+
)
43+
44+
def credential_type(self) -> str:
45+
"""The type of the credential.
46+
47+
Returns:
48+
the type of the credential.
49+
"""
50+
return self.GCS_TOKEN_CREDENTIAL_TYPE
51+
52+
def expire_time_in_ms(self) -> int:
53+
"""Returns the expiration time of the credential in milliseconds since
54+
the epoch, 0 means it will never expire.
55+
56+
Returns:
57+
The expiration time of the credential.
58+
"""
59+
return self._expire_time_in_ms
60+
61+
def credential_info(self) -> Dict[str, str]:
62+
"""The credential information.
63+
64+
Returns:
65+
The credential information.
66+
"""
67+
return {self._GCS_TOKEN_NAME: self._token}
68+
69+
def token(self) -> str:
70+
"""The GCS token.
71+
72+
Returns:
73+
The GCS token.
74+
"""
75+
return self._token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 S3SecretKeyCredential(Credential, ABC):
26+
"""Represents S3 secret key credential."""
27+
28+
S3_SECRET_KEY_CREDENTIAL_TYPE: str = "s3-secret-key"
29+
_GRAVITINO_S3_STATIC_ACCESS_KEY_ID: str = "s3-access-key-id"
30+
_GRAVITINO_S3_STATIC_SECRET_ACCESS_KEY: str = "s3-secret-access-key"
31+
32+
def __init__(self, credential_info: Dict[str, str], expire_time: int):
33+
self._access_key_id = credential_info[self._GRAVITINO_S3_STATIC_ACCESS_KEY_ID]
34+
self._secret_access_key = credential_info[
35+
self._GRAVITINO_S3_STATIC_SECRET_ACCESS_KEY
36+
]
37+
Precondition.check_string_not_empty(
38+
self._access_key_id, "S3 access key id should not be empty"
39+
)
40+
Precondition.check_string_not_empty(
41+
self._secret_access_key, "S3 secret access key should not be empty"
42+
)
43+
Precondition.check_argument(
44+
expire_time == 0,
45+
"The expiration time of S3 secret key credential should be 0",
46+
)
47+
48+
def credential_type(self) -> str:
49+
"""Returns the expiration time of the credential in milliseconds since
50+
the epoch, 0 means it will never expire.
51+
52+
Returns:
53+
The expiration time of the credential.
54+
"""
55+
return self.S3_SECRET_KEY_CREDENTIAL_TYPE
56+
57+
def expire_time_in_ms(self) -> int:
58+
"""Returns the expiration time of the credential in milliseconds since
59+
the epoch, 0 means it will never expire.
60+
61+
Returns:
62+
The expiration time of the credential.
63+
"""
64+
return 0
65+
66+
def credential_info(self) -> Dict[str, str]:
67+
"""The credential information.
68+
69+
Returns:
70+
The credential information.
71+
"""
72+
return {
73+
self._GRAVITINO_S3_STATIC_SECRET_ACCESS_KEY: self._secret_access_key,
74+
self._GRAVITINO_S3_STATIC_ACCESS_KEY_ID: self._access_key_id,
75+
}
76+
77+
def access_key_id(self) -> str:
78+
"""The S3 access key id.
79+
80+
Returns:
81+
The S3 access key id.
82+
"""
83+
return self._access_key_id
84+
85+
def secret_access_key(self) -> str:
86+
"""The S3 secret access key.
87+
88+
Returns:
89+
The S3 secret access key.
90+
"""
91+
return self._secret_access_key

0 commit comments

Comments
 (0)