-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENG-1229] create github API key system to authenticate konfig pr-mer…
…ge and pr-create (#225)
- Loading branch information
1 parent
a4cdd85
commit fc8a924
Showing
11 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Guard API Key Leak | ||
on: push | ||
|
||
jobs: | ||
detect-key-leak: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v3 | ||
|
||
- name: yarn build | ||
run: yarn && yarn build | ||
working-directory: ./generator/konfig-dash | ||
env: | ||
# required to avoid `Usage Error: Environment variable not found (NPM_TOKEN)` | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
|
||
- name: Detect API key leaks | ||
run: ./generator/konfig-dash/bash-scripts/guard-api-key-leak.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'konfig-openapi-spec': patch | ||
'konfig-cli': patch | ||
--- | ||
|
||
add api keys for /prCreate and /prMerge endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { API_KEY_HEADER_NAME } from 'konfig-lib' | ||
|
||
const apiKeys: Record<string, string> = { | ||
qXx6mYhoJgbj8brJe11NeBNsul375Nv3: 'humanloop', | ||
Mu82mVyHm4D1p1zljd8T8sQvxtaIZ5b2: 'konfig-dev', | ||
} | ||
|
||
export function validateApiKey({ | ||
key, | ||
owner, | ||
}: { | ||
key: string | undefined | ||
owner: string | ||
}) { | ||
if (key === undefined) throw Error(`Missing header ${API_KEY_HEADER_NAME}`) | ||
if (apiKeys[key] === undefined || apiKeys[key] !== owner) | ||
throw Error(`Invalid header ${API_KEY_HEADER_NAME}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/bin/bash | ||
|
||
yarn workspaces foreach -pitv -j unlimited run build | ||
yarn rw build api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
|
||
cd "$(dirname "$0")" | ||
|
||
# get the api keys from api-keys.ts | ||
keyFile="../api/src/lib/api-keys.ts" | ||
apiKeys=($(grep -oE '[A-Za-z0-9]{32}:' "$keyFile" | sed 's/://')) | ||
|
||
searchFolder="../../.." # entire repo | ||
foundKey=false | ||
|
||
# Files that are allowed to contain the api key | ||
whitelist=( | ||
"../../../generator/konfig-dash/api/dist/lib/api-keys.js" | ||
"../../../generator/konfig-dash/.redwood/prebuild/api/src/lib/api-keys.js" | ||
"../../../generator/konfig-dash/api/src/lib/api-keys.ts" | ||
"../../../generator/konfig-dash/api/dist/lib/api-keys.js.map" | ||
) | ||
|
||
for apiKey in "${apiKeys[@]}"; do | ||
files=$(grep -rl "$apiKey" "$searchFolder" | grep -vE "$(printf "%s\n" "${whitelist[@]}")") | ||
if [ -n "$files" ]; then | ||
foundKey=true | ||
echo "ERROR: Security risk detected. API key leaked in the following files:" | ||
echo "$files" | ||
echo | ||
fi | ||
done | ||
|
||
if [ "$foundKey" = true ]; then | ||
exit 1 | ||
fi | ||
echo No api key leaks detected. | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
generator/konfig-dash/packages/konfig-lib/src/util/konfig-api-key-header.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const API_KEY_HEADER_NAME = 'x-konfig-api-key' |