Skip to content

Commit

Permalink
Try to make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
jhiemstrawisc authored and turetske committed Mar 5, 2024
1 parent 333f1ce commit ed1ef01
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 51 deletions.
36 changes: 7 additions & 29 deletions client/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"])
Expand All @@ -81,15 +59,15 @@ 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
}

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{}
Expand Down
16 changes: 2 additions & 14 deletions client/director_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"]
}

Expand Down
11 changes: 5 additions & 6 deletions lotman/lotman.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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" {
Expand Down
4 changes: 2 additions & 2 deletions lotman/lotman_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions utils/web_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/pkg/errors"

Expand Down Expand Up @@ -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
}
38 changes: 38 additions & 0 deletions utils/web_utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ed1ef01

Please sign in to comment.