Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins/Nginx - Double prompted for basic auth, why? Why is there an internal Jenkins auth?

Below is my nginx configuration file for Jenkins. Most of it is exactly as per I've read in the documentation.

Config file:

upstream app_server {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80 default ipv6only=on;
    server_name sub.mydomain.net;

location ^~ /jenkins/ {

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
        proxy_pass http://app_server;
        break;
    }

    auth_basic "[....] Please confirm identity...";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

}

When navigating to http://sub.mydomain.net/jenkins I get prompted for my basic auth with Server says: [....] Please confirm identify....

This is correct, but as soon a I enter the proper credentials I then get PROMPTED AGAIN for basic auth once again, but this time: Server says: Jenkins.

Where is this second hidden basic_auth coming from?! It's not making any sense to me.

Hitting CANCEL on the first prompt I then correctly receive a 401 authorization required error.

Hitting CANCEL on the second basic auth ("Server says: Jenkins") I get:

HTTP ERROR 401

Problem accessing /jenkins/. Reason:

Invalid password/token for user: _____
Powered by Jetty://

Does anyone know what's possibly going on?

like image 234
skålfyfan Avatar asked Feb 22 '16 20:02

skålfyfan


People also ask

What is Nginx authentication?

To perform authentication, NGINX makes an HTTP subrequest to an external server where the subrequest is verified. If the subrequest returns a 2xx response code, the access is allowed, if it returns 401 or 403 , the access is denied.

Can Nginx handle authentication?

The auth_request module sits between the internet and your backend server that nginx passes requests onto, and any time a request comes in, it first forwards the request to a separate server to check whether the user is authenticated, and uses the HTTP response to decide whether to allow the request to continue to the ...

Why is Jenkins running behind Nginx?

If you are experiencing the following error when attempting to run long CLI commands in Jenkins and Jenkins is running behind Nginx, it is probably due to Nginx timing out the CLI connection. You can increase the proxy_read_timeout setting as necessary so the command will complete successfully.

How does Jenkins authentication work?

Technically authentication is completely delegated to the openid provider. ("a user claims to be x, openid provider what say you, is user this x?" and then jenkins goes with whatever it the provider said) As a security feature you never share your credentials (in your case username / password) with the jenkins instance.

How to give Nginx permission to read Jenkins web root folder?

To give Nginx permission to read Jenkins web root folder, add the nginx user to the Jenkins group: If the last command failed because the nginx user is not defined in the system, then you can try adding the www-data user to the Jenkins group:

How do I negotiate authorization in Jenkins?

Note that Jenkins does not do any authorization negotiation. i.e. it immediately returns a 403 (Forbidden) response instead of a 401 (Unauthorized) response, so make sure to send the authentication information from the first request (aka "preemptive authentication").


1 Answers

Found the solution to my issue by searching for Nginx used as a reverse proxy for any other application with basic_auth.

Solution was the answer found here: https://serverfault.com/questions/511846/basic-auth-for-a-tomcat-app-jira-with-nginx-as-reverse-proxy

The line I was missing from my nginx configuration was:

 # Don't forward auth to Tomcat
 proxy_set_header   Authorization "";

By default, it appears that after basic auth Nginx will additionally forward the auth headers to Jenkins and this is what was leading to my issue. Jenkins receives the forwarded auth headers and then thinks it needs to authorize itself too?!

If we set our reverse proxy to not forward any authorization headers as shown above then everything works as it should. Nginx will prompt basic_auth and after successful auth we explicitly clear (reset?) the auth headers when forwarding to our reverse proxy.

like image 138
skålfyfan Avatar answered Oct 28 '22 10:10

skålfyfan