Example #1
0
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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
Example #7
0
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)
Example #8
0
 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 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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
    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)
Example #13
0
    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)
Example #14
0
def _make_channel_creds(auth_func, ssl_creds):
    """Converts the auth func into the composite creds expected by grpc."""
    grpc_auth_func = _make_grpc_auth_func(auth_func)
    call_creds = implementations.metadata_call_credentials(grpc_auth_func)
    return implementations.composite_channel_credentials(ssl_creds, call_creds)
def make_channel_creds(ssl_creds, auth_func=auth_func):
    """Returns a channel with credentials callback."""
    call_creds = implementations.metadata_call_credentials(
        lambda ctx, callback: callback(auth_func(), None))
    return implementations.composite_channel_credentials(ssl_creds, call_creds)
Example #16
0
def make_channel_creds(ssl_creds, auth_func=auth_func):
    """Returns a channel with credentials callback."""
    call_creds = implementations.metadata_call_credentials(
        lambda ctx, callback: callback(auth_func(), None))
    return implementations.composite_channel_credentials(ssl_creds, call_creds)
Example #17
0
def _make_channel_creds(auth_func, ssl_creds):
    """Converts the auth func into the composite creds expected by grpc."""
    grpc_auth_func = _make_grpc_auth_func(auth_func)
    call_creds = implementations.metadata_call_credentials(grpc_auth_func)
    return implementations.composite_channel_credentials(ssl_creds, call_creds)