示例#1
0
    def setUp(self):
        # some words to test out
        self.good_nouns = ['good', 'good_noun', 'goodNoun', 'good1']

        # some words that should throw validation errors
        self.bad_nouns = [
            '_foo',
            'foo_',
            '-foo',
            'foo-',
            'foo bar',
            '1foo',
            'injection;attack',
            ';injection',
            'injection;',
        ]

        self.username = "******"
        self.password = "******"

        # mock connection pools so nothing gets a real db connection
        self.mock_pool_for_cred = self.create_patch(
            'core.db.backend.pg._pool_for_credentials')

        # mock open connection, only to check if it ever gets called directly
        self.mock_connect = self.create_patch(
            'core.db.backend.pg.psycopg2.connect')

        # open mocked connection
        self.backend = PGBackend(self.username,
                                 self.password,
                                 repo_base=self.username)
    def setUp(self):
        # some words to test out
        self.good_nouns = ['good', 'good_noun', 'good-noun']

        # some words that shoudl throw validation errors
        self.bad_nouns = [
            '_foo',
            'foo_',
            '-foo',
            'foo-',
            'foo bar',
            'injection;attack',
            ';injection',
            'injection;',
        ]

        self.username = "******"
        self.password = "******"

        # mock open connection,
        # or else it will try to create a real db connection
        self.mock_psychopg = self.create_patch('core.db.backend.pg.psycopg2')

        # open mocked connection
        self.backend = PGBackend(self.username,
                                 self.password,
                                 repo_base=self.username)
示例#3
0
def migrate_datahub_accounts(*args, **kwargs):
    old_users = DataHubLegacyUser.objects.all()
    apps = App.objects.exclude(legacy_user=None)
    users = User.objects.all()
    print("Old model users: {0} New model users: {1}".format(
        len(old_users), len(users)))
    print("Apps: {0}".format(len(apps)))

    new_users = []
    # Throw out all of these changes if it fails somehow
    with transaction.atomic():
        for old_user in old_users:
            try:
                User.objects.get(username=old_user.username)
            except User.DoesNotExist:
                new_user = User.objects.create_user(
                    username=old_user.username,
                    email=old_user.email)
                new_users.append(new_user)
            DataHubManager.create_user_data_folder(
                repo_base=old_user.username)

        for app in apps:
            username = app.legacy_user.username
            print("{0} legacy_user is {1}".format(
                app.app_id,
                username))
            if app.user is None:
                new_user = User.objects.get(username=username)
                app.user = new_user
                app.save(update_fields=['user'])
            print("    user is {0}".format(app.user.username))

    print("Migrated Users: {0}".format(new_users))

    # grant existing users access to the dh_public role,
    # which is used to share select-only repos
    superuser_username = settings.DATABASES['default']['USER']
    superuser_password = settings.DATABASES['default']['PASSWORD']

    users = User.objects.exclude(username__in=[settings.PUBLIC_ROLE,
                                 settings.OAUTH2_APP_OWNER])
    pg = PGBackend(superuser_username, superuser_password)

    for user in users:
        query = 'GRANT %s to %s' % (settings.PUBLIC_ROLE, user.username)
        print(query)
        try:
            pg.execute_sql(query)
        except ValueError:
            print('Skipped %s due to missing role.' % user.username)
            pass
    pg.close_connection()

    print 'grants complete'
示例#4
0
    def setUp(self):
        # some words to test out
        self.good_nouns = ['good', 'good_noun', 'good-noun']
        # some words that shoudl throw validation errors
        self.bad_nouns = [
            '_foo',
            'foo_',
            '-foo',
            'foo-',
            'foo bar',
            'injection;attack',
            ';injection',
            'injection;',
        ]

        self.username = "******"
        self.password = "******"

        # mock the execute_sql function
        self.mock_execute_sql = self.create_patch(
            'core.db.backend.pg.PGBackend.execute_sql')
        self.mock_execute_sql.return_value = True

        # mock the mock_check_for_injections, which checks for injection
        # attacks
        self.mock_check_for_injections = self.create_patch(
            'core.db.backend.pg.PGBackend._check_for_injections')

        self.mock_validate_table_name = self.create_patch(
            'core.db.backend.pg.PGBackend._validate_table_name')

        # mock open connection, or else it will try to
        # create a real db connection
        self.mock_open_connection = self.create_patch(
            'core.db.backend.pg.PGBackend.__open_connection__')

        # mock the psycopg2.extensions.AsIs - many of the pg.py methods use it
        # Its return value (side effect) is the call value
        self.mock_as_is = self.create_patch('core.db.backend.pg.AsIs')
        self.mock_as_is.side_effect = lambda x: x

        # create an instance of PGBackend
        self.backend = PGBackend(self.username,
                                 self.password,
                                 repo_base=self.username)