Browse Source

新增证书模块的proto以及证书的增删改功能

YuXinLong 1 year ago
parent
commit
1332c890e6
7 changed files with 462 additions and 24 deletions
  1. 1 0
      src/gencode.py
  2. 40 0
      src/llnwCERT_pb2.py
  3. 69 0
      src/llnwCERT_pb2.pyi
  4. 132 0
      src/llnwCERT_pb2_grpc.py
  5. 2 1
      src/llnw_pb2.py
  6. 88 0
      src/main.py
  7. 130 23
      测试.ipynb

+ 1 - 0
src/gencode.py

@@ -9,4 +9,5 @@ if __name__ == '__main__':
         '--grpc_python_out=.',
         '../proto/llnw.proto',
         '../proto/llnwLDS.proto',
+        '../proto/llnwCERT.proto',
     ))

File diff suppressed because it is too large
+ 40 - 0
src/llnwCERT_pb2.py


+ 69 - 0
src/llnwCERT_pb2.pyi

@@ -0,0 +1,69 @@
+from google.protobuf import descriptor as _descriptor
+from google.protobuf import message as _message
+from typing import ClassVar as _ClassVar, Mapping as _Mapping, Optional as _Optional, Union as _Union
+
+DESCRIPTOR: _descriptor.FileDescriptor
+
+class Base(_message.Message):
+    __slots__ = ["key", "username"]
+    KEY_FIELD_NUMBER: _ClassVar[int]
+    USERNAME_FIELD_NUMBER: _ClassVar[int]
+    key: str
+    username: str
+    def __init__(self, username: _Optional[str] = ..., key: _Optional[str] = ...) -> None: ...
+
+class CreateSSLCertRequest(_message.Message):
+    __slots__ = ["base", "cert", "certKey", "certName", "shortname"]
+    BASE_FIELD_NUMBER: _ClassVar[int]
+    CERTKEY_FIELD_NUMBER: _ClassVar[int]
+    CERTNAME_FIELD_NUMBER: _ClassVar[int]
+    CERT_FIELD_NUMBER: _ClassVar[int]
+    SHORTNAME_FIELD_NUMBER: _ClassVar[int]
+    base: Base
+    cert: str
+    certKey: str
+    certName: str
+    shortname: str
+    def __init__(self, base: _Optional[_Union[Base, _Mapping]] = ..., shortname: _Optional[str] = ..., cert: _Optional[str] = ..., certKey: _Optional[str] = ..., certName: _Optional[str] = ...) -> None: ...
+
+class CreateSSLCertResponse(_message.Message):
+    __slots__ = ["uuid"]
+    UUID_FIELD_NUMBER: _ClassVar[int]
+    uuid: str
+    def __init__(self, uuid: _Optional[str] = ...) -> None: ...
+
+class DeleteSSLCertRequest(_message.Message):
+    __slots__ = ["base", "uuid"]
+    BASE_FIELD_NUMBER: _ClassVar[int]
+    UUID_FIELD_NUMBER: _ClassVar[int]
+    base: Base
+    uuid: str
+    def __init__(self, base: _Optional[_Union[Base, _Mapping]] = ..., uuid: _Optional[str] = ...) -> None: ...
+
+class DeleteSSLCertResponse(_message.Message):
+    __slots__ = ["isSuccess"]
+    ISSUCCESS_FIELD_NUMBER: _ClassVar[int]
+    isSuccess: bool
+    def __init__(self, isSuccess: bool = ...) -> None: ...
+
+class UpdateSSLCertRequest(_message.Message):
+    __slots__ = ["base", "cert", "certKey", "certName", "shortname", "uuid"]
+    BASE_FIELD_NUMBER: _ClassVar[int]
+    CERTKEY_FIELD_NUMBER: _ClassVar[int]
+    CERTNAME_FIELD_NUMBER: _ClassVar[int]
+    CERT_FIELD_NUMBER: _ClassVar[int]
+    SHORTNAME_FIELD_NUMBER: _ClassVar[int]
+    UUID_FIELD_NUMBER: _ClassVar[int]
+    base: Base
+    cert: str
+    certKey: str
+    certName: str
+    shortname: str
+    uuid: str
+    def __init__(self, base: _Optional[_Union[Base, _Mapping]] = ..., uuid: _Optional[str] = ..., shortname: _Optional[str] = ..., cert: _Optional[str] = ..., certKey: _Optional[str] = ..., certName: _Optional[str] = ...) -> None: ...
+
+class UpdateSSLCertResponse(_message.Message):
+    __slots__ = ["isSuccess"]
+    ISSUCCESS_FIELD_NUMBER: _ClassVar[int]
+    isSuccess: bool
+    def __init__(self, isSuccess: bool = ...) -> None: ...

+ 132 - 0
src/llnwCERT_pb2_grpc.py

@@ -0,0 +1,132 @@
+# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
+"""Client and server classes corresponding to protobuf-defined services."""
+import grpc
+
+import llnwCERT_pb2 as llnwCERT__pb2
+
+
+class CertDeliveryServiceStub(object):
+    """Missing associated documentation comment in .proto file."""
+
+    def __init__(self, channel):
+        """Constructor.
+
+        Args:
+            channel: A grpc.Channel.
+        """
+        self.CreateSSLCert = channel.unary_unary(
+                '/llnwApi.Delivery.CERT.CertDeliveryService/CreateSSLCert',
+                request_serializer=llnwCERT__pb2.CreateSSLCertRequest.SerializeToString,
+                response_deserializer=llnwCERT__pb2.CreateSSLCertResponse.FromString,
+                )
+        self.UpdateSSLCert = channel.unary_unary(
+                '/llnwApi.Delivery.CERT.CertDeliveryService/UpdateSSLCert',
+                request_serializer=llnwCERT__pb2.UpdateSSLCertRequest.SerializeToString,
+                response_deserializer=llnwCERT__pb2.UpdateSSLCertResponse.FromString,
+                )
+        self.DeleteSSLCert = channel.unary_unary(
+                '/llnwApi.Delivery.CERT.CertDeliveryService/DeleteSSLCert',
+                request_serializer=llnwCERT__pb2.DeleteSSLCertRequest.SerializeToString,
+                response_deserializer=llnwCERT__pb2.DeleteSSLCertResponse.FromString,
+                )
+
+
+class CertDeliveryServiceServicer(object):
+    """Missing associated documentation comment in .proto file."""
+
+    def CreateSSLCert(self, request, context):
+        """Missing associated documentation comment in .proto file."""
+        context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+        context.set_details('Method not implemented!')
+        raise NotImplementedError('Method not implemented!')
+
+    def UpdateSSLCert(self, request, context):
+        """Missing associated documentation comment in .proto file."""
+        context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+        context.set_details('Method not implemented!')
+        raise NotImplementedError('Method not implemented!')
+
+    def DeleteSSLCert(self, request, context):
+        """Missing associated documentation comment in .proto file."""
+        context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+        context.set_details('Method not implemented!')
+        raise NotImplementedError('Method not implemented!')
+
+
+def add_CertDeliveryServiceServicer_to_server(servicer, server):
+    rpc_method_handlers = {
+            'CreateSSLCert': grpc.unary_unary_rpc_method_handler(
+                    servicer.CreateSSLCert,
+                    request_deserializer=llnwCERT__pb2.CreateSSLCertRequest.FromString,
+                    response_serializer=llnwCERT__pb2.CreateSSLCertResponse.SerializeToString,
+            ),
+            'UpdateSSLCert': grpc.unary_unary_rpc_method_handler(
+                    servicer.UpdateSSLCert,
+                    request_deserializer=llnwCERT__pb2.UpdateSSLCertRequest.FromString,
+                    response_serializer=llnwCERT__pb2.UpdateSSLCertResponse.SerializeToString,
+            ),
+            'DeleteSSLCert': grpc.unary_unary_rpc_method_handler(
+                    servicer.DeleteSSLCert,
+                    request_deserializer=llnwCERT__pb2.DeleteSSLCertRequest.FromString,
+                    response_serializer=llnwCERT__pb2.DeleteSSLCertResponse.SerializeToString,
+            ),
+    }
+    generic_handler = grpc.method_handlers_generic_handler(
+            'llnwApi.Delivery.CERT.CertDeliveryService', rpc_method_handlers)
+    server.add_generic_rpc_handlers((generic_handler,))
+
+
+ # This class is part of an EXPERIMENTAL API.
+class CertDeliveryService(object):
+    """Missing associated documentation comment in .proto file."""
+
+    @staticmethod
+    def CreateSSLCert(request,
+            target,
+            options=(),
+            channel_credentials=None,
+            call_credentials=None,
+            insecure=False,
+            compression=None,
+            wait_for_ready=None,
+            timeout=None,
+            metadata=None):
+        return grpc.experimental.unary_unary(request, target, '/llnwApi.Delivery.CERT.CertDeliveryService/CreateSSLCert',
+            llnwCERT__pb2.CreateSSLCertRequest.SerializeToString,
+            llnwCERT__pb2.CreateSSLCertResponse.FromString,
+            options, channel_credentials,
+            insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
+
+    @staticmethod
+    def UpdateSSLCert(request,
+            target,
+            options=(),
+            channel_credentials=None,
+            call_credentials=None,
+            insecure=False,
+            compression=None,
+            wait_for_ready=None,
+            timeout=None,
+            metadata=None):
+        return grpc.experimental.unary_unary(request, target, '/llnwApi.Delivery.CERT.CertDeliveryService/UpdateSSLCert',
+            llnwCERT__pb2.UpdateSSLCertRequest.SerializeToString,
+            llnwCERT__pb2.UpdateSSLCertResponse.FromString,
+            options, channel_credentials,
+            insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
+
+    @staticmethod
+    def DeleteSSLCert(request,
+            target,
+            options=(),
+            channel_credentials=None,
+            call_credentials=None,
+            insecure=False,
+            compression=None,
+            wait_for_ready=None,
+            timeout=None,
+            metadata=None):
+        return grpc.experimental.unary_unary(request, target, '/llnwApi.Delivery.CERT.CertDeliveryService/DeleteSSLCert',
+            llnwCERT__pb2.DeleteSSLCertRequest.SerializeToString,
+            llnwCERT__pb2.DeleteSSLCertResponse.FromString,
+            options, channel_credentials,
+            insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

File diff suppressed because it is too large
+ 2 - 1
src/llnw_pb2.py


+ 88 - 0
src/main.py

@@ -4,11 +4,13 @@ from concurrent import futures
 import grpc
 import pydash
 
+import llnwCERT_pb2_grpc
 import llnwLDS_pb2_grpc
 import llnw_pb2_grpc
 from ll_sdk.config_api import ConfigApiClient
 from llnw_pb2 import *
 from llnwLDS_pb2 import *
+from llnwCERT_pb2 import *
 
 
 def get_cl(base):
@@ -98,6 +100,23 @@ def lds_config_from_grpc(request):
     }
 
 
+def cert_config_from_grpc(request):
+    return {
+        "uuid": request.uuid,
+        "accounts": [
+            {
+                "shortname": request.shortname
+            }
+        ],
+        "body": {
+            "cert": request.cert,
+            "cerKey": request.certKey,
+            "certName": request.certName,
+            "contentType": "default"
+        }
+    }
+
+
 def lds_uuids_to_grpc(res):
     uuids = []
     results = res.get("results")
@@ -118,6 +137,36 @@ def check_error(res, context):
                 context.abort(grpc.StatusCode.UNKNOWN, json.dumps(res))
 
 
+def check_cert_create_error(res):
+    if res.get('isEnabled') is None:
+        return CreateSSLCertResponse(uuid=None)
+    else:
+        if res.get('isEnabled') is True:
+            return CreateSSLCertResponse(uuid=res.get('uuid'))
+        else:
+            return CreateSSLCertResponse(uuid=None)
+
+
+def check_cert_update_error(res):
+    if res.get('isEnabled') is None:
+        return UpdateSSLCertResponse(isSuccess=False)
+    else:
+        if res.get('isEnabled') is True:
+            return UpdateSSLCertResponse(isSuccess=True)
+        else:
+            return UpdateSSLCertResponse(isSuccess=False)
+
+
+def check_cert_delete_error(res):
+    if res.get('isEnabled') is None:
+        return DeleteSSLCertResponse(isSuccess=False)
+    else:
+        if res.get('isEnabled') is True:
+            return DeleteSSLCertResponse(isSuccess=True)
+        else:
+            return DeleteSSLCertResponse(isSuccess=False)
+
+
 def check_lds_create_result(res):
     if res.get('isEnabled') is None:
         return CreateLDSResponse(isSuccess=False)
@@ -210,6 +259,45 @@ class LogDeliveryServiceServicer(llnwLDS_pb2_grpc.LogDeliveryServiceServicer):
             return lds_uuids_to_grpc(res)
 
 
+class CertDeliveryServiceServicer(llnwCERT_pb2_grpc.CertDeliveryServiceServicer):
+
+    def CreateSSLCert(self, request, context):
+        print("CreateSSLCert")
+        print("请求:", request)
+        try:
+            cert_config = cert_config_from_grpc(request)
+            res = get_cl(request.base).create_customer_certificate(cert_config={"body": cert_config}).json()
+            print("响应:", res)
+        except:
+            return CreateSSLCertResponse(uuid=None)
+        else:
+            return check_cert_create_error(res)
+
+    def UpdateSSLCert(self, request, context):
+        print("UpdateSSLCert")
+        print("请求:", request)
+        try:
+            cert_config = cert_config_from_grpc(request)
+            res = get_cl(request.base).update_customer_certificate(
+                uuid=request.uuid, cert_config={"body": cert_config}).json()
+            print("响应:", res)
+        except:
+            return UpdateSSLCertResponse(isSuccess=False)
+        else:
+            return check_cert_update_error(res)
+
+    def DeleteSSLCert(self, request, context):
+        print("deleteSSLCert")
+        print("请求:", request)
+        try:
+            res = get_cl(request.base).delete_customer_certificate(uuid=request.uuid)
+            print("响应:", res)
+        except:
+            return DeleteSSLCertResponse(isSuccess=False)
+        else:
+            return check_cert_delete_error(res)
+
+
 if __name__ == '__main__':
     server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
     llnw_pb2_grpc.add_DeliveryServiceServicer_to_server(DeliveryServiceServicer(), server)

File diff suppressed because it is too large
+ 130 - 23
测试.ipynb