Skip to content

Commit

Permalink
Change credential helper argument to string and fetch credentials out…
Browse files Browse the repository at this point in the history
…side of cache module (#162)
  • Loading branch information
mortenmj authored Aug 15, 2023
1 parent b98bfde commit b02ab2c
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion snapshots/go/cmd/snapshots/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (cc *collectCmd) runCollect(cmd *cobra.Command, args []string) error {
BazelWorkspacePath: cc.workspacePath,
BazelWriteStderr: cc.bazelStderr,
BazelBuildEventsPath: cc.buildEventsPath,
CredentialHelper: exec.Cmd{Path: cc.credentialHelper, Dir: cc.workspacePath},
CredentialHelper: cc.credentialHelper,
OutPath: cc.outPath,
NoPrint: cc.noPrint,
}
Expand Down
2 changes: 1 addition & 1 deletion snapshots/go/cmd/snapshots/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (dc *diffCmd) runDiff(cmd *cobra.Command, args []string) error {
BazelWorkspacePath: dc.workspacePath,
BazelWriteStderr: dc.bazelStderr,
BuildEventsPath: dc.buildEventsPath,
CredentialHelper: exec.Cmd{Path: dc.credentialHelper, Dir: dc.workspacePath},
CredentialHelper: dc.credentialHelper,
OutPath: dc.outPath,
NoPrint: dc.noPrint,
FromSnapshot: dc.fromSnapshot,
Expand Down
1 change: 0 additions & 1 deletion snapshots/go/pkg/cache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go_library(
name = "cache",
srcs = [
"cache.go",
"credential_helper.go",
"grpc_client.go",
],
importpath = "github.com/cognitedata/bazel-snapshots/snapshots/go/pkg/cache",
Expand Down
17 changes: 8 additions & 9 deletions snapshots/go/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io/ioutil"
"net/url"
"os"
"os/exec"
"strings"

"google.golang.org/genproto/googleapis/bytestream"
Expand All @@ -36,14 +35,14 @@ type DelegatingBazelCache struct {
caches map[string]BazelCache
}

func NewDefaultDelegatingCache(credentialHelper *exec.Cmd, dialOptions ...grpc.DialOption) BazelCache {
func NewDefaultDelegatingCache(credentials string, dialOptions ...grpc.DialOption) BazelCache {
return &DelegatingBazelCache{
caches: map[string]BazelCache{
"file": &FileBazelCache{},
"bytestream": &RemoteBazelCache{
clients: make(map[string]bytestream.ByteStreamClient),
credentialHelper: NewCredentialHelper(credentialHelper),
DialOptions: dialOptions,
clients: make(map[string]bytestream.ByteStreamClient),
credentials: credentials,
DialOptions: dialOptions,
},
},
}
Expand Down Expand Up @@ -88,9 +87,9 @@ func (c *FileBazelCache) Read(ctx context.Context, secure bool, uri string) ([]b

// RemoteBazelCache provides access to cached items with 'bytestream://' uris.
type RemoteBazelCache struct {
clients map[string]bytestream.ByteStreamClient
credentialHelper *CredentialHelper
DialOptions []grpc.DialOption
clients map[string]bytestream.ByteStreamClient
credentials string
DialOptions []grpc.DialOption
}

func (c *RemoteBazelCache) Read(ctx context.Context, secure bool, uri string) ([]byte, error) {
Expand All @@ -102,7 +101,7 @@ func (c *RemoteBazelCache) Read(ctx context.Context, secure bool, uri string) ([
// obtain a client
client, ok := c.clients[u.Host]
if !ok {
conn, err := DialTargetWithOptions(uri, secure, c.credentialHelper, c.DialOptions...)
conn, err := DialTargetWithOptions(uri, secure, c.credentials, c.DialOptions...)
if err != nil {
return nil, fmt.Errorf("failed to dial host %s: %w", u.Host, err)
}
Expand Down
15 changes: 5 additions & 10 deletions snapshots/go/pkg/cache/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"google.golang.org/grpc/keepalive"
)

func DialTarget(target string, credentialHelper *CredentialHelper) (*grpc.ClientConn, error) {
return DialTargetWithOptions(target, true, credentialHelper)
func DialTarget(target string, credentials string) (*grpc.ClientConn, error) {
return DialTargetWithOptions(target, true, credentials)
}

func DialTargetWithOptions(target string, grpcsBytestream bool, credentialHelper *CredentialHelper, extraOptions ...grpc.DialOption) (*grpc.ClientConn, error) {
func DialTargetWithOptions(target string, grpcsBytestream bool, credentials string, extraOptions ...grpc.DialOption) (*grpc.ClientConn, error) {
dialOptions := CommonGRPCClientOptions()
dialOptions = append(dialOptions, extraOptions...)

Expand All @@ -30,13 +30,8 @@ func DialTargetWithOptions(target string, grpcsBytestream bool, credentialHelper
}

// Only get user credentials if no credential helper is provided
if credentialHelper != nil {
credentials, err := credentialHelper.GetAuthorization()
if err != nil {
return nil, err
}

dialOptions = append(dialOptions, grpc.WithPerRPCCredentials(newRPCCredentials(credentials[0])))
if len(credentials) != 0 {
dialOptions = append(dialOptions, grpc.WithPerRPCCredentials(newRPCCredentials(credentials)))
} else if u.User != nil {
dialOptions = append(dialOptions, grpc.WithPerRPCCredentials(newRPCCredentials(u.User.String())))
}
Expand Down
4 changes: 2 additions & 2 deletions snapshots/go/pkg/cache/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestDialTargetWithOptions(t *testing.T) {
var err error

// illegal scheme
conn, err = DialTargetWithOptions("wrongscheme://some-uri", false, nil)
conn, err = DialTargetWithOptions("wrongscheme://some-uri", false, "")
require.ErrorIs(t, err, ErrScheme)
require.Nil(t, conn)

// legal scheme
conn, err = DialTargetWithOptions("bytestream://some-uri", false, nil)
conn, err = DialTargetWithOptions("bytestream://some-uri", false, "")
require.Nil(t, err)
require.NotNil(t, conn)
}
5 changes: 4 additions & 1 deletion snapshots/go/pkg/collecter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "collecter",
srcs = ["collecter.go"],
srcs = [
"collecter.go",
"credential_helper.go",
],
importpath = "github.com/cognitedata/bazel-snapshots/snapshots/go/pkg/collecter",
visibility = ["//visibility:public"],
deps = [
Expand Down
15 changes: 12 additions & 3 deletions snapshots/go/pkg/collecter/collecter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"log"
"os"
"os/exec"
"strings"

"google.golang.org/grpc/metadata"
Expand All @@ -34,7 +33,7 @@ type CollectArgs struct {
BazelWorkspacePath string
BazelWriteStderr bool
BazelBuildEventsPath string
CredentialHelper exec.Cmd
CredentialHelper string
OutPath string
NoPrint bool
}
Expand All @@ -52,7 +51,17 @@ func (c *collecter) Collect(args *CollectArgs) (*models.Snapshot, error) {

var buildEvents []bazel.BuildEventOutput
ctx := context.Background()
bcache := cache.NewDefaultDelegatingCache(&args.CredentialHelper)

var credential string
if args.CredentialHelper != "" {
creds, err := getAuthorization(args.CredentialHelper, args.BazelWorkspacePath)
if err != nil {
return nil, fmt.Errorf("error getting credentials: %w", err)
}
credential = creds[0]
}

bcache := cache.NewDefaultDelegatingCache(credential)

// build digests, get the build events
log.Printf("collecting digests from %s", args.BazelExpression)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
/* Copyright 2022 Cognite AS */

package cache
package collecter

import (
"encoding/json"
"fmt"
"os/exec"
)

type CredentialHelper struct {
Cmd *exec.Cmd
}

type Credentials struct {
Headers struct {
Authorization []string `json:"Authorization"`
} `json:"headers"`
}

func NewCredentialHelper(cmd *exec.Cmd) *CredentialHelper {
return &CredentialHelper{Cmd: cmd}
}

func (ch *CredentialHelper) GetAuthorization() ([]string, error) {
// Having no credential helper isn't an error
if ch.Cmd == nil {
func getAuthorization(credentialHelper, workspacePath string) ([]string, error) {
if credentialHelper == "" {
return nil, nil
}

headers, err := ch.Cmd.Output()
cmd := exec.Cmd{Path: credentialHelper, Dir: workspacePath}

headers, err := cmd.Output()
if err != nil {
return nil, err
}
Expand All @@ -38,5 +31,9 @@ func (ch *CredentialHelper) GetAuthorization() ([]string, error) {
return nil, fmt.Errorf("invalid headers %s: %w", headers, err)
}

if len(credentials.Headers.Authorization) == 0 {
return nil, fmt.Errorf("empty authorization header")
}

return credentials.Headers.Authorization, nil
}
3 changes: 1 addition & 2 deletions snapshots/go/pkg/differ/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"os/exec"
"sort"
"strings"

Expand All @@ -30,7 +29,7 @@ type DiffArgs struct {
BazelWorkspacePath string
BazelWriteStderr bool
BuildEventsPath string
CredentialHelper exec.Cmd
CredentialHelper string
OutPath string
NoPrint bool
FromSnapshot *models.Snapshot
Expand Down

0 comments on commit b02ab2c

Please sign in to comment.