From 9c8458b6fd2164f79046225c793d624ed8afc04a Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Thu, 17 Oct 2024 23:20:57 -0500 Subject: [PATCH] refactor: VerifyAuthKey --- .../interchain/handlers/container_log_stream.go | 5 ++--- local-interchain/interchain/handlers/log_stream.go | 10 ++++------ local-interchain/interchain/handlers/types.go | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/local-interchain/interchain/handlers/container_log_stream.go b/local-interchain/interchain/handlers/container_log_stream.go index 31cadac73..0f629ab44 100644 --- a/local-interchain/interchain/handlers/container_log_stream.go +++ b/local-interchain/interchain/handlers/container_log_stream.go @@ -46,9 +46,8 @@ func NewContainerSteam(ctx context.Context, logger *zap.Logger, cli *dockerclien } func (cs *ContainerStream) StreamContainer(w http.ResponseWriter, r *http.Request) { - // ensure ?auth_key= is provided - if cs.authKey != "" && r.URL.Query().Get("auth_key") != cs.authKey { - http.Error(w, "Unauthorized, incorrect or no ?auth_key= provided", http.StatusUnauthorized) + if err := VerifyAuthKey(cs.authKey, r); err != nil { + http.Error(w, err.Error(), http.StatusUnauthorized) return } diff --git a/local-interchain/interchain/handlers/log_stream.go b/local-interchain/interchain/handlers/log_stream.go index f495bcbb3..9279a31bb 100644 --- a/local-interchain/interchain/handlers/log_stream.go +++ b/local-interchain/interchain/handlers/log_stream.go @@ -31,9 +31,8 @@ func NewLogSteam(logger *zap.Logger, file string, authKey string) *LogStream { } func (ls *LogStream) StreamLogs(w http.ResponseWriter, r *http.Request) { - // ensure ?auth_key= is provided - if ls.authKey != "" && r.URL.Query().Get("auth_key") != ls.authKey { - http.Error(w, "Unauthorized, incorrect or no ?auth_key= provided", http.StatusUnauthorized) + if err := VerifyAuthKey(ls.authKey, r); err != nil { + http.Error(w, err.Error(), http.StatusUnauthorized) return } @@ -84,9 +83,8 @@ func (ls *LogStream) StreamLogs(w http.ResponseWriter, r *http.Request) { } func (ls *LogStream) TailLogs(w http.ResponseWriter, r *http.Request) { - // ensure ?auth_key= is provided - if ls.authKey != "" && r.URL.Query().Get("auth_key") != ls.authKey { - http.Error(w, "Unauthorized, incorrect or no ?auth_key= provided", http.StatusUnauthorized) + if err := VerifyAuthKey(ls.authKey, r); err != nil { + http.Error(w, err.Error(), http.StatusUnauthorized) return } diff --git a/local-interchain/interchain/handlers/types.go b/local-interchain/interchain/handlers/types.go index 8889b241a..19de04af2 100644 --- a/local-interchain/interchain/handlers/types.go +++ b/local-interchain/interchain/handlers/types.go @@ -2,10 +2,24 @@ package handlers import ( "encoding/json" + "fmt" + "net/http" "github.com/strangelove-ventures/interchaintest/v8/ibc" ) +func VerifyAuthKey(expected string, r *http.Request) error { + if expected == "" { + return nil + } + + if r.URL.Query().Get("auth_key") == expected { + return nil + } + + return fmt.Errorf("unauthorized, incorrect or no ?auth_key= provided") +} + type IbcChainConfigAlias struct { Type string `json:"type" yaml:"type"` Name string `json:"name" yaml:"name"`