Skip to content

Commit 9ad679a

Browse files
committed
[RB] Support passing a different API key for uploading build events
1 parent 3197186 commit 9ad679a

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

app/invocation/invocation_action_card.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,8 @@ export default class InvocationActionCardComponent extends React.Component<Props
657657
) {
658658
const snapshotKey = this.getSnapshotKeyForSnapshotID(vmMetadata);
659659
const snapshotKeyJSON = JSON.stringify(snapshotKey);
660-
const cmd = `bb remote --run_from_snapshot='${snapshotKeyJSON}' --runner_exec_properties=debug-executor-id=${executionMetadata.executorId} --script='echo "My custom bash command!"'`;
660+
const impersonationAPIKey = this.state.user?.isImpersonating ? `--remote_run_header=x-buildbuddy-platform.env-overrides=BUILDBUDDY_BES_API_KEY=[SET BB ORG API KEY]>` : "";
661+
const cmd = `bb remote ${impersonationAPIKey} --run_from_snapshot='${snapshotKeyJSON}' --runner_exec_properties=debug-executor-id=${executionMetadata.executorId} --script='echo "My custom bash command!"'`;
661662
copyToClipboard(cmd);
662663
alert_service.success("Command copied to clipboard");
663664
this.setState({ showSnapshotMenu: false });

enterprise/server/cmd/ci_runner/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,13 @@ func run() error {
680680
defer cancel()
681681
}
682682

683+
besAPIKey := os.Getenv("BUILDBUDDY_BES_API_KEY")
684+
if besAPIKey == "" {
685+
besAPIKey = ws.buildbuddyAPIKey
686+
}
683687
// Use a context without a timeout for the build event reporter, so that even
684688
// if the `timeout` is reached, any events will finish getting published
685-
buildEventReporter, err := newBuildEventReporter(contextWithoutTimeout, *besBackend, ws.buildbuddyAPIKey, *invocationID, *workflowID != "" /*=isWorkflow*/)
689+
buildEventReporter, err := newBuildEventReporter(contextWithoutTimeout, *besBackend, besAPIKey, *invocationID, *workflowID != "" /*=isWorkflow*/)
686690
if err != nil {
687691
return err
688692
}

enterprise/server/hostedrunner/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ go_library(
1111
"//enterprise/server/remote_execution/platform",
1212
"//enterprise/server/util/ci_runner_util",
1313
"//enterprise/server/workflow/config",
14+
"//proto:auditlog_go_proto",
15+
"//proto:firecracker_go_proto",
1416
"//proto:remote_execution_go_proto",
1517
"//proto:runner_go_proto",
1618
"//server/endpoint_urls/build_buddy_url",
@@ -33,6 +35,7 @@ go_library(
3335
"@org_golang_google_genproto//googleapis/longrunning",
3436
"@org_golang_google_grpc//metadata",
3537
"@org_golang_google_grpc//status",
38+
"@org_golang_google_protobuf//encoding/protojson",
3639
"@org_golang_google_protobuf//types/known/durationpb",
3740
],
3841
)

enterprise/server/hostedrunner/hostedrunner.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/buildbuddy-io/buildbuddy/enterprise/server/remote_execution/platform"
1313
"github.com/buildbuddy-io/buildbuddy/enterprise/server/util/ci_runner_util"
1414
"github.com/buildbuddy-io/buildbuddy/enterprise/server/workflow/config"
15+
"github.com/buildbuddy-io/buildbuddy/proto/auditlog"
1516
"github.com/buildbuddy-io/buildbuddy/server/endpoint_urls/build_buddy_url"
1617
"github.com/buildbuddy-io/buildbuddy/server/endpoint_urls/cache_api_url"
1718
"github.com/buildbuddy-io/buildbuddy/server/endpoint_urls/events_api_url"
@@ -30,9 +31,11 @@ import (
3031
"github.com/google/uuid"
3132
"google.golang.org/genproto/googleapis/longrunning"
3233
"google.golang.org/grpc/metadata"
34+
"google.golang.org/protobuf/encoding/protojson"
3335
"google.golang.org/protobuf/types/known/durationpb"
3436
"gopkg.in/yaml.v2"
3537

38+
fcpb "github.com/buildbuddy-io/buildbuddy/proto/firecracker"
3639
repb "github.com/buildbuddy-io/buildbuddy/proto/remote_execution"
3740
rnpb "github.com/buildbuddy-io/buildbuddy/proto/runner"
3841
gstatus "google.golang.org/grpc/status"
@@ -420,6 +423,7 @@ func (r *runnerService) Run(ctx context.Context, req *rnpb.RunRequest) (*rnpb.Ru
420423
return nil, status.WrapError(err, "get credentials")
421424
}
422425

426+
var hasBESOverride bool
423427
for _, h := range req.GetRemoteHeaders() {
424428
parts := strings.SplitN(h, "=", 2)
425429
if len(parts) != 2 {
@@ -432,6 +436,10 @@ func (r *runnerService) Run(ctx context.Context, req *rnpb.RunRequest) (*rnpb.Ru
432436
// to credential-related env overrides that were set above.
433437
if headerKey == platform.OverrideHeaderPrefix+platform.EnvOverridesPropertyName {
434438
envOverrides = append(envOverrides, headerVal)
439+
440+
if strings.HasPrefix(headerVal, "BUILDBUDDY_BES_API_KEY=") {
441+
hasBESOverride = true
442+
}
435443
continue
436444
}
437445

@@ -441,6 +449,16 @@ func (r *runnerService) Run(ctx context.Context, req *rnpb.RunRequest) (*rnpb.Ru
441449
execCtx = platform.WithRemoteHeaderOverride(
442450
execCtx, platform.EnvOverridesPropertyName, strings.Join(envOverrides, ","))
443451

452+
if hasBESOverride {
453+
snapshotKeyStr := getExecProperty(req.GetExecProperties(), "snapshot-key-override")
454+
snapshotKey := &fcpb.SnapshotKey{}
455+
if err := protojson.Unmarshal([]byte(snapshotKeyStr), snapshotKey); err != nil {
456+
return nil, status.WrapError(err, "unmarshal SnapshotKey")
457+
}
458+
// TODO: Make sure this is logging for the correct group
459+
r.env.GetAuditLogger().LogForGroup(ctx, req.GetRequestContext().GetGroupId(), auditlog.Action_IMPERSONATE_REMOTE_RUN, snapshotKey)
460+
}
461+
444462
executionClient := r.env.GetRemoteExecutionClient()
445463
if executionClient == nil {
446464
return nil, status.UnimplementedError("Missing remote execution client.")

proto/auditlog.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ enum Action {
6161
CREATE_IMPERSONATION_API_KEY = 12;
6262
UPDATE_IP_RULES_CONFIG = 13;
6363
INVALIDATE_VM_SNAPSHOT = 14;
64+
IMPERSONATE_REMOTE_RUN = 15;
6465
}
6566

6667
message ResourceID {

0 commit comments

Comments
 (0)