|
| 1 | +#!/bin/sh |
| 2 | +#/ |
| 3 | +#/ NAME: |
| 4 | +#/ update-user-and-team-dn-for-ldap - For a GitHub Enterprise Instance using LDAP, |
| 5 | +#/ reads in a `users.txt` files and `teams.txt` files to change the distinguished |
| 6 | +#/ name (DN) of each user and team to your new LDAP provider's DN. |
| 7 | +#/ |
| 8 | +#/ AUTHOR: @IAmHughes |
| 9 | +#/ |
| 10 | +#/ DESCRIPTION: |
| 11 | +#/ For a GitHub Enterprise Instance using LDAP, reads in a `users.txt` files and |
| 12 | +#/ `teams.txt` files to change the distinguished name (DN) of each user and team to |
| 13 | +#/ your new LDAP provider's DN. |
| 14 | +#/ |
| 15 | +#/ PRE-REQUISITES: |
| 16 | +#/ Before running this script, you must create a Personal Access Token (PAT) |
| 17 | +#/ at https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ |
| 18 | +#/ with the permissions <repo>, <admin:org>, <user>, and <site_admin> scopes. Read more |
| 19 | +#/ about scopes here: https://developer.github.com/apps/building-oauth-apps/scopes-for-oauth-apps/ |
| 20 | +#/ |
| 21 | +#/ Once created, you must export your PAT as an environment variable |
| 22 | +#/ named <GITHUB_TOKEN>. |
| 23 | +#/ |
| 24 | +#/ - Exporting PAT as GITHUB_TOKEN |
| 25 | +#/ $ export GITHUB_TOKEN=abcd1234efg567 |
| 26 | +#/ |
| 27 | +#/ Additionally you will need to set the $API_ROOT at the top of the script to |
| 28 | +#/ your instance of GitHub Enterprise. |
| 29 | +#/ - _i.e._: https://MyGitHubEnterprise.com/api/v3 |
| 30 | +#/ |
| 31 | +#/ Finally, you need to set up your `users.txt` and `teams.txt` files in the directory you |
| 32 | +#/ will run the script from. They need to be in the format of <user>:<newDN> or <team>:<newDN> |
| 33 | +#/ where <user> or <team> is the respective username or team name in GitHub that should map to |
| 34 | +#/ the new DN, <newDN>, for that user or team in the new LDAP provider. |
| 35 | +#/ |
| 36 | +#/ - Sample users.txt file: |
| 37 | +#/ <user1>:<newDN> |
| 38 | +#/ <another_user>:<newDN> |
| 39 | +#/ <my_other_user>:<newDN> |
| 40 | +#/ |
| 41 | +#/ - Sample teams.txt file: |
| 42 | +#/ <team1>:<newDN> |
| 43 | +#/ <my_team>:<newDN> |
| 44 | +#/ <another_team>:<newDN> |
| 45 | +#/ |
| 46 | +#/ API DOCUMENTATION: |
| 47 | +#/ All documentation can be found at https://developer.github.com/v3/ |
| 48 | + |
| 49 | +######## |
| 50 | +# VARS # |
| 51 | +######## |
| 52 | +API_ROOT="https://<your-domain>/api/v3" |
| 53 | +GITHUB_TOKEN="" |
| 54 | +USER_MAPPING_FILE="./users.txt" |
| 55 | +TEAM_MAPPING_FILE="./teams.txt" |
| 56 | + |
| 57 | +##################### |
| 58 | +# PROCESS USER FILE # |
| 59 | +##################### |
| 60 | + |
| 61 | +# Read each line of text file, including last line |
| 62 | +while read -r line || [[ -n "${line}" ]]; do |
| 63 | + |
| 64 | + # Error Handling - Check if line is empty |
| 65 | + if [[ -z ${line} ]]; then |
| 66 | + echo "Line is empty, exiting script." |
| 67 | + continue |
| 68 | + fi |
| 69 | + |
| 70 | + # Get Username |
| 71 | + username=$(echo ${line} | awk -F':' {'print $1'}) |
| 72 | + |
| 73 | + # Get DN |
| 74 | + ldap_dn=$(echo ${line} | awk -F':' {'print $2'}) |
| 75 | + |
| 76 | + # Error Handling - Verify Username and LDAP DN were found |
| 77 | + if [[ -z ${username} ]]; then |
| 78 | + echo "Username not found. Username was set to: ${username}" |
| 79 | + continue |
| 80 | + fi |
| 81 | + |
| 82 | + if [[ -z ${ldap_dn} ]]; then |
| 83 | + echo "LDAP DN not found. LDAP DN was set to: ${ldap_dn} for Username: ${username}" |
| 84 | + fi |
| 85 | + |
| 86 | + # Error Handling - Verify user exists in GitHub Enterprise |
| 87 | + # Curl options used - more info [here](http://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html) |
| 88 | + # -s = silent |
| 89 | + # -o = output - we don't want the output other than the status code, so send to /dev/null |
| 90 | + # -I = fetch header only |
| 91 | + # -w = The option we want to write-out, so we specify %{http_code} |
| 92 | + response="$(curl -s -o /dev/null -I -w "%{http_code}" --request GET \ |
| 93 | + --url ${API_ROOT}/users/${username} \ |
| 94 | + --header "authorization: Bearer ${GITHUB_TOKEN}")" |
| 95 | + |
| 96 | + # Generate body for PATCH curl call below |
| 97 | + function generate_patch_data_for_users() |
| 98 | + { |
| 99 | + cat <<EOF |
| 100 | + { |
| 101 | + "ldap_dn": "$ldap_dn" |
| 102 | + } |
| 103 | +EOF |
| 104 | + } |
| 105 | + |
| 106 | + # User Exists, call API to Update LDAP Mapping |
| 107 | + if [[ response -eq 200 ]]; then |
| 108 | + curl -s --request PATCH \ |
| 109 | + --url ${API_ROOT}/admin/ldap/users/${username}/mapping \ |
| 110 | + --header "authorization: Bearer ${GITHUB_TOKEN}" \ |
| 111 | + --header "content-type: application/json" \ |
| 112 | + --data "$(generate_patch_data)" |
| 113 | + fi |
| 114 | +done < "${USER_MAPPING_FILE}" |
| 115 | + |
| 116 | +##################### |
| 117 | +# PROCESS TEAM FILE # |
| 118 | +##################### |
| 119 | + |
| 120 | +# Read each line of text file, including last line |
| 121 | +while read -r line || [[ -n "${line}" ]]; do |
| 122 | + |
| 123 | + # Error Handling - Check if Line is Empty |
| 124 | + if [[ -z ${line} ]]; then |
| 125 | + echo "Line is empty, exiting script." |
| 126 | + continue |
| 127 | + fi |
| 128 | + |
| 129 | + # Get Team ID |
| 130 | + team_id=$(echo ${line} | awk -F':' {'print $1'}) |
| 131 | + # Get DN |
| 132 | + ldap_dn=$(echo ${line} | awk -F':' {'print $2'} |
| 133 | +
|
| 134 | + # Error Handling - Verify Team ID and LDAP DN were found |
| 135 | + if [[ -z ${team_id} ]]; then |
| 136 | + echo "Team not found. Team ID was set to: ${team_id}" |
| 137 | + continue |
| 138 | + fi |
| 139 | +
|
| 140 | + if [[ -z ${ldap_dn} ]]; then |
| 141 | + echo "LDAP DN not found. LDAP DN was set to: ${ldap_dn} for Team: ${team_id}" |
| 142 | + fi |
| 143 | +
|
| 144 | + # Error Handling - Verify team exists in GitHub Enterprise |
| 145 | + # Curl options used - more info [here](http://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html) |
| 146 | + # -s = silent |
| 147 | + # -o = output - we don't want the output other than the status code, so send to /dev/null |
| 148 | + # -I = fetch header only |
| 149 | + # -w = The option we want to write-out, so we specify %{http_code} |
| 150 | + response="$(curl -s -o /dev/null -I -w "%{http_code}" --request GET \ |
| 151 | + --url ${API_ROOT}/teams/${team_id} \ |
| 152 | + --header 'accept: application/vnd.github.hellcat-preview+json' \ |
| 153 | + --header "authorization: Bearer ${GITHUB_TOKEN}")" |
| 154 | +
|
| 155 | + # Generate body for PATCH curl call below |
| 156 | + function generate_patch_data_for_teams() |
| 157 | + { |
| 158 | + cat <<EOF |
| 159 | + { |
| 160 | + "ldap_dn": "$ldap_dn" |
| 161 | + } |
| 162 | +EOF |
| 163 | + } |
| 164 | +
|
| 165 | + # Team Exists, call API to Update LDAP Mapping |
| 166 | + if [[ response -eq 200 ]]; then |
| 167 | + curl -s --request PATCH \ |
| 168 | + --url ${API_ROOT}/admin/ldap/teams/${team_id}/mapping \ |
| 169 | + --header 'accept: application/vnd.github.hellcat-preview+json' \ |
| 170 | + --header "authorization: Bearer ${GITHUB_TOKEN}" \ |
| 171 | + --header "content-type: application/json" \ |
| 172 | + --data "$(generate_patch_data)" |
| 173 | + fi |
| 174 | +done < "${TEAM_MAPPING_FILE}" |
0 commit comments