GitHub is the leading company for software development and version control using Git. That allows us to create public repositories as well as private ones. Both have their own benefits. The private repositories are secured with authentication, whereas the public repositories are accessible to everyone. Sometimes, we or our teams make repositories with public access that may contain sensitive data. It’s a good idea, to configure a script that notifies us of the repositories left public access.
In this tutorial, I have created a shell script that checks for all public repositories under a GitHub account and sends a notification email. The script can be scheduled under the cronjobs to automate this.
Pre-Requisiteis
This script uses Curl and SendEmail command line utilities. So make sure you have installed both packages on your system.
sudo apt install curl sendemail
The curl command is used to call GitHub API and SendEmail is used for sending email via the SMTP server.
Create a Shell Script
I have already created a bash script for you. You can simply copy the script and write it in a file on your system.
- Create a shell script in your system and edit it in your favorite text editor:
nano check-github-public-repos.sh
- Copy the below content and paste it into the edited file.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#!/usr/bin/env bash## Set your GitHub username or organization nameGITHUB_USER="tecadmin"## Temporary files and curl binary locationTEMP_FILE="/tmp/out.txt"MAIL_LOG="/tmp/github-mail.log"CURL_BIN="/usr/bin/curl"## SMTP details for sending email notifications.SMTP_HOST="email-smtp.us-east-1.amazonaws.com"SMTP_PORT="587"SMTP_USER="XXXXXXXXXXXXXXX"SMTP_PASS="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"EMAIL_FROM="[email protected]"EMAIL_TO="[email protected]"## Script begins here.## Find out all the GitHub repositories that are publically accessible.${CURL_BIN} -s https://api.github.com/users/${GITHUB_USER}/repos | grep "https://github.com/[a-zA-Z0-9-]*/[a-zA-Z0-9-]*.git" > ${TEMP_FILE}## Send an email notification if a public repository foundif [ $? -eq 0 ]; thenecho -e "Dear Admin \n" > ${MAIL_LOG}echo -e "We found few public repositories in your GitHub account (${GITHUB_USER}) \n" >> ${MAIL_LOG}count=1while read line; doREPO_URL=`echo ${line} | cut -d":" -f2,3 | cut -d"," -f1`echo "{count}. ${REPO_URL}" >> ${MAIL_LOG}((count++))done < ${TEMP_FILE}echo -e "\n\n--\nThanks" >> ${MAIL_LOG}## Modify the email subject as per your convenienceEMAIL_SUBJECT="WARNING: Github Public Repo Found"## Send the notification email. The system must have installed "SendEmail" (not Sendmail)## command on your system.cat ${MAIL_LOG} | sendemail -l /tmp/email.log \-f ${EMAIL_FROM} \-u ${EMAIL_SUBJECT} \-t ${EMAIL_TO} \-s ${SMTP_HOST}:${SMTP_PORT} \-o tls=yes \-xu ${SMTP_USER} \-xp ${SMTP_PASS}fi## Uncomment below to print the email as output#cat ${MAIL_LOG}
- Update the “GITHUB_USER” id with your GitHub user or organization name. Also, update the SMTP details to get email notifications.
- Press “CTRL + O” to write the changes and then “CTRL + X” to quit the editor. Then make the shell script executable with the following command.
chmod +x check-github-public-repos.sh
- Now, your script is ready to run. You can simply execute your script directly on the terminal. To print the results on screen, you can uncomment the last line of the script.
bash check-github-public-repos.sh
On successful execution, if the script found any public repository in your GitHub account. You will get an email notification in your mailbox.
Schedule Script with Crontab
You can schedule this script to run daily, weekly, or twice a week. The following crontab example will run this script daily at 12:00 AM.
1 2 | ## Check GitHub public repositories 0 0 * * * bash check-github-public-repos.sh |
Conclusion
It is most important to keep our hard work safe. This script will alert you to the pubic repositories in your GitHub account. You can modify the script as per your requirements. You can also provide your enhancement via the comments. Also provide your feedback about this script, which encourages us to write more.