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

feat: Enable unauthenticated API calls #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions cbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type tableLike interface {
func getCredentialOpts(opts []option.ClientOption) []option.ClientOption {
if ts := config.TokenSource; ts != nil {
opts = append(opts, option.WithTokenSource(ts))
} else if config.Unauthenticated {
opts = append(opts, option.WithoutAuthentication())
}
if tlsCreds := config.TLSCreds; tlsCreds != nil {
opts = append(opts, option.WithGRPCDialOption(grpc.WithTransportCredentials(tlsCreds)))
Expand Down
5 changes: 4 additions & 1 deletion cbtconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config struct {
UserAgent string // optional
AccessToken string // optional
AuthToken string // optional
Unauthenticated bool // optional
Timeout time.Duration // optional
TokenSource oauth2.TokenSource // derived
TLSCreds credentials.TransportCredentials // derived
Expand Down Expand Up @@ -250,6 +251,8 @@ func (c *Config) SetFromGcloud() error {
if c.Creds == "" {
log.Printf("-creds flag unset, will use gcloud credential")
}
} else if c.Creds == "none" {
c.Unauthenticated = true
} else {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", c.Creds)
}
Expand Down Expand Up @@ -282,7 +285,7 @@ func (c *Config) SetFromGcloud() error {
c.Project = gcloudConfig.Configuration.Properties.Core.Project
}

if c.AccessToken == "" && c.Creds == "" {
if c.AccessToken == "" && c.Creds == "" && !c.Unauthenticated {
c.TokenSource = oauth2.ReuseTokenSource(
gcloudConfig.Credential.Token(),
&GcloudCmdTokenSource{Command: gcloudCmd, Args: gcloudCmdArgs})
Expand Down