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

Add middleware for debug headers #497

Merged
merged 3 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 44 additions & 0 deletions pkg/controller/middleware/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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"
)

const (
HeaderDebug = "x-debug"
HeaderDebugBuildID = "x-build-id"
HeaderDebugBuildTag = "x-build-tag"
)

// 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 {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(HeaderDebug) != "" {
w.Header().Set(HeaderDebugBuildID, buildinfo.BuildID)
w.Header().Set(HeaderDebugBuildTag, buildinfo.BuildTag)
}

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