Skip to content

Commit

Permalink
Release to master
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Jun 4, 2020
2 parents 74e6085 + 1cb9351 commit d8bf015
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
23 changes: 14 additions & 9 deletions api/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/base64"
"errors"
"net/http"
"sync"
"time"
Expand All @@ -16,6 +17,10 @@ var (

authFnLock sync.Mutex
authFn Authenticator

// ErrAPIAccessDeniedMessage should be returned by Authenticator functions in
// order to signify a blocked request, including a error message for the user.
ErrAPIAccessDeniedMessage = errors.New("")
)

const (
Expand All @@ -28,7 +33,7 @@ const (
)

// Authenticator is a function that can be set as the authenticator for the API endpoint. If none is set, all requests will be allowed.
type Authenticator func(s *http.Server, r *http.Request) (grantAccess bool, err error)
type Authenticator func(s *http.Server, r *http.Request) (err error)

// SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be allowed.
func SetAuthenticator(fn Authenticator) error {
Expand Down Expand Up @@ -79,15 +84,15 @@ func authMiddleware(next http.Handler) http.Handler {
}

// get auth decision
grantAccess, err := authenticator(server, r)
err = authenticator(server, r)
if err != nil {
log.Warningf("api: authenticator failed: %s", err)
http.Error(w, "Bad Request: Could not identify client", http.StatusBadRequest)
return
}
if !grantAccess {
log.Warningf("api: denying api access to %s", r.RemoteAddr)
http.Error(w, "Forbidden", http.StatusForbidden)
if errors.Is(err, ErrAPIAccessDeniedMessage) {
log.Warningf("api: denying api access to %s", r.RemoteAddr)
http.Error(w, err.Error(), http.StatusForbidden)
} else {
log.Warningf("api: authenticator failed: %s", err)
http.Error(w, "Internal server error during authentication.", http.StatusInternalServerError)
}
return
}

Expand Down
31 changes: 20 additions & 11 deletions run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/signal"
"runtime/pprof"
Expand Down Expand Up @@ -36,6 +37,10 @@ func Run() int {
return 0
}

if printStackOnExit {
printStackTo(os.Stdout)
}

_ = modules.Shutdown()
return modules.GetExitStatusCode()
}
Expand Down Expand Up @@ -78,20 +83,13 @@ signalLoop:
}()

if printStackOnExit {
fmt.Println("=== PRINTING TRACES ===")
fmt.Println("=== GOROUTINES ===")
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
fmt.Println("=== BLOCKING ===")
_ = pprof.Lookup("block").WriteTo(os.Stdout, 1)
fmt.Println("=== MUTEXES ===")
_ = pprof.Lookup("mutex").WriteTo(os.Stdout, 1)
fmt.Println("=== END TRACES ===")
printStackTo(os.Stdout)
}

go func() {
time.Sleep(60 * time.Second)
fmt.Fprintln(os.Stderr, "===== TAKING TOO LONG FOR SHUTDOWN - PRINTING STACK TRACES =====")
_ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
time.Sleep(3 * time.Minute)
fmt.Fprintln(os.Stderr, "===== TAKING TOO LONG FOR SHUTDOWN =====")
printStackTo(os.Stderr)
os.Exit(1)
}()

Expand Down Expand Up @@ -124,3 +122,14 @@ func inputSignals(signalCh chan os.Signal) {
}
}
}

func printStackTo(writer io.Writer) {
fmt.Fprintln(writer, "=== PRINTING TRACES ===")
fmt.Fprintln(writer, "=== GOROUTINES ===")
_ = pprof.Lookup("goroutine").WriteTo(writer, 1)
fmt.Fprintln(writer, "=== BLOCKING ===")
_ = pprof.Lookup("block").WriteTo(writer, 1)
fmt.Fprintln(writer, "=== MUTEXES ===")
_ = pprof.Lookup("mutex").WriteTo(writer, 1)
fmt.Fprintln(writer, "=== END TRACES ===")
}

0 comments on commit d8bf015

Please sign in to comment.