From 7743d783d6ca4076476dadd4427c2384540cfc1b Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Fri, 15 Sep 2023 02:45:10 +0800 Subject: [PATCH 1/5] ci(workflow): update coverage workflow --- .github/workflows/test.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 230e61b..9b0a573 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,12 +36,7 @@ jobs: - name: Test code run: make test - - - name: check coverage - run: | - ls -la .cache - ls -la coverage.xml - +gi - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: From 957bf035b74f7ed4dbf90120a28a057502ec704e Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Tue, 12 Sep 2023 17:41:16 +0800 Subject: [PATCH 2/5] chore: add secure channel for auth --- instill_sdk/clients/client.py | 6 +++--- instill_sdk/clients/connector.py | 29 ++++++++++++++++++----------- instill_sdk/clients/mgmt.py | 29 ++++++++++++++++++----------- instill_sdk/clients/model.py | 29 ++++++++++++++++++----------- instill_sdk/clients/pipeline.py | 29 ++++++++++++++++++----------- tests/test_client.py | 26 +++++++++++++------------- 6 files changed, 88 insertions(+), 60 deletions(-) diff --git a/instill_sdk/clients/client.py b/instill_sdk/clients/client.py index 06b46f5..560a951 100644 --- a/instill_sdk/clients/client.py +++ b/instill_sdk/clients/client.py @@ -11,12 +11,12 @@ class Client(ABC): @property @abstractmethod - def protocol(self): + def token(self): pass - @protocol.setter + @token.setter @abstractmethod - def protocol(self): + def token(self): pass @property diff --git a/instill_sdk/clients/connector.py b/instill_sdk/clients/connector.py index 2572402..b29739e 100644 --- a/instill_sdk/clients/connector.py +++ b/instill_sdk/clients/connector.py @@ -14,33 +14,40 @@ class ConnectorClient(Client): def __init__( - self, user: mgmt_interface.User, protocol="http", host="localhost", port="8080" + self, user: mgmt_interface.User, token="", host="localhost", port="8080" ) -> None: """Initialize client for connector service with target host. Args: - protocol (str): http/https + token (str): api token for authentication host (str): host url port (str): host port """ - self.protocol = protocol + self.token = token self.host = host self.port = port self._user = user - self._channel = grpc.insecure_channel( - f"{host}:{port}".format(protocol=protocol, host=host, port=port) - ) + if len(token) == 0: + self._channel = grpc.insecure_channel(f"{host}:{port}") + else: + ssl_creds = grpc.ssl_channel_credentials() + call_creds = grpc.access_token_call_credentials(token) + creds = grpc.composite_channel_credentials(ssl_creds, call_creds) + self._channel = grpc.secure_channel( + target=f"{host}", + credentials=creds, + ) self._stub = connector_service.ConnectorPublicServiceStub(self._channel) @property - def protocol(self): - return self._protocol + def token(self): + return self._token - @protocol.setter - def protocol(self, protocol: str): - self._protocol = protocol + @token.setter + def token(self, token: str): + self._token = token @property def host(self): diff --git a/instill_sdk/clients/mgmt.py b/instill_sdk/clients/mgmt.py index 2a9eecb..8efcd29 100644 --- a/instill_sdk/clients/mgmt.py +++ b/instill_sdk/clients/mgmt.py @@ -12,31 +12,38 @@ class MgmtClient(Client): - def __init__(self, protocol="http", host="localhost", port="7080") -> None: + def __init__(self, token="", host="localhost", port="7080") -> None: """Initialize client for management service with target host. Args: - protocol (str): http/https + token (str): api token for authentication host (str): host url port (str): host port """ - self.protocol = protocol + self.token = token self.host = host self.port = port - self._channel = grpc.insecure_channel( - f"{host}:{port}".format(protocol=protocol, host=host, port=port) - ) + if len(token) == 0: + self._channel = grpc.insecure_channel(f"{host}:{port}") + else: + ssl_creds = grpc.ssl_channel_credentials() + call_creds = grpc.access_token_call_credentials(token) + creds = grpc.composite_channel_credentials(ssl_creds, call_creds) + self._channel = grpc.secure_channel( + target=f"{host}", + credentials=creds, + ) self._stub = mgmt_service.MgmtPublicServiceStub(self._channel) @property - def protocol(self): - return self._protocol + def token(self): + return self._token - @protocol.setter - def protocol(self, protocol: str): - self._protocol = protocol + @token.setter + def token(self, token: str): + self._token = token @property def host(self): diff --git a/instill_sdk/clients/model.py b/instill_sdk/clients/model.py index e6b5e9a..1e84a13 100644 --- a/instill_sdk/clients/model.py +++ b/instill_sdk/clients/model.py @@ -18,33 +18,40 @@ class ModelClient(Client): def __init__( - self, user: mgmt_interface.User, protocol="http", host="localhost", port="9080" + self, user: mgmt_interface.User, token="", host="localhost", port="9080" ) -> None: """Initialize client for model service with target host. Args: - protocol (str): http/https + token (str): api token for authentication host (str): host url port (str): host port """ - self.protocol = protocol + self.token = token self.host = host self.port = port self._user = user - self._channel = grpc.insecure_channel( - f"{host}:{port}".format(protocol=protocol, host=host, port=port) - ) + if len(token) == 0: + self._channel = grpc.insecure_channel(f"{host}:{port}") + else: + ssl_creds = grpc.ssl_channel_credentials() + call_creds = grpc.access_token_call_credentials(token) + creds = grpc.composite_channel_credentials(ssl_creds, call_creds) + self._channel = grpc.secure_channel( + target=f"{host}", + credentials=creds, + ) self._stub = model_service.ModelPublicServiceStub(self._channel) @property - def protocol(self): - return self._protocol + def token(self): + return self._token - @protocol.setter - def protocol(self, protocol: str): - self._protocol = protocol + @token.setter + def token(self, token: str): + self._token = token @property def host(self): diff --git a/instill_sdk/clients/pipeline.py b/instill_sdk/clients/pipeline.py index 8f93a08..d3df06c 100644 --- a/instill_sdk/clients/pipeline.py +++ b/instill_sdk/clients/pipeline.py @@ -20,33 +20,40 @@ class PipelineClient(Client): def __init__( - self, user: mgmt_interface.User, protocol="http", host="localhost", port="8080" + self, user: mgmt_interface.User, token="", host="localhost", port="8080" ) -> None: """Initialize client for pipeline service with target host. Args: - protocol (str): http/https + token (str): api token for authentication host (str): host url port (str): host port """ - self.protocol = protocol + self.token = token self.host = host self.port = port self._user = user - self._channel = grpc.insecure_channel( - f"{host}:{port}".format(protocol=protocol, host=host, port=port) - ) + if len(token) == 0: + self._channel = grpc.insecure_channel(f"{host}:{port}") + else: + ssl_creds = grpc.ssl_channel_credentials() + call_creds = grpc.access_token_call_credentials(token) + creds = grpc.composite_channel_credentials(ssl_creds, call_creds) + self._channel = grpc.secure_channel( + target=f"{host}", + credentials=creds, + ) self._stub = pipeline_service.PipelinePublicServiceStub(self._channel) @property - def protocol(self): - return self._protocol + def token(self): + return self._token - @protocol.setter - def protocol(self, protocol: str): - self._protocol = protocol + @token.setter + def token(self, token: str): + self._token = token @property def host(self): diff --git a/tests/test_client.py b/tests/test_client.py index dee9bf8..571e223 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -33,30 +33,30 @@ def when_set_correct_type(expect): connector_client.host = "instill" expect(connector_client.host) == "instill" - def describe_protocol(): + def describe_token(): def when_not_set(expect): mgmt_client = MgmtClient() - expect(mgmt_client.protocol) == "http" + expect(mgmt_client.token) == "" model_client = ModelClient(user=mgmt_client.get_user()) - expect(model_client.protocol) == "http" + expect(model_client.token) == "" pipeline_client = PipelineClient(user=mgmt_client.get_user()) - expect(pipeline_client.protocol) == "http" + expect(pipeline_client.token) == "" connector_client = ConnectorClient(user=mgmt_client.get_user()) - expect(connector_client.protocol) == "http" + expect(connector_client.token) == "" def when_set_correct_type(expect): mgmt_client = MgmtClient() - mgmt_client.protocol = "instill" - expect(mgmt_client.protocol) == "instill" + mgmt_client.token = "instill" + expect(mgmt_client.token) == "instill" model_client = ModelClient(user=mgmt_client.get_user()) - model_client.protocol = "instill" - expect(model_client.protocol) == "instill" + model_client.token = "instill" + expect(model_client.token) == "instill" pipeline_client = PipelineClient(user=mgmt_client.get_user()) - pipeline_client.protocol = "instill" - expect(pipeline_client.protocol) == "instill" + pipeline_client.token = "instill" + expect(pipeline_client.token) == "instill" connector_client = ConnectorClient(user=mgmt_client.get_user()) - connector_client.protocol = "instill" - expect(connector_client.protocol) == "instill" + connector_client.token = "instill" + expect(connector_client.token) == "instill" def describe_port(): def when_not_set(expect): From 19ce182a2a4d72aad3235a793b35d03776d90a17 Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Tue, 12 Sep 2023 17:47:03 +0800 Subject: [PATCH 3/5] chore: get token from env --- instill_sdk/clients/client.py | 2 ++ instill_sdk/clients/connector.py | 4 ++-- instill_sdk/clients/mgmt.py | 4 ++-- instill_sdk/clients/model.py | 4 ++-- instill_sdk/clients/pipeline.py | 4 ++-- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/instill_sdk/clients/client.py b/instill_sdk/clients/client.py index 560a951..e969340 100644 --- a/instill_sdk/clients/client.py +++ b/instill_sdk/clients/client.py @@ -1,6 +1,8 @@ # pylint: disable=no-member,wrong-import-position from abc import ABC, abstractmethod +import os +API_TOKEN = os.environ.get("INSTILL_AI_API_TOKEN", default="") class Client(ABC): """Base interface class for creating mgmt/pipeline/connector/model clients. diff --git a/instill_sdk/clients/connector.py b/instill_sdk/clients/connector.py index b29739e..60902e2 100644 --- a/instill_sdk/clients/connector.py +++ b/instill_sdk/clients/connector.py @@ -6,7 +6,7 @@ import instill_sdk.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck import instill_sdk.protogen.vdp.connector.v1alpha.connector_pb2 as connector_interface import instill_sdk.protogen.vdp.connector.v1alpha.connector_public_service_pb2_grpc as connector_service -from instill_sdk.clients.client import Client +from instill_sdk.clients.client import Client, API_TOKEN from instill_sdk.utils.error_handler import grpc_handler # from instill_sdk.utils.logger import Logger @@ -14,7 +14,7 @@ class ConnectorClient(Client): def __init__( - self, user: mgmt_interface.User, token="", host="localhost", port="8080" + self, user: mgmt_interface.User, token=API_TOKEN, host="localhost", port="8080" ) -> None: """Initialize client for connector service with target host. diff --git a/instill_sdk/clients/mgmt.py b/instill_sdk/clients/mgmt.py index 8efcd29..2bc220b 100644 --- a/instill_sdk/clients/mgmt.py +++ b/instill_sdk/clients/mgmt.py @@ -5,14 +5,14 @@ import instill_sdk.protogen.base.mgmt.v1alpha.mgmt_pb2 as mgmt_interface import instill_sdk.protogen.base.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service import instill_sdk.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck -from instill_sdk.clients.client import Client +from instill_sdk.clients.client import Client, API_TOKEN from instill_sdk.utils.error_handler import grpc_handler # from instill_sdk.utils.logger import Logger class MgmtClient(Client): - def __init__(self, token="", host="localhost", port="7080") -> None: + def __init__(self, token=API_TOKEN, host="localhost", port="7080") -> None: """Initialize client for management service with target host. Args: diff --git a/instill_sdk/clients/model.py b/instill_sdk/clients/model.py index 1e84a13..e579f20 100644 --- a/instill_sdk/clients/model.py +++ b/instill_sdk/clients/model.py @@ -12,13 +12,13 @@ # model import instill_sdk.protogen.model.model.v1alpha.model_pb2 as model_interface import instill_sdk.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service -from instill_sdk.clients.client import Client +from instill_sdk.clients.client import Client, API_TOKEN from instill_sdk.utils.error_handler import grpc_handler class ModelClient(Client): def __init__( - self, user: mgmt_interface.User, token="", host="localhost", port="9080" + self, user: mgmt_interface.User, token=API_TOKEN, host="localhost", port="9080" ) -> None: """Initialize client for model service with target host. diff --git a/instill_sdk/clients/pipeline.py b/instill_sdk/clients/pipeline.py index d3df06c..cfc16a4 100644 --- a/instill_sdk/clients/pipeline.py +++ b/instill_sdk/clients/pipeline.py @@ -14,13 +14,13 @@ import instill_sdk.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service # common -from instill_sdk.clients.client import Client +from instill_sdk.clients.client import Client, API_TOKEN from instill_sdk.utils.error_handler import grpc_handler class PipelineClient(Client): def __init__( - self, user: mgmt_interface.User, token="", host="localhost", port="8080" + self, user: mgmt_interface.User, token=API_TOKEN, host="localhost", port="8080" ) -> None: """Initialize client for pipeline service with target host. From 913f1a5ab01dfee172ba7e47e88252c7ff0a3bab Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Fri, 15 Sep 2023 02:27:09 +0800 Subject: [PATCH 4/5] chore: fix lint --- instill_sdk/clients/client.py | 3 ++- instill_sdk/clients/connector.py | 2 +- instill_sdk/clients/mgmt.py | 2 +- instill_sdk/clients/model.py | 2 +- instill_sdk/clients/pipeline.py | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/instill_sdk/clients/client.py b/instill_sdk/clients/client.py index e969340..e32105e 100644 --- a/instill_sdk/clients/client.py +++ b/instill_sdk/clients/client.py @@ -1,9 +1,10 @@ # pylint: disable=no-member,wrong-import-position -from abc import ABC, abstractmethod import os +from abc import ABC, abstractmethod API_TOKEN = os.environ.get("INSTILL_AI_API_TOKEN", default="") + class Client(ABC): """Base interface class for creating mgmt/pipeline/connector/model clients. diff --git a/instill_sdk/clients/connector.py b/instill_sdk/clients/connector.py index 60902e2..a54d4ae 100644 --- a/instill_sdk/clients/connector.py +++ b/instill_sdk/clients/connector.py @@ -6,7 +6,7 @@ import instill_sdk.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck import instill_sdk.protogen.vdp.connector.v1alpha.connector_pb2 as connector_interface import instill_sdk.protogen.vdp.connector.v1alpha.connector_public_service_pb2_grpc as connector_service -from instill_sdk.clients.client import Client, API_TOKEN +from instill_sdk.clients.client import API_TOKEN, Client from instill_sdk.utils.error_handler import grpc_handler # from instill_sdk.utils.logger import Logger diff --git a/instill_sdk/clients/mgmt.py b/instill_sdk/clients/mgmt.py index 2bc220b..f4f02b9 100644 --- a/instill_sdk/clients/mgmt.py +++ b/instill_sdk/clients/mgmt.py @@ -5,7 +5,7 @@ import instill_sdk.protogen.base.mgmt.v1alpha.mgmt_pb2 as mgmt_interface import instill_sdk.protogen.base.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service import instill_sdk.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck -from instill_sdk.clients.client import Client, API_TOKEN +from instill_sdk.clients.client import API_TOKEN, Client from instill_sdk.utils.error_handler import grpc_handler # from instill_sdk.utils.logger import Logger diff --git a/instill_sdk/clients/model.py b/instill_sdk/clients/model.py index e579f20..3f29c17 100644 --- a/instill_sdk/clients/model.py +++ b/instill_sdk/clients/model.py @@ -12,7 +12,7 @@ # model import instill_sdk.protogen.model.model.v1alpha.model_pb2 as model_interface import instill_sdk.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service -from instill_sdk.clients.client import Client, API_TOKEN +from instill_sdk.clients.client import API_TOKEN, Client from instill_sdk.utils.error_handler import grpc_handler diff --git a/instill_sdk/clients/pipeline.py b/instill_sdk/clients/pipeline.py index cfc16a4..976613e 100644 --- a/instill_sdk/clients/pipeline.py +++ b/instill_sdk/clients/pipeline.py @@ -14,7 +14,7 @@ import instill_sdk.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service # common -from instill_sdk.clients.client import Client, API_TOKEN +from instill_sdk.clients.client import API_TOKEN, Client from instill_sdk.utils.error_handler import grpc_handler From 939605adcd2459fd6cc75f621a20c187c8029226 Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Fri, 15 Sep 2023 03:10:31 +0800 Subject: [PATCH 5/5] chore: fix typo --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9b0a573..0327253 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: - name: Test code run: make test -gi + - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: