Skip to content

Commit

Permalink
refactor: VerifyAuthKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Oct 18, 2024
1 parent e13519f commit 9c8458b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 2 additions & 3 deletions local-interchain/interchain/handlers/container_log_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<authKey> 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
}

Expand Down
10 changes: 4 additions & 6 deletions local-interchain/interchain/handlers/log_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<authKey> 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
}

Expand Down Expand Up @@ -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=<authKey> 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
}

Expand Down
14 changes: 14 additions & 0 deletions local-interchain/interchain/handlers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 9c8458b

Please sign in to comment.