Esempio n. 1
0
def _stub(args):
    target = '{}:{}'.format(args.server_host, args.server_port)
    if args.test_case == 'oauth2_auth_token':
        google_credentials = _application_default_credentials()
        scoped_credentials = google_credentials.create_scoped(
            [args.oauth_scope])
        access_token = scoped_credentials.get_access_token().access_token
        call_credentials = grpc.access_token_call_credentials(access_token)
    elif args.test_case == 'compute_engine_creds':
        google_credentials = _application_default_credentials()
        scoped_credentials = google_credentials.create_scoped(
            [args.oauth_scope])
        # TODO(https://github.com/grpc/grpc/issues/6799): Eliminate this last
        # remaining use of the Beta API.
        call_credentials = implementations.google_call_credentials(
            scoped_credentials)
    elif args.test_case == 'jwt_token_creds':
        google_credentials = _application_default_credentials()
        # TODO(https://github.com/grpc/grpc/issues/6799): Eliminate this last
        # remaining use of the Beta API.
        call_credentials = implementations.google_call_credentials(
            google_credentials)
    else:
        call_credentials = 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_credentials = grpc.ssl_channel_credentials(root_certificates)
        if call_credentials is not None:
            channel_credentials = grpc.composite_channel_credentials(
                channel_credentials, call_credentials)

        channel = grpc.secure_channel(target, channel_credentials, ((
            'grpc.ssl_target_name_override',
            args.server_host_override,
        ), ))
    else:
        channel = grpc.insecure_channel(target)
    if args.test_case == "unimplemented_service":
        return test_pb2.UnimplementedServiceStub(channel)
    else:
        return test_pb2.TestServiceStub(channel)
Esempio n. 2
0
def _stub(args):
    target = '{}:{}'.format(args.server_host, args.server_port)
    if args.test_case == 'oauth2_auth_token':
        google_credentials, unused_project_id = google_auth.default(
            scopes=[args.oauth_scope])
        google_credentials.refresh(google_auth.transport.requests.Request())
        call_credentials = grpc.access_token_call_credentials(
            google_credentials.token)
    elif args.test_case == 'compute_engine_creds':
        google_credentials, unused_project_id = google_auth.default(
            scopes=[args.oauth_scope])
        call_credentials = grpc.metadata_call_credentials(
            google_auth.transport.grpc.AuthMetadataPlugin(
                credentials=google_credentials,
                request=google_auth.transport.requests.Request()))
    elif args.test_case == 'jwt_token_creds':
        google_credentials = google_auth_jwt.OnDemandCredentials.from_service_account_file(
            os.environ[google_auth.environment_vars.CREDENTIALS])
        call_credentials = grpc.metadata_call_credentials(
            google_auth.transport.grpc.AuthMetadataPlugin(
                credentials=google_credentials, request=None))
    else:
        call_credentials = 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_credentials = grpc.ssl_channel_credentials(root_certificates)
        if call_credentials is not None:
            channel_credentials = grpc.composite_channel_credentials(
                channel_credentials, call_credentials)

        channel = grpc.secure_channel(target, channel_credentials, ((
            'grpc.ssl_target_name_override',
            args.server_host_override,
        ), ))
    else:
        channel = grpc.insecure_channel(target)
    if args.test_case == "unimplemented_service":
        return test_pb2.UnimplementedServiceStub(channel)
    else:
        return test_pb2.TestServiceStub(channel)