security.pyx.pxi 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2016 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. from libc.string cimport memcpy
  15. cdef grpc_ssl_roots_override_result ssl_roots_override_callback(
  16. char **pem_root_certs) nogil:
  17. with gil:
  18. temporary_pem_root_certs = ''
  19. pem_root_certs[0] = <char *>gpr_malloc(len(temporary_pem_root_certs) + 1)
  20. memcpy(
  21. pem_root_certs[0], <char *>temporary_pem_root_certs,
  22. len(temporary_pem_root_certs))
  23. pem_root_certs[0][len(temporary_pem_root_certs)] = '\0'
  24. return GRPC_SSL_ROOTS_OVERRIDE_OK
  25. def peer_identities(Call call):
  26. cdef grpc_auth_context* auth_context
  27. cdef grpc_auth_property_iterator properties
  28. cdef const grpc_auth_property* property
  29. auth_context = grpc_call_auth_context(call.c_call)
  30. if auth_context == NULL:
  31. return None
  32. properties = grpc_auth_context_peer_identity(auth_context)
  33. identities = []
  34. while True:
  35. property = grpc_auth_property_iterator_next(&properties)
  36. if property == NULL:
  37. break
  38. if property.value != NULL:
  39. identities.append(<bytes>(property.value))
  40. grpc_auth_context_release(auth_context)
  41. return identities if identities else None
  42. def peer_identity_key(Call call):
  43. cdef grpc_auth_context* auth_context
  44. cdef const char* c_key
  45. auth_context = grpc_call_auth_context(call.c_call)
  46. if auth_context == NULL:
  47. return None
  48. c_key = grpc_auth_context_peer_identity_property_name(auth_context)
  49. if c_key == NULL:
  50. key = None
  51. else:
  52. key = <bytes> grpc_auth_context_peer_identity_property_name(auth_context)
  53. grpc_auth_context_release(auth_context)
  54. return key
  55. def auth_context(Call call):
  56. cdef grpc_auth_context* auth_context
  57. cdef grpc_auth_property_iterator properties
  58. cdef const grpc_auth_property* property
  59. auth_context = grpc_call_auth_context(call.c_call)
  60. if auth_context == NULL:
  61. return {}
  62. properties = grpc_auth_context_property_iterator(auth_context)
  63. py_auth_context = {}
  64. while True:
  65. property = grpc_auth_property_iterator_next(&properties)
  66. if property == NULL:
  67. break
  68. if property.name != NULL and property.value != NULL:
  69. key = <bytes> property.name
  70. if key in py_auth_context:
  71. py_auth_context[key].append(<bytes>(property.value))
  72. else:
  73. py_auth_context[key] = [<bytes> property.value]
  74. grpc_auth_context_release(auth_context)
  75. return py_auth_context