| // Copyright 2026 Google LLC | |
| // | |
| // 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 authcli provides standard command-line interface flags and configuration | |
| // for gRPC transport security, Google authentication (ADC, GCE), and external | |
| // credential helpers across rsclient tools. | |
| package authcli | |
| import ( | |
| "context" | |
| "flag" | |
| "fmt" | |
| "strings" | |
| "github.com/bazelbuild/remote-apis-sdks/go/pkg/credshelper" | |
| "github.com/bazelbuild/remote-apis-sdks/go/pkg/moreflag" | |
| "github.com/bazelbuild/rsclient/internal/pkg/auth" | |
| "google.golang.org/grpc" | |
| ) | |
| const ( | |
| // DefaultMaxConcurrentRequests is the default maximum number of concurrent | |
| // RPCs on a single gRPC connection. | |
| DefaultMaxConcurrentRequests = 128 | |
| ) | |
| // Config holds command-line flags and parameters for gRPC authentication, | |
| // TLS credentials, and external credential helpers. | |
| type Config struct { | |
| // DialParams embeds low-level transport and credential parameters. | |
| auth.DialParams | |
| // CredentialsHelper is the path to an external credential helper binary. | |
| CredentialsHelper string | |
| // CredentialsHelperArgs are whitespace-separated arguments passed to CredentialsHelper. | |
| CredentialsHelperArgs string | |
| // UseRPCCredentials controls whether per-RPC credentials are attached (default: true). | |
| // If false, disables per-RPC credentials and connects with transport credentials only. | |
| UseRPCCredentials bool | |
| // MaxConcurrentRequests specifies the maximum concurrent RPCs on a single connection. | |
| MaxConcurrentRequests uint | |
| } | |
| // NewConfig creates a Config with recommended defaults. | |
| func NewConfig() *Config { | |
| return &Config{ | |
| UseRPCCredentials: true, | |
| MaxConcurrentRequests: DefaultMaxConcurrentRequests, | |
| } | |
| } | |
| // RegisterFlags binds standard authentication and security flags to the given FlagSet. | |
| func (c *Config) RegisterFlags(fs *flag.FlagSet) { | |
| if c.MaxConcurrentRequests == 0 { | |
| c.MaxConcurrentRequests = DefaultMaxConcurrentRequests | |
| } | |
| c.UseRPCCredentials = true | |
| // Native Google Credentials: | |
| fs.StringVar(&c.CredFile, "credential_file", "", "The name of a file that contains service account credentials to use when streaming build events. Used only if --use_application_default_credentials and --use_gce_credentials are false.") | |
| fs.BoolVar(&c.UseApplicationDefault, "use_application_default_credentials", false, "If true, use application default credentials to connect to build event service. See https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login") | |
| fs.BoolVar(&c.UseComputeEngine, "use_gce_credentials", false, "If true (and --use_application_default_credentials is false), use the default GCE credentials to authenticate with build event service.") | |
| // External Credential Helper: | |
| fs.StringVar(&c.CredentialsHelper, credshelper.CredshelperPathFlag, "", "Path to the credentials helper binary. If given execrel://, looks for the `credshelper` binary in the same folder as the current executable.") | |
| fs.StringVar(&c.CredentialsHelperArgs, credshelper.CredshelperArgsFlag, "", "Arguments for the credentials helper, separated by space.") | |
| // Security & Transport Controls: | |
| fs.BoolVar(&c.UseRPCCredentials, "use_rpc_credentials", true, "If false, no per-RPC credentials will be used (disables --credential_file, --use_application_default_credentials, and --use_gce_credentials.") | |
| fs.BoolVar(&c.UseExternalAuthToken, "use_external_auth_token", false, "If true, se an externally provided auth token, given via PerRPCCreds when the SDK is initialized.") | |
| fs.BoolVar(&c.NoSecurity, "service_no_security", false, "If true, do not use TLS or authentication when connecting to the gRPC service.") | |
| fs.BoolVar(&c.NoAuth, "service_no_auth", false, "If true, do not authenticate with the service (implied by --service_no_security).") | |
| // TLS Options: | |
| fs.StringVar(&c.TLSServerName, "tls_server_name", "", "Override the TLS server name for both ResultStore and CAS") | |
| fs.StringVar(&c.TLSCACertFile, "tls_ca_cert", "", "Load TLS CA certificates from this file") | |
| fs.StringVar(&c.TLSClientAuthCert, "tls_client_auth_cert", "", "Certificate to use when using mTLS to connect to the RBE service.") | |
| fs.StringVar(&c.TLSClientAuthKey, "tls_client_auth_key", "", "Key to use when using mTLS to connect to the RBE service.") | |
| // Headers and Concurrency: | |
| fs.Var((*moreflag.StringListMapValue)(&c.RemoteHeaders), "remote_headers", "Comma-separated headers to pass with each RPC in the form key=value.") | |
| fs.UintVar(&c.MaxConcurrentRequests, "max_concurrent_requests_per_conn", c.MaxConcurrentRequests, "Maximum number of concurrent RPCs on a single gRPC connection.") | |
| } | |
| // HasAuthSettings returns true if any authentication or security flag was explicitly configured. | |
| func (c *Config) HasAuthSettings() bool { | |
| return c.CredentialsHelper != "" || | |
| c.UseApplicationDefault || | |
| c.UseComputeEngine || | |
| c.CredFile != "" || | |
| c.UseExternalAuthToken || | |
| !c.UseRPCCredentials || | |
| c.NoSecurity || | |
| c.NoAuth || | |
| c.TLSServerName != "" || | |
| c.TLSCACertFile != "" || | |
| c.TLSClientAuthCert != "" || | |
| c.TLSClientAuthKey != "" || | |
| len(c.RemoteHeaders) > 0 | |
| } | |
| // HasCredentials returns true if any per-RPC authentication credentials (helper, ADC, GCE, | |
| // key file, or external token) are configured and enabled. | |
| // Returns false if per-RPC credentials are disabled (--use_rpc_credentials=false) or if | |
| // unauthenticated operation is requested (--service_no_auth / --service_no_security). | |
| func (c *Config) HasCredentials() bool { | |
| if !c.UseRPCCredentials || c.NoAuth || c.NoSecurity { | |
| return false | |
| } | |
| return c.CredentialsHelper != "" || | |
| c.UseApplicationDefault || | |
| c.UseComputeEngine || | |
| c.CredFile != "" || | |
| c.UseExternalAuthToken | |
| } | |
| // ResolvePerRPCCredentials instantiates and returns the per-RPC credentials helper, if configured. | |
| // Skips helper execution if per-RPC credentials are disabled or unauthenticated operation is requested. | |
| func (c *Config) ResolvePerRPCCredentials() (*auth.PerRPCCreds, error) { | |
| if !c.UseRPCCredentials || c.NoAuth || c.NoSecurity { | |
| return nil, nil | |
| } | |
| if c.ExternalPerRPCCreds != nil { | |
| return c.ExternalPerRPCCreds, nil | |
| } | |
| if c.CredentialsHelper != "" { | |
| creds, err := credshelper.NewExternalCredentials(c.CredentialsHelper, strings.Fields(c.CredentialsHelperArgs)) | |
| if err != nil { | |
| return nil, fmt.Errorf("credentials helper failed. Please try again or use another method of authentication: %v", err) | |
| } | |
| c.UseExternalAuthToken = true | |
| c.ExternalPerRPCCreds = &auth.PerRPCCreds{Creds: creds.PerRPCCreds()} | |
| return c.ExternalPerRPCCreds, nil | |
| } | |
| return nil, nil | |
| } | |
| // MakeDialParams constructs a configured auth.DialParams for a specific service target and optional TLS server name override. | |
| func (c *Config) MakeDialParams(service string, tlsServerName string) (auth.DialParams, error) { | |
| perRPCCreds, err := c.ResolvePerRPCCredentials() | |
| if err != nil { | |
| return auth.DialParams{}, err | |
| } | |
| p := c.DialParams | |
| p.Service = service | |
| p.ExternalPerRPCCreds = perRPCCreds | |
| p.TransportCredsOnly = !c.HasCredentials() | |
| p.MaxConcurrentRequests = uint32(c.MaxConcurrentRequests) | |
| if tlsServerName != "" { | |
| p.TLSServerName = tlsServerName | |
| } | |
| return p, nil | |
| } | |
| // ToDialOptions generates the gRPC DialOptions to establish a connection to the given service. | |
| func (c *Config) ToDialOptions(ctx context.Context, service string, tlsServerName string) ([]grpc.DialOption, error) { | |
| params, err := c.MakeDialParams(service, tlsServerName) | |
| if err != nil { | |
| return nil, err | |
| } | |
| opts, _, err := auth.OptsFromParams(ctx, params) | |
| return opts, err | |
| } |