Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for token based auth for PCS #15

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/pcs-transition-abort.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ See ochami-pcs(1) for more details.`,
os.Exit(1)
}

// This endpoint requires authentication, so a token is needed
setTokenFromEnvVar(cmd)
checkToken(cmd)

// Create client to make request to PCS
pcsClient, err := pcs.NewClient(pcsBaseURI, insecure)
if err != nil {
Expand All @@ -47,7 +51,7 @@ See ochami-pcs(1) for more details.`,
useCACert(pcsClient.OchamiClient)

// Abort the transition
transitionHttpEnv, err := pcsClient.DeleteTransition(transitionID)
transitionHttpEnv, err := pcsClient.DeleteTransition(transitionID, token)
if err != nil {
if errors.Is(err, client.UnsuccessfulHTTPError) {
log.Logger.Fatal().Err(err).Msg("PCS transition abort request yielded unsuccessful HTTP response")
Expand Down
6 changes: 5 additions & 1 deletion cmd/pcs-transition-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ See ochami-pcs(1) for more details.`,
os.Exit(1)
}

// This endpoint requires authentication, so a token is needed
setTokenFromEnvVar(cmd)
checkToken(cmd)

// Create client to make request to PCS
pcsClient, err := pcs.NewClient(pcsBaseURI, insecure)
if err != nil {
Expand All @@ -45,7 +49,7 @@ See ochami-pcs(1) for more details.`,
useCACert(pcsClient.OchamiClient)

// Get transitions
transitionsHttpEnv, err := pcsClient.GetTransitions()
transitionsHttpEnv, err := pcsClient.GetTransitions(token)
if err != nil {
if errors.Is(err, client.UnsuccessfulHTTPError) {
log.Logger.Fatal().Err(err).Msg("PCS transitions request yielded unsuccessful HTTP response")
Expand Down
6 changes: 5 additions & 1 deletion cmd/pcs-transition-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ See ochami-pcs(1) for more details.`,
os.Exit(1)
}

// This endpoint requires authentication, so a token is needed
setTokenFromEnvVar(cmd)
checkToken(cmd)

// Create client to make request to PCS
pcsClient, err := pcs.NewClient(pcsBaseURI, insecure)
if err != nil {
Expand All @@ -100,7 +104,7 @@ See ochami-pcs(1) for more details.`,

// Poll transition state until it is complete or aborted
for {
transitionHttpEnv, err := pcsClient.GetTransition(transitionID)
transitionHttpEnv, err := pcsClient.GetTransition(transitionID, token)
if err != nil {
log.Logger.Fatal().Err(err).Msg("failed to get transition")
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/pcs-transition-show.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ See ochami-pcs(1) for more details.`,
os.Exit(1)
}

// This endpoint requires authentication, so a token is needed
setTokenFromEnvVar(cmd)
checkToken(cmd)

// Create client to make request to PCS
pcsClient, err := pcs.NewClient(pcsBaseURI, insecure)
if err != nil {
Expand All @@ -47,7 +51,7 @@ See ochami-pcs(1) for more details.`,
useCACert(pcsClient.OchamiClient)

// Get transition
transitionHttpEnv, err := pcsClient.GetTransition(transitionID)
transitionHttpEnv, err := pcsClient.GetTransition(transitionID, token)
if err != nil {
if errors.Is(err, client.UnsuccessfulHTTPError) {
log.Logger.Fatal().Err(err).Msg("PCS transitions request yielded unsuccessful HTTP response")
Expand Down
6 changes: 5 additions & 1 deletion cmd/pcs-transition-start.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ See ochami-pcs(1) for more details.`,
os.Exit(1)
}

// This endpoint requires authentication, so a token is needed
setTokenFromEnvVar(cmd)
checkToken(cmd)

// Create client to make request to PCS
pcsClient, err := pcs.NewClient(pcsBaseURI, insecure)
if err != nil {
Expand All @@ -85,7 +89,7 @@ See ochami-pcs(1) for more details.`,
}

// Create transition
transitionHttpEnv, err := pcsClient.CreateTransition(operation, nil, xnames)
transitionHttpEnv, err := pcsClient.CreateTransition(operation, nil, xnames, token)
if err != nil {
if errors.Is(err, client.UnsuccessfulHTTPError) {
log.Logger.Fatal().Err(err).Msg("PCS transition create request yielded unsuccessful HTTP response")
Expand Down
44 changes: 36 additions & 8 deletions pkg/client/pcs/pcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,16 @@ type locationEntry struct {

// CreateTransition is a wrapper function around OchamiClient.PostData to
// hit the /transitions endpoint
func (pc *PCSClient) CreateTransition(operation string, taskDeadline *int, xnames []string) (client.HTTPEnvelope, error) {
func (pc *PCSClient) CreateTransition(operation string, taskDeadline *int, xnames []string, token string) (client.HTTPEnvelope, error) {
var henv client.HTTPEnvelope

headers := client.NewHTTPHeaders()
if token != "" {
if err := headers.SetAuthorization(token); err != nil {
return henv, fmt.Errorf("CreateTransition(): error setting token in HTTP headers: %w", err)
}
}

// Create the request body
location := []locationEntry{}
for i := 0; i < len(xnames); i++ {
Expand All @@ -127,7 +134,7 @@ func (pc *PCSClient) CreateTransition(operation string, taskDeadline *int, xname
return henv, fmt.Errorf("CreateTransition(): failed to create HTTPBody: %w", err)
}

henv, err = pc.PostData(PCSTransitions, "", nil, httpBody)
henv, err = pc.PostData(PCSTransitions, "", headers, httpBody)
if err != nil {
err = fmt.Errorf("CreateTransition(): error creating PCS health: %w", err)
}
Expand All @@ -137,13 +144,20 @@ func (pc *PCSClient) CreateTransition(operation string, taskDeadline *int, xname

// GetTransitions is a wrapper function around OchamiClient.GetData to
// hit the /transitions endpoint
func (pc *PCSClient) GetTransitions() (client.HTTPEnvelope, error) {
func (pc *PCSClient) GetTransitions(token string) (client.HTTPEnvelope, error) {
var (
henv client.HTTPEnvelope
err error
)

henv, err = pc.GetData(PCSTransitions, "", nil)
headers := client.NewHTTPHeaders()
if token != "" {
if err := headers.SetAuthorization(token); err != nil {
return henv, fmt.Errorf("GetTransitions(): error setting token in HTTP headers: %w", err)
}
}

henv, err = pc.GetData(PCSTransitions, "", headers)
if err != nil {
err = fmt.Errorf("GetTransitions(): error getting PCS transitions: %w", err)
}
Expand All @@ -153,20 +167,27 @@ func (pc *PCSClient) GetTransitions() (client.HTTPEnvelope, error) {

// GetTransitions is a wrapper function around OchamiClient.GetData to
// hit the /transitions/{transitionID} endpoint
func (pc *PCSClient) GetTransition(id string) (client.HTTPEnvelope, error) {
func (pc *PCSClient) GetTransition(id string, token string) (client.HTTPEnvelope, error) {
var (
henv client.HTTPEnvelope
err error
pcsTransitionsEndpoint string
)

headers := client.NewHTTPHeaders()
if token != "" {
if err := headers.SetAuthorization(token); err != nil {
return henv, fmt.Errorf("GetTransition(): error setting token in HTTP headers: %w", err)
}
}

pcsTransitionsEndpoint, err = url.JoinPath(PCSTransitions, id)
if err != nil {
err = fmt.Errorf("GetTransition(): error joining PCS transitions endpoint: %w", err)
return henv, err
}

henv, err = pc.GetData(pcsTransitionsEndpoint, "", nil)
henv, err = pc.GetData(pcsTransitionsEndpoint, "", headers)
if err != nil {
err = fmt.Errorf("GetTransition(): error getting PCS transition: %w", err)
}
Expand All @@ -176,20 +197,27 @@ func (pc *PCSClient) GetTransition(id string) (client.HTTPEnvelope, error) {

// DeleteTransitions is a wrapper function around OchamiClient.DeleteData to
// hit the /transitions/{transitionID} endpoint
func (pc *PCSClient) DeleteTransition(id string) (client.HTTPEnvelope, error) {
func (pc *PCSClient) DeleteTransition(id string, token string) (client.HTTPEnvelope, error) {
var (
henv client.HTTPEnvelope
err error
pcsTransitionEndpoint string
)

headers := client.NewHTTPHeaders()
if token != "" {
if err := headers.SetAuthorization(token); err != nil {
return henv, fmt.Errorf("DeleteTransition(): error setting token in HTTP headers: %w", err)
}
}

pcsTransitionEndpoint, err = url.JoinPath(PCSTransitions, id)
if err != nil {
err = fmt.Errorf("DeleteTransition(): error joining PCS transition endpoint: %w", err)
return henv, err
}

henv, err = pc.DeleteData(pcsTransitionEndpoint, "", nil, nil)
henv, err = pc.DeleteData(pcsTransitionEndpoint, "", headers, nil)
if err != nil {
err = fmt.Errorf("DeleteTransition(): error deleting PCS transition: %w", err)
}
Expand Down