def test_create_channel_without_grpc_gcp(grpc_secure_channel): target = "example.com:443" scopes = ["test_scope"] credentials = mock.create_autospec(google.auth.credentials.Scoped, instance=True) credentials.requires_scopes = True grpc_helpers.create_channel(target, credentials=credentials, scopes=scopes) grpc_secure_channel.assert_called() credentials.with_scopes.assert_called_once_with(scopes)
def test_create_channel_explicit_scoped(unused_secure_authorized_channel): scopes = ['1', '2'] credentials = mock.create_autospec( google.auth.credentials.Scoped, instance=True) credentials.requires_scopes = True grpc_helpers.create_channel( mock.sentinel.target, credentials=credentials, scopes=scopes) credentials.with_scopes.assert_called_once_with(scopes)
def __init__(self, batch_settings=(), **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST") ) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "channel" not in kwargs: kwargs["channel"] = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=publisher_client.PublisherClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, }.items(), ) # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = publisher_client.PublisherClient(**kwargs) self.batch_settings = types.BatchSettings(*batch_settings) # The batches on the publisher client are responsible for holding # messages. One batch exists for each topic. self._batch_lock = self._batch_class.make_lock() self._batches = {}
def __init__(self, policy_class=thread.Policy, **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get('PUBSUB_EMULATOR_HOST'): kwargs['channel'] = grpc.insecure_channel( target=os.environ.get('PUBSUB_EMULATOR_HOST'), ) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if 'channel' not in kwargs: kwargs['channel'] = grpc_helpers.create_channel( credentials=kwargs.pop('credentials', None), target=self.target, scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES, options={ 'grpc.max_send_message_length': -1, 'grpc.max_receive_message_length': -1, 'grpc.keepalive_time_ms': 30000, }.items(), ) # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = subscriber_client.SubscriberClient(**kwargs) # The subcription class is responsible to retrieving and dispatching # messages. self._policy_class = policy_class
def test_create_channel_implicit_with_ssl_creds( grpc_secure_channel, default, composite_creds_call ): target = "example.com:443" ssl_creds = grpc.ssl_channel_credentials() grpc_helpers.create_channel(target, ssl_credentials=ssl_creds) default.assert_called_once_with(scopes=None) composite_creds_call.assert_called_once_with(ssl_creds, mock.ANY) composite_creds = composite_creds_call.return_value if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def __init__(self, **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST") ) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "transport" not in kwargs: channel = kwargs.pop("channel", None) if channel is None: channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, "grpc.keepalive_time_ms": 30000, }.items(), ) # cannot pass both 'channel' and 'credentials' kwargs.pop("credentials", None) transport = subscriber_grpc_transport.SubscriberGrpcTransport( channel=channel ) kwargs["transport"] = transport # Add the metrics headers, and instantiate the underlying GAPIC # client. self._api = subscriber_client.SubscriberClient(**kwargs)
def test_create_channel_implicit_with_scopes( secure_authorized_channel, default): target = 'example.com:443' channel = grpc_helpers.create_channel(target, scopes=['one', 'two']) assert channel is secure_authorized_channel.return_value default.assert_called_once_with(scopes=['one', 'two'])
def test_create_channel_explicit(secure_authorized_channel): target = 'example.com:443' channel = grpc_helpers.create_channel( target, credentials=mock.sentinel.credentials) assert channel is secure_authorized_channel.return_value secure_authorized_channel.assert_called_once_with( mock.sentinel.credentials, mock.ANY, target)
def test_create_channel_implicit(secure_authorized_channel, default): target = 'example.com:443' channel = grpc_helpers.create_channel(target) assert channel is secure_authorized_channel.return_value default.assert_called_once_with(scopes=None) secure_authorized_channel.assert_called_once_with( mock.sentinel.credentials, mock.ANY, target)
def test_create_channel_implicit(grpc_secure_channel, default, composite_creds_call): target = "example.com:443" composite_creds = composite_creds_call.return_value channel = grpc_helpers.create_channel(target) assert channel is grpc_secure_channel.return_value default.assert_called_once_with(scopes=None) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def test_create_channel_explicit(grpc_secure_channel, auth_creds, composite_creds_call): target = "example.com:443" composite_creds = composite_creds_call.return_value channel = grpc_helpers.create_channel(target, credentials=mock.sentinel.credentials) auth_creds.assert_called_once_with(mock.sentinel.credentials, None) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def test_create_channel_explicit_scoped(grpc_secure_channel, composite_creds_call): target = "example.com:443" scopes = ["1", "2"] composite_creds = composite_creds_call.return_value credentials = mock.create_autospec(google.auth.credentials.Scoped, instance=True) credentials.requires_scopes = True channel = grpc_helpers.create_channel( target, credentials=credentials, scopes=scopes ) credentials.with_scopes.assert_called_once_with(scopes) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def test_create_channel_implicit_with_default_scopes(grpc_secure_channel, default, composite_creds_call): target = "example.com:443" composite_creds = composite_creds_call.return_value channel = grpc_helpers.create_channel(target, default_scopes=["three", "four"]) assert channel is grpc_secure_channel.return_value default.assert_called_once_with(scopes=None, default_scopes=["three", "four"]) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def create_channel( cls, host: str = "bigquerydatatransfer.googleapis.com", credentials: credentials.Credentials = None, credentials_file: str = None, scopes: Optional[Sequence[str]] = None, quota_project_id: Optional[str] = None, **kwargs, ) -> grpc.Channel: """Create and return a gRPC channel object. Args: address (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. credentials_file (Optional[str]): A file with credentials that can be loaded with :func:`google.auth.load_credentials_from_file`. This argument is mutually exclusive with credentials. scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. quota_project_id (Optional[str]): An optional project to use for billing and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: grpc.Channel: A gRPC channel object. Raises: google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ scopes = scopes or cls.AUTH_SCOPES return grpc_helpers.create_channel( host, credentials=credentials, credentials_file=credentials_file, scopes=scopes, quota_project_id=quota_project_id, **kwargs, )
def test_create_channel_implicit(grpc_secure_channel, default, composite_creds_call): target = "example.com:443" composite_creds = composite_creds_call.return_value channel = grpc_helpers.create_channel(target) assert channel is grpc_secure_channel.return_value # TODO: remove this if/else once google-auth >= 1.25.0 is required if grpc_helpers._GOOGLE_AUTH_HAS_DEFAULT_SCOPES_AND_DEFAULT_HOST: default.assert_called_once_with(scopes=None, default_scopes=None) else: default.assert_called_once_with(scopes=None) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def test_create_channel_with_credentials_file(load_credentials_from_file, grpc_secure_channel, composite_creds_call): target = "example.com:443" credentials_file = "/path/to/credentials/file.json" composite_creds = composite_creds_call.return_value channel = grpc_helpers.create_channel(target, credentials_file=credentials_file) google.auth.load_credentials_from_file.assert_called_once_with( credentials_file, scopes=None, default_scopes=None) assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def test_create_channel_explicit_with_quota_project(grpc_secure_channel, composite_creds_call): target = "example.com:443" composite_creds = composite_creds_call.return_value credentials = mock.create_autospec( google.auth.credentials.CredentialsWithQuotaProject, instance=True) channel = grpc_helpers.create_channel(target, credentials=credentials, quota_project_id="project-foo") credentials.with_quota_project.assert_called_once_with("project-foo") assert channel is grpc_secure_channel.return_value if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def __init__(self, **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST")) # api_endpoint wont be applied if 'transport' is passed in. client_options = kwargs.pop("client_options", None) if (client_options and "api_endpoint" in client_options and isinstance( client_options["api_endpoint"], six.string_types)): self._target = client_options["api_endpoint"] else: self._target = subscriber_client.SubscriberClient.SERVICE_ADDRESS # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "transport" not in kwargs: channel = kwargs.pop("channel", None) if channel is None: channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, "grpc.keepalive_time_ms": 30000, }.items(), ) # cannot pass both 'channel' and 'credentials' kwargs.pop("credentials", None) transport = subscriber_grpc_transport.SubscriberGrpcTransport( channel=channel) kwargs["transport"] = transport # Add the metrics headers, and instantiate the underlying GAPIC # client. self._api = subscriber_client.SubscriberClient(**kwargs)
def create_channel(cls, host: str = "gameservices.googleapis.com", credentials: credentials.Credentials = None, **kwargs) -> grpc.Channel: """Create and return a gRPC channel object. Args: address (Optionsl[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: grpc.Channel: A gRPC channel object. """ return grpc_helpers.create_channel(host, credentials=credentials, scopes=cls.AUTH_SCOPES, **kwargs)
def test_create_channel_explicit_scoped(grpc_secure_channel, composite_creds_call): target = 'example.com:443' scopes = ['1', '2'] composite_creds = composite_creds_call.return_value credentials = mock.create_autospec(google.auth.credentials.Scoped, instance=True) credentials.requires_scopes = True channel = grpc_helpers.create_channel(target, credentials=credentials, scopes=scopes) credentials.with_scopes.assert_called_once_with(scopes) assert channel is grpc_secure_channel.return_value if (grpc_helpers.HAS_GRPC_GCP): grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def __init__(self, batch_settings=(), **kwargs): # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST")) # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "transport" not in kwargs: channel = kwargs.pop("channel", None) if channel is None: channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=publisher_client.PublisherClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, }.items(), ) # cannot pass both 'channel' and 'credentials' kwargs.pop("credentials", None) transport = publisher_grpc_transport.PublisherGrpcTransport( channel=channel) kwargs["transport"] = transport # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = publisher_client.PublisherClient(**kwargs) self.batch_settings = types.BatchSettings(*batch_settings) # The batches on the publisher client are responsible for holding # messages. One batch exists for each topic. self._batch_lock = self._batch_class.make_lock() self._batches = {}
def test_create_channel_implicit_with_default_host(grpc_secure_channel, default, composite_creds_call, request, auth_metadata_plugin): target = "example.com:443" default_host = "example.com" composite_creds = composite_creds_call.return_value channel = grpc_helpers.create_channel(target, default_host=default_host) assert channel is grpc_secure_channel.return_value default.assert_called_once_with(scopes=None, default_scopes=None) auth_metadata_plugin.assert_called_once_with(mock.sentinel.credentials, mock.sentinel.Request, default_host=default_host) if grpc_helpers.HAS_GRPC_GCP: grpc_secure_channel.assert_called_once_with(target, composite_creds, None) else: grpc_secure_channel.assert_called_once_with(target, composite_creds)
def __init__( self, *, host: str = "mediatranslation.googleapis.com", credentials: credentials.Credentials = None, channel: grpc.Channel = None, api_mtls_endpoint: str = None, client_cert_source: Callable[[], Tuple[bytes, bytes]] = None) -> None: """Instantiate the transport. Args: host (Optional[str]): The hostname to connect to. credentials (Optional[google.auth.credentials.Credentials]): The authorization credentials to attach to requests. These credentials identify the application to the service; if none are specified, the client will attempt to ascertain the credentials from the environment. This argument is ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A callback to provide client SSL certificate bytes and private key bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` is None. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport creation failed for any reason. """ if channel: # Sanity check: Ensure that channel and credentials are not both # provided. credentials = False # If a channel was explicitly provided, set it. self._grpc_channel = channel elif api_mtls_endpoint: host = (api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443") # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: cert, key = client_cert_source() ssl_credentials = grpc.ssl_channel_credentials( certificate_chain=cert, private_key=key) else: ssl_credentials = SslCredentials().ssl_credentials # create a new channel. The provided one is ignored. self._grpc_channel = grpc_helpers.create_channel( host, credentials=credentials, ssl_credentials=ssl_credentials, scopes=self.AUTH_SCOPES, ) # Run the base constructor. super().__init__(host=host, credentials=credentials) self._stubs = {} # type: Dict[str, Callable]
def __init__(self, batch_settings=(), publisher_options=(), **kwargs): assert ( type(batch_settings) is types.BatchSettings or len(batch_settings) == 0 ), "batch_settings must be of type BatchSettings or an empty tuple." assert ( type(publisher_options) is types.PublisherOptions or len(publisher_options) == 0 ), "publisher_options must be of type PublisherOptions or an empty tuple." # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST")) # The GAPIC client has mTLS logic to determine the api endpoint and the # ssl credentials to use. Here we create a GAPIC client to help compute the # api endpoint and ssl credentials. The api endpoint will be used to set # `self._target`, and ssl credentials will be passed to # `grpc_helpers.create_channel` to establish a mTLS channel (if ssl # credentials is not None). client_options = kwargs.get("client_options", None) credentials = kwargs.get("credentials", None) client_for_mtls_info = publisher_client.PublisherClient( credentials=credentials, client_options=client_options) self._target = client_for_mtls_info._transport._host # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "transport" not in kwargs: channel = kwargs.pop("channel", None) if channel is None: channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, ssl_credentials=client_for_mtls_info._transport. _ssl_channel_credentials, scopes=publisher_client.PublisherClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, }.items(), ) # cannot pass both 'channel' and 'credentials' kwargs.pop("credentials", None) transport = publisher_grpc_transport.PublisherGrpcTransport( channel=channel) kwargs["transport"] = transport # For a transient failure, retry publishing the message infinitely. self.publisher_options = types.PublisherOptions(*publisher_options) self._enable_message_ordering = self.publisher_options[0] # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = publisher_client.PublisherClient(**kwargs) self._batch_class = thread.Batch self.batch_settings = types.BatchSettings(*batch_settings) # The batches on the publisher client are responsible for holding # messages. One batch exists for each topic. self._batch_lock = self._batch_class.make_lock() # (topic, ordering_key) => sequencers object self._sequencers = {} self._is_stopped = False # Thread created to commit all sequencers after a timeout. self._commit_thread = None # The object controlling the message publishing flow self._flow_controller = FlowController( self.publisher_options.flow_control)
def __init__(self, batch_settings=(), publisher_options=(), **kwargs): assert ( type(batch_settings) is types.BatchSettings or len(batch_settings) == 0 ), "batch_settings must be of type BatchSettings or an empty tuple." assert ( type(publisher_options) is types.PublisherOptions or len(publisher_options) == 0 ), "publisher_options must be of type PublisherOptions or an empty tuple." # Sanity check: Is our goal to use the emulator? # If so, create a grpc insecure channel with the emulator host # as the target. if os.environ.get("PUBSUB_EMULATOR_HOST"): kwargs["channel"] = grpc.insecure_channel( target=os.environ.get("PUBSUB_EMULATOR_HOST")) client_options = kwargs.pop("client_options", None) if (client_options and "api_endpoint" in client_options and isinstance( client_options["api_endpoint"], six.string_types)): self._target = client_options["api_endpoint"] else: self._target = publisher_client.PublisherClient.SERVICE_ADDRESS # Use a custom channel. # We need this in order to set appropriate default message size and # keepalive options. if "transport" not in kwargs: channel = kwargs.pop("channel", None) if channel is None: channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, scopes=publisher_client.PublisherClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, "grpc.max_receive_message_length": -1, }.items(), ) # cannot pass both 'channel' and 'credentials' kwargs.pop("credentials", None) transport = publisher_grpc_transport.PublisherGrpcTransport( channel=channel) kwargs["transport"] = transport # For a transient failure, retry publishing the message infinitely. self.publisher_options = types.PublisherOptions(*publisher_options) self._enable_message_ordering = self.publisher_options[0] if self._enable_message_ordering: # Set retry timeout to "infinite" when message ordering is enabled. # Note that this then also impacts messages added with an empty ordering # key. client_config = _set_nested_value( kwargs.pop("client_config", {}), 2**32, [ "interfaces", "google.pubsub.v1.Publisher", "retry_params", "messaging", "total_timeout_millis", ], ) kwargs["client_config"] = client_config # Add the metrics headers, and instantiate the underlying GAPIC # client. self.api = publisher_client.PublisherClient(**kwargs) self._batch_class = thread.Batch self.batch_settings = types.BatchSettings(*batch_settings) # The batches on the publisher client are responsible for holding # messages. One batch exists for each topic. self._batch_lock = self._batch_class.make_lock() # (topic, ordering_key) => sequencers object self._sequencers = {} self._is_stopped = False # Thread created to commit all sequencers after a timeout. self._commit_thread = None # The object controlling the message publishing flow self._flow_controller = FlowController( self.publisher_options.flow_control)