From ed1ef012e0d0f37748af251f00689f3110c20ddd Mon Sep 17 00:00:00 2001 From: Justin Hiemstra Date: Tue, 5 Mar 2024 17:48:53 +0000 Subject: [PATCH] Try to make linter happy --- client/director.go | 36 +++++++----------------------------- client/director_test.go | 16 ++-------------- lotman/lotman.go | 11 +++++------ lotman/lotman_ui.go | 4 ++-- utils/web_utils.go | 25 +++++++++++++++++++++++++ utils/web_utils_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 51 deletions(-) create mode 100644 utils/web_utils_test.go diff --git a/client/director.go b/client/director.go index 39f19bdc4..d503b7f37 100644 --- a/client/director.go +++ b/client/director.go @@ -27,40 +27,18 @@ import ( "strconv" "strings" - "github.com/pelicanplatform/pelican/config" - namespaces "github.com/pelicanplatform/pelican/namespaces" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + + "github.com/pelicanplatform/pelican/config" + namespaces "github.com/pelicanplatform/pelican/namespaces" + "github.com/pelicanplatform/pelican/utils" ) type directorResponse struct { Error string `json:"error"` } -// Simple parser to that takes a "values" string from a header and turns it -// into a map of key/value pairs -func HeaderParser(values string) (retMap map[string]string) { - retMap = map[string]string{} - - // Some headers might not have values, such as the - // X-OSDF-Authorization header when the resource is public - if values == "" { - return - } - - mapPairs := strings.Split(values, ",") - for _, pair := range mapPairs { - // Remove any unwanted spaces - pair = strings.ReplaceAll(pair, " ", "") - - // Break out key/value pairs and put in the map - split := strings.Split(pair, "=") - retMap[split[0]] = split[1] - } - - return retMap -} - // Given the Director response, create the ordered list of caches // and store it as namespace.SortedDirectorCaches func CreateNsFromDirectorResp(dirResp *http.Response) (namespace namespaces.Namespace, err error) { @@ -69,7 +47,7 @@ func CreateNsFromDirectorResp(dirResp *http.Response) (namespace namespaces.Name err = errors.New("Pelican director did not include mandatory X-Pelican-Namespace header in response") return } - xPelicanNamespace := HeaderParser(pelicanNamespaceHdr[0]) + xPelicanNamespace := utils.HeaderParser(pelicanNamespaceHdr[0]) namespace.Path = xPelicanNamespace["namespace"] namespace.UseTokenOnRead, _ = strconv.ParseBool(xPelicanNamespace["require-token"]) namespace.ReadHTTPS, _ = strconv.ParseBool(xPelicanNamespace["readhttps"]) @@ -81,7 +59,7 @@ func CreateNsFromDirectorResp(dirResp *http.Response) (namespace namespaces.Name //So it's a map entry - HeaderParser returns a max entry //We want to appen the value for _, authEntry := range dirResp.Header.Values("X-Pelican-Authorization") { - parsedEntry := HeaderParser(authEntry) + parsedEntry := utils.HeaderParser(authEntry) xPelicanAuthorization = append(xPelicanAuthorization, parsedEntry["issuer"]) } namespace.Issuer = xPelicanAuthorization @@ -89,7 +67,7 @@ func CreateNsFromDirectorResp(dirResp *http.Response) (namespace namespaces.Name var xPelicanTokenGeneration map[string]string if len(dirResp.Header.Values("X-Pelican-Token-Generation")) > 0 { - xPelicanTokenGeneration = HeaderParser(dirResp.Header.Values("X-Pelican-Token-Generation")[0]) + xPelicanTokenGeneration = utils.HeaderParser(dirResp.Header.Values("X-Pelican-Token-Generation")[0]) // Instantiate the cred gen struct namespace.CredentialGen = &namespaces.CredentialGeneration{} diff --git a/client/director_test.go b/client/director_test.go index db0e3406a..eb597656e 100644 --- a/client/director_test.go +++ b/client/director_test.go @@ -29,21 +29,9 @@ import ( "github.com/stretchr/testify/assert" namespaces "github.com/pelicanplatform/pelican/namespaces" + "github.com/pelicanplatform/pelican/utils" ) -func TestHeaderParser(t *testing.T) { - header1 := "namespace=/foo/bar, issuer = https://get-your-tokens.org, readhttps=False" - newMap1 := HeaderParser(header1) - - assert.Equal(t, "/foo/bar", newMap1["namespace"]) - assert.Equal(t, "https://get-your-tokens.org", newMap1["issuer"]) - assert.Equal(t, "False", newMap1["readhttps"]) - - header2 := "" - newMap2 := HeaderParser(header2) - assert.Equal(t, map[string]string{}, newMap2) -} - func TestGetCachesFromDirectorResponse(t *testing.T) { // Construct the Director's Response, comprising headers and a body directorHeaders := make(map[string][]string) @@ -125,7 +113,7 @@ func TestCreateNsFromDirectorResp(t *testing.T) { var xPelicanAuthorization map[string]string var issuer string if len(directorResponse.Header.Values("X-Pelican-Authorization")) > 0 { - xPelicanAuthorization = HeaderParser(directorResponse.Header.Values("X-Pelican-Authorization")[0]) + xPelicanAuthorization = utils.HeaderParser(directorResponse.Header.Values("X-Pelican-Authorization")[0]) issuer = xPelicanAuthorization["issuer"] } diff --git a/lotman/lotman.go b/lotman/lotman.go index cb9211305..0ed823868 100644 --- a/lotman/lotman.go +++ b/lotman/lotman.go @@ -132,12 +132,12 @@ type ( LotName string `json:"lot_name" mapstructure:"LotName"` Owner string `json:"owner,omitempty" mapstructure:"Owner"` // We don't expose Owners via map structure because that's not something we can configure. It's a derived value - Owners []string `json:"owners,omitempty"` - Parents []string `json:"parents" mapstructure:"Parents"` + Owners []string `json:"owners,omitempty"` + Parents []string `json:"parents" mapstructure:"Parents"` // While we _could_ expose Children, that complicates things so for now we keep it hidden from the config - Children *[]string `json:"children,omitempty"` - Paths []LotPaths `json:"paths,omitempty" mapstructure:"Paths"` - MPA *MPA `json:"management_policy_attrs,omitempty" mapstructure:"ManagementPolicyAttrs"` + Children *[]string `json:"children,omitempty"` + Paths []LotPaths `json:"paths,omitempty" mapstructure:"Paths"` + MPA *MPA `json:"management_policy_attrs,omitempty" mapstructure:"ManagementPolicyAttrs"` // Again, these are derived RestrictiveMPA *RestrictiveMPA `json:"restrictive_management_policy_attrs,omitempty"` Usage *LotUsage `json:"usage,omitempty"` @@ -520,7 +520,6 @@ func InitLotman() bool { log.Infof("Created root lot") } - // Now instantiate any other lots that are in the config for _, lot := range initializedLots { if lot.LotName != "default" && lot.LotName != "root" { diff --git a/lotman/lotman_ui.go b/lotman/lotman_ui.go index 373d73ef6..1b07bf119 100644 --- a/lotman/lotman_ui.go +++ b/lotman/lotman_ui.go @@ -35,12 +35,12 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" - "github.com/pelicanplatform/pelican/client" "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" + "github.com/pelicanplatform/pelican/utils" ) // Given a token and a list of authorized callers, check that the token is signed by one of the authorized callers. Return @@ -244,7 +244,7 @@ func VerifyNewLotToken(lot *Lot, strToken string) (bool, error) { // Get the namespace from the X-Pelican-Namespace header namespaceHeader := resp.Header.Values("X-Pelican-Namespace") - xPelicanNamespaceMap := client.HeaderParser(namespaceHeader[0]) + xPelicanNamespaceMap := utils.HeaderParser(namespaceHeader[0]) namespace := xPelicanNamespaceMap["namespace"] // Get the issuer URL for that namespace diff --git a/utils/web_utils.go b/utils/web_utils.go index eeb7e87cc..8b9053706 100644 --- a/utils/web_utils.go +++ b/utils/web_utils.go @@ -24,6 +24,7 @@ import ( "fmt" "io" "net/http" + "strings" "github.com/pkg/errors" @@ -173,3 +174,27 @@ func CopyHeader(dst, src http.Header) { } } } + +// Simple parser to that takes a "values" string from a header and turns it +// into a map of key/value pairs +func HeaderParser(values string) (retMap map[string]string) { + retMap = map[string]string{} + + // Some headers might not have values, such as the + // X-OSDF-Authorization header when the resource is public + if values == "" { + return + } + + mapPairs := strings.Split(values, ",") + for _, pair := range mapPairs { + // Remove any unwanted spaces + pair = strings.ReplaceAll(pair, " ", "") + + // Break out key/value pairs and put in the map + split := strings.Split(pair, "=") + retMap[split[0]] = split[1] + } + + return retMap +} diff --git a/utils/web_utils_test.go b/utils/web_utils_test.go new file mode 100644 index 000000000..ae47b8316 --- /dev/null +++ b/utils/web_utils_test.go @@ -0,0 +1,38 @@ +/*************************************************************** +* +* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research +* +* 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 utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHeaderParser(t *testing.T) { + header1 := "namespace=/foo/bar, issuer = https://get-your-tokens.org, readhttps=False" + newMap1 := HeaderParser(header1) + + assert.Equal(t, "/foo/bar", newMap1["namespace"]) + assert.Equal(t, "https://get-your-tokens.org", newMap1["issuer"]) + assert.Equal(t, "False", newMap1["readhttps"]) + + header2 := "" + newMap2 := HeaderParser(header2) + assert.Equal(t, map[string]string{}, newMap2) +}