_plugin_wrapping.py
Go to the documentation of this file.
1 # Copyright 2015 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import collections
16 import logging
17 import threading
18 
19 import grpc
20 from grpc import _common
21 from grpc._cython import cygrpc
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
27  collections.namedtuple('AuthMetadataContext', (
28  'service_url',
29  'method_name',
31  pass
32 
33 
34 class _CallbackState(object):
35 
36  def __init__(self):
37  self.lock = threading.Lock()
38  self.called = False
39  self.exception = None
40 
41 
43 
44  def __init__(self, state, callback):
45  self._state = state
46  self._callback = callback
47 
48  def __call__(self, metadata, error):
49  with self._state.lock:
50  if self._state.exception is None:
51  if self._state.called:
52  raise RuntimeError(
53  'AuthMetadataPluginCallback invoked more than once!')
54  else:
55  self._state.called = True
56  else:
57  raise RuntimeError(
58  'AuthMetadataPluginCallback raised exception "{}"!'.format(
59  self._state.exception))
60  if error is None:
61  self._callback(metadata, cygrpc.StatusCode.ok, None)
62  else:
63  self._callback(None, cygrpc.StatusCode.internal,
64  _common.encode(str(error)))
65 
66 
67 class _Plugin(object):
68 
69  def __init__(self, metadata_plugin):
70  self._metadata_plugin = metadata_plugin
71  self._stored_ctx = None
72 
73  try:
74  import contextvars # pylint: disable=wrong-import-position
75 
76  # The plugin may be invoked on a thread created by Core, which will not
77  # have the context propagated. This context is stored and installed in
78  # the thread invoking the plugin.
79  self._stored_ctx = contextvars.copy_context()
80  except ImportError:
81  # Support versions predating contextvars.
82  pass
83 
84  def __call__(self, service_url, method_name, callback):
85  context = _AuthMetadataContext(_common.decode(service_url),
86  _common.decode(method_name))
87  callback_state = _CallbackState()
88  try:
89  self._metadata_plugin(
90  context, _AuthMetadataPluginCallback(callback_state, callback))
91  except Exception as exception: # pylint: disable=broad-except
92  _LOGGER.exception(
93  'AuthMetadataPluginCallback "%s" raised exception!',
94  self._metadata_plugin)
95  with callback_state.lock:
96  callback_state.exception = exception
97  if callback_state.called:
98  return
99  callback(None, cygrpc.StatusCode.internal,
100  _common.encode(str(exception)))
101 
102 
103 def metadata_plugin_call_credentials(metadata_plugin, name):
104  if name is None:
105  try:
106  effective_name = metadata_plugin.__name__
107  except AttributeError:
108  effective_name = metadata_plugin.__class__.__name__
109  else:
110  effective_name = name
111  return grpc.CallCredentials(
112  cygrpc.MetadataPluginCallCredentials(_Plugin(metadata_plugin),
113  _common.encode(effective_name)))
xds_interop_client.str
str
Definition: xds_interop_client.py:487
grpc._plugin_wrapping._AuthMetadataPluginCallback.__call__
def __call__(self, metadata, error)
Definition: _plugin_wrapping.py:48
http2_test_server.format
format
Definition: http2_test_server.py:118
grpc._plugin_wrapping._CallbackState.exception
exception
Definition: _plugin_wrapping.py:39
grpc._plugin_wrapping._CallbackState
Definition: _plugin_wrapping.py:34
grpc._plugin_wrapping._CallbackState.lock
lock
Definition: _plugin_wrapping.py:37
grpc.AuthMetadataContext
Definition: src/python/grpcio/grpc/__init__.py:595
grpc._plugin_wrapping._AuthMetadataPluginCallback._callback
_callback
Definition: _plugin_wrapping.py:46
grpc._plugin_wrapping._AuthMetadataContext
Definition: _plugin_wrapping.py:30
grpc._plugin_wrapping._Plugin.__call__
def __call__(self, service_url, method_name, callback)
Definition: _plugin_wrapping.py:84
grpc._plugin_wrapping.metadata_plugin_call_credentials
def metadata_plugin_call_credentials(metadata_plugin, name)
Definition: _plugin_wrapping.py:103
grpc._plugin_wrapping._CallbackState.called
called
Definition: _plugin_wrapping.py:38
grpc._plugin_wrapping._AuthMetadataPluginCallback.__init__
def __init__(self, state, callback)
Definition: _plugin_wrapping.py:44
grpc._plugin_wrapping._Plugin.__init__
def __init__(self, metadata_plugin)
Definition: _plugin_wrapping.py:69
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
grpc._plugin_wrapping._AuthMetadataPluginCallback._state
_state
Definition: _plugin_wrapping.py:45
grpc._plugin_wrapping._CallbackState.__init__
def __init__(self)
Definition: _plugin_wrapping.py:36
grpc._plugin_wrapping._Plugin
Definition: _plugin_wrapping.py:67
grpc._plugin_wrapping._Plugin._metadata_plugin
_metadata_plugin
Definition: _plugin_wrapping.py:70
grpc._plugin_wrapping._AuthMetadataPluginCallback
Definition: _plugin_wrapping.py:42
grpc._cython
Definition: src/python/grpcio/grpc/_cython/__init__.py:1
grpc._plugin_wrapping._Plugin._stored_ctx
_stored_ctx
Definition: _plugin_wrapping.py:71
grpc::CallCredentials
Definition: include/grpcpp/security/credentials.h:132
grpc.AuthMetadataPluginCallback
Definition: src/python/grpcio/grpc/__init__.py:604


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:27