blob: 75b7164070873dadb0e25db58c559e3b55c88cb3 [file] [edit]
// 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
import (
"flag"
"os"
"path/filepath"
"testing"
_ "github.com/bazelbuild/rsclient/internal/pkg/testsetup"
)
// TestCredentialHelperExecution verifies that configuring a credentials helper
// successfully executes the command and yields per-RPC credentials.
func TestCredentialHelperExecution(t *testing.T) {
tempDir := t.TempDir()
mockHelper := filepath.Join(tempDir, "mock_helper.sh")
// Create a mock helper script that prints standard JSON token output.
scriptContent := `#!/bin/sh
cat << 'EOF'
{
"token": "mock-secret-token-12345"
}
EOF
`
if err := os.WriteFile(mockHelper, []byte(scriptContent), 0755); err != nil {
t.Fatalf("failed to write mock helper script: %v", err)
}
cfg := NewConfig()
cfg.CredentialsHelper = mockHelper
perCreds, err := cfg.ResolvePerRPCCredentials()
if err != nil {
t.Fatalf("ResolvePerRPCCredentials failed: %v", err)
}
if perCreds == nil || perCreds.Creds == nil {
t.Fatalf("expected non-nil perRPCCreds")
}
perCreds2, err := cfg.ResolvePerRPCCredentials()
if err != nil {
t.Fatalf("second ResolvePerRPCCredentials failed: %v", err)
}
if perCreds2 != perCreds {
t.Errorf("expected cached perRPCCreds pointer, got %p want %p", perCreds2, perCreds)
}
params, err := cfg.MakeDialParams("remotebuildexecution.googleapis.com:443", "custom.sni")
if err != nil {
t.Fatalf("MakeDialParams failed: %v", err)
}
if params.Service != "remotebuildexecution.googleapis.com:443" {
t.Errorf("Service = %q, want remotebuildexecution.googleapis.com:443", params.Service)
}
if params.TLSServerName != "custom.sni" {
t.Errorf("TLSServerName = %q, want custom.sni", params.TLSServerName)
}
if !params.UseExternalAuthToken {
t.Errorf("expected UseExternalAuthToken to be true")
}
if params.ExternalPerRPCCreds == nil {
t.Fatalf("expected non-nil ExternalPerRPCCreds")
}
}
// TestFlagParsing verifies that standard auth flags are registered and parsed correctly.
func TestFlagParsing(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
cfg := NewConfig()
cfg.RegisterFlags(fs)
args := []string{
"--credentials_helper=/bin/reclient-helper",
"--credentials_helper_args=--server=foo",
"--use_application_default_credentials=true",
"--tls_server_name=custom.server.com",
}
if err := fs.Parse(args); err != nil {
t.Fatalf("fs.Parse failed: %v", err)
}
if cfg.CredentialsHelper != "/bin/reclient-helper" {
t.Errorf("CredentialsHelper = %q, want /bin/reclient-helper", cfg.CredentialsHelper)
}
if cfg.CredentialsHelperArgs != "--server=foo" {
t.Errorf("CredentialsHelperArgs = %q, want --server=foo", cfg.CredentialsHelperArgs)
}
if !cfg.UseApplicationDefault {
t.Errorf("UseApplicationDefault = false, want true")
}
if cfg.TLSServerName != "custom.server.com" {
t.Errorf("TLSServerName = %q, want custom.server.com", cfg.TLSServerName)
}
if !cfg.HasAuthSettings() {
t.Errorf("HasAuthSettings = false, want true")
}
}
func TestHasCredentials(t *testing.T) {
cfg := NewConfig()
if cfg.HasCredentials() {
t.Errorf("empty config: HasCredentials = true, want false")
}
cfg.CredentialsHelper = "/bin/helper"
if !cfg.HasCredentials() {
t.Errorf("helper set: HasCredentials = false, want true")
}
cfg.UseRPCCredentials = false
if cfg.HasCredentials() {
t.Errorf("UseRPCCredentials=false: HasCredentials = true, want false")
}
cfg.UseRPCCredentials = true
cfg.NoAuth = true
if cfg.HasCredentials() {
t.Errorf("NoAuth=true: HasCredentials = true, want false")
}
cfg.NoAuth = false
cfg.NoSecurity = true
if cfg.HasCredentials() {
t.Errorf("NoSecurity=true: HasCredentials = true, want false")
}
}
func TestResolvePerRPCCredentials_Disabled(t *testing.T) {
cfg := NewConfig()
cfg.CredentialsHelper = "/bin/helper"
cfg.UseRPCCredentials = false
creds, err := cfg.ResolvePerRPCCredentials()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if creds != nil {
t.Errorf("expected nil creds when UseRPCCredentials=false, got %v", creds)
}
}