-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposed simple header filtering (#218)
* proposed header filtering * add change note
- Loading branch information
Showing
5 changed files
with
82 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/xmidt-org/candlelight" | ||
"github.com/xmidt-org/webpa-common/logging" | ||
) | ||
|
||
func sanitizeHeaders(headers http.Header) (filtered http.Header) { | ||
filtered = headers.Clone() | ||
if authHeader := filtered.Get("Authorization"); authHeader != "" { | ||
filtered.Del("Authorization") | ||
parts := strings.Split(authHeader, " ") | ||
if len(parts) == 2 { | ||
filtered.Set("Authorization-Type", parts[0]) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func SetLogger(logger log.Logger) func(delegate http.Handler) http.Handler { | ||
return func(delegate http.Handler) http.Handler { | ||
return http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
kvs := []interface{}{"requestHeaders", sanitizeHeaders(r.Header), "requestURL", r.URL.EscapedPath(), "method", r.Method} | ||
kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs) | ||
ctx := r.WithContext(logging.WithLogger(r.Context(), log.With(logger, kvs...))) | ||
delegate.ServeHTTP(w, ctx) | ||
}) | ||
} | ||
} | ||
|
||
func GetLogger(ctx context.Context) log.Logger { | ||
logger := log.With(logging.GetLogger(ctx), "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller) | ||
return logger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSanitizeHeaders(t *testing.T) { | ||
testCases := []struct { | ||
Description string | ||
Input http.Header | ||
Expected http.Header | ||
}{ | ||
{ | ||
Description: "Filtered", | ||
Input: http.Header{"Authorization": []string{"Basic xyz"}, "HeaderA": []string{"x"}}, | ||
Expected: http.Header{"HeaderA": []string{"x"}, "Authorization-Type": []string{"Basic"}}, | ||
}, | ||
{ | ||
Description: "Handled human error", | ||
Input: http.Header{"Authorization": []string{"BasicXYZ"}, "HeaderB": []string{"y"}}, | ||
Expected: http.Header{"HeaderB": []string{"y"}}, | ||
}, | ||
{ | ||
Description: "Not a perfect system", | ||
Input: http.Header{"Authorization": []string{"MySecret IWantToLeakIt"}}, | ||
Expected: http.Header{"Authorization-Type": []string{"MySecret"}}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
actual := sanitizeHeaders(tc.Input) | ||
assert.Equal(tc.Expected, actual) | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters