Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Add middleware for debug headers
Browse files Browse the repository at this point in the history
If the request sends an x-debug header with any value, the response headers will be populated with the build ID and tag.
  • Loading branch information
sethvargo committed Sep 9, 2020
1 parent 360a07c commit 4e35a84
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/adminapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func realMain(ctx context.Context) error {
// Install common security headers
r.Use(middleware.SecureHeaders(ctx, config.DevMode, "json"))

// Enable debug headers
processDebug := middleware.ProcessDebug(ctx)
r.Use(processDebug)

// Create the renderer
h, err := render.New(ctx, "", config.DevMode)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func realMain(ctx context.Context) error {
// Install common security headers
r.Use(middleware.SecureHeaders(ctx, config.DevMode, "json"))

// Enable debug headers
processDebug := middleware.ProcessDebug(ctx)
r.Use(processDebug)

// Create the renderer
h, err := render.New(ctx, "", config.DevMode)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func realMain(ctx context.Context) error {
// Install common security headers
r.Use(middleware.SecureHeaders(ctx, config.DevMode, "html"))

// Enable debug headers
processDebug := middleware.ProcessDebug(ctx)
r.Use(processDebug)

// Install the CSRF protection middleware.
configureCSRF := middleware.ConfigureCSRF(ctx, config, h)
r.Use(configureCSRF)
Expand Down
40 changes: 40 additions & 0 deletions pkg/controller/middleware/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package middleware

import (
"context"
"net/http"

"github.com/google/exposure-notifications-verification-server/pkg/buildinfo"
"github.com/gorilla/mux"
)

// ProcessDebug adds additional debugging information to the response if the
// request included the "X-Debug" header with any value.
func ProcessDebug(ctx context.Context) mux.MiddlewareFunc {
header := "x-debug"

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(header) != "" {
w.Header().Set("x-build-id", buildinfo.BuildID)
w.Header().Set("x-build-tag", buildinfo.BuildTag)
}

next.ServeHTTP(w, r)
})
}
}

0 comments on commit 4e35a84

Please sign in to comment.