@@ -23,6 +23,7 @@ import (
2323 "time"
2424
2525 "github.com/buildbuddy-io/buildbuddy/enterprise/server/bes_artifacts"
26+ "github.com/buildbuddy-io/buildbuddy/enterprise/server/util/ci_runner_env"
2627 "github.com/buildbuddy-io/buildbuddy/enterprise/server/workflow/config"
2728 "github.com/buildbuddy-io/buildbuddy/server/build_event_publisher"
2829 "github.com/buildbuddy-io/buildbuddy/server/real_environment"
@@ -107,9 +108,8 @@ const (
107108 // Env vars set by workflow runner
108109 // NOTE: These env vars are not populated for non-private repos.
109110
110- buildbuddyAPIKeyEnvVarName = "BUILDBUDDY_API_KEY"
111- repoUserEnvVarName = "REPO_USER"
112- repoTokenEnvVarName = "REPO_TOKEN"
111+ repoUserEnvVarName = "REPO_USER"
112+ repoTokenEnvVarName = "REPO_TOKEN"
113113
114114 // Exit code placeholder used when a command doesn't return an exit code on its own.
115115 noExitCode = - 1
@@ -142,7 +142,7 @@ const (
142142 ansiGray = "\033 [90m"
143143 ansiReset = "\033 [0m"
144144
145- clientIdentityEnvVar = "BB_GRPC_CLIENT_IDENTITY"
145+ clientIdentityEnvVar = ci_runner_env . BBGrpcClientIdentityEnvVarName
146146
147147 // We save the startup options used for the last executed bazel command so we can apply
148148 // them on future bazel commands without restarting the Bazel server.
@@ -313,7 +313,7 @@ type buildEventReporter struct {
313313 progressCount int32
314314}
315315
316- func newBuildEventReporter (ctx context.Context , besBackend string , apiKey string , forcedInvocationID string , isWorkflow bool ) (* buildEventReporter , error ) {
316+ func newBuildEventReporter (ctx context.Context , besBackend string , apiKey string , forcedInvocationID string , isWorkflow bool , redactionValues [] string ) (* buildEventReporter , error ) {
317317 iid := forcedInvocationID
318318 if iid == "" {
319319 var err error
@@ -338,7 +338,7 @@ func newBuildEventReporter(ctx context.Context, besBackend string, apiKey string
338338 uploader = ul
339339 }
340340
341- return & buildEventReporter {apiKey : apiKey , bep : bep , uploader : uploader , log : newInvocationLog (), invocationID : iid , isWorkflow : isWorkflow , childInvocations : []string {}}, nil
341+ return & buildEventReporter {apiKey : apiKey , bep : bep , uploader : uploader , log : newInvocationLog (redactionValues ), invocationID : iid , isWorkflow : isWorkflow , childInvocations : []string {}}, nil
342342}
343343
344344func (r * buildEventReporter ) InvocationID () string {
@@ -664,7 +664,7 @@ func run() error {
664664
665665 ws := & workspace {
666666 startTime : time .Now (),
667- buildbuddyAPIKey : os .Getenv (buildbuddyAPIKeyEnvVarName ),
667+ buildbuddyAPIKey : os .Getenv (ci_runner_env . BuildBuddyAPIKeyEnvVarName ),
668668 forcedInvocationID : * invocationID ,
669669 runID : runID ,
670670 }
@@ -685,7 +685,8 @@ func run() error {
685685
686686 // Use a context without a timeout for the build event reporter, so that even
687687 // if the `timeout` is reached, any events will finish getting published
688- buildEventReporter , err := newBuildEventReporter (contextWithoutTimeout , * besBackend , ws .buildbuddyAPIKey , * invocationID , * workflowID != "" /*=isWorkflow*/ )
688+ redactionValues := parseSecretRedactionValues (os .Getenv (ci_runner_env .BuildBuddySecretEnvVarNamesForRedaction ))
689+ buildEventReporter , err := newBuildEventReporter (contextWithoutTimeout , * besBackend , ws .buildbuddyAPIKey , * invocationID , * workflowID != "" /*=isWorkflow*/ , redactionValues )
689690 if err != nil {
690691 return err
691692 }
@@ -950,20 +951,24 @@ func (r *buildEventReporter) Printf(format string, vals ...interface{}) {
950951
951952type invocationLog struct {
952953 lockingbuffer.LockingBuffer
953- writer io.Writer
954- writeListener func (s string )
954+ writer io.Writer
955+ writeListener func (s string )
956+ redactionValues []string
955957}
956958
957- func newInvocationLog () * invocationLog {
958- invLog := & invocationLog {writeListener : func (s string ) {}}
959+ func newInvocationLog (redactionValues [] string ) * invocationLog {
960+ invLog := & invocationLog {writeListener : func (s string ) {}, redactionValues : redactionValues }
959961 invLog .writer = io .MultiWriter (& invLog .LockingBuffer , os .Stderr )
960962 return invLog
961963}
962964
963965func (invLog * invocationLog ) Write (b []byte ) (int , error ) {
964966 output := string (b )
965967
966- redacted := redact .RedactText (output )
968+ // Use value-aware redaction so user-defined secret values injected into the
969+ // runner environment are masked in invocation logs (including overlapping
970+ // values handled safely by longest-first replacement in redact package).
971+ redacted := redact .RedactTextWithValues (output , invLog .redactionValues )
967972
968973 invLog .writeListener (redacted )
969974 _ , err := invLog .writer .Write ([]byte (redacted ))
@@ -2285,11 +2290,11 @@ func writeBazelrc(path, invocationID, runID, rootDir string) error {
22852290 if isPushedRefInFork () {
22862291 lines = append (lines , "common --build_metadata=FORK_REPO_URL=" + * pushedRepoURL )
22872292 }
2288- if apiKey := os .Getenv (buildbuddyAPIKeyEnvVarName ); apiKey != "" {
2293+ if apiKey := os .Getenv (ci_runner_env . BuildBuddyAPIKeyEnvVarName ); apiKey != "" {
22892294 lines = append (lines , "common --remote_header=x-buildbuddy-api-key=" + apiKey )
22902295 lines = append (lines , "build:buildbuddy_api_key --remote_header=x-buildbuddy-api-key=" + apiKey )
22912296 }
2292- if origin := os .Getenv ("BB_GRPC_CLIENT_ORIGIN" ); origin != "" {
2297+ if origin := os .Getenv (ci_runner_env . BBGrpcClientOriginEnvVarName ); origin != "" {
22932298 lines = append (lines , fmt .Sprintf ("common --remote_header=%s=%s" , usageutil .OriginHeaderName , origin ))
22942299 lines = append (lines , fmt .Sprintf ("common --bes_header=%s=%s" , usageutil .OriginHeaderName , origin ))
22952300 }
@@ -2782,3 +2787,24 @@ func diskUsage() (*diskUsageStats, error) {
27822787 usedBytes : int64 (usedBytes ),
27832788 }, nil
27842789}
2790+
2791+ func parseSecretRedactionValues (serializedSecretNames string ) []string {
2792+ if serializedSecretNames == "" {
2793+ return nil
2794+ }
2795+ var names []string
2796+ if err := json .Unmarshal ([]byte (serializedSecretNames ), & names ); err != nil {
2797+ backendLog .Warningf ("Failed to parse %s env var for secret redaction: %s" , ci_runner_env .BuildBuddySecretEnvVarNamesForRedaction , err )
2798+ return nil
2799+ }
2800+ values := make ([]string , 0 , len (names ))
2801+ for _ , name := range names {
2802+ if name == "" {
2803+ continue
2804+ }
2805+ if val , ok := os .LookupEnv (name ); ok && val != "" {
2806+ values = append (values , val )
2807+ }
2808+ }
2809+ return values
2810+ }
0 commit comments