-
Notifications
You must be signed in to change notification settings - Fork 288
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 buf registry whoami
command
#3416
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
00a1b80
Add `buf beta registry whoami` command
doriable 800e723
Add changelog
doriable 137df84
Merge remote-tracking branch 'origin/main' into 3414-buf-registry-whoami
doriable d13cfcb
Address comments
doriable 2872b32
Add godocs
doriable c5c4e72
Merge remote-tracking branch 'origin/main' into 3414-buf-registry-whoami
doriable ef73b4a
Return instructions for users to refresh their credentials.
doriable e7dc6d8
Slight wording change
doriable 32dda66
Merge remote-tracking branch 'origin/main' into 3414-buf-registry-whoami
doriable ae4bee4
Fix error message on invalid users
emcfarlane 9ad3f2e
Merge remote-tracking branch 'origin/main' into 3414-buf-registry-whoami
doriable a1f0290
Change registry login error to syserror
doriable 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 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 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,139 @@ | ||
// Copyright 2020-2024 Buf Technologies, Inc. | ||
// | ||
// 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 whoami | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/bufbuild/buf/private/buf/bufcli" | ||
"github.com/bufbuild/buf/private/buf/bufprint" | ||
"github.com/bufbuild/buf/private/bufpkg/bufconnect" | ||
"github.com/bufbuild/buf/private/gen/proto/connect/buf/alpha/registry/v1alpha1/registryv1alpha1connect" | ||
registryv1alpha1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/registry/v1alpha1" | ||
"github.com/bufbuild/buf/private/pkg/app/appcmd" | ||
"github.com/bufbuild/buf/private/pkg/app/appext" | ||
"github.com/bufbuild/buf/private/pkg/connectclient" | ||
"github.com/bufbuild/buf/private/pkg/netext" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
const ( | ||
formatFlagName = "format" | ||
|
||
loginCommand = "buf registry login" | ||
) | ||
|
||
// NewCommand returns a new Command. | ||
func NewCommand( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No godoc |
||
name string, | ||
builder appext.SubCommandBuilder, | ||
) *appcmd.Command { | ||
flags := newFlags() | ||
return &appcmd.Command{ | ||
Use: name + " <domain>", | ||
Short: `Check if you are logged in to the Buf Schema Registry`, | ||
Long: `This command checks if you are currently logged into the Buf Schema Registry at the provided <domain>. | ||
The <domain> argument will default to buf.build if not specified.`, | ||
Args: appcmd.MaximumNArgs(1), | ||
Run: builder.NewRunFunc( | ||
func(ctx context.Context, container appext.Container) error { | ||
return run(ctx, container, flags) | ||
}, | ||
), | ||
BindFlags: flags.Bind, | ||
} | ||
} | ||
|
||
type flags struct { | ||
Format string | ||
} | ||
|
||
func newFlags() *flags { | ||
return &flags{} | ||
} | ||
|
||
func (f *flags) Bind(flagSet *pflag.FlagSet) { | ||
flagSet.StringVar( | ||
&f.Format, | ||
formatFlagName, | ||
bufprint.FormatText.String(), | ||
fmt.Sprintf(`The output format to use. Must be one of %s`, bufprint.AllFormatsString), | ||
) | ||
} | ||
|
||
func run( | ||
ctx context.Context, | ||
container appext.Container, | ||
flags *flags, | ||
) error { | ||
remote := bufconnect.DefaultRemote | ||
if container.NumArgs() == 1 { | ||
remote = container.Arg(0) | ||
if _, err := netext.ValidateHostname(remote); err != nil { | ||
return err | ||
} | ||
} | ||
clientConfig, err := bufcli.NewConnectClientConfig(container) | ||
if err != nil { | ||
return err | ||
} | ||
authnService := connectclient.Make(clientConfig, remote, registryv1alpha1connect.NewAuthnServiceClient) | ||
currentUserResponse, err := authnService.GetCurrentUser(ctx, connect.NewRequest(®istryv1alpha1.GetCurrentUserRequest{})) | ||
if err != nil { | ||
if connectErr := new(connect.Error); errors.As(err, &connectErr) && connectErr.Code() == connect.CodeUnauthenticated { | ||
return fmt.Errorf("Not currently logged in for %s.", remote) | ||
} | ||
return err | ||
} | ||
user := currentUserResponse.Msg.User | ||
if user == nil { | ||
return fmt.Errorf( | ||
`No user is logged in to %s. Run %q to refresh your credentials. If you have the %s environment variable set, ensure that the token is valid.`, | ||
remote, | ||
loginCommandForRemote(remote), | ||
bufconnect.TokenEnvKey, | ||
) | ||
} | ||
format, err := bufprint.ParseFormat(flags.Format) | ||
if err != nil { | ||
return appcmd.WrapInvalidArgumentError(err) | ||
} | ||
// ParseFormat always expects a format that is either text or json, otherwise it returns | ||
// an error, so do not need a default case for this switch. | ||
switch format { | ||
case bufprint.FormatText: | ||
_, err = fmt.Fprintf(container.Stdout(), "Logged in as %s.\n", user.Username) | ||
return err | ||
case bufprint.FormatJSON: | ||
return bufprint.PrintEntity( | ||
container.Stdout(), | ||
format, | ||
bufprint.NewUserEntity(user), | ||
) | ||
} | ||
return nil | ||
} | ||
|
||
// loginCommandForRemote returns the login command for the given remote, | ||
// the default remote is excluded in the command. | ||
func loginCommandForRemote(remote string) string { | ||
if remote == bufconnect.DefaultRemote { | ||
return loginCommand | ||
} | ||
return fmt.Sprintf("%s %s", loginCommand, remote) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No godoc