Files @ f734d107296e
Branch filter:

Location: kallithea/Jenkinsfile - annotation

Mads Kiilerich
auth: for default permissions, use existing explicit query result values instead of following dot references in ORM result objects

There has been reports of spurious crashes on resolving references like
.repository from Permissions:

File ".../kallithea/lib/auth.py", line 678, in __wrapper
if self.check_permissions(user):
File ".../kallithea/lib/auth.py", line 718, in check_permissions
return user.has_repository_permission_level(repo_name, self.required_perm)
File ".../kallithea/lib/auth.py", line 450, in has_repository_permission_level
actual_perm = self.permissions['repositories'].get(repo_name)
File ".../kallithea/lib/vcs/utils/lazy.py", line 41, in __get__
value = self._func(obj)
File ".../kallithea/lib/auth.py", line 442, in permissions
return self.__get_perms(user=self, cache=False)
File ".../kallithea/lib/auth.py", line 498, in __get_perms
return compute(user_id, user_is_admin)
File ".../kallithea/lib/auth.py", line 190, in _cached_perms_data
r_k = perm.UserRepoToPerm.repository.repo_name
File ".../sqlalchemy/orm/attributes.py", line 285, in __get__
return self.impl.get(instance_state(instance), dict_)
File ".../sqlalchemy/orm/attributes.py", line 721, in get
value = self.callable_(state, passive)
File ".../sqlalchemy/orm/strategies.py", line 710, in _load_for_state
% (orm_util.state_str(state), self.key)

sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <UserRepoToPerm at ...> is not bound to a Session; lazy load operation of attribute 'repository' cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3)

Permissions are cached between requests: SA result records are stored in in
beaker.cache.sql_cache_short and resued in following requests after the initial
session as been removed. References in Permission objects would usually give
lazy lookup ... but not outside the original session, where we would get an
error like this.

Permissions are indeed implemented/used incorrectly. That might explain a part
of the problem. Even if not fully explaining or fixing this problem, it is
still worth fixing:

Permissions are fetched from the database using Session().query with multiple
class/table names (joined together in way that happens to match the references
specified in the table definitions) - including Repository. The results are
thus "structs" with selected objects. If repositories always were retrieved
using this selected repository, everything would be fine. In some places, this
was what we did.

But in some places, the code happened to do what was more intuitive: just use
.repository and rely on "lazy" resolving. SA was not aware that this one
already was present in the result struct, and would try to fetch it again. Best
case, that could be inefficient. Worst case, it would fail as we see here.

Fix this by only querying from one table but use the "joinedload" option to
also fetch other referenced tables in the same select. (This might
inefficiently return the main record multiple times ... but that was already
the case with the previous approach.)

This change is thus doing multiple things with circular dependencies that can't
be split up in minor parts without taking detours:

The existing repository join like:
.join((Repository, UserGroupRepoToPerm.repository_id == Repository.repo_id))
is thus replaced by:
.options(joinedload(UserGroupRepoToPerm.repository))

Since we only are doing Session.query() on one table, the results will be of
that type instead of "structs" with multiple objects. If only querying for
UserRepoToPerm this means:
- perm.UserRepoToPerm.repository becomes perm.repository
- perm.Permission.permission_name looked at the explicitly queried Permission
in the result struct - instead it should look in the the dereferenced
repository as perm.permission.permission_name
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8d788789b38a
58713c2ebfff
58713c2ebfff
58713c2ebfff
58713c2ebfff
58713c2ebfff
58713c2ebfff
8d788789b38a
89e9aef9b983
ad275fcc5a90
8d788789b38a
89e9aef9b983
ad275fcc5a90
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
ad275fcc5a90
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
d9e37f7fd35b
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
bc1b11a5d548
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8d788789b38a
15a12f2a47b4
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
0acb46763886
8d788789b38a
8d788789b38a
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8d788789b38a
15a12f2a47b4
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
0acb46763886
8d788789b38a
8d788789b38a
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8d788789b38a
15a12f2a47b4
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
0acb46763886
8d788789b38a
8d788789b38a
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8a60eb2b7603
8d788789b38a
15a12f2a47b4
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
0acb46763886
0acb46763886
8d788789b38a
8d788789b38a
8d788789b38a
8d788789b38a
8a60eb2b7603
8a60eb2b7603
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
0acb46763886
8a60eb2b7603
def createvirtualenv = ''
def activatevirtualenv = ''

node {
    properties([[$class: 'BuildDiscarderProperty',
                  strategy: [$class: 'LogRotator',
                              artifactDaysToKeepStr: '',
                              artifactNumToKeepStr: '10',
                              daysToKeepStr: '',
                              numToKeepStr: '']]]);
    if (isUnix()) {
        createvirtualenv = 'rm -r $JENKINS_HOME/venv/$JOB_NAME || true && python3 -m venv $JENKINS_HOME/venv/$JOB_NAME'
        activatevirtualenv = '. $JENKINS_HOME/venv/$JOB_NAME/bin/activate'
    } else {
        createvirtualenv = 'rmdir /s /q %JENKINS_HOME%\\venv\\%JOB_NAME% || true && python3 -m venv %JENKINS_HOME%\\venv\\%JOB_NAME%'
        activatevirtualenv = 'call %JENKINS_HOME%\\venv\\%JOB_NAME%\\Scripts\\activate.bat'
    }

    stage('checkout') {
        checkout scm
        if (isUnix()) {
            sh 'hg --config extensions.purge= purge --all'
        } else {
            bat 'hg --config extensions.purge= purge --all'
        }
    }
    stage('virtual env') {
        def virtualenvscript = """$createvirtualenv
            $activatevirtualenv
            python -m pip install --upgrade pip
            pip install --upgrade setuptools
            pip install --upgrade pylint
            pip install --upgrade pytest-cov
            """
        if (isUnix()) {
            virtualenvscript += """
                pip install --upgrade python-ldap
                pip install --upgrade python-pam
                """
            sh virtualenvscript
        } else {
            bat virtualenvscript
        }
    }
    stage('setup') {
        def virtualenvscript = """$activatevirtualenv
            pip install --upgrade -e . -r dev_requirements.txt
            python setup.py compile_catalog
            """
        if (isUnix()) {
            sh virtualenvscript
        } else {
            bat virtualenvscript
        }
        stash name: 'kallithea', useDefaultExcludes: false
    }
    stage('pylint') {
        sh script: """$activatevirtualenv
            pylint -j 0 --disable=C -f parseable kallithea > pylint.out
            """, returnStatus: true
        archiveArtifacts 'pylint.out'
        try {
            step([$class: 'WarningsPublisher', canComputeNew: false, canResolveRelativePaths: false, defaultEncoding: '', excludePattern: '', healthy: '', includePattern: '', messagesPattern: '', parserConfigurations: [[parserName: 'PyLint', pattern: 'pylint.out']], unHealthy: ''])
        } catch (java.lang.IllegalArgumentException exc) {
            echo "You need to install the 'Warnings Plug-in' to display the pylint report."
            currentBuild.result = 'UNSTABLE'
            echo "Caught: ${exc}"
        }
    }
}

def pytests = [:]
pytests['sqlite'] = {
    node {
        ws {
            deleteDir()
            unstash name: 'kallithea'
            if (isUnix()) {
                sh script: """$activatevirtualenv
                    py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_sqlite.xml --cov=kallithea
                    """, returnStatus: true
            } else {
                bat script: """$activatevirtualenv
                    py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_sqlite.xml --cov=kallithea
                    """, returnStatus: true
            }
            sh 'sed --in-place "s/\\(classname=[\'\\"]\\)/\\1SQLITE./g" pytest_sqlite.xml'
            archiveArtifacts 'pytest_sqlite.xml'
            junit 'pytest_sqlite.xml'
            writeFile(file: '.coverage.sqlite', text: readFile('.coverage'))
            stash name: 'coverage.sqlite', includes: '.coverage.sqlite'
        }
    }
}

pytests['de'] = {
    node {
        if (isUnix()) {
            ws {
                deleteDir()
                unstash name: 'kallithea'
                withEnv(['LANG=de_DE.UTF-8',
                    'LANGUAGE=de',
                    'LC_ADDRESS=de_DE.UTF-8',
                    'LC_IDENTIFICATION=de_DE.UTF-8',
                    'LC_MEASUREMENT=de_DE.UTF-8',
                    'LC_MONETARY=de_DE.UTF-8',
                    'LC_NAME=de_DE.UTF-8',
                    'LC_NUMERIC=de_DE.UTF-8',
                    'LC_PAPER=de_DE.UTF-8',
                    'LC_TELEPHONE=de_DE.UTF-8',
                    'LC_TIME=de_DE.UTF-8',
                ]) {
                    sh script: """$activatevirtualenv
                        py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_de.xml --cov=kallithea
                        """, returnStatus: true
                }
                sh 'sed --in-place "s/\\(classname=[\'\\"]\\)/\\1DE./g" pytest_de.xml'
                archiveArtifacts 'pytest_de.xml'
                junit 'pytest_de.xml'
                writeFile(file: '.coverage.de', text: readFile('.coverage'))
                stash name: 'coverage.de', includes: '.coverage.de'
            }
        }
    }
}
pytests['mysql'] = {
    node {
        if (isUnix()) {
            ws {
                deleteDir()
                unstash name: 'kallithea'
                sh """$activatevirtualenv
                    pip install --upgrade MySQL-python
                    """
                withEnv(['TEST_DB=mysql://kallithea:kallithea@jenkins_mysql/kallithea_test?charset=utf8']) {
                    if (isUnix()) {
                        sh script: """$activatevirtualenv
                            py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_mysql.xml --cov=kallithea
                            """, returnStatus: true
                    } else {
                        bat script: """$activatevirtualenv
                            py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_mysql.xml --cov=kallithea
                            """, returnStatus: true
                    }
                }
                sh 'sed --in-place "s/\\(classname=[\'\\"]\\)/\\1MYSQL./g" pytest_mysql.xml'
                archiveArtifacts 'pytest_mysql.xml'
                junit 'pytest_mysql.xml'
                writeFile(file: '.coverage.mysql', text: readFile('.coverage'))
                stash name: 'coverage.mysql', includes: '.coverage.mysql'
            }
        }
    }
}
pytests['postgresql'] = {
    node {
        if (isUnix()) {
            ws {
                deleteDir()
                unstash name: 'kallithea'
                sh """$activatevirtualenv
                    pip install --upgrade psycopg2
                    """
                withEnv(['TEST_DB=postgresql://kallithea:kallithea@jenkins_postgresql/kallithea_test']) {
                    if (isUnix()) {
                        sh script: """$activatevirtualenv
                            py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_postgresql.xml --cov=kallithea
                            """, returnStatus: true
                    } else {
                        bat script: """$activatevirtualenv
                            py.test -p no:sugar --cov-config .coveragerc --junit-xml=pytest_postgresql.xml --cov=kallithea
                            """, returnStatus: true
                    }
                }
                sh 'sed --in-place "s/\\(classname=[\'\\"]\\)/\\1POSTGRES./g" pytest_postgresql.xml'
                archiveArtifacts 'pytest_postgresql.xml'
                junit 'pytest_postgresql.xml'
                writeFile(file: '.coverage.postgresql', text: readFile('.coverage'))
                stash name: 'coverage.postgresql', includes: '.coverage.postgresql'
            }
        }
    }
}
stage('Tests') {
    parallel pytests
    node {
        unstash 'coverage.sqlite'
        unstash 'coverage.de'
        unstash 'coverage.mysql'
        unstash 'coverage.postgresql'
        if (isUnix()) {
            sh script: """$activatevirtualenv
                coverage combine .coverage.sqlite .coverage.de .coverage.mysql .coverage.postgresql
                coverage xml
                """, returnStatus: true
        } else {
            bat script: """$activatevirtualenv
                coverage combine .coverage.sqlite .coverage.de .coverage.mysql .coverage.postgresql
                coverage xml
                """, returnStatus: true
        }
        try {
            step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'coverage.xml', failNoReports: false, failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, zoomCoverageChart: false])
        } catch (java.lang.IllegalArgumentException exc) {
            echo "You need to install the pipeline compatible 'CoberturaPublisher Plug-in' to display the coverage report."
            currentBuild.result = 'UNSTABLE'
            echo "Caught: ${exc}"
        }
    }
}