Skip to content

Commit f77ba3a

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

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

app/invocation/invocation_action_card.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,10 @@ 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
661+
? `--remote_run_header=x-buildbuddy-platform.env-overrides=BUILDBUDDY_BES_API_KEY=[SET BB ORG API KEY]>`
662+
: "";
663+
const cmd = `bb remote ${impersonationAPIKey} --run_from_snapshot='${snapshotKeyJSON}' --runner_exec_properties=debug-executor-id=${executionMetadata.executorId} --script='echo "My custom bash command!"'`;
661664
copyToClipboard(cmd);
662665
alert_service.success("Command copied to clipboard");
663666
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 && r.env.GetAuditLogger() != nil {
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/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ proto_library(
5656
":api_key_proto",
5757
":context_proto",
5858
":encryption_proto",
59+
":firecracker_proto",
5960
":github_proto",
6061
":group_proto",
6162
":invocation_proto",
@@ -926,6 +927,7 @@ go_proto_library(
926927
":api_key_go_proto",
927928
":context_go_proto",
928929
":encryption_go_proto",
930+
":firecracker_go_proto",
929931
":github_go_proto",
930932
":group_go_proto",
931933
":invocation_go_proto",
@@ -2070,6 +2072,7 @@ ts_proto_library(
20702072
":api_key_ts_proto",
20712073
":context_ts_proto",
20722074
":encryption_ts_proto",
2075+
":firecracker_ts_proto",
20732076
":github_ts_proto",
20742077
":group_ts_proto",
20752078
":invocation_ts_proto",

proto/auditlog.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package auditlog;
55
import "proto/api_key.proto";
66
import "proto/context.proto";
77
import "proto/encryption.proto";
8+
import "proto/firecracker.proto";
89
import "proto/github.proto";
910
import "proto/grp.proto";
1011
import "proto/invocation.proto";
@@ -61,6 +62,7 @@ enum Action {
6162
CREATE_IMPERSONATION_API_KEY = 12;
6263
UPDATE_IP_RULES_CONFIG = 13;
6364
INVALIDATE_VM_SNAPSHOT = 14;
65+
IMPERSONATE_REMOTE_RUN = 15;
6466
}
6567

6668
message ResourceID {
@@ -101,6 +103,7 @@ message Entry {
101103
iprules.DeleteRuleRequest delete_ip_rule = 17;
102104
iprules.SetRulesConfigRequest set_rules_config = 18;
103105
workflow.InvalidateSnapshotRequest invalidate_snapshot = 19;
106+
firecracker.SnapshotKey impersonate_remote_run = 20;
104107
}
105108
message Request {
106109
APIRequest api_request = 1;

0 commit comments

Comments
 (0)