7
votes

I have a GitHub repo setup with a Jenkinsfile. The GitHub Organization Folder Plugin will execute the pipeline from the supplied Jenkinsfile.

The final step of the pipeline is the deploy step. The deploy step checks if the branch has AWS credentials using the CloudBees Amazon Web Services Credentials Plugin. If it detects credentials it will deploy otherwise it won't.

All members have read only access to the GitHub repository, whenever they want to change something they have to create a pull request.(Only admins can merge) If there is a new pull request the Jenkins server will run the pipeline until the deploy step, to check if the pull request can be integrated to the master branch. The finalstep of the pipeline is the deploy step, this shouldn't be executed for pull requests.

stage('Deploy') {

    // Deploy with the right credentials
    try {
        withCredentials([[
            $class: 'AmazonWebServicesCredentialsBinding', 
            accessKeyVariable: 'AWS_ACCESS_KEY_ID', 
            credentialsId: env.BRANCH_NAME + '_AWS_Credentials', 
            secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
        ]]) {
            echo("Deploying to " + env.BRANCH_NAME + "...")
            ...
        }
    } catch(all) {
        echo("Not deploying for branch: " + env.BRANCH_NAME)
    }
}

The problem is that team members can create a pull request with a changed Jenkinsfile.

So let's say one of the team members get's hacked. They can now infect the production environment by creating a pull request with a changed Jenkinsfile which does the following:

credentialsId: 'master_AWS_Credentials', 

How do I prevent Jenkins from running the pipeline for a changed Jenkinsfile? Or how do I make pull request use the Jenkinsfile from the master branch instead?

2
How you find a solution eventually? - tamerlaha

2 Answers

0
votes

As far as I know it is not documented, but only a pull request from a branch of the repository can execute a changed JenkinsFile. If someone make a fork and a pull request, the Jenkins file exectued by Jenkins will be the one of the targeted branch of the pull request, and not the one of the pull request.

If admins level is required to merge anything (not only the master branch) so you are safe.

-3
votes

you probably forgot to add the handler for the jenkins file, look at the repository to get the setting of the preventation. Hope this will fix it for you.