<commit_before>from cassandra import Unauthorized
from dtest import Tester, debug
from assertions import assert_all, assert_invalid
from tools import since
import time

from ccmlib import repository

@since('3.0')
class TestAuthUpgrade(Tester):

    def __init__(self, *args, **kwargs):
        # Ignore these log patterns:
        self.ignore_log_patterns = [
            # This one occurs if we do a non-rolling upgrade, the node
            # it's trying to send the migration to hasn't started yet,
            # and when it does, it gets replayed and everything is fine.
            r'Can\'t send migration request: node.*is down',
        ]

        # Force cluster options that are common among versions:
        kwargs['cluster_options'] = {'authenticator': 'PasswordAuthenticator',
                                     'authorizer': 'CassandraAuthorizer'}
        Tester.__init__(self, *args, **kwargs)

    def upgrade_with_internal_auth_test(self):
        """
        Tests upgrade between 2.1->3.0 as the schema and apis around authn/authz changed

        @jira_ticket CASSANDRA-7653
        """
        cluster = self.cluster

        # Forcing cluster version on purpose
        cluster.set_install_dir(version="git:cassandra-2.1")
        cluster.populate(3).start()

        node1, node2, node3 = cluster.nodelist()

        # wait for default superuser creation
        # The log message
        # node.watch_log_for('Created default superuser')
        # will only appear on one of the three nodes, and we don't know
        # which ahead of time. Grepping all three in parallel is unpleasant.
        # See auth_test and auth_roles test for instances of this as well.
        # Should be fixed by C*-6177
        time.sleep(15)

        session = self.patient_cql_connection(node1, user='cassandra', password='<PASSWORD>')
        session.execute("CREATE USER klaus WITH PASSWORD '<PASSWORD>' SUPERUSER")
        session.execute("CREATE USER michael WITH PASSWORD '<PASSWORD>' NOSUPERUSER")
        session.execute("CREATE KEYSPACE ks WITH replication = {'class':'SimpleStrategy', 'replication_factor':1}")
        session.execute("CREATE TABLE ks.cf1 (id int primary key, val int)")
        session.execute("CREATE TABLE ks.cf2 (id int primary key, val int)")
        session.execute("GRANT MODIFY ON ks.cf1 TO michael")
        session.execute("GRANT SELECT ON ks.cf2 TO michael")

        self.check_permissions(node1)
        session.shutdown()
        # upgrade the cluster to 3.0, restarting node 1 in the process
        # note - will need changing when we branch for 3.0
        self.upgrade_to_version("git:trunk", nodes=[node1])
        # conversion of legacy auth info won't complete on the first upgraded node
        node1.watch_log_for('Unable to complete conversion of legacy permissions')
        time.sleep(1)
        # run the permissions checking queries on the upgraded node
        # this will be using the legacy tables as the conversion didn't complete
        self.check_permissions(node1)
        # and check on those still on the old version
        self.check_permissions(node2)
        self.check_permissions(node3)

        # now upgrade the remaining nodes
        self.restart_nodes_to_upgrade([node2, node3])
        time.sleep(15)
        self.check_permissions(node2)
        self.check_permissions(node3)

        # we should now be able to drop the old auth tables
        session = self.patient_cql_connection(node1, user='cassandra', password='<PASSWORD>')
        session.execute('DROP TABLE system_auth.users')
        session.execute('DROP TABLE system_auth.credentials')
        session.execute('DROP TABLE system_auth.permissions')
        # and we should still be able to authenticate and check authorization
        self.check_permissions(node1)

        debug('Test completed successfully')

    def check_permissions(self, node):
        klaus = self.patient_cql_connection(node, user='klaus', password='<PASSWORD>')
        # klaus is a superuser, so should be able to list all permissions<commit_msg>Remove some sleeps<commit_after>from cassandra import Unauthorized
from dtest import Tester, debug
from assertions import assert_all, assert_invalid
from tools import since
import time

from ccmlib import repository

@since('3.0')
class TestAuthUpgrade(Tester):

    def __init__(self, *args, **kwargs):
        # Ignore these log patterns:
        self.ignore_log_patterns = [
            # This one occurs if we do a non-rolling upgrade, the node
            # it's trying to send the migration to hasn't started yet,
            # and when it does, it gets replayed and everything is fine.
            r'Can\'t send migration request: node.*is down',
        ]

        # Force cluster options that are common among versions:
        kwargs['cluster_options'] = {'authenticator': 'PasswordAuthenticator',
                                     'authorizer': 'CassandraAuthorizer'}
        Tester.__init__(self, *args, **kwargs)

    def upgrade_with_internal_auth_test(self):
        """
        Tests upgrade between 2.1->3.0 as the schema and apis around authn/authz changed

        @jira_ticket CASSANDRA-7653
        """
        cluster = self.cluster

        # Forcing cluster version on purpose
        cluster.set_install_dir(version="git:cassandra-2.1")
        cluster.populate(3).start()

        node1, node2, node3 = cluster.nodelist()

        # wait for default superuser creation
        # The log message
        # node.watch_log_for('Created default superuser')
        # will only appear on one of the three nodes, and we don't know
        # which ahead of time. Grepping all three in parallel is unpleasant.
        # See auth_test and auth_roles test for instances of this as well.
        # Should be fixed by C*-6177
        time.sleep(15)

        session = self.patient_cql_connection(node1, user='cassandra', password='<PASSWORD>')
        session.execute("CREATE USER klaus WITH PASSWORD '<PASSWORD>' SUPERUSER")
        session.execute("CREATE USER michael WITH PASSWORD '<PASSWORD>' NOSUPERUSER")
        session.execute("CREATE KEYSPACE ks WITH replication = {'class':'SimpleStrategy', 'replication_factor':1}")
        session.execute("CREATE TABLE ks.cf1 (id int primary key, val int)")
        session.execute("CREATE TABLE ks.cf2 (id int primary key, val int)")
        session.execute("GRANT MODIFY ON ks.cf1 TO michael")
        session.execute("GRANT SELECT ON ks.cf2 TO michael")

        self.check_permissions(node1)
        session.shutdown()
        # upgrade the cluster to 3.0, restarting node 1 in the process
        # note - will need changing when we branch for 3.0
        self.upgrade_to_version("git:trunk", nodes=[node1])
        # conversion of legacy auth info won't complete on the first upgraded node
        node1.watch_log_for('Unable to complete conversion of legacy permissions')
        # run the permissions checking queries on the upgraded node
        # this will be using the legacy tables as the conversion didn't complete
        self.check_permissions(node1)
        # and check on those still on the old version
        self.check_permissions(node2)
        self.check_permissions(node3)

        # now upgrade the remaining nodes
        self.restart_nodes_to_upgrade([node2, node3])
        self.check_permissions(node2)
        self.check_permissions(node3)

        # we should now be able to drop the old auth tables
        session = self.patient_cql_connection(node1, user='cassandra', password='<PASSWORD>')
        session.execute('DROP TABLE system_auth.users')
        session.execute('DROP TABLE system_auth.credentials')
        session.execute('DROP TABLE system_auth.permissions')
        # and we should still be able to authenticate and check authorization
        self.check_permissions(node1)

        debug('Test completed successfully')

    def check_permissions(self, node):
        klaus = self.patient_cql_connection(node, user='klaus', password='<PASSWORD>')
        # klaus is a superuser, so should be able to list all permissions
{'fraction_non_alphanumeric': 0.08246913580246913, 'fraction_numerical': 0.011481481481481481, 'mean_word_length': 2.8557829604950022, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '336501', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2163, 'n_words': 741}