Browse Source

Initial commit.

main
Atridad Lahiji 2 months ago
parent
commit
eb4e3eb97d
  1. 54
      .drone.yml
  2. 4
      .env.example
  3. 2
      .gitignore
  4. 15
      README.md
  5. 20
      build.sh
  6. 8
      go.mod
  7. 32
      go.sum
  8. 80
      main.go

54
.drone.yml

@ -0,0 +1,54 @@
kind: pipeline
type: docker
name: dev
trigger:
branch:
- master
event:
- push
steps:
- image: golang
name: test-build
commands:
- go build
---
kind: pipeline
type: docker
name: prod
trigger:
event:
- tag
steps:
- image: golang
name: build-backend
commands:
- chmod +x ./build.sh
- ./build.sh
- image: node
name: pre-upload
commands:
- curl -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 > jq
- chmod +x ./jq
- LATEST_RELEASE_ID=$(curl -s https://git.atrilahiji.dev/api/v1/repos/atrilahiji/middlewhere/releases | ./jq -r '.[0].name')
- rm -rf bin/darwin-amd64
- rm -rf bin/linux-amd64
- mv bin/darwin-amd64.tar.gz bin/$LATEST_RELEASE_ID-darwin-amd64.tar.gz
- mv bin/linux-amd64.tar.gz bin/$LATEST_RELEASE_ID-linux-amd64.tar.gz
- image: plugins/s3
name: upload
settings:
access_key:
from_secret: S3_ACCESS
secret_key:
from_secret: S3_SECRET
bucket: middlewhere
source: bin/**/*
target: /
path_style: true
endpoint: https://s3.atrilahiji.dev

4
.env.example

@ -0,0 +1,4 @@
ENDPOINT_URL="https://google.com"
ENDPOINT_AUTH_TOKEN=
ENDPOINT_REQUST_METHOD=POST
SECURE_TOKEN=

2
.gitignore

@ -0,0 +1,2 @@
bin/
.env

15
README.md

@ -1,2 +1,17 @@
# MiddleWhere
MiddleWhere is an open source endpoint Auth Header -> Query String Token proxy in Golang.
## Use cases
1. You have a secured endpoint you would like to publicly expose without exposing your secure auth token
2. You would like to have an application make a request to a secure endpoint but have no place to add an auth bearer token
## Default config
```
ENDPOINT_URL="https://google.com"
ENDPOINT_AUTH_TOKEN=
ENDPOINT_REQUST_METHOD=POST
SECURE_TOKEN=
```
## Our use case:
W use directus for the CMS for a number of our sites, but they also use a static site generator. So we have a webhook that sends a POST request to DroneCI to re-build the site with the new data. Directus webhooks do not let you set an auth header, so we needed a way to include a token in a querystring within the URL. This is our solution.

20
build.sh

@ -0,0 +1,20 @@
export OLD_GOOS=$(go env GOOS)
export OLD_GOARCH=$(go env GOARCH)
mkdir bin
# buid for macos x86 64-bit
export GOOS=darwin
export GOARCH=amd64
mkdir bin/$(go env GOOS)-$(go env GOARCH)
cp .env.example ./bin/$(go env GOOS)-$(go env GOARCH)/.env.example
go build -o ./bin/$(go env GOOS)-$(go env GOARCH)/middlewhere-$(go env GOOS)-$(go env GOARCH)
tar -czvf ./bin/$(go env GOOS)-$(go env GOARCH).tar.gz ./bin/$(go env GOOS)-$(go env GOARCH)
# buid for linux x86 64-bit
export GOOS=linux
export GOARCH=amd64
mkdir bin/$(go env GOOS)-$(go env GOARCH)
cp .env.example ./bin/$(go env GOOS)-$(go env GOARCH)/.env.example
go build -o ./bin/$(go env GOOS)-$(go env GOARCH)/middlewhere-$(go env GOOS)-$(go env GOARCH)
tar -czvf ./bin/$(go env GOOS)-$(go env GOARCH).tar.gz ./bin/$(go env GOOS)-$(go env GOARCH)

8
go.mod

@ -0,0 +1,8 @@
module git.atrilahiji.dev/atrilahiji/middlewhere
go 1.16
require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/joho/godotenv v1.3.0 // indirect
)

32
go.sum

@ -0,0 +1,32 @@
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
github.com/GeertJohan/go.rice v1.0.2 h1:PtRw+Tg3oa3HYwiDBZyvOJ8LdIyf6lAovJJtr7YOAYk=
github.com/GeertJohan/go.rice v1.0.2/go.mod h1:af5vUNlDNkCjOZeSGFgIJxDje9qdjsO6hshx0gTmZt4=
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb h1:EVl3FJLQCzSbgBezKo/1A4ADnJ4mtJZ0RvnNzDJ44nY=
github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/campoy/svg-badge v0.0.0-20180116034456-40c38fcad9f6 h1:tqkfnr9GEZzMyWtQKvgPwWvWxH8RCMA+GeNylbYYEb0=
github.com/campoy/svg-badge v0.0.0-20180116034456-40c38fcad9f6/go.mod h1:kDWVB5nIXF847uNfomMkNoV4QEdH0q0JwMPQvgsvsLI=
github.com/daaku/go.zipexe v1.0.0 h1:VSOgZtH418pH9L16hC/JrgSNJbbAL26pj7lmD1+CGdY=
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stripe/stripe-go/v72 v72.37.0/go.mod h1:QwqJQtduHubZht9mek5sds9CtQcKFdsykV9ZepRWwo0=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

80
main.go

@ -0,0 +1,80 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
func loadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func frontEndHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, "<h1>MiddleWhere?!?!?!?!?!</h1>")
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Health Check"))
}
func webHookHandler(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
if token == os.Getenv("SECURE_TOKEN") || os.Getenv("SECURE_TOKEN") == "" {
// Create a Bearer string by appending string access token
// Create a new request using http
req, err := http.NewRequest(os.Getenv("ENDPOINT_REQUEST_METHOD"), os.Getenv("ENDPOINT_URL"), nil)
// add authorization header to the req
authToken := os.Getenv("ENDPOINT_AUTH_TOKEN")
if authToken != "" {
var bearer = "Bearer " + authToken
req.Header.Add("Authorization", bearer)
}
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Internal Server Error!"))
log.Println("Error while reading the response bytes:", err)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 - Success!"))
log.Println("Success: ", body)
} else {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("401 - Unauthorized!"))
}
}
func main() {
loadEnv()
http.HandleFunc("/", frontEndHandler)
http.HandleFunc("/api/healthcheck", healthCheckHandler)
http.HandleFunc("/api/triggerWebHook", webHookHandler)
log.Println("Listening on :3000...")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}
Loading…
Cancel
Save