Skip to content

Commit

Permalink
#2 use viper to read config file and env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
derektamsen committed Feb 22, 2017
1 parent 79e6a52 commit 976999b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
awss3urlsigner
config.yaml
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -8,15 +8,15 @@ 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`
1. User make a request to `<your domain>/some/s3/asset`.
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`
Expand Down
10 changes: 7 additions & 3 deletions 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)
Expand Down
29 changes: 21 additions & 8 deletions 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())
}

0 comments on commit 976999b

Please sign in to comment.