Skip to content

Commit

Permalink
Send back the logged in LDAP username to haproxy (#27)
Browse files Browse the repository at this point in the history
In case of successful login using the LDAP backend, the agent now sends
an SPOE message containing the username of the logged in user.

This commit also:
- Updates the haproxy test config to write a X-Authorized-User header
with the logged in username
- Updates the nginx backend config to copy that header in a response
header, to be able to test it
- Updates TestShouldAuthenticateSuccessfullyInLDAP to test the new
behavior
  • Loading branch information
manu-ns committed Feb 13, 2023
1 parent ca87a1e commit 27a3c5d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/auth/authenticator_ldap.go
Expand Up @@ -159,5 +159,5 @@ func (la *LDAPAuthenticator) Authenticate(msg *spoe.Message) (bool, []spoe.Actio
}

logrus.Debug("User is authenticated")
return true, nil, nil
return true, []spoe.Action{AuthenticatedUserMessage(username)}, nil
}
9 changes: 9 additions & 0 deletions internal/auth/messages.go
Expand Up @@ -19,3 +19,12 @@ func BuildHasErrorMessage() spoe.ActionSetVar {
Value: true,
}
}

// AuthenticatedUserMessage build a message containing the username of the authenticated user
func AuthenticatedUserMessage(username string) spoe.ActionSetVar {
return spoe.ActionSetVar{
Name: "authenticated_user",
Scope: spoe.VarScopeSession,
Value: username,
}
}
2 changes: 1 addition & 1 deletion resources/haproxy/haproxy.cfg
Expand Up @@ -28,7 +28,6 @@ frontend haproxynode
acl oauth2logout path_beg /oauth2/logout

acl dex_domain hdr_beg(host) -i dex.example.com

# define the spoe agent
filter spoe engine spoe-auth config /usr/local/etc/haproxy/spoe-auth.conf

Expand Down Expand Up @@ -66,6 +65,7 @@ backend backend_public
backend backend_app
mode http
balance roundrobin
http-request add-header X-Authorized-User %[var(sess.auth.authenticated_user)]

server node-protected-app protected-backend:80 check

Expand Down
3 changes: 3 additions & 0 deletions resources/nginx/default.conf
Expand Up @@ -9,6 +9,9 @@ server {
location / {
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
if ($http_X_Authorized_User) {
add_header Request-X-Authorized-User $http_X_Authorized_User;
}
if_modified_since off;
expires off;
etag off;
Expand Down
2 changes: 1 addition & 1 deletion tests/ldap_authentication_test.go
Expand Up @@ -14,7 +14,7 @@ func TestShouldAuthenticateSuccessfullyInLDAP(t *testing.T) {

res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)

assert.Equal(t, "john", res.Header.Get("request-x-authorized-user"))
assert.Equal(t, 200, res.StatusCode)
}

Expand Down

0 comments on commit 27a3c5d

Please sign in to comment.