-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[git] implement scope elevation in server #3565
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,121 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
|
||
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol" | ||
supervisor "github.com/gitpod-io/gitpod/supervisor/api" | ||
) | ||
|
||
var gitTokenValidatorOpts struct { | ||
User string | ||
Token string | ||
TokenScopes string | ||
Host string | ||
RepoURL string | ||
GitCommand string | ||
} | ||
|
||
var gitTokenValidator = &cobra.Command{ | ||
Use: "git-token-validator", | ||
Short: "Gitpod's Git token validator", | ||
Long: "Tries to guess the scopes needed for a git operation and requests an appropriate token.", | ||
Args: cobra.ExactArgs(0), | ||
Hidden: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
log.Infof("gp git-token-validator") | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) | ||
defer cancel() | ||
supervisorAddr := os.Getenv("SUPERVISOR_ADDR") | ||
if supervisorAddr == "" { | ||
supervisorAddr = "localhost:22999" | ||
} | ||
supervisorConn, err := grpc.Dial(supervisorAddr, grpc.WithInsecure()) | ||
if err != nil { | ||
log.WithError(err).Fatal("error connecting to supervisor") | ||
} | ||
wsinfo, err := supervisor.NewInfoServiceClient(supervisorConn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{}) | ||
if err != nil { | ||
log.WithError(err).Fatal("error getting workspace info from supervisor") | ||
} | ||
clientToken, err := supervisor.NewTokenServiceClient(supervisorConn).GetToken(ctx, &supervisor.GetTokenRequest{ | ||
Host: wsinfo.GitpodApi.Host, | ||
Kind: "gitpod", | ||
Scope: []string{ | ||
"function:guessGitTokenScopes", | ||
}, | ||
}) | ||
if err != nil { | ||
log.WithError(err).Fatal("error getting token from supervisor") | ||
} | ||
client, err := serverapi.ConnectToServer(wsinfo.GitpodApi.Endpoint, serverapi.ConnectToServerOpts{Token: clientToken.Token, Context: ctx}) | ||
if err != nil { | ||
log.WithError(err).Fatal("error connecting to server") | ||
} | ||
params := &serverapi.GuessGitTokenScopesParams{ | ||
Host: gitTokenValidatorOpts.Host, | ||
RepoURL: gitTokenValidatorOpts.RepoURL, | ||
GitCommand: gitTokenValidatorOpts.GitCommand, | ||
CurrentToken: &serverapi.GitToken{ | ||
Token: gitTokenValidatorOpts.Token, | ||
Scopes: strings.Split(gitTokenValidatorOpts.TokenScopes, ","), | ||
User: gitTokenValidatorOpts.User, | ||
}, | ||
} | ||
guessedTokenScopes, err := client.GuessGitTokenScopes(ctx, params) | ||
if err != nil { | ||
log.WithError(err).Fatal("error guessing token scopes on server") | ||
} | ||
if guessedTokenScopes.Message != "" { | ||
message := fmt.Sprintf("%s Please check the permissions on the [access control page](%s/access-control).", guessedTokenScopes.Message, wsinfo.GetGitpodHost()) | ||
_, err := supervisor.NewNotificationServiceClient(supervisorConn).Notify(ctx, | ||
&supervisor.NotifyRequest{ | ||
Level: supervisor.NotifyRequest_INFO, | ||
Message: message, | ||
}) | ||
log.WithError(err).Fatalf("error notifying client: '%s'", message) | ||
} | ||
JanKoehnlein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(guessedTokenScopes.Scopes) > 0 { | ||
_, err = supervisor.NewTokenServiceClient(supervisorConn).GetToken(ctx, | ||
&supervisor.GetTokenRequest{ | ||
Host: gitTokenValidatorOpts.Host, | ||
Scope: guessedTokenScopes.Scopes, | ||
Description: "", | ||
Kind: "git", | ||
}) | ||
if err != nil { | ||
log.WithError(err).Fatal("error getting new token from token service") | ||
return | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(gitTokenValidator) | ||
gitTokenValidator.Flags().StringVarP(&gitTokenValidatorOpts.User, "user", "u", "", "Git user") | ||
gitTokenValidator.Flags().StringVarP(&gitTokenValidatorOpts.Token, "token", "t", "", "The Git token to be validated") | ||
gitTokenValidator.Flags().StringVarP(&gitTokenValidatorOpts.TokenScopes, "scopes", "s", "", "A comma spearated list of the scopes of given token") | ||
gitTokenValidator.Flags().StringVar(&gitTokenValidatorOpts.Host, "host", "", "The Git host") | ||
gitTokenValidator.Flags().StringVarP(&gitTokenValidatorOpts.RepoURL, "repoURL", "r", "", "The URL of the Git repository") | ||
gitTokenValidator.Flags().StringVarP(&gitTokenValidatorOpts.GitCommand, "gitCommand", "c", "", "The Git command to be performed") | ||
gitTokenValidator.MarkFlagRequired("user") | ||
gitTokenValidator.MarkFlagRequired("token") | ||
gitTokenValidator.MarkFlagRequired("scopes") | ||
gitTokenValidator.MarkFlagRequired("host") | ||
gitTokenValidator.MarkFlagRequired("repoURL") | ||
gitTokenValidator.MarkFlagRequired("gitCommand") | ||
} |
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
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.