-
Notifications
You must be signed in to change notification settings - Fork 32
Add an embedded OAuth2 issuer #3151
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
Draft
bbockelm
wants to merge
12
commits into
PelicanPlatform:main
Choose a base branch
from
bbockelm:embedded-oauth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
708804f
Initial review round of embedded fosite issuer
bbockelm d21faba
Add pelican credentials token setup command with credential file support
Copilot 8b48284
Next round of human-reviewed changes
bbockelm 3fa3410
WIP: Remaining new files
bbockelm 3338c5f
Fixup redirection when returning from login
CannonLock 55617b6
Fix linter issues
bbockelm dcf1c38
Human reviews for the OAuth2 issuer
bbockelm cf33e37
Add audience verification for the POSIXv2 origin
bbockelm 6aaef1f
Misc linter and Windows fixes
bbockelm 12ebe3a
Additional Windows fixes
bbockelm a10aae8
Fixup redirection when returning from login
CannonLock 4e83e66
Lint
CannonLock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /*************************************************************** | ||
| * | ||
| * Copyright (C) 2026, 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 main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/pkg/errors" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| "github.com/pelicanplatform/pelican/client" | ||
| "github.com/pelicanplatform/pelican/config" | ||
| ) | ||
|
|
||
| var ( | ||
| tokenSetupNoPassword bool | ||
| tokenSetupCredentialFile string | ||
| tokenSetupRead bool | ||
| tokenSetupWrite bool | ||
| ) | ||
|
|
||
| // addCredentialsTokenSetupCommand adds the "setup" subcommand to the given | ||
| // credentials token command. | ||
| func addCredentialsTokenSetupCommand(credentialsTokenCmd *cobra.Command) { | ||
| setupCmd := &cobra.Command{ | ||
| Use: "setup <pelican-url>", | ||
| Short: "Set up a credential file containing tokens for a Pelican namespace", | ||
| Long: `Acquire a token for the specified Pelican namespace and save it to a | ||
| credential file on disk. The credential file contains the access token, | ||
| refresh token, and OAuth2 client credentials needed to obtain fresh tokens | ||
| later without re-authenticating. | ||
|
|
||
| By default, the credential file is password-protected. Use --no-password to | ||
| save the file without encryption, which is useful for non-interactive contexts | ||
| where password prompts would fail. | ||
|
|
||
| Use --credential-file to specify an alternative path for the credential file. | ||
|
|
||
| Examples: | ||
| # Set up credentials for reading from a namespace | ||
| pelican credentials token setup --read pelican://federation.example.org/namespace/path | ||
|
|
||
| # Set up credentials for reading and writing | ||
| pelican credentials token setup --write pelican://federation.example.org/namespace/path | ||
|
|
||
| # Set up credentials without password protection | ||
| pelican credentials token setup --no-password --read pelican://federation.example.org/namespace/path | ||
|
|
||
| # Set up credentials to a specific file | ||
| pelican credentials token setup --credential-file /path/to/creds.pem --read pelican://federation.example.org/namespace/path`, | ||
| RunE: credentialsTokenSetupMain, | ||
| Args: cobra.ExactArgs(1), | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| setupCmd.Flags().BoolVar(&tokenSetupNoPassword, "no-password", false, "Save the credential file without password protection") | ||
| setupCmd.Flags().BoolVarP(&tokenSetupRead, "read", "r", false, "Request a read token") | ||
| setupCmd.Flags().BoolVarP(&tokenSetupWrite, "write", "w", false, "Request a write token (implies read)") | ||
|
|
||
| setupCmd.Flags().StringVar(&tokenSetupCredentialFile, "credential-file", "", "Path to the credential file to write") | ||
| if err := viper.BindPFlag("Client.CredentialFile", setupCmd.Flags().Lookup("credential-file")); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| credentialsTokenCmd.AddCommand(setupCmd) | ||
| } | ||
|
|
||
| func credentialsTokenSetupMain(cmd *cobra.Command, args []string) error { | ||
| err := config.InitClient() | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to initialize client configuration") | ||
| } | ||
|
|
||
| // Parse the Pelican URL | ||
| rawUrl := args[0] | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| pUrl, err := client.ParseRemoteAsPUrl(ctx, rawUrl) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to parse URL: %s", rawUrl) | ||
| } | ||
|
|
||
| // Default to read if neither --read nor --write is specified | ||
| if !tokenSetupRead && !tokenSetupWrite { | ||
| tokenSetupRead = true | ||
| } | ||
|
|
||
| // Determine the HTTP method based on access mode | ||
| httpMethod := http.MethodGet | ||
| if tokenSetupWrite { | ||
| httpMethod = http.MethodPut | ||
| } | ||
|
|
||
| // Get director info for the path | ||
| dirResp, err := client.GetDirectorInfoForPath(ctx, pUrl, httpMethod, "") | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to get director info for %s", rawUrl) | ||
| } | ||
|
|
||
| // Public prefixes don't require tokens | ||
| if !dirResp.XPelNsHdr.RequireToken { | ||
| fmt.Fprintln(os.Stderr, "The specified namespace does not require tokens; no credential file is needed.") | ||
| return nil | ||
| } | ||
|
|
||
| // Determine the operation type. | ||
| // --write implies read: request all scopes so the token works for both. | ||
| var operation config.TokenOperation | ||
| if tokenSetupWrite { | ||
| operation.Set(config.TokenWrite) | ||
| operation.Set(config.TokenDelete) | ||
| } | ||
| operation.Set(config.TokenRead) | ||
| operation.Set(config.TokenList) | ||
|
|
||
| // Acquire a token (this will also register the OAuth2 client and save | ||
| // credentials to the credential file as a side effect) | ||
| opts := config.TokenGenerationOpts{ | ||
| Operation: operation, | ||
| } | ||
|
|
||
| token, err := client.AcquireToken(pUrl.GetRawUrl(), dirResp, opts) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to acquire token") | ||
| } | ||
|
|
||
| if token == "" { | ||
| return errors.New("acquired token is empty") | ||
| } | ||
|
|
||
| // Now read the credential config that was just saved and optionally | ||
| // re-save it as a passwordless file | ||
| credFilePath, err := config.GetEncryptedConfigName() | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to determine credential file path") | ||
| } | ||
|
|
||
| if tokenSetupNoPassword { | ||
| // Read the current config and re-save without password | ||
| osdfConfig, err := config.GetCredentialConfigContents() | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to read credential configuration") | ||
| } | ||
|
|
||
| if err := config.SaveConfigContentsToFile(&osdfConfig, credFilePath, false); err != nil { | ||
| return errors.Wrap(err, "failed to save passwordless credential file") | ||
| } | ||
|
|
||
| log.Infof("Credential file saved without password protection to %s", credFilePath) | ||
| } else { | ||
| log.Infof("Credential file saved to %s", credFilePath) | ||
|
||
| } | ||
|
|
||
| fmt.Fprintf(os.Stderr, "Successfully set up credentials for %s\n", dirResp.XPelNsHdr.Namespace) | ||
| fmt.Fprintf(os.Stderr, "Credential file: %s\n", credFilePath) | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.