Skip to content

Commit

Permalink
Add binary to convert bazel style credshelper output to the output sdks
Browse files Browse the repository at this point in the history
accepts
  • Loading branch information
banikharbanda committed Nov 12, 2024
1 parent da9b611 commit 4cf251c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
15 changes: 15 additions & 0 deletions go/cmd/bazelcredswrapper/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "bazelcredswrapper_lib",
srcs = ["main.go"],
importpath = "github.com/bazelbuild/remote-apis-sdks/go/cmd/bazelcredswrapper",
visibility = ["//visibility:private"],
deps = ["@com_github_golang_glog//:glog"],
)

go_binary(
name = "bazelcredswrapper",
embed = [":bazelcredswrapper_lib"],
visibility = ["//visibility:public"],
)
86 changes: 86 additions & 0 deletions go/cmd/bazelcredswrapper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Binary bazelcredswrapper is used to authenticate using bazel style credentials helper with the remote-apis-sdks

package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"

log "github.com/golang/glog"
)

var (
credsPath = flag.String("credentials_helper_path", "", "Path to the user's credentials helper binary.")
uri = flag.String("uri", "", "The URI of the credentials request.")
)

func main() {
defer log.Flush()
flag.Parse()
log.Flush()
var err error
if *credsPath == "" {
log.Errorf("No credentials helper path provided.")
os.Exit(1)
}
uriObj := fmt.Sprintf(`{"uri":"%v"}`, *uri)
cmd := exec.Command(*credsPath, "get")
var stdin, stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
stdin.Write([]byte(uriObj))
cmd.Stdin = &stdin
err = cmd.Run()
out := stdout.String()
if stderr.String() != "" {
log.Error(stderr.String())
}
if err != nil {
log.Fatalf("Failed running the credentials helper: %v, with err: %v", *credsPath, err)
}

headers, expiry := parseCredsOut(out)
// Bazel-style headers are of the form map[string][]string but we need them
// to be of the form map[string]string to match PerRPC credentials
hdrs := map[string]string{}
for k, v := range headers {
hdrs[k] = strings.Join(v, ",")
}
jsonHdrs, err := json.Marshal(hdrs)
if err != nil {
log.Error(err)
os.Exit(1)
}
fmt.Printf(`{"headers":%s, "token":"%s", "expiry":"%s"}`, jsonHdrs,
"", expiry.Format(time.UnixDate))
}

type CredshelperOut struct {
Headers map[string][]string `json:"headers"`
Expires string `json:"expires"`
}

func parseCredsOut(out string) (map[string][]string, time.Time) {
var credsOut CredshelperOut
if err := json.Unmarshal([]byte(out), &credsOut); err != nil {
log.Errorf("Error while decoding credshelper output: %v", err)
os.Exit(1)
}
hdrs := credsOut.Headers
var exp time.Time
if credsOut.Expires != "" {
expiry, err := time.Parse(time.RFC3339, credsOut.Expires)
if err != nil {
log.Errorf("Failed to parse creds expiry: %v", err)
os.Exit(1)
}
exp = expiry
}
return hdrs, exp
}

0 comments on commit 4cf251c

Please sign in to comment.