Skip to content

Commit 37ae55f

Browse files
authored
Merge pull request #417 from some-natalie/master
Add SQL queries for GitHub Enterprise Server
2 parents f591871 + 7902037 commit 37ae55f

30 files changed

+710
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ Make a pull request and we'll consider it.
1515
* _hooks_: want to find out how to write a consumer for [our web hooks](https://developer.github.com/webhooks/)? The examples in this subdirectory show you how. We are open for more contributions via pull requests.
1616
* _pre-receive-hooks_: this one contains [pre-receive-hooks](https://help.github.com/enterprise/admin/guides/developer-workflow/about-pre-receive-hooks/) that can block commits on GitHub Enterprise that do not fit your requirements. Do you have more great examples? Create a pull request and we will check it out.
1717
* _scripts_: want to analyze or clean-up your Git repository? The scripts in this subdirectory show you how. We are open for more contributions via pull requests
18+
* _sql_: here are sql scripts for custom reporting for GitHub Enterprise Server. We are open for more contributions via pull requests.

sql/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SQL Queries for GitHub Enterprise Server
2+
3+
:warning: While these are all read-only queries and do not write to the database, run these directly against your GitHub Enterprise Server database at your own risk. A safer method to run these is outlined [here](USAGE.md).
4+
5+
Each query has a comment at the top of the file elaborating what it does, etc.
6+
7+
## Audit queries
8+
9+
The `audit` folder has queries that are all around auditing credentials, webhooks, apps, etc.
10+
11+
- `admin-tokens.sql` - A report of all tokens with the `site_admin` scope and when they were last used.
12+
- `authorizations.sql` - A report of all personal access tokens and when they were last used. Same as above, but without the `site_admin` scope limitation. This is a big report.
13+
- `deploy-keys.sql` - A report of all deploy keys, when it was last used, who set it up and when, how long the key is, and what repository it's tied to.
14+
- `github-apps.sql` - A report of all GitHub apps, who owns them, the scope it's installed at, if it's public or not, and the URL it's sending data to.
15+
- `hooks-repos.sql` - A report of all repository webhooks used in the past week, who owns it, and where the webhook goes. This is limited to a week based on the length of time these are kept in the `hookshot_delivery_logs` table.
16+
- `hooks-users.sql` - Same report as above, but for user-owned webhooks.
17+
- `oauth-apps.sql` - A report of all OAuth apps, who owns it, where it goes, and when it was last used.
18+
- `user-emails.sql` - A report of all emails that don't match a list of approved domains you define in the `WHERE` clause. This query should be deprecated by [this issue](https://github.com/github/roadmap/issues/204).
19+
- `user-ssh-keys.sql` - A report of all user SSH keys, when it was last used, when it was set up, and how long the key is.
20+
21+
## Metrics queries
22+
23+
The `metrics` folder has queries that are all around usage of various features in GitHub Enterprise Server.
24+
25+
- `actions-summary.sql` - A monthly summary of runtime hours, seconds waiting in queue before dispatch, and job count for GitHub Actions usage.
26+
- `commit-count.sql` - This pulls a "high score" report of all users, all commits, from all time.
27+
- `commit-summary.sql` - A month-by-month summary of commits pushed to GitHub Enterprise Server (using the commit date).
28+
- `count-tabs.sql` - A report of the custom tabs users put in their repositories.
29+
- `issue-report.sql` - A report of active issues within the past X days.
30+
- `linguist-report.sql` - This returns the "size" of each language in each repository and when the repo was last updated. This can be a very large report.
31+
- `linguist-stats.sql` - This returns the count of repositories containing each language and a sum "size" of code in that language for all repos pushed to in the past year. The time limit is adjustable.
32+
- `most-recent-active-repos.sql` - A list of repositories, when they were last updated, who owns them, and the disk space associated with each.
33+
- `pr-report.sql` - This pulls a report of pull requests including the repo name, user name, files included, times it was created/updated/merged, and comments. It can filter by organization or return all PRs in GHES.
34+
- `prereceive-hooks.sql` - A list of pre-receive hooks that are enabled by each repository and who owns the repo.
35+
- `public-repo-owners.sql` - A list of all users or orgs who own repositories marked as "public", a count of public repos, and the user or org email address.
36+
- `reaction-stats.sql` - A count of the reactions used in GHES for trivia.
37+
- `staff-notes.sql` - Returns a list of organizations or users with `staff_notes`.
38+
- `user-report.sql` - Returns username, id, created/suspended date, issues created for all time and in the past 30 days, number of repos owned, and how many pull requests they've opened.
39+
40+
## Security queries
41+
42+
The `security` folder has queries that are all around dependency alerts and any other security features.
43+
44+
- `active-repo-report.sql` - A list of all detected HIGH and CRITICAL vulnerabilities from repos pushed to in the past 90 days. It also returns who owns it and further details on the exact vulnerability. The threshold of time and severity to return is adjustable.
45+
- `vuln-critical-count.sql` - A count of repositories affected by each CRITICAL vulnerability.
46+
- `vuln-report.sql` - A report of all detected vulnerabilities in every single repo in GHES, who owns it, when it was last pushed to, the platform of the vulnerability, and the GHSA/MITRE/WhiteSource info on it. This can be a very large report.

sql/USAGE.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Using the SQL queries
2+
3+
The safest way to run these queries is by using the backup created by [backup-utils](https://github.com/github/backup-utils) loaded into another database server. This database can be quite large and GitHub Enterprise Server can be sensitive to I/O intensive operations that aren't part of anticipated load.
4+
5+
:warning: This database contains sensitive information. Please treat it appropriately within your company / network!
6+
7+
A simple way to do this would be to install a MySQL 5.7 server on the VM receiving the backups and load it automatically. You can then connect to in using `root` with no password, or whatever you set up for authentication. What this looks like in practice would be similar to this shell script:
8+
9+
```shell
10+
# Stop MySQL
11+
sudo systemctl stop mysqld.service
12+
13+
# Unzip the most current backup
14+
gunzip -c /data/current/mysql.sql.gz > /data/mysql.tar
15+
16+
# Untar the current backup
17+
tar xf /data/mysql.tar --directory=/home/github/restore-job/
18+
19+
# Remove the temporary tarball
20+
rm /data/mysql.tar
21+
22+
# Clear the data directory before restoring
23+
sudo rm -rf /var/lib/mysql-data/*
24+
25+
# Run the Percona backup restore
26+
cd /home/github/restore-job && sudo innobackupex --defaults-file=backup-my.cnf --copy-back --datadir=/var/lib/mysql-data .
27+
28+
# Restore the innodb buffer pool
29+
sudo cp -n /var/lib/mysql/ib_buffer_pool /var/lib/mysql-data/
30+
31+
# Restore the innodb data
32+
sudo cp -n /var/lib/mysql/ibdata1 /var/lib/mysql-data/
33+
34+
# Restore the first and second logs
35+
sudo cp -n /var/lib/mysql/ib_logfile0 /var/lib/mysql-data/
36+
sudo cp -n /var/lib/mysql/ib_logfile1 /var/lib/mysql-data/
37+
38+
# Reset ownership
39+
sudo chown -R mysql:mysql /var/lib/mysql-data
40+
41+
# Restore SELinux contexts (if applicable)
42+
sudo restorecon -R /var/lib/mysql-data
43+
44+
# Start MySQL
45+
sudo systemctl start mysqld.service
46+
47+
# Clear the working directory to save some disk space
48+
rm -rf /home/github/restore-job/*
49+
```

sql/audit/admin-tokens.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* This pulls a list of all apps, tokens, and scopes associated with that token
3+
* as well as when it was last used, created, and updated for anything with
4+
* the `site_admin` scope.
5+
*/
6+
SELECT
7+
z.id,
8+
u.login as owner_name,
9+
u.type as owner_type,
10+
a.name as app_name,
11+
z.accessed_at,
12+
z.created_at,
13+
z.updated_at,
14+
z.description,
15+
z.scopes
16+
FROM
17+
github_enterprise.oauth_authorizations z
18+
JOIN github_enterprise.users u ON
19+
z.user_id = u.id
20+
LEFT JOIN github_enterprise.oauth_applications a ON
21+
z.application_id = a.id
22+
WHERE
23+
z.scopes LIKE "%site_admin%"

sql/audit/authorizations.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This pulls a list of all apps, tokens, and scopes associated with that token
3+
* as well as when it was last used, created, and updated.
4+
*/
5+
SELECT
6+
z.id,
7+
u.login as owner_name,
8+
u.type as owner_type,
9+
a.name as app_name,
10+
z.accessed_at,
11+
z.created_at,
12+
z.updated_at,
13+
z.description,
14+
z.scopes
15+
FROM
16+
github_enterprise.oauth_authorizations z
17+
JOIN github_enterprise.users u ON
18+
z.user_id = u.id
19+
LEFT JOIN github_enterprise.oauth_applications a ON
20+
z.application_id = a.id;

sql/audit/deploy-keys.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This query returns SSH deploy keys and what repo they're tied to, when last
3+
* used, etc.
4+
*/
5+
SELECT
6+
d.title as key_name,
7+
d.created_at,
8+
d.updated_at,
9+
d.verified_at,
10+
d.accessed_at as last_used,
11+
length(d.key) as key_length,
12+
u.login as created_by_name,
13+
d.created_by as created_by_type,
14+
r.name as repo_name,
15+
x.login as repo_owner_name
16+
FROM
17+
github_enterprise.public_keys d
18+
LEFT JOIN github_enterprise.users u ON
19+
d.creator_id = u.id
20+
LEFT JOIN github_enterprise.repositories r ON
21+
d.repository_id = r.id
22+
LEFT JOIN (
23+
SELECT
24+
id,
25+
login,
26+
type
27+
FROM
28+
github_enterprise.users u2
29+
) x ON
30+
x.id = r.owner_id
31+
WHERE
32+
d.repository_id IS NOT NULL;

sql/audit/github-apps.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* This pulls a list of all github apps, who owns them, and when they were
3+
* created or updated.
4+
*/
5+
SELECT
6+
i.id,
7+
i.bot_id,
8+
i.name as integration_name,
9+
u.login as owner,
10+
u.type,
11+
i.url,
12+
i.created_at,
13+
i.updated_at,
14+
i.public,
15+
i.slug as friendly_name,
16+
i.public
17+
FROM
18+
github_enterprise.integrations i
19+
JOIN github_enterprise.users u ON
20+
i.owner_id = u.id;

sql/audit/hooks-repos.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This brings up the list of REPOSITORY webhooks that have been active in the
3+
* past week, who owns them, and where the webhook goes.
4+
*/
5+
SELECT
6+
DISTINCT h.id,
7+
u.login as creator,
8+
h.updated_at,
9+
r.name as repo_name,
10+
u.login as repo_owner,
11+
u.type as owner_type,
12+
c.value as url,
13+
MAX(l.delivered_at) as latest_delivery
14+
FROM
15+
github_enterprise.hooks h
16+
JOIN github_enterprise.hook_config_attributes c ON
17+
h.id = c.hook_id
18+
JOIN github_enterprise.users u ON
19+
h.creator_id = u.id
20+
JOIN github_enterprise.hookshot_delivery_logs l ON
21+
h.id = l.hook_id
22+
JOIN github_enterprise.repositories r ON
23+
h.installation_target_id = r.id
24+
WHERE
25+
c.key = 'url'
26+
AND h.installation_target_type = 'Repository'
27+
GROUP BY
28+
h.id
29+
ORDER BY
30+
MAX(l.delivered_at) DESC;

sql/audit/hooks-users.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This brings up the list of USER webhooks that have been active in the past
3+
* week, who owns them, and where the webhook goes.
4+
*/
5+
SELECT
6+
DISTINCT h.id,
7+
u.login as creator,
8+
h.updated_at,
9+
u.login as repo_owner,
10+
u.type as owner_type,
11+
c.value as url,
12+
MAX(l.delivered_at) as latest_delivery
13+
FROM
14+
github_enterprise.hooks h
15+
JOIN github_enterprise.hook_config_attributes c ON
16+
h.id = c.hook_id
17+
JOIN github_enterprise.users u ON
18+
h.creator_id = u.id
19+
JOIN github_enterprise.hookshot_delivery_logs l ON
20+
h.id = l.hook_id
21+
WHERE
22+
c.key = 'url'
23+
AND h.installation_target_type = 'User'
24+
GROUP BY
25+
h.id
26+
ORDER BY
27+
MAX(l.delivered_at) DESC;

sql/audit/oauth-apps.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This pulls up a list of all OAuth apps and where they go, as well as when
3+
* they were last updated and what login they are associated with.
4+
*/
5+
SELECT
6+
o.name,
7+
o.url,
8+
o.callback_url,
9+
o.created_at,
10+
o.updated_at,
11+
u.login,
12+
u.type
13+
FROM
14+
github_enterprise.oauth_applications o
15+
JOIN github_enterprise.users u ON
16+
o.user_id = u.id;

0 commit comments

Comments
 (0)