Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,8 @@ def connect(

:type database_role: str
:param database_role: (Optional) The database role to connect as when using
fine-grained access controls.
fine-grained access controls. If ``pool`` is also provided with an
explicit ``database_role``, the pool's ``database_role`` takes precedence.

:type data_boost_enabled: bool
:param data_boost_enabled: (Optional) Whether to enable DataBoost for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
_merge_query_options,
_metadata_with_leader_aware_routing,
_metadata_with_prefix,
_resolve_database_role,
)
from google.cloud.spanner_v1.keyset import KeySet
from google.cloud.spanner_v1.merged_result_set import MergedResultSet
Expand Down Expand Up @@ -174,7 +175,9 @@ class Database(object):
:param database_dialect:
(Optional) database dialect for the database
:type database_role: str or None
:param database_role: (Optional) user-assigned database_role for the session.
:param database_role: (Optional) user-assigned database_role for sessions
created for this database. Overridden if an explicit ``database_role``
is set on ``pool`` or on an individual session.
:type enable_drop_protection: boolean
:param enable_drop_protection: (Optional) Represents whether the database
has drop protection enabled or not.
Expand Down Expand Up @@ -445,9 +448,13 @@ def default_schema_name(self):

@property
def database_role(self):
"""User-assigned database_role for sessions created by the pool.
:rtype: str
:returns: a str with the name of the database role.
"""User-assigned database_role for sessions created for this database.

Overridden if an explicit ``database_role`` is set on the session pool
or on an individual session.

:rtype: str or None
:returns: a str with the name of the database role, or None if unset.
"""
return self._database_role

Expand Down Expand Up @@ -1037,13 +1044,18 @@ def session(self, labels=None, database_role=None):

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
Defaults to the pool's explicitly configured
``database_role`` if set, then the database's
``database_role``.

:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: a session bound to this database.
"""
# If role is specified in param, then that role is used
# instead.
role = database_role or self._database_role
role = _resolve_database_role(
session_role=database_role,
pool=self._pool,
database=self,
)
is_multiplexed = False
if self.sessions_manager._use_multiplexed(
transaction_type=TransactionType.READ_ONLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from google.cloud.aio._cross_sync import CrossSync
from google.cloud.spanner_v1._async.session import Session
from google.cloud.spanner_v1._helpers import _resolve_database_role
from google.cloud.spanner_v1._opentelemetry_tracing import (
add_span_event,
get_current_span,
Expand Down Expand Up @@ -153,11 +154,19 @@ async def _get_multiplexed_session(self) -> Session:
async def _build_multiplexed_session(self) -> Session:
"""Builds and returns a new multiplexed session for the database session manager.

Uses the pool's explicitly configured ``database_role`` if set, falling
back to the database's ``database_role``, matching non-multiplexed pooled
sessions.

:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: a new multiplexed session."""
database_role = _resolve_database_role(
pool=self._pool,
database=self._database,
)
session = Session(
database=self._database,
database_role=self._database.database_role,
database_role=database_role,
is_multiplexed=True,
)
await session.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from google.cloud.spanner_v1._helpers import (
_metadata_with_leader_aware_routing,
_metadata_with_prefix,
_resolve_database_role,
_resource_info_from_database,
)
from google.cloud.spanner_v1._opentelemetry_tracing import (
Expand Down Expand Up @@ -91,15 +92,20 @@ class AbstractSessionPool(object):
by the pool.

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
:param database_role: (Optional) user-assigned database_role for sessions
created for this pool. When explicitly set, takes precedence over the
bound database's ``database_role`` for both pooled and multiplexed
sessions.
"""

_database = None
_explicit_database_role = None

def __init__(self, labels=None, database_role=None):
if labels is None:
labels = {}
self._labels = labels
self._explicit_database_role = database_role
self._database_role = database_role

@property
Expand All @@ -118,10 +124,13 @@ def labels(self):

@property
def database_role(self):
"""User-assigned database_role for sessions created by the pool.
"""Database role for sessions created by the pool.

:rtype: str
:returns: database_role assigned by the user
Returns the role explicitly configured on the pool, or the role
inherited from the bound database via :meth:`bind`.

:rtype: str or None
:returns: database_role for the pool, or None if unset
"""
return self._database_role

Expand Down Expand Up @@ -182,8 +191,7 @@ def _new_session(self):
:rtype: :class:`~google.cloud.spanner_v1.session.Session`
:returns: new session instance.
"""

role = self.database_role or self._database.database_role
role = _resolve_database_role(pool=self, database=self._database)
return Session(database=self._database, labels=self.labels, database_role=role)

def session(self, **kwargs):
Expand Down Expand Up @@ -248,7 +256,10 @@ class FixedSizePool(AbstractSessionPool):
by the pool.

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
:param database_role: (Optional) user-assigned database_role for sessions
created for this pool. When explicitly set, takes precedence over the
bound database's ``database_role`` for both pooled and multiplexed
sessions.
"""

DEFAULT_SIZE = 10
Expand Down Expand Up @@ -279,7 +290,7 @@ async def bind(self, database):
when needed.
"""
self._database = database
self._database_role = self._database_role or self._database.database_role
self._database_role = _resolve_database_role(pool=self, database=self._database)
await self._fill_pool()

@CrossSync.convert
Expand Down Expand Up @@ -308,7 +319,7 @@ async def _fill_pool(self):
metadata = _metadata_with_prefix(database.name)
if database._route_to_leader_enabled:
metadata.append(_metadata_with_leader_aware_routing(True))
self._database_role = self._database_role or self._database.database_role
self._database_role = _resolve_database_role(pool=self, database=self._database)
if requested_session_count > 0:
add_span_event(
span,
Expand Down Expand Up @@ -516,7 +527,10 @@ class BurstyPool(AbstractSessionPool):
by the pool.

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
:param database_role: (Optional) user-assigned database_role for sessions
created for this pool. When explicitly set, takes precedence over the
bound database's ``database_role`` for both pooled and multiplexed
sessions.
"""

def __init__(self, target_size=10, labels=None, database_role=None):
Expand All @@ -534,7 +548,7 @@ async def bind(self, database):
when needed.
"""
self._database = database
self._database_role = self._database_role or self._database.database_role
self._database_role = _resolve_database_role(pool=self, database=self._database)

@CrossSync.convert
async def get(self):
Expand Down Expand Up @@ -649,7 +663,10 @@ class PingingPool(FixedSizePool):
by the pool.

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
:param database_role: (Optional) user-assigned database_role for sessions
created for this pool. When explicitly set, takes precedence over the
bound database's ``database_role`` for both pooled and multiplexed
sessions.
"""

def __init__(
Expand Down Expand Up @@ -684,7 +701,7 @@ async def bind(self, database):
metadata = _metadata_with_prefix(database.name)
if database._route_to_leader_enabled:
metadata.append(_metadata_with_leader_aware_routing(True))
self._database_role = self._database_role or self._database.database_role
self._database_role = _resolve_database_role(pool=self, database=self._database)

request = BatchCreateSessionsRequest(
database=database.name,
Expand Down Expand Up @@ -904,7 +921,10 @@ class TransactionPingingPool(PingingPool):
by the pool.

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
:param database_role: (Optional) user-assigned database_role for sessions
created for this pool. When explicitly set, takes precedence over the
bound database's ``database_role`` for both pooled and multiplexed
sessions.
"""

def __init__(
Expand Down Expand Up @@ -941,7 +961,7 @@ async def bind(self, database):
when needed.
"""
await super(TransactionPingingPool, self).bind(database)
self._database_role = self._database_role or self._database.database_role
self._database_role = _resolve_database_role(pool=self, database=self._database)
# await self.begin_pending_transactions() # This is now async, so cannot be called here.

@CrossSync.convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
_get_retry_delay,
_metadata_with_leader_aware_routing,
_metadata_with_prefix,
_resolve_database_role,
_resource_info_from_database,
)
from google.cloud.spanner_v1._opentelemetry_tracing import (
Expand Down Expand Up @@ -75,6 +76,7 @@ class Session(object):

:type database_role: str
:param database_role: (Optional) user-assigned database_role for the session.
When set, overrides the database's ``database_role`` during :meth:`create`.

:type is_multiplexed: bool
:param is_multiplexed: (Optional) whether this session is a multiplexed session.
Expand Down Expand Up @@ -124,10 +126,13 @@ def last_use_time(self):

@property
def database_role(self):
"""User-assigned database-role for the session.
"""Effective database role for the session.

:rtype: str
:returns: the database role str (None if no database role were assigned)."""
Initialized from ``database_role`` and updated to the database's
``database_role`` during :meth:`create` if inherited.

:rtype: str or None
:returns: the database role str (None if no database role was assigned)."""
return self._database_role

@property
Expand Down Expand Up @@ -164,6 +169,9 @@ def name(self):
async def create(self):
"""Create this session, bound to its database.

Uses this session's :attr:`database_role` as ``creator_role`` if set,
falling back to the database's ``database_role``.

See
https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.CreateSession

Expand All @@ -185,8 +193,12 @@ async def create(self):
)

create_session_request = CreateSessionRequest(database=database.name)
if database.database_role is not None:
create_session_request.session.creator_role = database.database_role
database_role = _resolve_database_role(
session_role=self._database_role,
database=database,
)
if database_role:
create_session_request.session.creator_role = database_role

if self._labels:
create_session_request.session.labels = self._labels
Expand Down Expand Up @@ -221,6 +233,8 @@ async def create(self):
metadata=call_metadata,
)
self._session_id = session_pb.name.split("/")[-1]
if database_role:
self._database_role = database_role

@CrossSync.convert
async def exists(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1552,3 +1552,39 @@ def _drain_stream(iterator):
while consuming trailing gRPC metadata so the stream terminates cleanly with status OK.
"""
_GLOBAL_STREAM_DRAINER.drain(iterator)


def _resolve_database_role(session_role=None, pool=None, database=None):
"""Resolve the effective FGAC database role with precedence:
session > explicit pool > database.

When ``pool`` is an :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`
instance, only a role explicitly configured on the pool (not a role inherited
from a previously bound database via ``bind()``) takes precedence over
``database.database_role``.

:type session_role: str or None
:param session_role: (Optional) Role explicitly requested for a session.

:type pool: :class:`~google.cloud.spanner_v1.pool.AbstractSessionPool` or None
:param pool: (Optional) Session pool associated with the operation.

:type database: :class:`~google.cloud.spanner_v1.database.Database` or None
:param database: (Optional) Database associated with the operation.

:rtype: str or None
:returns: The resolved database role, or ``None`` if unset.
"""
if session_role:
return session_role
pool_role = None
if pool is not None:
if hasattr(type(pool), "_explicit_database_role"):
pool_role = pool._explicit_database_role if pool._database_role else None
else:
pool_role = getattr(pool, "database_role", None)
if pool_role:
return pool_role
if database is not None:
return getattr(database, "database_role", None) or None
return None
Loading
Loading