From 4123f2446622240d94b9500764938d6a24eaa3d3 Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 4 Mar 2024 11:46:54 +0800 Subject: [PATCH 1/4] support auth for scan --- example/ScanVertexEdgeExample.py | 10 ++-- nebula3/sclient/GraphStorageClient.py | 15 ++++++ nebula3/storage/ttypes.py | 77 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/example/ScanVertexEdgeExample.py b/example/ScanVertexEdgeExample.py index 2366169c..0697458a 100644 --- a/example/ScanVertexEdgeExample.py +++ b/example/ScanVertexEdgeExample.py @@ -59,8 +59,9 @@ def scan_person_vertex(graph_storage_client): print("======== Scan vertexes in ScanSpace ======") while resp.has_next(): result = resp.next() - for vertex_data in result: - print(vertex_data) + if (result is not None): + for vertex_data in result: + print(vertex_data) def scan_person_edge(graph_storage_client): @@ -70,8 +71,9 @@ def scan_person_edge(graph_storage_client): print("======== Scan edges in ScanSpace ======") while resp.has_next(): result = resp.next() - for edge_data in result: - print(edge_data) + if(result is not None): + for edge_data in result: + print(edge_data) """ diff --git a/nebula3/sclient/GraphStorageClient.py b/nebula3/sclient/GraphStorageClient.py index dbf6b888..0db2fffa 100644 --- a/nebula3/sclient/GraphStorageClient.py +++ b/nebula3/sclient/GraphStorageClient.py @@ -34,6 +34,8 @@ class GraphStorageClient(object): DEFAULT_START_TIME = 0 DEFAULT_END_TIME = sys.maxsize DEFAULT_LIMIT = 1000 + user = "" + passwd = "" def __init__(self, meta_cache, storage_addrs=None, time_out=60000): self._meta_cache = meta_cache @@ -42,6 +44,13 @@ def __init__(self, meta_cache, storage_addrs=None, time_out=60000): self._connections = [] self._create_connection() + def set_userPasswd(self, user, passwd): + """set user and password for scan. only useful for enterprise + :return: + """ + self.user = user + self.passwd = passwd + def get_conns(self): """get all connections which connect to storaged, the ScanResult use it @@ -225,6 +234,9 @@ def _scan_vertex( req.filter = where req.only_latest_version = only_latest_version req.enable_read_from_follower = enable_read_from_follower + req.username = self.user.encode('utf-8') + req.password = self.passwd.encode('utf-8') + req.need_authenticate = True return ScanResult( self, req=req, @@ -368,6 +380,9 @@ def _scan_edge( req.filter = where req.only_latest_version = only_latest_version req.enable_read_from_follower = enable_read_from_follower + req.username = self.user.encode('utf-8') + req.password = self.passwd.encode('utf-8') + req.need_authenticate = True return ScanResult( self, req=req, diff --git a/nebula3/storage/ttypes.py b/nebula3/storage/ttypes.py index 331d3205..9dd5a5cb 100644 --- a/nebula3/storage/ttypes.py +++ b/nebula3/storage/ttypes.py @@ -5316,6 +5316,9 @@ class ScanVertexRequest: - only_latest_version - enable_read_from_follower - common + - username + - password + - need_authenticate """ thrift_spec = None @@ -5413,6 +5416,14 @@ def read(self, iprot): if ftype == TType.STRUCT: self.common = RequestCommon() self.common.read(iprot) + elif fid == 12: + if ftype == TType.STRING: + self.password = iprot.readString() + else: + iprot.skip(ftype) + elif fid == 13: + if ftype == TType.BOOL: + self.need_authenticate = iprot.readBool() else: iprot.skip(ftype) else: @@ -5475,6 +5486,18 @@ def write(self, oprot): oprot.writeFieldBegin('common', TType.STRUCT, 10) self.common.write(oprot) oprot.writeFieldEnd() + if self.username != None: + oprot.writeFieldBegin('username', TType.STRING, 11) + oprot.writeString(self.username) + oprot.writeFieldEnd() + if self.password != None: + oprot.writeFieldBegin('password', TType.STRING, 12) + oprot.writeString(self.password) + oprot.writeFieldEnd() + if self.need_authenticate != None: + oprot.writeFieldBegin('need_authenticate', TType.BOOL, 13) + oprot.writeBool(self.need_authenticate) + oprot.writeFieldEnd() oprot.writeFieldStop() oprot.writeStructEnd() @@ -5521,6 +5544,18 @@ def __repr__(self): value = pprint.pformat(self.common, indent=0) value = padding.join(value.splitlines(True)) L.append(' common=%s' % (value)) + if self.username is not None: + value = pprint.pformat(self.username, indent=0) + value = padding.join(value.splitlines(True)) + L.append(' username=%s' % (value)) + if self.password is not None: + value = pprint.pformat(self.password, indent=0) + value = padding.join(value.splitlines(True)) + L.append(' password=%s' % (value)) + if self.need_authenticate is not None: + value = pprint.pformat(self.need_authenticate, indent=0) + value = padding.join(value.splitlines(True)) + L.append(' need_authenticate=%s' % (value)) return "%s(%s)" % (self.__class__.__name__, "\n" + ",\n".join(L) if L else '') def __eq__(self, other): @@ -5548,6 +5583,9 @@ class ScanEdgeRequest: - only_latest_version - enable_read_from_follower - common + - username + - password + - need_authenticate """ thrift_spec = None @@ -5647,6 +5685,21 @@ def read(self, iprot): self.common.read(iprot) else: iprot.skip(ftype) + elif fid == 11: + if ftype == TType.STRING: + self.username = iprot.readString() + else: + iprot.skip(ftype) + elif fid == 12: + if ftype == TType.STRING: + self.password = iprot.readString() + else: + iprot.skip(ftype) + elif fid == 13: + if ftype == TType.BOOL: + self.need_authenticate = iprot.readBool() + else: + iprot.skip(ftype) else: iprot.skip(ftype) iprot.readFieldEnd() @@ -5707,6 +5760,18 @@ def write(self, oprot): oprot.writeFieldBegin('common', TType.STRUCT, 10) self.common.write(oprot) oprot.writeFieldEnd() + if self.username != None: + oprot.writeFieldBegin('username', TType.STRING, 11) + oprot.writeString(self.username) + oprot.writeFieldEnd() + if self.password != None: + oprot.writeFieldBegin('password', TType.STRING, 12) + oprot.writeString(self.password) + oprot.writeFieldEnd() + if self.need_authenticate != None: + oprot.writeFieldBegin('need_authenticate', TType.BOOL, 13) + oprot.writeBool(self.need_authenticate) + oprot.writeFieldEnd() oprot.writeFieldStop() oprot.writeStructEnd() @@ -5753,6 +5818,18 @@ def __repr__(self): value = pprint.pformat(self.common, indent=0) value = padding.join(value.splitlines(True)) L.append(' common=%s' % (value)) + if self.username is not None: + value = pprint.pformat(self.username, indent=0) + value = padding.join(value.splitlines(True)) + L.append(' username=%s' % (value)) + if self.password is not None: + value = pprint.pformat(self.password, indent=0) + value = padding.join(value.splitlines(True)) + L.append(' password=%s' % (value)) + if self.need_authenticate is not None: + value = pprint.pformat(self.need_authenticate, indent=0) + value = padding.join(value.splitlines(True)) + L.append(' need_authenticate=%s' % (value)) return "%s(%s)" % (self.__class__.__name__, "\n" + ",\n".join(L) if L else '') def __eq__(self, other): From 74c445b9879eafe223efd11d3717f742cc5d63b7 Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 4 Mar 2024 11:49:19 +0800 Subject: [PATCH 2/4] fmt --- example/ScanVertexEdgeExample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/ScanVertexEdgeExample.py b/example/ScanVertexEdgeExample.py index 0697458a..19194ae6 100644 --- a/example/ScanVertexEdgeExample.py +++ b/example/ScanVertexEdgeExample.py @@ -71,7 +71,7 @@ def scan_person_edge(graph_storage_client): print("======== Scan edges in ScanSpace ======") while resp.has_next(): result = resp.next() - if(result is not None): + if (result is not None): for edge_data in result: print(edge_data) From 947f202b3a632ac20b4971c22a68cab9c78a3215 Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 4 Mar 2024 11:54:37 +0800 Subject: [PATCH 3/4] fmt --- example/ScanVertexEdgeExample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/ScanVertexEdgeExample.py b/example/ScanVertexEdgeExample.py index 19194ae6..c70b3c8e 100644 --- a/example/ScanVertexEdgeExample.py +++ b/example/ScanVertexEdgeExample.py @@ -59,7 +59,7 @@ def scan_person_vertex(graph_storage_client): print("======== Scan vertexes in ScanSpace ======") while resp.has_next(): result = resp.next() - if (result is not None): + if result is not None: for vertex_data in result: print(vertex_data) @@ -71,7 +71,7 @@ def scan_person_edge(graph_storage_client): print("======== Scan edges in ScanSpace ======") while resp.has_next(): result = resp.next() - if (result is not None): + if result is not None: for edge_data in result: print(edge_data) From 9347960aa50f61963d545a3491e92594622a7877 Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 4 Mar 2024 14:08:26 +0800 Subject: [PATCH 4/4] refactor set func --- example/ScanVertexEdgeExample.py | 1 + nebula3/sclient/GraphStorageClient.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/example/ScanVertexEdgeExample.py b/example/ScanVertexEdgeExample.py index c70b3c8e..ec2cf388 100644 --- a/example/ScanVertexEdgeExample.py +++ b/example/ScanVertexEdgeExample.py @@ -131,6 +131,7 @@ def scan_person_edge(graph_storage_client): [("172.28.1.1", 9559), ("172.28.1.2", 9559), ("172.28.1.3", 9559)], 50000 ) graph_storage_client = GraphStorageClient(meta_cache) + graph_storage_client.set_user_passwd("root", "nebula") prepare_data() scan_person_vertex(graph_storage_client) scan_person_edge(graph_storage_client) diff --git a/nebula3/sclient/GraphStorageClient.py b/nebula3/sclient/GraphStorageClient.py index 0db2fffa..c1e73c4a 100644 --- a/nebula3/sclient/GraphStorageClient.py +++ b/nebula3/sclient/GraphStorageClient.py @@ -44,7 +44,7 @@ def __init__(self, meta_cache, storage_addrs=None, time_out=60000): self._connections = [] self._create_connection() - def set_userPasswd(self, user, passwd): + def set_user_passwd(self, user, passwd): """set user and password for scan. only useful for enterprise :return: """