Update Python C wrapping layers to new credentials API

pull/3765/head
Masood Malekghassemi 9 years ago
parent 29c2a9e9b5
commit 36297accb6
  1. 3
      src/python/grpcio/grpc/_adapter/_c/types.c
  2. 34
      src/python/grpcio/grpc/_adapter/_c/types.h
  3. 4
      src/python/grpcio/grpc/_adapter/_c/types/call.c
  4. 116
      src/python/grpcio/grpc/_adapter/_c/types/call_credentials.c
  5. 4
      src/python/grpcio/grpc/_adapter/_c/types/channel.c
  6. 165
      src/python/grpcio/grpc/_adapter/_c/types/channel_credentials.c
  7. 6
      src/python/grpcio/grpc/_cython/_cygrpc/call.pyx
  8. 8
      src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx
  9. 10
      src/python/grpcio/grpc/_cython/_cygrpc/credentials.pxd
  10. 67
      src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx
  11. 34
      src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxd
  12. 29
      src/python/grpcio/grpc/_cython/cygrpc.pyx
  13. 3
      src/python/grpcio/setup.py

@ -40,7 +40,8 @@
int pygrpc_module_add_types(PyObject *module) {
int i;
PyTypeObject *types[] = {
&pygrpc_ClientCredentials_type,
&pygrpc_CallCredentials_type,
&pygrpc_ChannelCredentials_type,
&pygrpc_ServerCredentials_type,
&pygrpc_CompletionQueue_type,
&pygrpc_Call_type,

@ -44,27 +44,35 @@
/* Client-side credentials */
/*=========================*/
typedef struct ClientCredentials {
typedef struct ChannelCredentials {
PyObject_HEAD
grpc_credentials *c_creds;
} ClientCredentials;
void pygrpc_ClientCredentials_dealloc(ClientCredentials *self);
ClientCredentials *pygrpc_ClientCredentials_google_default(
grpc_channel_credentials *c_creds;
} ChannelCredentials;
void pygrpc_ChannelCredentials_dealloc(ChannelCredentials *self);
ChannelCredentials *pygrpc_ChannelCredentials_google_default(
PyTypeObject *type, PyObject *ignored);
ClientCredentials *pygrpc_ClientCredentials_ssl(
ChannelCredentials *pygrpc_ChannelCredentials_ssl(
PyTypeObject *type, PyObject *args, PyObject *kwargs);
ClientCredentials *pygrpc_ClientCredentials_composite(
ChannelCredentials *pygrpc_ChannelCredentials_composite(
PyTypeObject *type, PyObject *args, PyObject *kwargs);
ClientCredentials *pygrpc_ClientCredentials_compute_engine(
extern PyTypeObject pygrpc_ChannelCredentials_type;
typedef struct CallCredentials {
PyObject_HEAD
grpc_call_credentials *c_creds;
} CallCredentials;
void pygrpc_CallCredentials_dealloc(CallCredentials *self);
CallCredentials *pygrpc_CallCredentials_composite(
PyTypeObject *type, PyObject *args, PyObject *kwargs);
CallCredentials *pygrpc_CallCredentials_compute_engine(
PyTypeObject *type, PyObject *ignored);
ClientCredentials *pygrpc_ClientCredentials_jwt(
CallCredentials *pygrpc_CallCredentials_jwt(
PyTypeObject *type, PyObject *args, PyObject *kwargs);
ClientCredentials *pygrpc_ClientCredentials_refresh_token(
CallCredentials *pygrpc_CallCredentials_refresh_token(
PyTypeObject *type, PyObject *args, PyObject *kwargs);
ClientCredentials *pygrpc_ClientCredentials_iam(
CallCredentials *pygrpc_CallCredentials_iam(
PyTypeObject *type, PyObject *args, PyObject *kwargs);
extern PyTypeObject pygrpc_ClientCredentials_type;
extern PyTypeObject pygrpc_CallCredentials_type;
/*=========================*/
/* Server-side credentials */

@ -173,12 +173,12 @@ PyObject *pygrpc_Call_peer(Call *self) {
}
PyObject *pygrpc_Call_set_credentials(Call *self, PyObject *args,
PyObject *kwargs) {
ClientCredentials *creds;
CallCredentials *creds;
grpc_call_error errcode;
static char *keywords[] = {"creds", NULL};
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "O!:set_credentials", keywords,
&pygrpc_ClientCredentials_type, &creds)) {
&pygrpc_CallCredentials_type, &creds)) {
return NULL;
}
errcode = grpc_call_set_credentials(self->c_call, creds->c_creds);

@ -39,31 +39,28 @@
#include <grpc/grpc_security.h>
PyMethodDef pygrpc_ClientCredentials_methods[] = {
{"google_default", (PyCFunction)pygrpc_ClientCredentials_google_default,
METH_CLASS|METH_NOARGS, ""},
{"ssl", (PyCFunction)pygrpc_ClientCredentials_ssl,
METH_CLASS|METH_KEYWORDS, ""},
{"composite", (PyCFunction)pygrpc_ClientCredentials_composite,
PyMethodDef pygrpc_CallCredentials_methods[] = {
{"composite", (PyCFunction)pygrpc_CallCredentials_composite,
METH_CLASS|METH_KEYWORDS, ""},
{"compute_engine", (PyCFunction)pygrpc_ClientCredentials_compute_engine,
{"compute_engine", (PyCFunction)pygrpc_CallCredentials_compute_engine,
METH_CLASS|METH_NOARGS, ""},
{"jwt", (PyCFunction)pygrpc_ClientCredentials_jwt,
{"jwt", (PyCFunction)pygrpc_CallCredentials_jwt,
METH_CLASS|METH_KEYWORDS, ""},
{"refresh_token", (PyCFunction)pygrpc_ClientCredentials_refresh_token,
{"refresh_token", (PyCFunction)pygrpc_CallCredentials_refresh_token,
METH_CLASS|METH_KEYWORDS, ""},
{"iam", (PyCFunction)pygrpc_ClientCredentials_iam,
{"iam", (PyCFunction)pygrpc_CallCredentials_iam,
METH_CLASS|METH_KEYWORDS, ""},
{NULL}
};
const char pygrpc_ClientCredentials_doc[] = "";
PyTypeObject pygrpc_ClientCredentials_type = {
const char pygrpc_CallCredentials_doc[] = "";
PyTypeObject pygrpc_CallCredentials_type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"ClientCredentials", /* tp_name */
sizeof(ClientCredentials), /* tp_basicsize */
"CallCredentials", /* tp_name */
sizeof(CallCredentials), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pygrpc_ClientCredentials_dealloc, /* tp_dealloc */
(destructor)pygrpc_CallCredentials_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@ -79,14 +76,14 @@ PyTypeObject pygrpc_ClientCredentials_type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
pygrpc_ClientCredentials_doc, /* tp_doc */
pygrpc_CallCredentials_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
pygrpc_ClientCredentials_methods, /* tp_methods */
pygrpc_CallCredentials_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
@ -99,67 +96,26 @@ PyTypeObject pygrpc_ClientCredentials_type = {
0 /* tp_new */
};
void pygrpc_ClientCredentials_dealloc(ClientCredentials *self) {
grpc_credentials_release(self->c_creds);
void pygrpc_CallCredentials_dealloc(CallCredentials *self) {
grpc_call_credentials_release(self->c_creds);
self->ob_type->tp_free((PyObject *)self);
}
ClientCredentials *pygrpc_ClientCredentials_google_default(
PyTypeObject *type, PyObject *ignored) {
ClientCredentials *self = (ClientCredentials *)type->tp_alloc(type, 0);
self->c_creds = grpc_google_default_credentials_create();
if (!self->c_creds) {
Py_DECREF(self);
PyErr_SetString(PyExc_RuntimeError,
"couldn't create Google default credentials");
return NULL;
}
return self;
}
ClientCredentials *pygrpc_ClientCredentials_ssl(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ClientCredentials *self;
const char *root_certs;
const char *private_key = NULL;
const char *cert_chain = NULL;
grpc_ssl_pem_key_cert_pair key_cert_pair;
static char *keywords[] = {"root_certs", "private_key", "cert_chain", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|zz:ssl", keywords,
&root_certs, &private_key, &cert_chain)) {
return NULL;
}
self = (ClientCredentials *)type->tp_alloc(type, 0);
if (private_key && cert_chain) {
key_cert_pair.private_key = private_key;
key_cert_pair.cert_chain = cert_chain;
self->c_creds =
grpc_ssl_credentials_create(root_certs, &key_cert_pair, NULL);
} else {
self->c_creds = grpc_ssl_credentials_create(root_certs, NULL, NULL);
}
if (!self->c_creds) {
Py_DECREF(self);
PyErr_SetString(PyExc_RuntimeError, "couldn't create ssl credentials");
return NULL;
}
return self;
}
ClientCredentials *pygrpc_ClientCredentials_composite(
CallCredentials *pygrpc_CallCredentials_composite(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ClientCredentials *self;
ClientCredentials *creds1;
ClientCredentials *creds2;
CallCredentials *self;
CallCredentials *creds1;
CallCredentials *creds2;
static char *keywords[] = {"creds1", "creds2", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O!:composite", keywords,
&pygrpc_ClientCredentials_type, &creds1,
&pygrpc_ClientCredentials_type, &creds2)) {
&pygrpc_CallCredentials_type, &creds1,
&pygrpc_CallCredentials_type, &creds2)) {
return NULL;
}
self = (ClientCredentials *)type->tp_alloc(type, 0);
self = (CallCredentials *)type->tp_alloc(type, 0);
self->c_creds =
grpc_composite_credentials_create(creds1->c_creds, creds2->c_creds, NULL);
grpc_composite_call_credentials_create(
creds1->c_creds, creds2->c_creds, NULL);
if (!self->c_creds) {
Py_DECREF(self);
PyErr_SetString(PyExc_RuntimeError, "couldn't create composite credentials");
@ -168,9 +124,9 @@ ClientCredentials *pygrpc_ClientCredentials_composite(
return self;
}
ClientCredentials *pygrpc_ClientCredentials_compute_engine(
CallCredentials *pygrpc_CallCredentials_compute_engine(
PyTypeObject *type, PyObject *ignored) {
ClientCredentials *self = (ClientCredentials *)type->tp_alloc(type, 0);
CallCredentials *self = (CallCredentials *)type->tp_alloc(type, 0);
self->c_creds = grpc_google_compute_engine_credentials_create(NULL);
if (!self->c_creds) {
Py_DECREF(self);
@ -182,9 +138,9 @@ ClientCredentials *pygrpc_ClientCredentials_compute_engine(
}
/* TODO: Rename this credentials to something like service_account_jwt_access */
ClientCredentials *pygrpc_ClientCredentials_jwt(
CallCredentials *pygrpc_CallCredentials_jwt(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ClientCredentials *self;
CallCredentials *self;
const char *json_key;
double lifetime;
static char *keywords[] = {"json_key", "token_lifetime", NULL};
@ -192,7 +148,7 @@ ClientCredentials *pygrpc_ClientCredentials_jwt(
&json_key, &lifetime)) {
return NULL;
}
self = (ClientCredentials *)type->tp_alloc(type, 0);
self = (CallCredentials *)type->tp_alloc(type, 0);
self->c_creds = grpc_service_account_jwt_access_credentials_create(
json_key, pygrpc_cast_double_to_gpr_timespec(lifetime), NULL);
if (!self->c_creds) {
@ -203,16 +159,16 @@ ClientCredentials *pygrpc_ClientCredentials_jwt(
return self;
}
ClientCredentials *pygrpc_ClientCredentials_refresh_token(
CallCredentials *pygrpc_CallCredentials_refresh_token(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ClientCredentials *self;
CallCredentials *self;
const char *json_refresh_token;
static char *keywords[] = {"json_refresh_token", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:refresh_token", keywords,
&json_refresh_token)) {
return NULL;
}
self = (ClientCredentials *)type->tp_alloc(type, 0);
self = (CallCredentials *)type->tp_alloc(type, 0);
self->c_creds =
grpc_google_refresh_token_credentials_create(json_refresh_token, NULL);
if (!self->c_creds) {
@ -224,9 +180,9 @@ ClientCredentials *pygrpc_ClientCredentials_refresh_token(
return self;
}
ClientCredentials *pygrpc_ClientCredentials_iam(
CallCredentials *pygrpc_CallCredentials_iam(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ClientCredentials *self;
CallCredentials *self;
const char *authorization_token;
const char *authority_selector;
static char *keywords[] = {"authorization_token", "authority_selector", NULL};
@ -234,7 +190,7 @@ ClientCredentials *pygrpc_ClientCredentials_iam(
&authorization_token, &authority_selector)) {
return NULL;
}
self = (ClientCredentials *)type->tp_alloc(type, 0);
self = (CallCredentials *)type->tp_alloc(type, 0);
self->c_creds = grpc_google_iam_credentials_create(authorization_token,
authority_selector, NULL);
if (!self->c_creds) {

@ -94,11 +94,11 @@ Channel *pygrpc_Channel_new(
Channel *self;
const char *target;
PyObject *py_args;
ClientCredentials *creds = NULL;
ChannelCredentials *creds = NULL;
grpc_channel_args c_args;
char *keywords[] = {"target", "args", "creds", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|O!:Channel", keywords,
&target, &py_args, &pygrpc_ClientCredentials_type, &creds)) {
&target, &py_args, &pygrpc_ChannelCredentials_type, &creds)) {
return NULL;
}
if (!pygrpc_produce_channel_args(py_args, &c_args)) {

@ -0,0 +1,165 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "grpc/_adapter/_c/types.h"
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
PyMethodDef pygrpc_ChannelCredentials_methods[] = {
{"google_default", (PyCFunction)pygrpc_ChannelCredentials_google_default,
METH_CLASS|METH_NOARGS, ""},
{"ssl", (PyCFunction)pygrpc_ChannelCredentials_ssl,
METH_CLASS|METH_KEYWORDS, ""},
{"composite", (PyCFunction)pygrpc_ChannelCredentials_composite,
METH_CLASS|METH_KEYWORDS, ""},
{NULL}
};
const char pygrpc_ChannelCredentials_doc[] = "";
PyTypeObject pygrpc_ChannelCredentials_type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"ChannelCredentials", /* tp_name */
sizeof(ChannelCredentials), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pygrpc_ChannelCredentials_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
pygrpc_ChannelCredentials_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
pygrpc_ChannelCredentials_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0 /* tp_new */
};
void pygrpc_ChannelCredentials_dealloc(ChannelCredentials *self) {
grpc_channel_credentials_release(self->c_creds);
self->ob_type->tp_free((PyObject *)self);
}
ChannelCredentials *pygrpc_ChannelCredentials_google_default(
PyTypeObject *type, PyObject *ignored) {
ChannelCredentials *self = (ChannelCredentials *)type->tp_alloc(type, 0);
self->c_creds = grpc_google_default_credentials_create();
if (!self->c_creds) {
Py_DECREF(self);
PyErr_SetString(PyExc_RuntimeError,
"couldn't create Google default credentials");
return NULL;
}
return self;
}
ChannelCredentials *pygrpc_ChannelCredentials_ssl(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ChannelCredentials *self;
const char *root_certs;
const char *private_key = NULL;
const char *cert_chain = NULL;
grpc_ssl_pem_key_cert_pair key_cert_pair;
static char *keywords[] = {"root_certs", "private_key", "cert_chain", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|zz:ssl", keywords,
&root_certs, &private_key, &cert_chain)) {
return NULL;
}
self = (ChannelCredentials *)type->tp_alloc(type, 0);
if (private_key && cert_chain) {
key_cert_pair.private_key = private_key;
key_cert_pair.cert_chain = cert_chain;
self->c_creds =
grpc_ssl_credentials_create(root_certs, &key_cert_pair, NULL);
} else {
self->c_creds = grpc_ssl_credentials_create(root_certs, NULL, NULL);
}
if (!self->c_creds) {
Py_DECREF(self);
PyErr_SetString(PyExc_RuntimeError, "couldn't create ssl credentials");
return NULL;
}
return self;
}
ChannelCredentials *pygrpc_ChannelCredentials_composite(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
ChannelCredentials *self;
ChannelCredentials *creds1;
CallCredentials *creds2;
static char *keywords[] = {"creds1", "creds2", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O!:composite", keywords,
&pygrpc_ChannelCredentials_type, &creds1,
&pygrpc_CallCredentials_type, &creds2)) {
return NULL;
}
self = (ChannelCredentials *)type->tp_alloc(type, 0);
self->c_creds =
grpc_composite_channel_credentials_create(
creds1->c_creds, creds2->c_creds, NULL);
if (!self->c_creds) {
Py_DECREF(self);
PyErr_SetString(
PyExc_RuntimeError, "couldn't create composite credentials");
return NULL;
}
return self;
}

@ -29,6 +29,7 @@
cimport cpython
from grpc._cython._cygrpc cimport credentials
from grpc._cython._cygrpc cimport grpc
from grpc._cython._cygrpc cimport records
@ -73,6 +74,11 @@ cdef class Call:
else:
return grpc.grpc_call_cancel(self.c_call, NULL)
def set_credentials(
self, credentials.CallCredentials call_credentials not None):
return grpc.grpc_call_set_credentials(
self.c_call, call_credentials.c_credentials)
def __dealloc__(self):
if self.c_call != NULL:
grpc.grpc_call_destroy(self.c_call)

@ -37,7 +37,7 @@ from grpc._cython._cygrpc cimport records
cdef class Channel:
def __cinit__(self, target, records.ChannelArgs arguments=None,
credentials.ClientCredentials client_credentials=None):
credentials.ChannelCredentials channel_credentials=None):
cdef grpc.grpc_channel_args *c_arguments = NULL
self.c_channel = NULL
self.references = []
@ -49,13 +49,13 @@ cdef class Channel:
target = target.encode()
else:
raise TypeError("expected target to be str or bytes")
if client_credentials is None:
if channel_credentials is None:
self.c_channel = grpc.grpc_insecure_channel_create(target, c_arguments,
NULL)
else:
self.c_channel = grpc.grpc_secure_channel_create(
client_credentials.c_credentials, target, c_arguments, NULL)
self.references.append(client_credentials)
channel_credentials.c_credentials, target, c_arguments, NULL)
self.references.append(channel_credentials)
self.references.append(target)
self.references.append(arguments)

@ -30,13 +30,19 @@
from grpc._cython._cygrpc cimport grpc
cdef class ClientCredentials:
cdef class ChannelCredentials:
cdef grpc.grpc_credentials *c_credentials
cdef grpc.grpc_channel_credentials *c_credentials
cdef grpc.grpc_ssl_pem_key_cert_pair c_ssl_pem_key_cert_pair
cdef list references
cdef class CallCredentials:
cdef grpc.grpc_call_credentials *c_credentials
cdef list references
cdef class ServerCredentials:
cdef grpc.grpc_server_credentials *c_credentials

@ -31,7 +31,7 @@ from grpc._cython._cygrpc cimport grpc
from grpc._cython._cygrpc cimport records
cdef class ClientCredentials:
cdef class ChannelCredentials:
def __cinit__(self):
self.c_credentials = NULL
@ -47,7 +47,24 @@ cdef class ClientCredentials:
def __dealloc__(self):
if self.c_credentials != NULL:
grpc.grpc_credentials_release(self.c_credentials)
grpc.grpc_channel_credentials_release(self.c_credentials)
cdef class CallCredentials:
def __cinit__(self):
self.c_credentials = NULL
self.references = []
# The object *can* be invalid in Python if we fail to make the credentials
# (and the core thus returns NULL credentials). Used primarily for debugging.
@property
def is_valid(self):
return self.c_credentials != NULL
def __dealloc__(self):
if self.c_credentials != NULL:
grpc.grpc_call_credentials_release(self.c_credentials)
cdef class ServerCredentials:
@ -60,12 +77,12 @@ cdef class ServerCredentials:
grpc.grpc_server_credentials_release(self.c_credentials)
def client_credentials_google_default():
cdef ClientCredentials credentials = ClientCredentials();
def channel_credentials_google_default():
cdef ChannelCredentials credentials = ChannelCredentials();
credentials.c_credentials = grpc.grpc_google_default_credentials_create()
return credentials
def client_credentials_ssl(pem_root_certificates,
def channel_credentials_ssl(pem_root_certificates,
records.SslPemKeyCertPair ssl_pem_key_cert_pair):
if pem_root_certificates is None:
pass
@ -75,7 +92,7 @@ def client_credentials_ssl(pem_root_certificates,
pem_root_certificates = pem_root_certificates.encode()
else:
raise TypeError("expected str or bytes for pem_root_certificates")
cdef ClientCredentials credentials = ClientCredentials()
cdef ChannelCredentials credentials = ChannelCredentials()
cdef const char *c_pem_root_certificates = NULL
if pem_root_certificates is not None:
c_pem_root_certificates = pem_root_certificates
@ -88,26 +105,38 @@ def client_credentials_ssl(pem_root_certificates,
credentials.c_credentials = grpc.grpc_ssl_credentials_create(
c_pem_root_certificates, NULL, NULL)
def client_credentials_composite_credentials(
ClientCredentials credentials_1 not None,
ClientCredentials credentials_2 not None):
def channel_credentials_composite(
ChannelCredentials credentials_1 not None,
CallCredentials credentials_2 not None):
if not credentials_1.is_valid or not credentials_2.is_valid:
raise ValueError("passed credentials must both be valid")
cdef ChannelCredentials credentials = ChannelCredentials()
credentials.c_credentials = grpc.grpc_composite_channel_credentials_create(
credentials_1.c_credentials, credentials_2.c_credentials, NULL)
credentials.references.append(credentials_1)
credentials.references.append(credentials_2)
return credentials
def call_credentials_composite(
CallCredentials credentials_1 not None,
CallCredentials credentials_2 not None):
if not credentials_1.is_valid or not credentials_2.is_valid:
raise ValueError("passed credentials must both be valid")
cdef ClientCredentials credentials = ClientCredentials()
credentials.c_credentials = grpc.grpc_composite_credentials_create(
cdef CallCredentials credentials = CallCredentials()
credentials.c_credentials = grpc.grpc_composite_call_credentials_create(
credentials_1.c_credentials, credentials_2.c_credentials, NULL)
credentials.references.append(credentials_1)
credentials.references.append(credentials_2)
return credentials
def client_credentials_google_compute_engine():
cdef ClientCredentials credentials = ClientCredentials()
def call_credentials_google_compute_engine():
cdef CallCredentials credentials = CallCredentials()
credentials.c_credentials = (
grpc.grpc_google_compute_engine_credentials_create(NULL))
return credentials
#TODO rename to something like client_credentials_service_account_jwt_access.
def client_credentials_service_account_jwt_access(
def call_credentials_service_account_jwt_access(
json_key, records.Timespec token_lifetime not None):
if isinstance(json_key, bytes):
pass
@ -115,27 +144,27 @@ def client_credentials_service_account_jwt_access(
json_key = json_key.encode()
else:
raise TypeError("expected json_key to be str or bytes")
cdef ClientCredentials credentials = ClientCredentials()
cdef CallCredentials credentials = CallCredentials()
credentials.c_credentials = (
grpc.grpc_service_account_jwt_access_credentials_create(
json_key, token_lifetime.c_time, NULL))
credentials.references.append(json_key)
return credentials
def client_credentials_google_refresh_token(json_refresh_token):
def call_credentials_google_refresh_token(json_refresh_token):
if isinstance(json_refresh_token, bytes):
pass
elif isinstance(json_refresh_token, basestring):
json_refresh_token = json_refresh_token.encode()
else:
raise TypeError("expected json_refresh_token to be str or bytes")
cdef ClientCredentials credentials = ClientCredentials()
cdef CallCredentials credentials = CallCredentials()
credentials.c_credentials = grpc.grpc_google_refresh_token_credentials_create(
json_refresh_token, NULL)
credentials.references.append(json_refresh_token)
return credentials
def client_credentials_google_iam(authorization_token, authority_selector):
def call_credentials_google_iam(authorization_token, authority_selector):
if isinstance(authorization_token, bytes):
pass
elif isinstance(authorization_token, basestring):
@ -148,7 +177,7 @@ def client_credentials_google_iam(authorization_token, authority_selector):
authority_selector = authority_selector.encode()
else:
raise TypeError("expected authority_selector to be str or bytes")
cdef ClientCredentials credentials = ClientCredentials()
cdef CallCredentials credentials = CallCredentials()
credentials.c_credentials = grpc.grpc_google_iam_credentials_create(
authorization_token, authority_selector, NULL)
credentials.references.append(authorization_token)

@ -316,32 +316,40 @@ cdef extern from "grpc/grpc_security.h":
const char *private_key
const char *certificate_chain "cert_chain"
ctypedef struct grpc_credentials:
ctypedef struct grpc_channel_credentials:
# We don't care about the internals (and in fact don't know them)
pass
grpc_credentials *grpc_google_default_credentials_create()
grpc_credentials *grpc_ssl_credentials_create(
ctypedef struct grpc_call_credentials:
# We don't care about the internals (and in fact don't know them)
pass
grpc_channel_credentials *grpc_google_default_credentials_create()
grpc_channel_credentials *grpc_ssl_credentials_create(
const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair,
void *reserved)
grpc_channel_credentials *grpc_composite_channel_credentials_create(
grpc_channel_credentials *creds1, grpc_call_credentials *creds2,
void *reserved)
void grpc_channel_credentials_release(grpc_channel_credentials *creds)
grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1,
grpc_credentials *creds2,
void *reserved)
grpc_credentials *grpc_google_compute_engine_credentials_create(
grpc_call_credentials *grpc_composite_call_credentials_create(
grpc_call_credentials *creds1, grpc_call_credentials *creds2,
void *reserved)
grpc_call_credentials *grpc_google_compute_engine_credentials_create(
void *reserved)
grpc_credentials *grpc_service_account_jwt_access_credentials_create(
grpc_call_credentials *grpc_service_account_jwt_access_credentials_create(
const char *json_key,
gpr_timespec token_lifetime, void *reserved)
grpc_credentials *grpc_google_refresh_token_credentials_create(
grpc_call_credentials *grpc_google_refresh_token_credentials_create(
const char *json_refresh_token, void *reserved)
grpc_credentials *grpc_google_iam_credentials_create(
grpc_call_credentials *grpc_google_iam_credentials_create(
const char *authorization_token, const char *authority_selector,
void *reserved)
void grpc_credentials_release(grpc_credentials *creds)
void grpc_call_credentials_release(grpc_call_credentials *creds)
grpc_channel *grpc_secure_channel_create(
grpc_credentials *creds, const char *target,
grpc_channel_credentials *creds, const char *target,
const grpc_channel_args *args, void *reserved)
ctypedef struct grpc_server_credentials:
@ -358,4 +366,4 @@ cdef extern from "grpc/grpc_security.h":
grpc_server_credentials *creds)
grpc_call_error grpc_call_set_credentials(grpc_call *call,
grpc_credentials *creds)
grpc_call_credentials *creds)

@ -70,21 +70,24 @@ operation_receive_close_on_server = records.operation_receive_close_on_server
Operations = records.Operations
ClientCredentials = credentials.ClientCredentials
CallCredentials = credentials.CallCredentials
ChannelCredentials = credentials.ChannelCredentials
ServerCredentials = credentials.ServerCredentials
client_credentials_google_default = (
credentials.client_credentials_google_default)
client_credentials_ssl = credentials.client_credentials_ssl
client_credentials_composite_credentials = (
credentials.client_credentials_composite_credentials)
client_credentials_google_compute_engine = (
credentials.client_credentials_google_compute_engine)
client_credentials_jwt_access = (
credentials.client_credentials_service_account_jwt_access)
client_credentials_refresh_token = (
credentials.client_credentials_google_refresh_token)
client_credentials_google_iam = credentials.client_credentials_google_iam
channel_credentials_google_default = (
credentials.channel_credentials_google_default)
channel_credentials_ssl = credentials.channel_credentials_ssl
channel_credentials_composite = (
credentials.channel_credentials_composite)
call_credentials_composite = (
credentials.call_credentials_composite)
call_credentials_google_compute_engine = (
credentials.call_credentials_google_compute_engine)
call_credentials_jwt_access = (
credentials.call_credentials_service_account_jwt_access)
call_credentials_refresh_token = (
credentials.call_credentials_google_refresh_token)
call_credentials_google_iam = credentials.call_credentials_google_iam
server_credentials_ssl = credentials.server_credentials_ssl
CompletionQueue = completion_queue.CompletionQueue

@ -52,7 +52,8 @@ _C_EXTENSION_SOURCES = (
'grpc/_adapter/_c/module.c',
'grpc/_adapter/_c/types.c',
'grpc/_adapter/_c/utility.c',
'grpc/_adapter/_c/types/client_credentials.c',
'grpc/_adapter/_c/types/call_credentials.c',
'grpc/_adapter/_c/types/channel_credentials.c',
'grpc/_adapter/_c/types/server_credentials.c',
'grpc/_adapter/_c/types/completion_queue.c',
'grpc/_adapter/_c/types/call.c',

Loading…
Cancel
Save