<commit_before># Copyright 2013-2021 Aerospike, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from lib.client.info import (
    ASResponse,
    ASProtocolError,
    add_privileges, 
    authenticate_new, 
    authenticate_old, 
    change_password, 
    create_role, 
    create_user, 
    delete_privileges, 
    delete_role, 
    delete_whitelist, 
    drop_user, 
    grant_roles, 
    info, 
    login, 
    query_role, 
    query_roles, 
    query_user, 
    query_users, 
    revoke_roles, 
    set_password, 
    set_whitelist
 )
import socket
import warnings

try:
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        warnings.filterwarnings("ignore", message="Python 3.5 support will be dropped in the next release of cryptography. Please upgrade your Python.")
        import cryptography
        from cryptography import utils
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        from OpenSSL import SSL
    HAVE_PYOPENSSL = True
except ImportError:
    HAVE_PYOPENSSL = False


class ASSocket():

    def __init__(self, ip, port, tls_name, user, password, auth_mode, ssl_context, timeout=5):
        self.sock = None
        self.ip = ip
        self.port = port
        self.tls_name = tls_name
        self.user = user
        self.password = password
        self.auth_mode = auth_mode
        self.ssl_context = ssl_context
        self._timeout = timeout

    def _wrap_socket(self, sock, ctx):
        if ctx:
            if HAVE_PYOPENSSL:
                sock = SSL.Connection(ctx, sock)
            else:
                raise ImportError("No module named pyOpenSSL")

        return sock

    def _create_socket_for_addrinfo(self, addrinfo):
        sock = None
        try:
            # sock_info format : (family, socktype, proto, canonname, sockaddr)
            addr_family = addrinfo[0]
            sock_addr = addrinfo[4]

            sock = socket.socket(addr_family, socket.SOCK_STREAM)
            sock.settimeout(self._timeout)

            sock = self._wrap_socket(sock, self.ssl_context)
            sock.connect(sock_addr)

            if self.ssl_context:
                try:
                    sock.set_app_data(self.tls_name)

                    # timeout on wrapper might give errors
                    sock.setblocking(1)

                    sock.do_handshake()
                except Exception as tlse:
                    print("TLS connection exception: " + str(tlse))
                    if sock:
                        sock.close()
                        sock = None
                    return None

        except Exception:
            sock = None
            pass

        return sock

    def _create_socket(self):

        sock = None
        for addrinfo in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC,
                                           socket.SOCK_STREAM):
            # for DNS it will try all possible addresses
            try:
                sock = self._create_socket_for_addrinfo(addrinfo)
                if sock:
                    break
            except Exception:
                pass
        return sock

    def login(self):
        if self.user is None:
            return True

        if not self.sock:
            return False

        resp_code, self.session_token, self.session_expiration = login(self.sock, self.user, self.password, self.auth_mode)

        if resp_code != ASResponse.OK:
            self.sock.close()
            return False

        return True

    def authenticate(self, session_token):
        if self.user is None:
            return True

        if not self.sock:
            return False

        if session_token is None:
            # old authentication
            resp_code = authenticate_old(self.sock, self.user, self.password)
        else:
            # new authentication with session_token
            resp_code = authenticate_new(self.sock, self.user, session_token)

        if resp_code != ASResponse.OK:
            print("Authentication failed for", self.user, ": ", str(ASResponse(resp_code)))
            self.sock.close()
            return False

        return True

    def get_session_info(self):
        return self.session_token, self.session_expiration

    def connect(self):
        try:
            self.sock = self._create_socket()

            if not self.sock:
                return False
        except Exception:
            return False
        return True
<commit_msg>Add login failed print message<commit_after># Copyright 2013-2021 Aerospike, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from lib.client.info import (
    ASResponse,
    ASProtocolError,
    add_privileges, 
    authenticate_new, 
    authenticate_old, 
    change_password, 
    create_role, 
    create_user, 
    delete_privileges, 
    delete_role, 
    delete_whitelist, 
    drop_user, 
    grant_roles, 
    info, 
    login, 
    query_role, 
    query_roles, 
    query_user, 
    query_users, 
    revoke_roles, 
    set_password, 
    set_whitelist
 )
import socket
import warnings

try:
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        warnings.filterwarnings("ignore", message="Python 3.5 support will be dropped in the next release of cryptography. Please upgrade your Python.")
        import cryptography
        from cryptography import utils
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        from OpenSSL import SSL
    HAVE_PYOPENSSL = True
except ImportError:
    HAVE_PYOPENSSL = False


class ASSocket():

    def __init__(self, ip, port, tls_name, user, password, auth_mode, ssl_context, timeout=5):
        self.sock = None
        self.ip = ip
        self.port = port
        self.tls_name = tls_name
        self.user = user
        self.password = password
        self.auth_mode = auth_mode
        self.ssl_context = ssl_context
        self._timeout = timeout

    def _wrap_socket(self, sock, ctx):
        if ctx:
            if HAVE_PYOPENSSL:
                sock = SSL.Connection(ctx, sock)
            else:
                raise ImportError("No module named pyOpenSSL")

        return sock

    def _create_socket_for_addrinfo(self, addrinfo):
        sock = None
        try:
            # sock_info format : (family, socktype, proto, canonname, sockaddr)
            addr_family = addrinfo[0]
            sock_addr = addrinfo[4]

            sock = socket.socket(addr_family, socket.SOCK_STREAM)
            sock.settimeout(self._timeout)

            sock = self._wrap_socket(sock, self.ssl_context)
            sock.connect(sock_addr)

            if self.ssl_context:
                try:
                    sock.set_app_data(self.tls_name)

                    # timeout on wrapper might give errors
                    sock.setblocking(1)

                    sock.do_handshake()
                except Exception as tlse:
                    print("TLS connection exception: " + str(tlse))
                    if sock:
                        sock.close()
                        sock = None
                    return None

        except Exception:
            sock = None
            pass

        return sock

    def _create_socket(self):

        sock = None
        for addrinfo in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC,
                                           socket.SOCK_STREAM):
            # for DNS it will try all possible addresses
            try:
                sock = self._create_socket_for_addrinfo(addrinfo)
                if sock:
                    break
            except Exception:
                pass
        return sock

    def login(self):
        if self.user is None:
            return True

        if not self.sock:
            return False

        resp_code, self.session_token, self.session_expiration = login(self.sock, self.user, self.password, self.auth_mode)

        if resp_code != ASResponse.OK:
            # TODO remove print statement and raise an exception like requests
            print("Login failed for", self.user, ": ", str(ASResponse(resp_code)))
            self.sock.close()
            return False

        return True

    def authenticate(self, session_token):
        if self.user is None:
            return True

        if not self.sock:
            return False

        if session_token is None:
            # old authentication
            resp_code = authenticate_old(self.sock, self.user, self.password)
        else:
            # new authentication with session_token
            resp_code = authenticate_new(self.sock, self.user, session_token)

        if resp_code != ASResponse.OK:
            # TODO remove print statement and raise an exception like requests
            print("Authentication failed for", self.user, ": ", str(ASResponse(resp_code)))
            self.sock.close()
            return False

        return True

    def get_session_info(self):
        return self.session_token, self.session_expiration

    def connect(self):
        try:
            self.sock = self._create_socket()

            if not self.sock:
                return False
        except Exception:
            return False
        return True

{'fraction_non_alphanumeric': 0.06811175337186898, 'fraction_numerical': 0.003468208092485549, 'mean_word_length': 1.879611650485437, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6849945', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2663, 'n_words': 803}