Skip to content

Commit

Permalink
re-added endpoint parsing login (originally removed and replced with …
Browse files Browse the repository at this point in the history
…a region flag) (#4)

made -region flag optional, if not supplied will go through the endpoint parsing logic instead
edited README.md description for region flag
added "assume" description to --help in README.md
  • Loading branch information
DustinKLo authored Nov 15, 2020
1 parent 54f37ec commit 5ef81bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ Usage of ./aws-es-proxy:
Print user requests
-version
Print aws-es-proxy version
-assume
Optionally specify role to assume
-region
AWS region (ex. us-west-2) (Required)
AWS Region, optional (ex. us-west-2)
-insecure
Will not verify SSL (default false)
```
Expand Down
35 changes: 30 additions & 5 deletions aws-es-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -120,8 +121,9 @@ func newProxy(args ...interface{}) *proxy {

func (p *proxy) parseEndpoint() error {
var (
link *url.URL
err error
link *url.URL
err error
isAWSEndpoint bool
)

if link, err = url.Parse(p.endpoint); err != nil {
Expand Down Expand Up @@ -150,12 +152,35 @@ func (p *proxy) parseEndpoint() error {
p.scheme = link.Scheme
p.host = link.Host

// AWS SignV4 enabled, extract required parts for signing process
if !p.nosignreq {
// AWS SignV4 enabled, extract required parts for signing process (if region flag is not supplied)
if !p.nosignreq && p.region != "" {
split := strings.SplitAfterN(link.Hostname(), ".", 2)

if len(split) < 2 {
logrus.Debugln("Endpoint split is less than 2")
}
awsEndpoints := []string{}
for _, partition := range endpoints.DefaultPartitions() {
for region := range partition.Regions() {
awsEndpoints = append(awsEndpoints, fmt.Sprintf("%s.es.%s", region, partition.DNSSuffix()))
}
}

isAWSEndpoint = false
for _, v := range awsEndpoints {
if split[1] == v {
logrus.Debugln("Provided endpoint is a valid AWS Elasticsearch endpoint")
isAWSEndpoint = true
break
}
}

if isAWSEndpoint {
// Extract region and service from link. This should be save now
parts := strings.Split(link.Host, ".")
p.region, p.service = parts[1], "es"
logrus.Debugln("AWS Region", p.region)
}
}

return nil
Expand Down Expand Up @@ -456,7 +481,7 @@ func main() {
flag.StringVar(&realm, "realm", "", "Authentication Required")
flag.BoolVar(&remoteTerminate, "remote-terminate", false, "Allow HTTP remote termination")
flag.StringVar(&assumeRole, "assume", "", "Optionally specify role to assume")
flag.StringVar(&region, "region", "", "AWS Region (ex. us-west-2)")
flag.StringVar(&region, "region", "", "AWS Region, optional (ex. us-west-2)")
flag.BoolVar(&insecure, "insecure", false, "Verify SSL")
flag.Parse()

Expand Down

0 comments on commit 5ef81bb

Please sign in to comment.