Skip to content

Commit

Permalink
Code cleanup (#2)
Browse files Browse the repository at this point in the history
* added region flag to avoid having to parse the endpoint to get the region

hard-coded proxy.service value to "es"
added insecure flag and logic to avoid SSL verification

* cleaning up code, moving insecure logic to main function (TODO: still need to test)

* removed redundant use of insecure flag
  • Loading branch information
DustinKLo authored Oct 21, 2020
1 parent c656b87 commit 97edea5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
vendor
glide.lock
dist
.idea
.idea
aws-es-proxy
28 changes: 7 additions & 21 deletions aws-es-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
)

func logger(debug bool) {

formatFilePath := func(path string) string {
arr := strings.Split(path, "/")
return arr[len(arr)-1]
Expand Down Expand Up @@ -87,7 +86,6 @@ type proxy struct {
password string
realm string
remoteTerminate bool
insecure bool
}

func newProxy(args ...interface{}) *proxy {
Expand All @@ -101,12 +99,6 @@ func newProxy(args ...interface{}) *proxy {
CheckRedirect: noRedirect,
}

if args[12].(bool) == true {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

return &proxy{
endpoint: args[0].(string),
verbose: args[1].(bool),
Expand All @@ -120,7 +112,7 @@ func newProxy(args ...interface{}) *proxy {
realm: args[9].(string),
remoteTerminate: args[10].(bool),
region: args[11].(string),
insecure: args[12].(bool),
service: "es",
}
}

Expand Down Expand Up @@ -156,14 +148,9 @@ func (p *proxy) parseEndpoint() error {
p.scheme = link.Scheme
p.host = link.Host

p.service = "es"
logrus.Debugln("AWS Region", p.region)

// AWS SignV4 enabled, extract required parts for signing process
if !p.nosignreq {

split := strings.SplitAfterN(link.Hostname(), ".", 2)

if len(split) < 2 {
logrus.Debugln("Endpoint split is less than 2")
}
Expand All @@ -175,7 +162,6 @@ func (p *proxy) parseEndpoint() error {
func (p *proxy) getSigner() *v4.Signer {
// Refresh credentials after expiration. Required for STS
if p.credentials == nil {

sess, err := session.NewSession(
&aws.Config{
Region: aws.String(p.region),
Expand Down Expand Up @@ -324,7 +310,6 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {

fmt.Println()
fmt.Println("========================")
fmt.Println("Region: ", p.region)
fmt.Println(t.Format("2006/01/02 15:04:05"))
fmt.Println("Remote Address: ", r.RemoteAddr)
fmt.Println("Request URI: ", proxied.RequestURI())
Expand All @@ -342,7 +327,6 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if p.logtofile {

requestID := primitive.NewObjectID().Hex()

reqStruct := &requestStruct{
Expand Down Expand Up @@ -408,7 +392,6 @@ func copyHeaders(dst, src http.Header) {
dst.Add(k, v)
}
}

}
}

Expand Down Expand Up @@ -499,16 +482,20 @@ func main() {
realm,
remoteTerminate,
region,
insecure,
)

if insecure == true {
p.httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

if err = p.parseEndpoint(); err != nil {
logrus.Fatalln(err)
os.Exit(1)
}

if p.logtofile {

requestFname := fmt.Sprintf("request-%s.log", primitive.NewObjectID().Hex())
if fileRequest, err = os.Create(requestFname); err != nil {
log.Fatalln(err.Error())
Expand All @@ -523,7 +510,6 @@ func main() {

p.fileRequest = fileRequest
p.fileResponse = fileResponse

}

logrus.Infof("Listening on %s...\n", listenAddress)
Expand Down

0 comments on commit 97edea5

Please sign in to comment.