Skip to content

Commit

Permalink
Fix cyclic import
Browse files Browse the repository at this point in the history
The cyclic import entailed config-->server_structs-->utils-->config.

Since I don't think the utils package should import config (config is
specific to a session, utils should be broadly applicable), I fixed the
cycle by modifying a few utility functions not to need config. Instead,
the required variables must now be passed via the function signature.
  • Loading branch information
jhiemstrawisc committed Aug 12, 2024
1 parent 461a673 commit ba59413
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 28 deletions.
6 changes: 3 additions & 3 deletions cache/advertise.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ func (server *CacheServer) GetNamespaceAdsFromDirector() error {

// Attempt to get data from the 2.0 endpoint, if that returns a 404 error, then attempt to get data
// from the 1.0 endpoint and convert from V1 to V2

respData, err := utils.MakeRequest(context.Background(), directorNSListEndpointURL, "GET", nil, nil)
tr := config.GetTransport()
respData, err := utils.MakeRequest(context.Background(), tr, directorNSListEndpointURL, "GET", nil, nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
directorNSListEndpointURL, err = url.JoinPath(fedInfo.DirectorEndpoint, "api", "v1.0", "director", "listNamespaces")
if err != nil {
return err
}
respData, err = utils.MakeRequest(context.Background(), directorNSListEndpointURL, "GET", nil, nil)
respData, err = utils.MakeRequest(context.Background(), tr, directorNSListEndpointURL, "GET", nil, nil)
var respNSV1 []server_structs.NamespaceAdV1
if err != nil {
return errors.Wrap(err, "Failed to make request")
Expand Down
3 changes: 2 additions & 1 deletion director/origin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func verifyAdvertiseToken(ctx context.Context, token, namespace string) (bool, e
}

if keyset == nil {
keyset, err = utils.GetJwks(ctx, keyLoc)
tr := config.GetTransport()
keyset, err = utils.GetJwks(ctx, tr, keyLoc)
if err != nil {
return false, errors.Wrapf(err, "failed to get jwks at %s", keyLoc)
}
Expand Down
3 changes: 2 additions & 1 deletion launcher_utils/advertise.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func getSitenameFromReg(ctx context.Context, prefix string) (sitename string, er
if err != nil {
return
}
res, err := utils.MakeRequest(context.Background(), requestUrl, http.MethodGet, nil, nil)
tr := config.GetTransport()
res, err := utils.MakeRequest(context.Background(), tr, requestUrl, http.MethodGet, nil, nil)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions local_cache/cache_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func TestForcePurge(t *testing.T) {
Scheme: "unix",
Path: param.LocalCache_Socket.GetString(),
}

_, err = utils.MakeRequest(ft.Ctx, param.Server_ExternalWebUrl.GetString()+"/api/v1.0/localcache/purge", "POST", nil, map[string]string{"Authorization": "Bearer abcd"})
tr := config.GetTransport()
_, err = utils.MakeRequest(ft.Ctx, tr, param.Server_ExternalWebUrl.GetString()+"/api/v1.0/localcache/purge", "POST", nil, map[string]string{"Authorization": "Bearer abcd"})
assert.Error(t, err)
require.Equal(t, fmt.Sprintf("The POST attempt to %s/api/v1.0/localcache/purge resulted in status code 403", param.Server_ExternalWebUrl.GetString()), err.Error())

Expand Down Expand Up @@ -172,7 +172,7 @@ func TestForcePurge(t *testing.T) {
}()
}

_, err = utils.MakeRequest(ft.Ctx, param.Server_ExternalWebUrl.GetString()+"/api/v1.0/localcache/purge", "POST", nil, map[string]string{"Authorization": "Bearer " + token})
_, err = utils.MakeRequest(ft.Ctx, tr, param.Server_ExternalWebUrl.GetString()+"/api/v1.0/localcache/purge", "POST", nil, map[string]string{"Authorization": "Bearer " + token})
require.NoError(t, err)

// Low water mark is small enough that a force purge will delete a file.
Expand Down
3 changes: 2 additions & 1 deletion local_cache/local_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,8 @@ func (sc *LocalCache) updateConfig() error {
return errors.Wrap(err, "Unable to generate the director's listNamespaces endpoint")
}

respData, err := utils.MakeRequest(sc.ctx, directorNSListEndpointURL, "GET", nil, nil)
tr := config.GetTransport()
respData, err := utils.MakeRequest(sc.ctx, tr, directorNSListEndpointURL, "GET", nil, nil)
if err != nil {
return err
} else {
Expand Down
21 changes: 13 additions & 8 deletions registry/client_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/server_utils"
"github.com/pelicanplatform/pelican/token"
"github.com/pelicanplatform/pelican/token_scopes"
Expand All @@ -58,7 +59,8 @@ func NamespaceRegisterWithIdentity(privateKey jwk.Key, namespaceRegistryEndpoint
// it's also registered already

}
resp, err := utils.MakeRequest(context.Background(), namespaceRegistryEndpoint, "POST", identifiedPayload, nil)
tr := config.GetTransport()
resp, err := utils.MakeRequest(context.Background(), tr, namespaceRegistryEndpoint, "POST", identifiedPayload, nil)

var respData clientResponseData
// Handle case where there was an error encoded in the body
Expand All @@ -81,7 +83,7 @@ func NamespaceRegisterWithIdentity(privateKey jwk.Key, namespaceRegistryEndpoint
"identity_required": "true",
"device_code": respData.DeviceCode,
}
resp, err = utils.MakeRequest(context.Background(), namespaceRegistryEndpoint, "POST", identifiedPayload, nil)
resp, err = utils.MakeRequest(context.Background(), tr, namespaceRegistryEndpoint, "POST", identifiedPayload, nil)
if err != nil {
return errors.Wrap(err, "Failed to make request")
}
Expand Down Expand Up @@ -137,7 +139,8 @@ func NamespaceRegister(privateKey jwk.Key, namespaceRegistryEndpoint string, acc
"pubkey": keySet,
}

resp, err := utils.MakeRequest(context.Background(), namespaceRegistryEndpoint, "POST", data, nil)
tr := config.GetTransport()
resp, err := utils.MakeRequest(context.Background(), tr, namespaceRegistryEndpoint, "POST", data, nil)

var respData clientResponseData
// Handle case where there was an error encoded in the body
Expand Down Expand Up @@ -182,7 +185,7 @@ func NamespaceRegister(privateKey jwk.Key, namespaceRegistryEndpoint string, acc
}

// Send the second POST request
resp, err = utils.MakeRequest(context.Background(), namespaceRegistryEndpoint, "POST", unidentifiedPayload, nil)
resp, err = utils.MakeRequest(context.Background(), tr, namespaceRegistryEndpoint, "POST", unidentifiedPayload, nil)

// Handle case where there was an error encoded in the body
if unmarshalErr := json.Unmarshal(resp, &respData); unmarshalErr == nil {
Expand All @@ -204,7 +207,8 @@ func NamespaceRegister(privateKey jwk.Key, namespaceRegistryEndpoint string, acc
}

func NamespaceList(endpoint string) error {
respData, err := utils.MakeRequest(context.Background(), endpoint, "GET", nil, nil)
tr := config.GetTransport()
respData, err := utils.MakeRequest(context.Background(), tr, endpoint, "GET", nil, nil)
var respErr clientResponseData
if err != nil {
if jsonErr := json.Unmarshal(respData, &respErr); jsonErr == nil { // Error creating json
Expand All @@ -217,7 +221,8 @@ func NamespaceList(endpoint string) error {
}

func NamespaceGet(endpoint string) error {
respData, err := utils.MakeRequest(context.Background(), endpoint, "GET", nil, nil)
tr := config.GetTransport()
respData, err := utils.MakeRequest(context.Background(), tr, endpoint, "GET", nil, nil)
var respErr clientResponseData
if err != nil {
if jsonErr := json.Unmarshal(respData, &respErr); jsonErr == nil { // Error creating json
Expand Down Expand Up @@ -264,8 +269,8 @@ func NamespaceDelete(endpoint string, prefix string) error {
authHeader := map[string]string{
"Authorization": "Bearer " + tok,
}

respData, err := utils.MakeRequest(context.Background(), endpoint, "DELETE", nil, authHeader)
tr := config.GetTransport()
respData, err := utils.MakeRequest(context.Background(), tr, endpoint, "DELETE", nil, authHeader)
var respErr clientResponseData
if err != nil {
if unmarshalErr := json.Unmarshal(respData, &respErr); unmarshalErr == nil { // Error creating json
Expand Down
5 changes: 2 additions & 3 deletions utils/ca_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/param"
)

Expand Down Expand Up @@ -86,13 +85,13 @@ func WriteCABundle(filename string) (int, error) {
//
// If we're on a platform (Mac, Windows) that does not provide a CA bundle, we return
// a count of 0 and do not launch the go routine.
func LaunchPeriodicWriteCABundle(ctx context.Context, filename string, sleepTime time.Duration) (count int, err error) {
func LaunchPeriodicWriteCABundle(ctx context.Context, egrpKey string, filename string, sleepTime time.Duration) (count int, err error) {
count, err = WriteCABundle(filename)
if err != nil || count == 0 {
return
}

egrp, ok := ctx.Value(config.EgrpKey).(*errgroup.Group)
egrp, ok := ctx.Value(egrpKey).(*errgroup.Group)
if !ok {
egrp = &errgroup.Group{}
}
Expand Down
9 changes: 3 additions & 6 deletions utils/web_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ import (
"github.com/gin-gonic/gin"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/pkg/errors"

"github.com/pelicanplatform/pelican/config"
)

// MakeRequest makes an http request with our custom http client. It acts similarly to the http.NewRequest but
// it only takes json as the request data.
func MakeRequest(ctx context.Context, url string, method string, data map[string]interface{}, headers map[string]string) ([]byte, error) {
func MakeRequest(ctx context.Context, tr *http.Transport, url string, method string, data map[string]interface{}, headers map[string]string) ([]byte, error) {
payload, _ := json.Marshal(data)
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(payload))
if err != nil {
Expand All @@ -49,7 +47,6 @@ func MakeRequest(ctx context.Context, url string, method string, data map[string
for key, val := range headers {
req.Header.Set(key, val)
}
tr := config.GetTransport()
client := &http.Client{Transport: tr}

resp, err := client.Do(req)
Expand Down Expand Up @@ -140,11 +137,11 @@ func HasContentType(r *http.Response, mimetype string) bool {
return false
}

func GetJwks(ctx context.Context, location string) (jwk.Set, error) {
func GetJwks(ctx context.Context, tr *http.Transport, location string) (jwk.Set, error) {
if location == "" {
return nil, errors.New("jwks location is empty")
}
client := http.Client{Transport: config.GetTransport()}
client := http.Client{Transport: tr}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, location, nil)
if err != nil {
return nil, err
Expand Down
120 changes: 120 additions & 0 deletions web_ui/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web_ui/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func runtimeInfo() (api_v1.RuntimeInfo, error) {

func onceLaunchCABundleUpdate(ctx context.Context, caBundle string) (certCtn int, err error) {
onceCABundle.Do(func() {
certCtn, err = utils.LaunchPeriodicWriteCABundle(ctx, caBundle, 2*time.Minute)
egrpKey := string(pelican_config.EgrpKey)
certCtn, err = utils.LaunchPeriodicWriteCABundle(ctx, egrpKey, caBundle, 2*time.Minute)
})
return
}
Expand Down
3 changes: 2 additions & 1 deletion xrootd/xrootd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ func ConfigXrootd(ctx context.Context, isOrigin bool) (string, error) {
if !isOrigin {
runtimeCAs = filepath.Join(param.Cache_RunLocation.GetString(), "ca-bundle.crt")
}
caCount, err := utils.LaunchPeriodicWriteCABundle(ctx, runtimeCAs, 2*time.Minute)
egrpKey := string(config.EgrpKey)
caCount, err := utils.LaunchPeriodicWriteCABundle(ctx, egrpKey, runtimeCAs, 2*time.Minute)
if err != nil {
return "", errors.Wrap(err, "Failed to setup the runtime CA bundle")
}
Expand Down

0 comments on commit ba59413

Please sign in to comment.