from __future__ import print_function
from pygelf.tcp import GelfTcpHandler
import ssl
import socket
import sys


class GelfTlsHandler(GelfTcpHandler):

    def __init__(self, cert=None, **kwargs):
        super(GelfTlsHandler, self).__init__(**kwargs)
        self.cert = cert

    def makeSocket(self, timeout=1):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if hasattr(s, 'settimeout'):
            s.settimeout(timeout)

        try:
            if self.cert is None:
                wrapped_socket = ssl.wrap_socket(s)
            else:
                wrapped_socket = ssl.wrap_socket(s, ca_certs=self.cert, cert_reqs=ssl.CERT_REQUIRED)
            wrapped_socket.connect((self.host, self.port))
            return wrapped_socket
        except Exception as e:
            print('SSL socket exception:', e, file=sys.stderr)

from __future__ import print_function
from pygelf.tcp import GelfTcpHandler
import ssl
import socket
import sys


class GelfTlsHandler(GelfTcpHandler):

    def __init__(self, validate=False, ca_certs=None, **kwargs):
        super(GelfTlsHandler, self).__init__(**kwargs)

        if validate and ca_certs is None:
            raise ValueError('CA bundle file path must be specified')

        self.ca_certs = ca_certs
        self.reqs = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE

    def makeSocket(self, timeout=1):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if hasattr(s, 'settimeout'):
            s.settimeout(timeout)

        try:
            wrapped_socket = ssl.wrap_socket(s, ca_certs=self.ca_certs, cert_reqs=self.reqs)
            wrapped_socket.connect((self.host, self.port))
            return wrapped_socket
        except Exception as e:
            print('SSL socket exception:', e, file=sys.stderr)

Add parameter 'validate' to TLS handler
Add parameter 'validate' to TLS handler
  from __future__ import print_function
  from pygelf.tcp import GelfTcpHandler
  import ssl
  import socket
  import sys
  
  
  class GelfTlsHandler(GelfTcpHandler):
  
-     def __init__(self, cert=None, **kwargs):
+     def __init__(self, validate=False, ca_certs=None, **kwargs):
          super(GelfTlsHandler, self).__init__(**kwargs)
-         self.cert = cert
+ 
+         if validate and ca_certs is None:
+             raise ValueError('CA bundle file path must be specified')
+ 
+         self.ca_certs = ca_certs
+         self.reqs = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE
  
      def makeSocket(self, timeout=1):
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          if hasattr(s, 'settimeout'):
              s.settimeout(timeout)
  
          try:
-             if self.cert is None:
-                 wrapped_socket = ssl.wrap_socket(s)
-             else:
-                 wrapped_socket = ssl.wrap_socket(s, ca_certs=self.cert, cert_reqs=ssl.CERT_REQUIRED)
+             wrapped_socket = ssl.wrap_socket(s, ca_certs=self.ca_certs, cert_reqs=self.reqs)
              wrapped_socket.connect((self.host, self.port))
              return wrapped_socket
          except Exception as e:
              print('SSL socket exception:', e, file=sys.stderr)
  
Add parameter 'validate' to TLS handler
## Code Before:
from __future__ import print_function
from pygelf.tcp import GelfTcpHandler
import ssl
import socket
import sys


class GelfTlsHandler(GelfTcpHandler):

    def __init__(self, cert=None, **kwargs):
        super(GelfTlsHandler, self).__init__(**kwargs)
        self.cert = cert

    def makeSocket(self, timeout=1):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if hasattr(s, 'settimeout'):
            s.settimeout(timeout)

        try:
            if self.cert is None:
                wrapped_socket = ssl.wrap_socket(s)
            else:
                wrapped_socket = ssl.wrap_socket(s, ca_certs=self.cert, cert_reqs=ssl.CERT_REQUIRED)
            wrapped_socket.connect((self.host, self.port))
            return wrapped_socket
        except Exception as e:
            print('SSL socket exception:', e, file=sys.stderr)

## Instruction:
Add parameter 'validate' to TLS handler
## Code After:
from __future__ import print_function
from pygelf.tcp import GelfTcpHandler
import ssl
import socket
import sys


class GelfTlsHandler(GelfTcpHandler):

    def __init__(self, validate=False, ca_certs=None, **kwargs):
        super(GelfTlsHandler, self).__init__(**kwargs)

        if validate and ca_certs is None:
            raise ValueError('CA bundle file path must be specified')

        self.ca_certs = ca_certs
        self.reqs = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE

    def makeSocket(self, timeout=1):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if hasattr(s, 'settimeout'):
            s.settimeout(timeout)

        try:
            wrapped_socket = ssl.wrap_socket(s, ca_certs=self.ca_certs, cert_reqs=self.reqs)
            wrapped_socket.connect((self.host, self.port))
            return wrapped_socket
        except Exception as e:
            print('SSL socket exception:', e, file=sys.stderr)
