Esempio n. 1
0
 def setUp(self):
   server_credentials = cygrpc.server_credentials_ssl(
       None, [cygrpc.SslPemKeyCertPair(resources.private_key(),
                                       resources.certificate_chain())], False)
   client_credentials = cygrpc.channel_credentials_ssl(
       resources.test_root_certificates(), None)
   self.setUpMixin(server_credentials, client_credentials, _SSL_HOST_OVERRIDE)
Esempio n. 2
0
def ssl_server_credentials(private_key_certificate_chain_pairs,
                           root_certificates=None,
                           require_client_auth=False):
    """Creates a ServerCredentials for use with an SSL-enabled Server.

  Args:
    private_key_certificate_chain_pairs: A nonempty sequence each element of
      which is a pair the first element of which is a PEM-encoded private key
      and the second element of which is the corresponding PEM-encoded
      certificate chain.
    root_certificates: PEM-encoded client root certificates to be used for
      verifying authenticated clients. If omitted, require_client_auth must also
      be omitted or be False.
    require_client_auth: A boolean indicating whether or not to require clients
      to be authenticated. May only be True if root_certificates is not None.

  Returns:
    A ServerCredentials for use with an SSL-enabled Server.
  """
    if len(private_key_certificate_chain_pairs) == 0:
        raise ValueError(
            'At least one private key-certificate chain pair is required!')
    elif require_client_auth and root_certificates is None:
        raise ValueError(
            'Illegal to require client auth without providing root certificates!'
        )
    else:
        return ServerCredentials(
            _cygrpc.server_credentials_ssl(root_certificates, [
                _cygrpc.SslPemKeyCertPair(key, pem)
                for key, pem in private_key_certificate_chain_pairs
            ], require_client_auth))
Esempio n. 3
0
def ssl_server_credentials(private_key_certificate_chain_pairs,
                           root_certificates=None,
                           require_client_auth=False):
    """Creates a ServerCredentials for use with an SSL-enabled Server.

  Args:
    private_key_certificate_chain_pairs: A list of pairs of the form
      [PEM-encoded private key, PEM-encoded certificate chain].
    root_certificates: An optional byte string of PEM-encoded client root
      certificates that the server will use to verify client authentication.
      If omitted, require_client_auth must also be False.
    require_client_auth: A boolean indicating whether or not to require
      clients to be authenticated. May only be True if root_certificates
      is not None.

  Returns:
    A ServerCredentials for use with an SSL-enabled Server. Typically, this
    object is an argument to add_secure_port() method during server setup.
  """
    if len(private_key_certificate_chain_pairs) == 0:
        raise ValueError(
            'At least one private key-certificate chain pair is required!')
    elif require_client_auth and root_certificates is None:
        raise ValueError(
            'Illegal to require client auth without providing root certificates!'
        )
    else:
        return ServerCredentials(
            _cygrpc.server_credentials_ssl(root_certificates, [
                _cygrpc.SslPemKeyCertPair(key, pem)
                for key, pem in private_key_certificate_chain_pairs
            ], require_client_auth))
Esempio n. 4
0
def channel_credentials_ssl(root_certificates, private_key, certificate_chain):
    pair = None
    if private_key is not None or certificate_chain is not None:
        pair = cygrpc.SslPemKeyCertPair(private_key, certificate_chain)
    if root_certificates is None:
        root_certificates = pkg_resources.resource_string(
            __name__, _ROOT_CERTIFICATES_RESOURCE_PATH)
    return cygrpc.channel_credentials_ssl(root_certificates, pair)
Esempio n. 5
0
def ssl_channel_credentials(
    root_certificates=None, private_key=None, certificate_chain=None):
  """Creates a ChannelCredentials for use with an SSL-enabled Channel.

  Args:
    root_certificates: The PEM-encoded root certificates or unset to ask for
      them to be retrieved from a default location.
    private_key: The PEM-encoded private key to use or unset if no private key
      should be used.
    certificate_chain: The PEM-encoded certificate chain to use or unset if no
      certificate chain should be used.

  Returns:
    A ChannelCredentials for use with an SSL-enabled Channel.
  """
  if private_key is not None or certificate_chain is not None:
    pair = _cygrpc.SslPemKeyCertPair(private_key, certificate_chain)
  else:
    pair = None
  return ChannelCredentials(
      _cygrpc.channel_credentials_ssl(root_certificates, pair))
Esempio n. 6
0
 def setUp(self):
     server_credentials = cygrpc.server_credentials_ssl(
         None, [
             cygrpc.SslPemKeyCertPair(resources.private_key(),
                                      resources.certificate_chain())
         ], False)
     channel_credentials = cygrpc.channel_credentials_ssl(
         resources.test_root_certificates(), None)
     self.server_completion_queue = cygrpc.CompletionQueue()
     self.server = cygrpc.Server()
     self.server.register_completion_queue(self.server_completion_queue)
     self.port = self.server.add_http2_port('[::]:0', server_credentials)
     self.server.start()
     self.client_completion_queue = cygrpc.CompletionQueue()
     client_channel_arguments = cygrpc.ChannelArgs([
         cygrpc.ChannelArg(cygrpc.ChannelArgKey.ssl_target_name_override,
                           _SSL_HOST_OVERRIDE)
     ])
     self.client_channel = cygrpc.Channel('localhost:{}'.format(self.port),
                                          client_channel_arguments,
                                          channel_credentials)
Esempio n. 7
0
def ssl_channel_credentials(root_certificates=None,
                            private_key=None,
                            certificate_chain=None):
    """Creates a ChannelCredentials for use with an SSL-enabled Channel.

  Args:
    root_certificates: The PEM-encoded root certificates as a byte string,
    or None to retrieve them from a default location chosen by gRPC runtime.
    private_key: The PEM-encoded private key as a byte string, or None if no
    private key should be used.
    certificate_chain: The PEM-encoded certificate chain as a byte string
    to use or or None if no certificate chain should be used.

  Returns:
    A ChannelCredentials for use with an SSL-enabled Channel.
  """
    if private_key is not None or certificate_chain is not None:
        pair = _cygrpc.SslPemKeyCertPair(private_key, certificate_chain)
    else:
        pair = None
    return ChannelCredentials(
        _cygrpc.channel_credentials_ssl(root_certificates, pair))
Esempio n. 8
0
def server_credentials_ssl(root_credentials, pair_sequence, force_client_auth):
    return cygrpc.server_credentials_ssl(
        root_credentials,
        [cygrpc.SslPemKeyCertPair(key, pem)
         for key, pem in pair_sequence], force_client_auth)
Esempio n. 9
0
def channel_credentials_ssl(root_certificates, private_key, certificate_chain):
    pair = None
    if private_key is not None or certificate_chain is not None:
        pair = cygrpc.SslPemKeyCertPair(private_key, certificate_chain)
    return cygrpc.channel_credentials_ssl(root_certificates, pair)