Skip to content

Commit 271a72a

Browse files
committed
add audit queries
1 parent f591871 commit 271a72a

11 files changed

+234
-0
lines changed

sql/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SQL Queries for GitHub Enterprise Server
2+
3+
:warning: 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+
## Audit queries
6+
7+
The `audit` folder has queries that are all around auditing credentials, webhooks, apps, etc.
8+
9+
- `admin-tokens.sql` - A report of all tokens with the `site_admin` scope and when they were last used.
10+
- `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.
11+
- `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.
12+
- `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.
13+
- `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.
14+
- `hooks-users.sql` - Same report as above, but for user-owned webhooks.
15+
- `oauth-apps.sql` - A report of all OAuth apps, who owns it, where it goes, and when it was last used.
16+
- `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).
17+
- `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.
18+
19+
## Security queries
20+
21+
The `security` folder has queries that are all around dependency alerts and any other security features.
22+
23+
## Usage queries
24+
25+
The `usage` folder has queries that are all around usage of various features in GitHub Enterprise Server.

sql/USAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Using the SQL queries

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;

sql/audit/user-emails.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* This pulls a list of all email addresses and the user account it is tied to
3+
* that don't match the list of domains in the WHERE clause. Add however many
4+
* "%domain.com" needed to cover your company's approved domains.
5+
*
6+
* This query should be deprecated by this issue:
7+
* https://github.com/github/roadmap/issues/204
8+
*
9+
* If you want a list of all emails, remove the WHERE clause.
10+
*/
11+
SELECT
12+
u.login,
13+
e.email,
14+
u.suspended_at
15+
FROM
16+
github_enterprise.users u
17+
JOIN github_enterprise.user_emails e ON
18+
e.user_id = u.id
19+
WHERE
20+
u.gravatar_email != e.email
21+
AND e.email not like "%company.com"
22+
AND e.email not like "%.tld";

0 commit comments

Comments
 (0)