Skip to content

Commit fc54d25

Browse files
committed
PullRequest: 57 feat: support obproxy
Merge branch sp_obproxy of git@code.alipay.com:oceanbase/OBShell-SDK-Python.git into master https://code.alipay.com/oceanbase/OBShell-SDK-Python/pull_requests/57?tab=diff Signed-off-by: 雪染 <xin.sunhx@oceanbase.com> * feat: support obproxy * fix: modify default rpc_listen_port of obproxy. * chore: add tenant_root_password(parameter) to set_tenant_variables(method) * refract of security * chore: delete parameter of delete_obproxy * chore: delete parameter of delete_obproxy_sync * chore: add comment for create_user * optz * chore: delete parameter of delete_obproxy_sync * chore: add is_obproxy_agent into info * add agent_passwords for agg_create_cluster * set task type for upload_obproxy_pkg * fix: modify the default value of exporter_port * code optz
1 parent 514589b commit fc54d25

5 files changed

Lines changed: 349 additions & 39 deletions

File tree

obshell/auth/password.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
class PasswordAuth(base.Auth):
3535
"""Password-based authentication method."""
3636

37-
def __init__(self, password: str = "", version=None, lifetime=60) -> None:
37+
def __init__(self, password: str = None, agent_password: str = None, version=None, lifetime=60) -> None:
3838
"""Initialize a new PasswordAuth instance.
3939
4040
Args:
@@ -49,14 +49,15 @@ def __init__(self, password: str = "", version=None, lifetime=60) -> None:
4949
5050
- "v1": supported by OBShell version 4.2.2.0.
5151
- "v2": supported by OBShell version 4.2.3.0 or later.
52-
lifetime (int, optional):
52+
lifetime (int, optional):
5353
lifetime of the authentication information in reqeust header.
5454
Defalut to 60 second.
5555
"""
5656
super().__init__(base.AuthType.PASSWORD,
5757
[base.AuthVersion.V1, base.AuthVersion.V2])
5858
self.password = password
5959
self.lifetime = lifetime
60+
self.agent_password = agent_password
6061
if version is not None:
6162
if version not in _AUTHS_VERSION:
6263
raise ValueError("Version not supported")
@@ -67,17 +68,20 @@ def auth(self, request) -> None:
6768
version = self.get_version()
6869
if version not in _AUTHS:
6970
raise base.AuthError(f"Unsupported auth version: {version}")
70-
self._method = _AUTHS[version](self.password, self.lifetime)
71+
self._method = _AUTHS[version](
72+
self.password, self.agent_password, self.lifetime)
7173
self._method.auth(request)
7274

7375

7476
class PasswordAuthMethod:
7577

76-
def __init__(self, password: str, lifetime: int) -> None:
78+
def __init__(self, password: str, agent_password: str, lifetime: int) -> None:
7779
self.password = password
80+
self.agent_password = agent_password
7881
self.pk = None
7982
self.lifetime = lifetime
8083
self.check_identity = False
84+
self.agent_password_is_set = False
8185

8286
def reset(self) -> None:
8387
self.pk = None
@@ -92,6 +96,10 @@ def _check(self, server: str):
9296
info = get_info(server)
9397
if info.identity == Agentidentity.SINGLE:
9498
self.password = ""
99+
if info.security:
100+
self.agent_password_is_set = True
101+
else:
102+
self.agent_password = None
95103
self.check_identity = True
96104

97105
def auth(self, req) -> None:
@@ -159,15 +167,23 @@ def auth(self, req: requests.Request) -> None:
159167
cipher.encrypt(pad(bytes(body), AES.block_size))
160168
).decode('utf8')
161169

170+
header = 'X-OCS-Header'
171+
if (self.agent_password is not None and self.agent_password_is_set) or req.task_type == "obproxy":
172+
password = self.agent_password
173+
header = 'X-OCS-Agent-Header'
174+
else:
175+
password = self.password
176+
162177
uri = urlparse(req.url).path if not urlparse(
163178
req.url).query else urlparse(req.url).path + "?" + urlparse(req.url).query
164179
headers = {
165-
'auth': self.password,
180+
'auth': "" if password is None else password,
166181
'ts': str(int(time.time()) + self.lifetime),
167182
'uri': uri,
168183
'keys': base64.b64encode(aes_key+aes_iv).decode('utf-8')
169184
}
170-
req.headers['X-OCS-Header'] = self.encrypt_header(headers)
185+
186+
req.headers[header] = self.encrypt_header(headers)
171187
return
172188

173189

obshell/info.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ def get_info(server: str) -> AgentInfo:
2525
url = f"http://{server}/api/v1/info"
2626
resp = requests.get(url, timeout=DEFAULT_TIMEOUT)
2727
if resp.status_code != 200:
28-
raise Exception(f"Failed to get version from {server}, "
28+
raise Exception(f"Failed to get info from {server}, "
2929
f"status code: {resp.status_code}")
3030
data = resp.json().get("data", {})
3131
if not data:
32-
raise Exception(f"Failed to get version from {server}, no data")
32+
raise Exception(f"Failed to get info from {server}, no data")
3333
identity = data.get("identity")
3434
version = data.get("version")
3535
supported_auth = data.get("supportedAuth", [])
36-
info = AgentInfo(identity, version, supported_auth)
36+
hold_obproxy = data.get("security", False)
37+
is_obproxy_agent = data.get("isObproxyAgent", False)
38+
info = AgentInfo(identity, version, supported_auth,
39+
hold_obproxy, is_obproxy_agent)
3740
return info
3841

3942

@@ -46,6 +49,6 @@ def get_public_key(server: str) -> str:
4649

4750
data = resp.json().get("data", {})
4851
if not data:
49-
raise Exception(f"Failed to get version from {server}, no data")
52+
raise Exception(f"Failed to get info from {server}, no data")
5053

5154
return data.get("public_key")

obshell/model/info.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ class AgentInfo:
3333
def __init__(self,
3434
identity: str,
3535
version: str,
36-
supported_auth: List[str]):
36+
supported_auth: List[str],
37+
security: bool = False,
38+
is_obproxy_agent: bool = False):
3739
self.identity = Agentidentity(identity)
3840
self.version = Version(version)
41+
self.security = security
42+
self.is_obproxy_agent = is_obproxy_agent
3943
self.supported_auth = [AuthVersion(version)
4044
for version in supported_auth]
4145

@@ -175,6 +179,8 @@ def __init__(self, data: dict):
175179
self.home_path = data.get("homePath", "")
176180
self.ip = data.get("ip", "")
177181
self.port = data.get("port", 0)
182+
self.is_obproxy_agent = data.get("isObproxyAgent", False)
183+
self.secrity = data.get("security", False)
178184

179185
@classmethod
180186
def from_dict(cls, data: dict):

obshell/request.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self, uri: str,
2525
data: dict = None,
2626
query_param: dict = None,
2727
headers: dict = None,
28+
task_type: str = "ob",
2829
timeout: int = 100000):
2930
if data is None:
3031
data = {}
@@ -43,6 +44,7 @@ def __init__(self, uri: str,
4344
self.original_data = data
4445
self.headers = headers
4546
self.timeout = timeout
47+
self.task_type = task_type
4648

4749
@property
4850
def url(self):

0 commit comments

Comments
 (0)