def _stub(args): if args.oauth_scope: if args.test_case == "oauth2_auth_token": # TODO(jtattermusch): This testcase sets the auth metadata key-value # manually, which also means that the user would need to do the same # thing every time he/she would like to use and out of band oauth token. # The transformer function that produces the metadata key-value from # the access token should be provided by gRPC auth library. access_token = _oauth_access_token(args) metadata_transformer = lambda x: [("authorization", "Bearer %s" % access_token)] else: metadata_transformer = lambda x: [("authorization", "Bearer %s" % _oauth_access_token(args))] else: metadata_transformer = lambda x: [] if args.use_tls: if args.use_test_ca: root_certificates = resources.test_root_certificates() else: root_certificates = None # will load default roots. channel = test_utilities.not_really_secure_channel( args.server_host, args.server_port, implementations.ssl_channel_credentials(root_certificates), args.server_host_override, ) stub = test_pb2.beta_create_TestService_stub(channel, metadata_transformer=metadata_transformer) else: channel = implementations.insecure_channel(args.server_host, args.server_port) stub = test_pb2.beta_create_TestService_stub(channel) return stub
def _stub(args): if args.test_case == 'oauth2_auth_token': creds = oauth2client_client.GoogleCredentials.get_application_default() scoped_creds = creds.create_scoped([args.oauth_scope]) access_token = scoped_creds.get_access_token().access_token call_creds = implementations.access_token_call_credentials(access_token) elif args.test_case == 'compute_engine_creds': creds = oauth2client_client.GoogleCredentials.get_application_default() scoped_creds = creds.create_scoped([args.oauth_scope]) call_creds = implementations.google_call_credentials(scoped_creds) elif args.test_case == 'jwt_token_creds': creds = oauth2client_client.GoogleCredentials.get_application_default() call_creds = implementations.google_call_credentials(creds) else: call_creds = None if args.use_tls: if args.use_test_ca: root_certificates = resources.test_root_certificates() else: root_certificates = None # will load default roots. channel_creds = implementations.ssl_channel_credentials(root_certificates) if call_creds is not None: channel_creds = implementations.composite_channel_credentials( channel_creds, call_creds) channel = test_utilities.not_really_secure_channel( args.server_host, args.server_port, channel_creds, args.server_host_override) stub = test_pb2.beta_create_TestService_stub(channel) else: channel = implementations.insecure_channel( args.server_host, args.server_port) stub = test_pb2.beta_create_TestService_stub(channel) return stub
def create_trace_stub(host=TRACE_ENDPOINT, port=SSL_PORT): """Creates a secure channel.""" ssl_creds = implementations.ssl_channel_credentials(None, None, None) call_creds = implementations.metadata_call_credentials(make_auth_func()) channel_creds = implementations.composite_channel_credentials(ssl_creds, call_creds) channel = implementations.secure_channel(host, port, channel_creds) return trace_pb2.beta_create_TraceService_stub(channel)
def _stub(args): if args.oauth_scope: if args.test_case == 'oauth2_auth_token': # TODO(jtattermusch): This testcase sets the auth metadata key-value # manually, which also means that the user would need to do the same # thing every time he/she would like to use and out of band oauth token. # The transformer function that produces the metadata key-value from # the access token should be provided by gRPC auth library. access_token = _oauth_access_token(args) metadata_transformer = lambda x: [ ('authorization', 'Bearer %s' % access_token)] else: metadata_transformer = lambda x: [ ('authorization', 'Bearer %s' % _oauth_access_token(args))] else: metadata_transformer = lambda x: [] if args.use_tls: if args.use_test_ca: root_certificates = resources.test_root_certificates() else: root_certificates = None # will load default roots. channel = test_utilities.not_really_secure_channel( args.server_host, args.server_port, implementations.ssl_channel_credentials(root_certificates, None, None), args.server_host_override) stub = test_pb2.beta_create_TestService_stub( channel, metadata_transformer=metadata_transformer) else: channel = implementations.insecure_channel( args.server_host, args.server_port) stub = test_pb2.beta_create_TestService_stub(channel) return stub
def __init__(self, server, config, hist): # Create the stub host, port = server.split(':') port = int(port) if config.HasField('security_params'): creds = implementations.ssl_channel_credentials( resources.test_root_certificates()) channel = test_utilities.not_really_secure_channel( host, port, creds, config.security_params.server_host_override) else: channel = implementations.insecure_channel(host, port) if config.payload_config.WhichOneof('payload') == 'simple_params': self._generic = False self._stub = services_pb2.beta_create_BenchmarkService_stub( channel) payload = messages_pb2.Payload( body='\0' * config.payload_config.simple_params.req_size) self._request = messages_pb2.SimpleRequest( payload=payload, response_size=config.payload_config.simple_params.resp_size) else: self._generic = True self._stub = implementations.generic_stub(channel) self._request = '\0' * config.payload_config.bytebuf_params.req_size self._hist = hist self._response_callbacks = []
def setUp(self): self._servicer = _Servicer() self._method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } self._cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } self._server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server_credentials = implementations.ssl_server_credentials([( resources.private_key(), resources.certificate_chain(),),]) self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE)
def setUp(self): self._servicer = _Servicer() self._method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } self._cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } self._server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE)
def instantiate( self, methods, method_implementations, multi_method_implementation): serialization_behaviors = _serialization_behaviors_from_test_methods( methods) # TODO(nathaniel): Add a "groups" attribute to _digest.TestServiceDigest. service = next(iter(methods))[0] # TODO(nathaniel): Add a "cardinalities_by_group" attribute to # _digest.TestServiceDigest. cardinalities = { method: method_object.cardinality() for (group, method), method_object in methods.iteritems()} server_options = implementations.server_options( request_deserializers=serialization_behaviors.request_deserializers, response_serializers=serialization_behaviors.response_serializers, thread_pool_size=test_constants.POOL_SIZE) server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain(),),]) port = server.add_secure_port('[::]:0', server_credentials) server.start() channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates(), None, None) channel = test_utilities.not_really_secure_channel( 'localhost', port, channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( request_serializers=serialization_behaviors.request_serializers, response_deserializers=serialization_behaviors.response_deserializers, thread_pool_size=test_constants.POOL_SIZE) generic_stub = implementations.generic_stub(channel, options=stub_options) dynamic_stub = implementations.dynamic_stub( channel, service, cardinalities, options=stub_options) return generic_stub, {service: dynamic_stub}, server
def _stub(args): if args.test_case == 'oauth2_auth_token': creds = oauth2client_client.GoogleCredentials.get_application_default() scoped_creds = creds.create_scoped([args.oauth_scope]) access_token = scoped_creds.get_access_token().access_token call_creds = implementations.access_token_call_credentials( access_token) elif args.test_case == 'compute_engine_creds': creds = oauth2client_client.GoogleCredentials.get_application_default() scoped_creds = creds.create_scoped([args.oauth_scope]) call_creds = implementations.google_call_credentials(scoped_creds) else: call_creds = None if args.use_tls: if args.use_test_ca: root_certificates = resources.test_root_certificates() else: root_certificates = None # will load default roots. channel_creds = implementations.ssl_channel_credentials( root_certificates) if call_creds is not None: channel_creds = implementations.composite_channel_credentials( channel_creds, call_creds) channel = test_utilities.not_really_secure_channel( args.server_host, args.server_port, channel_creds, args.server_host_override) stub = test_pb2.beta_create_TestService_stub(channel) else: channel = implementations.insecure_channel(args.server_host, args.server_port) stub = test_pb2.beta_create_TestService_stub(channel) return stub
def _make_stub(client, stub_factory, host, port): """Makes a stub for an RPC service. Uses / depends on the beta implementation of gRPC. :type client: :class:`.client.Client` :param client: The client that owns the cluster. Provides authorization and user agent. :type stub_factory: callable :param stub_factory: A factory which will create a gRPC stub for a given service. :type host: str :param host: The host for the service. :type port: int :param port: The port for the service. :rtype: :class:`grpc.beta._stub._AutoIntermediary` :returns: The stub object used to make gRPC requests to a given API. """ # Leaving the first argument to ssl_channel_credentials() as None # loads root certificates from `grpc/_adapter/credentials/roots.pem`. transport_creds = implementations.ssl_channel_credentials(None, None, None) custom_metadata_plugin = _MetadataPlugin(client) auth_creds = implementations.metadata_call_credentials( custom_metadata_plugin, name='google_creds') channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) channel = implementations.secure_channel(host, port, channel_creds) return stub_factory(channel)
def do_inference(): # get deployed model details model_name = request.path[1:] query_string = {"modelName": model_name} headers = { 'Authorization': get_access_token(), 'Cache-Control': "no-cache" } res = requests.request("GET", deployment_url, headers=headers, params=query_string) model_info = json.loads(res.text) # check model is available if int(model_info["count"]) < 1: return Response('404 Not Found: Model ' + model_name + ' is unavailable.', status=404) else: # get details for the latest model version latest_version = [0, 0] for index, model in enumerate(model_info["modelServers"]): if int(model["specs"]["models"][0] ["modelVersion"]) > latest_version[0]: latest_version = [ int(model["specs"]["models"][0]["modelVersion"]), index ] model_host = model_info["modelServers"][ latest_version[1]]["endpoints"][0] credentials = implementations.ssl_channel_credentials( root_certificates=str(model_host["caCrt"])) channel = implementations.secure_channel(str(model_host["host"]), int(model_host["port"]), credentials) stub = prediction_service_pb2.beta_create_PredictionService_stub( channel, metadata_transformer=metadata_transformer) # check file was uploaded if not 'file' in globals.request.files: return Response('404 Not Found: File not provided.', status=404) else: # perform inference on the file data = skimage.io.imread(globals.request.files['file']) req = predict_pb2.PredictRequest() req.model_spec.name = model_name req.model_spec.signature_name = 'predict_images' req.inputs["images"].CopyFrom( tf.contrib.util.make_tensor_proto(data, shape=[1, data.size], dtype="float32")) res = stub.Predict(req, 150) # convert scores to JSON res = str(res).split('}')[3].split('\n') res.pop(11) res.pop(0) scores = {} for i, estimate in enumerate(res): scores[str(i)] = float(estimate[14:]) return Response(json.dumps(scores), status=200, mimetype='application/json')
def make_channel(host, port): ssl_channel = implementations.ssl_channel_credentials(None, None, None) creds = get_credentials().create_scoped(args.speech_scope) auth_header = ("authorization", "Bearer " + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials(lambda _, func: func([auth_header], None), name="google_creds") composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def make_stub(credentials, user_agent, stub_factory, host, port): """Makes a stub for an RPC service. Uses / depends on the beta implementation of gRPC. :type credentials: :class:`oauth2client.client.OAuth2Credentials` :param credentials: The OAuth2 Credentials to use for creating access tokens. :type user_agent: str :param user_agent: (Optional) The user agent to be used with API requests. :type stub_factory: callable :param stub_factory: A factory which will create a gRPC stub for a given service. :type host: str :param host: The host for the service. :type port: int :param port: The port for the service. :rtype: :class:`grpc.beta._stub._AutoIntermediary` :returns: The stub object used to make gRPC requests to a given API. """ # Leaving the first argument to ssl_channel_credentials() as None # loads root certificates from `grpc/_adapter/credentials/roots.pem`. transport_creds = implementations.ssl_channel_credentials(None, None, None) custom_metadata_plugin = MetadataPlugin(credentials, user_agent) auth_creds = implementations.metadata_call_credentials( custom_metadata_plugin, name='google_creds') channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) channel = implementations.secure_channel(host, port, channel_creds) return stub_factory(channel)
def __init__(self, server, config, hist): # Create the stub host, port = server.split(':') port = int(port) if config.HasField('security_params'): creds = implementations.ssl_channel_credentials( resources.test_root_certificates()) channel = test_utilities.not_really_secure_channel( host, port, creds, config.security_params.server_host_override) else: channel = implementations.insecure_channel(host, port) connected_event = threading.Event() def wait_for_ready(connectivity): if connectivity == grpc.ChannelConnectivity.READY: connected_event.set() channel.subscribe(wait_for_ready, try_to_connect=True) connected_event.wait() if config.payload_config.WhichOneof('payload') == 'simple_params': self._generic = False self._stub = services_pb2.beta_create_BenchmarkService_stub(channel) payload = messages_pb2.Payload( body='\0' * config.payload_config.simple_params.req_size) self._request = messages_pb2.SimpleRequest( payload=payload, response_size=config.payload_config.simple_params.resp_size) else: self._generic = True self._stub = implementations.generic_stub(channel) self._request = '\0' * config.payload_config.bytebuf_params.req_size self._hist = hist self._response_callbacks = []
def create_stub(generated_create_stub, service_path, port, ssl_creds=None, channel=None, metadata_transformer=None, scopes=None): """Creates a gRPC client stub. Args: generated_create_stub: The generated gRPC method to create a stub. service_path: The domain name of the API remote host. port: The port on which to connect to the remote host. ssl_creds: A ClientCredentials object for use with an SSL-enabled Channel. If none, credentials are pulled from a default location. channel: A Channel object through which to make calls. If none, a secure channel is constructed. metadata_transformer: A function that transforms the metadata for requests, e.g., to give OAuth credentials. scopes: The OAuth scopes for this service. This parameter is ignored if a custom metadata_transformer is supplied. Returns: A gRPC client stub. """ if channel is None: if ssl_creds is None: ssl_creds = implementations.ssl_channel_credentials( None, None, None) if metadata_transformer is None: if scopes is None: scopes = [] metadata_transformer = auth.make_auth_func(scopes) channel_creds = _make_channel_creds(metadata_transformer, ssl_creds) channel = implementations.secure_channel( service_path, port, channel_creds) return generated_create_stub(channel)
def instantiate( self, methods, method_implementations, multi_method_implementation): serialization_behaviors = _serialization_behaviors_from_test_methods( methods) # TODO(nathaniel): Add a "groups" attribute to _digest.TestServiceDigest. service = next(iter(methods))[0] # TODO(nathaniel): Add a "cardinalities_by_group" attribute to # _digest.TestServiceDigest. cardinalities = { method: method_object.cardinality() for (group, method), method_object in six.iteritems(methods)} server_options = implementations.server_options( request_deserializers=serialization_behaviors.request_deserializers, response_serializers=serialization_behaviors.response_serializers, thread_pool_size=test_constants.POOL_SIZE) server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain(),),]) port = server.add_secure_port('[::]:0', server_credentials) server.start() channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) channel = test_utilities.not_really_secure_channel( 'localhost', port, channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( request_serializers=serialization_behaviors.request_serializers, response_deserializers=serialization_behaviors.response_deserializers, thread_pool_size=test_constants.POOL_SIZE) generic_stub = implementations.generic_stub(channel, options=stub_options) dynamic_stub = implementations.dynamic_stub( channel, service, cardinalities, options=stub_options) return generic_stub, {service: dynamic_stub}, server
def _make_stub(client, stub_factory, host, port): """Makes a stub for an RPC service. Uses / depends on the beta implementation of gRPC. :type client: :class:`.client.Client` :param client: The client that owns the instance. Provides authorization and user agent. :type stub_factory: callable :param stub_factory: A factory which will create a gRPC stub for a given service. :type host: str :param host: The host for the service. :type port: int :param port: The port for the service. :rtype: :class:`grpc.beta._stub._AutoIntermediary` :returns: The stub object used to make gRPC requests to a given API. """ # Leaving the first argument to ssl_channel_credentials() as None # loads root certificates from `grpc/_adapter/credentials/roots.pem`. transport_creds = implementations.ssl_channel_credentials(None, None, None) custom_metadata_plugin = _MetadataPlugin(client) auth_creds = implementations.metadata_call_credentials( custom_metadata_plugin, name='google_creds') channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) channel = implementations.secure_channel(host, port, channel_creds) return stub_factory(channel)
def run(): # TSL连接方式 >>> with open( 'G:\DotNet\SevenTiny.Cloud.FaaS\Code\Python\SevenTiny.Cloud.FaaS.GRpc\ca\client.pem', 'rb') as f: pem = f.read() creds = implementations.ssl_channel_credentials(pem, None, None) channel = implementations.secure_channel('localhost', 5001, creds) # TSL连接方式 <<< # 连接 rpc 服务器 # channel = grpc.insecure_channel('localhost:5001') # 调用 rpc 服务 stub = seventiny_cloud_faas_pb2_grpc.DynamicScriptExecutorStub(channel) response = stub.CheckScript( seventiny_cloud_faas_proto_pb2.DynamicScript(TenantId=100000, Script='123123123')) print("CheckScript client received: " + response.Message) response = stub.Execute( seventiny_cloud_faas_proto_pb2.DynamicScript(TenantId=100000, Script='123123123')) print("Execute client received: " + response.Message)
def __init__(self, host, port, timeout, user, password, creds=None, options=None): """This class creates grpc calls using python. :param username: Username for device login :param password: Password for device login :param host: The ip address for the device :param port: The port for the device :param timeout: how long before the rpc call timesout :param creds: Input of the pem file :param options: TLS server name :type password: str :type username: str :type server: str :type port: int :type timeout:int :type creds: str :type options: str """ if creds != None: self._target = '%s:%d' % (host, port) self._creds = implementations.ssl_channel_credentials(creds) self._options = options channel = grpc.secure_channel( self._target, self._creds, (('grpc.ssl_target_name_override', self._options,),)) self._channel = implementations.Channel(channel) else: self._host = host self._port = port self._channel = implementations.insecure_channel(self._host, self._port) self._stub = ems_grpc_pb2.beta_create_gRPCConfigOper_stub(self._channel) self._timeout = int(timeout) self._metadata = [('username', user), ('password', password)]
def run(): creds = implementations.ssl_channel_credentials(open('../../../cert.pem' ).read(), None, None) channel = implementations.secure_channel('localhost', 8000, creds) stub = search_pb2.beta_create_Google_stub(channel) search = raw_input('Google: ') query = search_pb2.Request(query=search) results = stub.Search.future(query, _TIMEOUT_) for result in results: print result.title + '\n' + result.url + '\n' + result.content
def channel(self): ssl_channel = implementations.ssl_channel_credentials(None, None, None) creds = get_credentials().create_scoped([SPEECH_SCOPE]) auth_header = ('Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(SPEECH_API_HOST, SPEECH_API_PORT, composite_channel)
def setUp(self): self.server = test_pb2.beta_create_TestService_server(methods.TestService()) port = self.server.add_secure_port( '[::]:0', implementations.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain())])) self.server.start() self.stub = test_pb2.beta_create_TestService_stub( test_utilities.not_really_secure_channel( '[::]', port, implementations.ssl_channel_credentials( resources.test_root_certificates()), _SERVER_HOST_OVERRIDE))
def dial(self): if self.stub: self.stub.close() p = urlparse('http://' + self.addr) if self.root_certificates or self.private_key or self.certificate_chain: creds = implementations.ssl_channel_credentials( self.root_certificates, self.private_key, self.certificate_chain) channel = implementations.secure_channel(p.hostname, p.port, creds) else: channel = implementations.insecure_channel(p.hostname, p.port) self.stub = vtgateservice_pb2.beta_create_Vitess_stub(channel)
def make_channel(host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = get_credentials().create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ("Authorization", "Bearer " + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials(lambda _, cb: cb([auth_header], None), name="google_creds") # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def EstablishChannel(address, port, client_id, user, password): # Open a grpc channel to the device creds = implementations.ssl_channel_credentials(open('/tmp/host.pem').read(), None, None) channel = implementations.secure_channel(address, port, creds) # Create stub for authentication login_stub = authentication_service_pb2.beta_create_Login_stub(channel) # Fill the login request message structure login_request = authentication_service_pb2.LoginRequest(user_name=user, password=password, client_id=client_id) # Invoke the login check API login_response = login_stub.LoginCheck(login_request, _TIMEOUT_SECONDS) print login_response return channel
def run(): serverAddr = 'open.didiyunapi.com:8080' transport_creds = implementations.ssl_channel_credentials() auth_creds = implementations.metadata_call_credentials( oauth2token_credentials) channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) with grpc.secure_channel(serverAddr, channel_creds) as channel: dc2_stub = dc2_pb2_grpc.Dc2Stub(channel) try: response = dc2_stub.ListImage( dc2_pb2.ListImageRequest(header=base_pb2.Header( regionId='gz'))) print(response.error) print(response.data) except Exception as e: print("except: ", e)
def setUp(self): self._servicer = _Servicer() method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server = implementations.server(method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) port = self._server.add_secure_port('[::]:0', server_credentials) self._server.start() self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._call_credentials = implementations.metadata_call_credentials( _metadata_plugin) channel = test_utilities.not_really_secure_channel( 'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE) self._dynamic_stub = implementations.dynamic_stub(channel, _GROUP, cardinalities, options=stub_options)
def make_channel(host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = get_credentials().create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ('Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def run(): # 连接 rpc 服务器 # TSL连接方式 >>> with open('G:\\DotNet\\SevenTiny.Cloud.FaaS\\Code\\Python\\SevenTiny.Cloud.FaaS.GRpc\\ca\\client.pem', 'rb') as f: pem = f.read() creds = implementations.ssl_channel_credentials( pem, None, None) channel = implementations.secure_channel('localhost', 5001, creds) # TSL连接方式 <<< # channel = grpc.insecure_channel('localhost:39901') # 调用 rpc 服务 stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='czl')) print("Greeter client received: " + response.message) response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='daydaygo')) print("Greeter client received: " + response.message)
def EstablishChannel(address, port, client_id, user, password): # Open a grpc channel to the device creds = implementations.ssl_channel_credentials( open('/tmp/host.pem').read(), None, None) channel = implementations.secure_channel(address, port, creds) # Create stub for authentication login_stub = authentication_service_pb2.beta_create_Login_stub(channel) # Fill the login request message structure login_request = authentication_service_pb2.LoginRequest( user_name=user, password=password, client_id=client_id) # Invoke the login check API login_response = login_stub.LoginCheck(login_request, _TIMEOUT_SECONDS) print login_response return channel
def main(): deployment_url = DEPLOYMENT_URL deployment_url += "/api/v2/modelServers" token = globals.request.headers.get("Authorization") headers = {'Authorization': token, 'Cache-Control': "no-cache"} querystring = {"modelName": MODEL_NAME, "modelVersion": MODEL_VERSION} response = requests.request("GET", deployment_url, headers=headers, params=querystring) model_info = json.loads(response.text) model_info = model_info["modelServers"][0] endpoint = model_info["endpoints"][0] credentials = implementations.ssl_channel_credentials( root_certificates=str(endpoint["caCrt"])) channel = implementations.secure_channel(str(endpoint["host"]), int(endpoint["port"]), credentials) stub = prediction_service_pb2.beta_create_PredictionService_stub( channel, metadata_transformer=metadata_transformer) data_str = globals.request.get_data().split(",") data = [] for idx in range(len(data_str)): data.append(float(data_str[idx])) request = predict_pb2.PredictRequest() request.model_spec.name = MODEL_NAME request.model_spec.signature_name = 'serving_default' tfutil = tf.contrib.util t_proto = tfutil.make_tensor_proto(data, shape=[1, len(data)], dtype="float") request.inputs["input_image"].CopyFrom(t_proto) predict = stub.Predict(request, 1500) print predict res = predict.outputs['dense_2/Sigmoid:0'].float_val[0] print res return str(res)
def setUp(self): self._servicer = _Servicer() method_implementations = { (_GROUP, _UNARY_UNARY): utilities.unary_unary_inline(self._servicer.unary_unary), (_GROUP, _UNARY_STREAM): utilities.unary_stream_inline(self._servicer.unary_stream), (_GROUP, _STREAM_UNARY): utilities.stream_unary_inline(self._servicer.stream_unary), (_GROUP, _STREAM_STREAM): utilities.stream_stream_inline(self._servicer.stream_stream), } cardinalities = { _UNARY_UNARY: cardinality.Cardinality.UNARY_UNARY, _UNARY_STREAM: cardinality.Cardinality.UNARY_STREAM, _STREAM_UNARY: cardinality.Cardinality.STREAM_UNARY, _STREAM_STREAM: cardinality.Cardinality.STREAM_STREAM, } server_options = implementations.server_options( thread_pool_size=test_constants.POOL_SIZE) self._server = implementations.server( method_implementations, options=server_options) server_credentials = implementations.ssl_server_credentials([ ( resources.private_key(), resources.certificate_chain(), ), ]) port = self._server.add_secure_port('[::]:0', server_credentials) self._server.start() self._channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self._call_credentials = implementations.metadata_call_credentials( _metadata_plugin) channel = test_utilities.not_really_secure_channel( 'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE) stub_options = implementations.stub_options( thread_pool_size=test_constants.POOL_SIZE) self._dynamic_stub = implementations.dynamic_stub( channel, _GROUP, cardinalities, options=stub_options)
def __init__(self, host, port, timeout, user, password, creds=None, options=None): """This class creates grpc calls using python. :param username: Username for device login :param password: Password for device login :param host: The ip address for the device :param port The port for the device :param timeout: how long before the rpc call timesout :param creds: Input of the pem file :param options: TLS server name :type password: str :type username: str :type server: str :type port: int :type timeout:int :type creds: str :type options: str """ if creds != None: self._target = '%s:%d' % (host, port) self._creds = implementations.ssl_channel_credentials(creds) self._options = options channel = grpc.secure_channel(self._target, self._creds, (( 'grpc.ssl_target_name_override', self._options, ), )) self._channel = implementations.Channel(channel) else: self._host = host self._port = port self._channel = implementations.insecure_channel( self._host, self._port) self._stub = ems_grpc_pb2.beta_create_gRPCConfigOper_stub( self._channel) self._timeout = int(timeout) self._metadata = [('username', user), ('password', password)]
def __init__(self, oauth2_token='Your-Token', addr='open.didiyunapi.com:8080'): def oauth2token_credentials(context, callback): callback([('authorization', 'Bearer %s' % oauth2_token)], None) transport_creds = implementations.ssl_channel_credentials() auth_creds = implementations.metadata_call_credentials( oauth2token_credentials) channel_creds = implementations.composite_channel_credentials( transport_creds, auth_creds) self.channel = grpc.secure_channel(addr, channel_creds) self.commonStub = common_pb2_grpc.CommonStub(self.channel) self.billStub = bill_pb2_grpc.BillStub(self.channel) self.dc2Stub = dc2_pb2_grpc.Dc2Stub(self.channel) self.eipStub = eip_pb2_grpc.EipStub(self.channel) self.ebsStub = ebs_pb2_grpc.EbsStub(self.channel) self.sgStub = sg_pb2_grpc.SgStub(self.channel) self.snapStub = snap_pb2_grpc.SnapStub(self.channel) self.vpcStub = vpc_pb2_grpc.VpcStub(self.channel)
def main(): credentials = implementations.ssl_channel_credentials( root_certificates=ROOT_CERT) channel = implementations.secure_channel(MODEL_SERVER_HOST, MODEL_SERVER_PORT, credentials) stub = prediction_service_pb2.beta_create_PredictionService_stub( channel, metadata_transformer=metadata_transformer) # process the first file only uploaded_files = globals.request.files.getlist('file') data = uploaded_files[0].read() # See prediction_service.proto for gRPC request/response details. # change input type and data shapes according to your model request = predict_pb2.PredictRequest() request.model_spec.name = MODEL_NAME request.model_spec.signature_name = 'predict_images' request.inputs['images'].CopyFrom( tf.contrib.util.make_tensor_proto(data, shape=[1])) return str(stub.Predict(request, 120))
def google_grpc_channel(self, host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = GoogleCredentials.from_stream(os.path.join(app_dir,"audio_creds.json")).create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ( 'Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def init(self): if not self._initialized: if self._config['root_certificates'] != '': self._creds = implementations.ssl_channel_credentials( utils.resource_string(self._config['root_certificates']), None, None) self._channels = {} self._stubs = {} for label, server in self._servers.items(): if server['ssl']: chan = implementations.secure_channel( server['host'], server['port'], self._creds) else: chan = implementations.insecure_channel( server['host'], server['port']) st = message_pb2.beta_create_Communication_stub(chan) self._channels[label] = chan self._stubs[label] = st self._initialized = True
def create_stub(generated_create_stub, service_path, port, ssl_creds=None, channel=None, metadata_transformer=None, scopes=None): """Creates a gRPC client stub. Args: generated_create_stub: The generated gRPC method to create a stub. service_path: The domain name of the API remote host. port: The port on which to connect to the remote host. ssl_creds: A ClientCredentials object for use with an SSL-enabled Channel. If none, credentials are pulled from a default location. channel: A Channel object through which to make calls. If none, a secure channel is constructed. metadata_transformer: A function that transforms the metadata for requests, e.g., to give OAuth credentials. scopes: The OAuth scopes for this service. This parameter is ignored if a custom metadata_transformer is supplied. Returns: A gRPC client stub. """ if channel is None: if ssl_creds is None: ssl_creds = implementations.ssl_channel_credentials( None, None, None) if metadata_transformer is None: if scopes is None: scopes = [] metadata_transformer = auth.make_auth_func(scopes) channel_creds = _make_channel_creds(metadata_transformer, ssl_creds) channel = implementations.secure_channel(service_path, port, channel_creds) return generated_create_stub(channel)
''' import grpc import telemetry_pb2 import ems_grpc_pb2 from google.protobuf.json_format import MessageToJson from grpc.beta import implementations host = '10.75.58.60' port = 57400 options = 'ems.cisco.com' ca_cert = 'ems.pem' # credential file scp from devices creds = open(ca_cert).read() target = '%s:%d' % (host, port) creds = implementations.ssl_channel_credentials(creds.encode( ('utf-8'))) # args with byte type channel = grpc.secure_channel(target, creds, (( 'grpc.ssl_target_name_override', options, ), )) channel = implementations.Channel(channel) stub = ems_grpc_pb2.beta_create_gRPCConfigOper_stub(channel) sub_id = 'test_sub' # Telemetry MDT subscribtion sub_args = ems_grpc_pb2.CreateSubsArgs(ReqId=1, encode=3, subidstr=sub_id) timeout = float(100000) metadata = [('username', 'cisco'), ('password', 'cisco')] stream = stub.CreateSubs(sub_args, timeout=timeout, metadata=metadata)
from grpc.beta import implementations from gen import query_pb2 from os import path _TIMEOUT_SECONDS = 30 DIR=path.dirname(path.dirname(path.abspath(__file__))) creds = implementations.ssl_channel_credentials( open(path.join(DIR, "ssl/domain.crt")).read(), None, None ) channel = implementations.secure_channel('localhost', 50051, creds) stub = query_pb2.beta_create_SearchService_stub(channel) search_request = query_pb2.SearchRequest() search_request.query = "search" search_request.page_number = 1 search_request.result_per_page = 30 print(stub.Search(search_request, _TIMEOUT_SECONDS)) hello_request = query_pb2.HelloRequest() hello_request.name = "python" print(stub.SayHello(hello_request, _TIMEOUT_SECONDS))
def main(): deployment_url = "https://mlftrial-deployment-api.cfapps.eu10.hana.ondemand.com" + "/api/v2/modelServers" querystring = {"modelName": MODEL_NAME} headers = { 'Authorization': get_access_token(), 'Cache-Control': "no-cache" } response = requests.request("GET", deployment_url, headers=headers, params=querystring) #print(response.text) model_info = json.loads(response.text) latest_version = [0, 0] for index, model in enumerate(model_info["modelServers"]): if int(model["specs"]["models"][0] ["modelVersion"]) > latest_version[0]: latest_version = [ int(model["specs"]["models"][0]["modelVersion"]), index ] model_host = model_info["modelServers"][latest_version[1]]["endpoints"][0] credentials = implementations.ssl_channel_credentials( root_certificates=str(model_host["caCrt"])) channel = implementations.secure_channel(str(model_host["host"]), int(model_host["port"]), credentials) stub = prediction_service_pb2.beta_create_PredictionService_stub( channel) #, metadata_transformer=metadata_transformer) #uploaded_files = globals.request.files.getlist('file') #data = skimage.io.imread(uploaded_files[0]) feature_dict = { 'DIFGRIRV': tf.train.Feature(int64_list=tf.train.Int64List(value=[-38100])), 'NODLIR': tf.train.Feature(int64_list=tf.train.Int64List(value=[90])), 'VSTATU': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"1"])), 'NODLGR': tf.train.Feature(int64_list=tf.train.Int64List(value=[0])), 'DIFGRIRD': tf.train.Feature(int64_list=tf.train.Int64List(value=[-80])), 'VPATD': tf.train.Feature(int64_list=tf.train.Int64List(value=[30])), 'WERKS': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"ML01"])), 'EKORG': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"1"])), 'TOTGRQTY': tf.train.Feature(int64_list=tf.train.Int64List(value=[0])), 'SCENARIO': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"3"])), 'TOTIRQTY': tf.train.Feature(int64_list=tf.train.Int64List(value=[80])), 'KTOKK': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"1"])), 'EKGRP': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"A"])), } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) data = example.SerializeToString() # data = {"DIFGRIRV": [-38100],"NODLIR": [90],"VSTATU": ["1"],"NODLGR": [0],"DIFGRIRD": [-80],"VPATD": [30], # "WERKS": ["ML01"], # "EKORG": ["1"],"TOTGRQTY": [0],"SCENARIO": ["3"],"TOTIRQTY": [80],"KTOKK": ["1"],"EKGRP": ["A"]} req = predict_pb2.PredictRequest() req.model_spec.name = MODEL_NAME #req.model_spec.signature_name = 'predict_images' req.inputs["inputs"].CopyFrom( tf.contrib.util.make_tensor_proto(data, shape=[1, 1])) res = str(stub.Predict(req, 150)) # res = str(stub.Predict(req, 150)).split('}')[3].split('\n') print(res) res.pop(11) res.pop(0) out_val = 0.0 out = 0 for i, estimate in enumerate(res): if float(estimate[14:]) > out_val: out_val = float(estimate[14:]) out = i return "Result: " + str(out)
def test_runtime_provided_root_certificates(self): channel_credentials = implementations.ssl_channel_credentials() self.assertIsInstance(channel_credentials, implementations.ChannelCredentials)
def secure_channel(): with open(FLAGS.certificate) as f: trusted_certs = f.read() credentials = implementations.ssl_channel_credentials( trusted_certs, None, None) return implementations.secure_channel(FLAGS.address, FLAGS.port, credentials)
def test_application_provided_root_certificates(self): channel_credentials = implementations.ssl_channel_credentials( resources.test_root_certificates()) self.assertIsInstance(channel_credentials, implementations.ChannelCredentials)
def create_pubsub_stub(host=PUBSUB_ENDPOINT, port=SSL_PORT): """Creates a secure pubsub channel.""" ssl_creds = implementations.ssl_channel_credentials(None, None, None) channel_creds = make_channel_creds(ssl_creds, auth_func) channel = implementations.secure_channel(host, port, channel_creds) return pubsub_pb2.beta_create_Publisher_stub(channel)
def create_sub_stub(host=PUBSUB_ENDPOINT, port=SSL_PORT): """Creates a secure pubsub channel.""" ssl_creds = implementations.ssl_channel_credentials(None, None, None) channel_creds = make_channel_creds(ssl_creds, auth_func) channel = implementations.secure_channel(host, port, channel_creds) return pubsub_pb2.beta_create_Subscriber_stub(channel)