From 976999bba761689cd4af188c85c60be907d8b65d Mon Sep 17 00:00:00 2001 From: Derek Tamsen Date: Tue, 21 Feb 2017 23:51:02 -0800 Subject: [PATCH] #2 use viper to read config file and env vars --- .gitignore | 1 + README.md | 4 ++-- aws/awsurl.go | 10 +++++++--- main.go | 29 +++++++++++++++++++++-------- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index befcc8e..e08647a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ awss3urlsigner +config.yaml diff --git a/README.md b/README.md index 51d3870..5a61140 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This service runs as a http server to receive requests, sign urls, and redirect ## Design ``` -user -> nginx (auth + proxy) -> aws-s3-url-signer (url signing + redirect) -> user (redirected) -> S3/cloudfront +user -> nginx (auth + proxy) -> awss3urlsigner (url signing + redirect) -> user (redirected) -> S3/cloudfront ``` 1. `user` @@ -16,7 +16,7 @@ user -> nginx (auth + proxy) -> aws-s3-url-signer (url signing + redirect) -> us 2. `nginx` 1. Authenticates the user with client PKI certificates or another form of authentication. 2. Proxies the traffic to this service. -3. `aws-s3-url-signer` +3. `awss3urlsigner` 1. Takes the requested url from the user and signs it using the aws sdk 2. Redirects user to the S3 or cloudfront endpoint with a signed url 4. `user` diff --git a/aws/awsurl.go b/aws/awsurl.go index 4737c33..25bd646 100644 --- a/aws/awsurl.go +++ b/aws/awsurl.go @@ -1,14 +1,18 @@ package awsurl import ( + "log" + "time" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" - "log" - "time" ) -func PreSign(obj string) string{ +// S3PreSign presigns the url for s3 GET allowing for signed downloads of an s3 asset. +// The obj to be signed is sent as a string. +// Returns the signed url as a string. +func S3PreSign(obj string) string { sess, err := session.NewSession() if err != nil { panic(err) diff --git a/main.go b/main.go index 84145ca..8ff7a3d 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,37 @@ package main import ( - "github.com/derektamsen/awss3urlsigner/aws" + "fmt" "log" "net/http" "time" + + "github.com/derektamsen/awss3urlsigner/aws" + "github.com/spf13/viper" ) -func handler(w http.ResponseWriter, r *http.Request) { - presigned_url := awsurl.PreSign(r.URL.Path[1:]) - http.Redirect(w, r, presigned_url, http.StatusFound) +func getConfig() { + viper.SetConfigName("config") + viper.AddConfigPath(".") + err := viper.ReadInConfig() + if err != nil { + panic(fmt.Errorf("Fatal error config file: %s", err)) + } +} + +func httphandler(w http.ResponseWriter, r *http.Request) { + presignedURL := awsurl.S3PreSign(r.URL.Path[1:]) + http.Redirect(w, r, presignedURL, http.StatusFound) } func main() { - httpserver := &http.Server{ - Addr: ":8080", + getConfig() + httpServer := &http.Server{ + Addr: ":" + viper.GetString("http_port"), ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } - http.HandleFunc("/", handler) - log.Fatal(httpserver.ListenAndServe()) + http.HandleFunc("/", httphandler) + log.Fatal(httpServer.ListenAndServe()) }