-
Notifications
You must be signed in to change notification settings - Fork 4
Add LDAP-based git identity and SSSD config for immediate git config setup #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6229f03
acda133
d16a353
faab59c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #!/usr/bin/env bash | ||
| # Automatically configures git user.name and user.email from the user's LDAP | ||
| # profile on first interactive login to a container. Subsequent logins skip | ||
| # this entirely once git config is set. | ||
| # | ||
| # The name is read from the NSS gecos field (mapped from LDAP cn via sssd.conf). | ||
| # The email is read from LDAP via an anonymous ldapsearch (REQUIRE_AUTH_FOR_SEARCH | ||
| # is disabled on the internal ldap-gateway, so no bind credentials are needed). | ||
|
|
||
| # Only run for interactive shells | ||
| [[ $- != *i* ]] && return | ||
|
|
||
| # Only if git is available | ||
| command -v git >/dev/null 2>&1 || return | ||
| command -v ldapsearch >/dev/null 2>&1 || return | ||
|
|
||
| # Skip if already configured — user-set values always take precedence | ||
| [ -n "$(git config --global user.email 2>/dev/null)" ] && [ -n "$(git config --global user.name 2>/dev/null)" ] && return | ||
|
|
||
| _GIT_SETUP_USER="${USER:-$(id -un 2>/dev/null)}" | ||
| [ -z "$_GIT_SETUP_USER" ] && return | ||
| [ "$_GIT_SETUP_USER" = "root" ] && return | ||
|
|
||
| # Full name from NSS (SSSD reads the LDAP gecos attribute by default via ldap_user_gecos) | ||
| _GIT_SETUP_NAME=$(getent passwd "$_GIT_SETUP_USER" 2>/dev/null | cut -d: -f5) | ||
|
|
||
| # Email from LDAP anonymous query | ||
| _GIT_SETUP_LDAP_HOST="${LDAP_URI:-ldaps://ldap1:636}" | ||
|
|
||
| # Resolve baseDN the same way SSSD does: rootDSE namingContexts autodiscovery. | ||
| # Use LDAP_BASE_DN if explicitly set; otherwise query rootDSE. | ||
| # - Single namingContexts entry -> use it directly | ||
| # - Multiple namingContexts -> use defaultNamingContext | ||
| # - Neither resolvable -> abort | ||
| if [ -n "${LDAP_BASE_DN:-}" ]; then | ||
| _GIT_SETUP_LDAP_BASE="$LDAP_BASE_DN" | ||
| else | ||
| _GIT_SETUP_ROOTDSE=$(ldapsearch -x -H "$_GIT_SETUP_LDAP_HOST" -b "" -s base namingContexts defaultNamingContext 2>/dev/null) | ||
| _GIT_SETUP_NC_COUNT=$(echo "$_GIT_SETUP_ROOTDSE" | grep -c '^namingContexts:') | ||
| if [ "$_GIT_SETUP_NC_COUNT" -eq 1 ]; then | ||
| _GIT_SETUP_LDAP_BASE=$(echo "$_GIT_SETUP_ROOTDSE" | awk '/^namingContexts:/{print $2; exit}') | ||
| elif [ "$_GIT_SETUP_NC_COUNT" -gt 1 ]; then | ||
| _GIT_SETUP_LDAP_BASE=$(echo "$_GIT_SETUP_ROOTDSE" | awk '/^defaultNamingContext:/{print $2; exit}') | ||
| fi | ||
| unset _GIT_SETUP_ROOTDSE _GIT_SETUP_NC_COUNT | ||
| fi | ||
| [ -z "${_GIT_SETUP_LDAP_BASE:-}" ] && return | ||
|
|
||
| _GIT_SETUP_EMAIL=$(ldapsearch -x \ | ||
| -H "$_GIT_SETUP_LDAP_HOST" \ | ||
| -b "$_GIT_SETUP_LDAP_BASE" \ | ||
| "(uid=${_GIT_SETUP_USER})" mail 2>/dev/null \ | ||
| | awk '/^mail:/{print $2; exit}') | ||
|
|
||
| if [ -n "$_GIT_SETUP_NAME" ]; then | ||
| git config --global user.name "$_GIT_SETUP_NAME" | ||
| fi | ||
|
|
||
| if [ -n "$_GIT_SETUP_EMAIL" ]; then | ||
| git config --global user.email "$_GIT_SETUP_EMAIL" | ||
| fi | ||
|
|
||
| unset _GIT_SETUP_USER _GIT_SETUP_NAME _GIT_SETUP_EMAIL _GIT_SETUP_LDAP_HOST _GIT_SETUP_LDAP_BASE _GIT_SETUP_ROOTDSE _GIT_SETUP_NC_COUNT | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # OpenLDAP client configuration for containers. | ||
| # Mirrors the TLS check level used by SSSD (ldap_tls_reqcert = allow) so that | ||
| # ldapsearch and other ldap-utils tools accept the internal certificate without | ||
| # additional flags. | ||
| TLS_REQCERT allow |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,9 @@ auth_provider = ldap | |
| ldap_uri = ldaps://ldap1:636, ldaps://ldap2:636 | ||
| ldap_tls_reqcert = allow | ||
|
|
||
| # Map LDAP cn attribute to the NSS gecos field so that tools like getent, | ||
| # finger, and the git-identity profile script can read the user's full name. | ||
| ldap_user_gecos = cn | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value of this setting
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went ahead and removed these lines |
||
|
|
||
| # set a timeout long enough for a push notification to be responded to | ||
| ldap_opt_timeout = 60 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we bailout here as well if user is root? Just to avoid the unnessecary LDAP lookup?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.