-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7e8c87
commit 277c84e
Showing
313 changed files
with
56,677 additions
and
4,309 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
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,95 @@ | ||
name: "Database Migrations" | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
action: | ||
description: 'What action should be taken?' | ||
required: true | ||
type: choice | ||
default: 'up' | ||
options: | ||
- 'up' | ||
- 'down' | ||
steps: | ||
description: 'How many migrations should be applied? (0 for all)' | ||
required: true | ||
type: number | ||
default: 0 | ||
workflow_call: | ||
inputs: | ||
action: | ||
description: 'What action should be taken?' | ||
required: true | ||
type: string | ||
default: 'up' | ||
steps: | ||
description: 'How many migrations should be applied? (0 for all)' | ||
required: true | ||
type: number | ||
|
||
concurrency: | ||
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
migrate: | ||
name: Migrate | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: See if the database connection string is available | ||
id: db-connection | ||
run: | | ||
if [ "${{ secrets.DATABASE_URL }}" ==" ]; then | ||
echo "No DATABASE_URL secret set. Trying to authenticate with Vault..." | ||
if [ "${{ secrets.VAULT_ADDR }}" == "" ]; then | ||
echo "No VAULT_ADDR secret set. Exiting..." | ||
exit 1 | ||
elif [ "${{ secrets.VAULT_USER }}" == "" ]; then | ||
echo "No VAULT_USER secret set. Exiting..." | ||
exit 1 | ||
elif [ "${{ secrets.VAULT_PASS }}" == "" ]; then | ||
echo "No VAULT_PASS secret set. Exiting..." | ||
exit 1 | ||
fi | ||
url=$(curl -s -X GET https://api.github.com/repos/jacobbrewer1/goschema/releases/latest | jq '.assets[] | select(.name == "vaultdb")' | jq -r .browser_download_url) | ||
wget $url | ||
chmod +x vaultdb | ||
mv vaultdb /usr/local/bin | ||
export DATABASE_URL=$(vaultdb -addr=${{ secrets.VAULT_ADDR }} -user=${{ secrets.VAULT_USER }} -pass=${{ secrets.VAULT_PASS }} -path=${{ secrets.VAULT_PATH }}) -host=${{ secrets.DATABSE_HOST }} -schema=${{ secrets.DATABASE_SCHEMA }} | ||
if [ "${{ DATABASE_URL }}" == "" ]; then | ||
echo "Failed to authenticate with Vault. Exiting..." | ||
exit 1 | ||
fi | ||
fi | ||
echo "DATABASE_URL is set. Continuing..." | ||
- name: Install GoSchema | ||
run: | | ||
url=$(curl -s -X GET https://api.github.com/repos/jacobbrewer1/goschema/releases/latest | jq '.assets[] | select(.name == "goschema")' | jq -r .browser_download_url) | ||
wget $url | ||
chmod +x goschema | ||
mv goschema /usr/local/bin | ||
- name: Run Migrations | ||
run: | | ||
goschema migrate --${{ github.event.inputs.action }} --steps=${{ github.event.inputs.steps }} --loc=./example/database/migrations | ||
- name: Cleanup | ||
if: ${{ always() }} | ||
run: | | ||
rm -f /usr/local/bin/goschema | ||
rm -f /usr/local/bin/vaultdb | ||
if [ "${{ secrets.DATABASE_URL }}" == "" ]; then | ||
unset DATABASE_URL | ||
fi | ||
echo "Cleanup complete." |
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,3 @@ | ||
# Vault Connector | ||
# Vault DB | ||
|
||
This app is primarily designed to be used in the CICD that prints a connection string for the database. |
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,52 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
hashiVault "github.com/hashicorp/vault/api" | ||
"github.com/jacobbrewer1/vaulty" | ||
) | ||
|
||
var ( | ||
vaultAddr = flag.String("addr", "http://localhost:8200", "The address of the vault server") | ||
vaultUser = flag.String("user", "root", "The username to authenticate with") | ||
vaultPass = flag.String("pass", "root", "The password to authenticate with") | ||
vaultPath = flag.String("path", "secret", "The path to the secrets") | ||
dbHost = flag.String("host", "localhost:3306", "The host of the database") | ||
dbSchema = flag.String("schema", "test", "The schema of the database") | ||
) | ||
|
||
func init() { | ||
flag.Parse() | ||
} | ||
|
||
func generateConnectionStr(vs *hashiVault.Secret) string { | ||
return fmt.Sprintf("%s:%s@tcp(%s)/%s?timeout=90s&multiStatements=true&parseTime=true", | ||
vs.Data["username"], | ||
vs.Data["password"], | ||
*dbHost, | ||
*dbSchema, | ||
) | ||
} | ||
|
||
func main() { | ||
vc, err := vaulty.NewClient( | ||
vaulty.WithGeneratedVaultClient(*vaultAddr), | ||
vaulty.WithUserPassAuth( | ||
*vaultUser, | ||
*vaultPass, | ||
), | ||
) | ||
if err != nil { | ||
panic(fmt.Errorf("error creating vault client: %w", err)) | ||
} | ||
|
||
got, err := vc.Path(*vaultPath).GetSecret(context.Background()) | ||
if err != nil { | ||
panic(fmt.Errorf("error getting secret: %w", err)) | ||
} | ||
|
||
fmt.Println(generateConnectionStr(got)) | ||
} |
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
Oops, something went wrong.