Skip to content

Commit

Permalink
chore: Address PR comemnts
Browse files Browse the repository at this point in the history
  • Loading branch information
bakjos committed Jul 4, 2022
1 parent e876028 commit 4e1cc1e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,19 @@ http_address: 0.0.0.0:8080
# tenant_id: TENANT_ID
# storage_account: STORAGE_ACCOUNT
# container_name: CONTAINER_NAME

# client secret credentials
# auth_method: client_secret
# client_id: APP_ID
# client_secret: APP_SECRET


# client certificate credentials
# auth_method: client_certificate
# cert_path: PATH_TO_CERTS FILE

# default and environment methods don't have any additional parameters
# auth_method: default/environment_credential

# If set to a valid port number, then serve /debug/pprof/* URLs here:
#profile_port: 7070
Expand Down
2 changes: 0 additions & 2 deletions cache/azblobproxy/auth_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const (
AuthMethodClientCertificate = "client_certificate"
AuthMethodClientSecret = "client_secret"
AuthMethodEnvironmentCredential = "environment_credential"
AuthMethodDeviceCode = "device_code"
AuthMethodDefault = "default"
)

Expand All @@ -13,7 +12,6 @@ func GetAuthMethods() []string {
AuthMethodClientCertificate,
AuthMethodClientSecret,
AuthMethodEnvironmentCredential,
AuthMethodDeviceCode,
AuthMethodDefault,
}
}
Expand Down
17 changes: 6 additions & 11 deletions cache/azblobproxy/azblobproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"errors"
"fmt"
"io"
"log"
"path"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/buchgr/bazel-remote/cache"
"github.com/buchgr/bazel-remote/cache/disk/casblob"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"io"
"log"
"path"
)

var (
Expand All @@ -31,7 +32,6 @@ type uploadReq struct {
size int64
kind cache.EntryKind
rc io.ReadCloser
cxt context.Context
}

type azBlobCache struct {
Expand All @@ -58,7 +58,6 @@ func (c *azBlobCache) Put(ctx context.Context, kind cache.EntryKind, hash string
size: size,
kind: kind,
rc: rc,
cxt: context.Background(),
}:
default:
c.errorLogger.Printf("too many uploads queued\n")
Expand All @@ -82,7 +81,6 @@ func (c *azBlobCache) Get(ctx context.Context, kind cache.EntryKind, hash string
resp, err := client.Download(context.Background(), nil)

if err != nil {

cacheMisses.Inc()
logResponse(c.accessLogger, "DOWNLOAD", c.storageAccount, c.container, key, err)
return nil, -1, err
Expand Down Expand Up @@ -131,7 +129,6 @@ func (c *azBlobCache) Contains(ctx context.Context, kind cache.EntryKind, hash s
logResponse(c.accessLogger, "CONTAINS", c.storageAccount, c.container, key, err)

return exists, size

}

func New(
Expand All @@ -142,7 +139,6 @@ func New(
storageMode string, accessLogger cache.Logger,
errorLogger cache.Logger, numUploaders, maxQueuedUploads int,
) cache.Proxy {

url := fmt.Sprintf("https://%s.blob.core.windows.net/", storageAccount)
serviceClient, err := azblob.NewServiceClient(url, creds, nil)
if err != nil {
Expand Down Expand Up @@ -196,6 +192,7 @@ func New(
}

func (c *azBlobCache) uploadFile(item uploadReq) {
defer item.rc.Close()
key := c.objectKey(item.hash, item.kind)
if c.prefix != "" {
key = c.prefix + "/" + key
Expand All @@ -207,11 +204,9 @@ func (c *azBlobCache) uploadFile(item uploadReq) {
return
}

_, err = client.Upload(item.cxt, item.rc.(io.ReadSeekCloser), nil)
_, err = client.Upload(context.Background(), item.rc.(io.ReadSeekCloser), nil)

logResponse(c.accessLogger, "UPLOAD", c.storageAccount, c.container, key, err)

item.rc.Close()
}

func objectKeyV2(prefix string, hash string, kind cache.EntryKind) string {
Expand Down
19 changes: 12 additions & 7 deletions config/azblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package config

import (
"fmt"
"log"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/buchgr/bazel-remote/cache/azblobproxy"
"log"
"os"
)

type AzBlobStorageConfig struct {
Expand All @@ -24,7 +25,9 @@ func (azblobc AzBlobStorageConfig) GetCredentials() (azcore.TokenCredential, err
if azblobc.AuthMethod == azblobproxy.AuthMethodDefault {
log.Println("AzBlob Credentials: using Default Credentials")
return azidentity.NewDefaultAzureCredential(nil)
} else if azblobc.AuthMethod == azblobproxy.AuthMethodClientCertificate {
}

if azblobc.AuthMethod == azblobproxy.AuthMethodClientCertificate {
log.Println("AzBlob Credentials: using client certificate credentials")

certData, err := os.ReadFile(azblobc.CertPath)
Expand All @@ -35,16 +38,18 @@ func (azblobc AzBlobStorageConfig) GetCredentials() (azcore.TokenCredential, err
if err != nil {
return nil, fmt.Errorf(`failed to load certificate from "%s": %v`, azblobc.CertPath, err)
}

return azidentity.NewClientCertificateCredential(azblobc.TenantID, azblobc.ClientID, certs, key, nil)
} else if azblobc.AuthMethod == azblobproxy.AuthMethodClientSecret {
}

if azblobc.AuthMethod == azblobproxy.AuthMethodClientSecret {
log.Println("AzBlob Credentials: using client secret credentials")
return azidentity.NewClientSecretCredential(azblobc.TenantID, azblobc.ClientID, azblobc.ClientSecret, nil)
} else if azblobc.AuthMethod == azblobproxy.AuthMethodEnvironmentCredential {
}

if azblobc.AuthMethod == azblobproxy.AuthMethodEnvironmentCredential {
log.Println("AzBlob Credentials: using client secret credentials")
return azidentity.NewEnvironmentCredential(nil)
}

return nil, fmt.Errorf("invalid azblob.auth_method: %s", azblobc.AuthMethod)

}
19 changes: 10 additions & 9 deletions utils/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package flags

import (
"fmt"
"github.com/buchgr/bazel-remote/cache/azblobproxy"

"math"
"strconv"
"strings"

"github.com/buchgr/bazel-remote/cache/azblobproxy"
"github.com/buchgr/bazel-remote/cache/s3proxy"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -283,52 +284,52 @@ func GetCliFlags() []cli.Flag {
&cli.StringFlag{
Name: "azblob.tenant_id",
Value: "",
Usage: "The AzBlob tenant id to use when using azblob proxy backend.",
Usage: "The Azure blob storage tenant id to use when using azblob proxy backend.",
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_TENANT_ID", "AZURE_TENANT_ID"},
},

&cli.StringFlag{
Name: "azblob.storage_account",
Value: "",
Usage: "The AzBlob storage account to use when using azblob proxy backend.",
Usage: "The Azure blob storage storage account to use when using azblob proxy backend.",
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_STORAGE_ACCOUNT"},
},

&cli.StringFlag{
Name: "azblob.container_name",
Value: "",
Usage: "The AzBlob container name to use when using azblob proxy backend.",
Usage: "The Azure blob storage container name to use when using azblob proxy backend.",
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_CONTAINER_NAME"},
},

&cli.StringFlag{
Name: "azblob.prefix",
Value: "",
Usage: "The AZBLOB object prefix to use when using az blob proxy backend.",
Usage: "The Azure blob storage object prefix to use when using azblob proxy backend.",
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_PREFIX"},
},
&cli.StringFlag{
Name: "azblob.auth_method",
Value: "",
Usage: fmt.Sprintf("The AzBlob authentication method. This argument is required when an AzBlob proxy backend is used. Allowed values: %s.", strings.Join(azblobproxy.GetAuthMethods(), ", ")),
Usage: fmt.Sprintf("The Azure blob storage authentication method. This argument is required when an azblob proxy backend is used. Allowed values: %s.", strings.Join(azblobproxy.GetAuthMethods(), ", ")),
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_AUTH_METHOD"},
},
&cli.StringFlag{
Name: "azblob.client_id",
Value: "",
Usage: "The AzBlob client id to use when using AzBlob proxy backend. " + azBlobAuthMsg(azblobproxy.AuthMethodClientSecret),
Usage: "The Azure blob storage client id to use when using azblob proxy backend. " + azBlobAuthMsg(azblobproxy.AuthMethodClientSecret),
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_CLIENT_ID", "AZURE_CLIENT_ID"},
},
&cli.StringFlag{
Name: "azblob.client_secret",
Value: "",
Usage: "The AzBlob cliensecret key to use when using S3 proxy backend. " + azBlobAuthMsg(azblobproxy.AuthMethodClientSecret),
Usage: "The Azure blob storage client secret key to use when using azblob proxy backend. " + azBlobAuthMsg(azblobproxy.AuthMethodClientSecret),
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_SECRET_CLIENT_SECRET", "AZURE_CLIENT_SECRET"},
},
&cli.StringFlag{
Name: "azblob.cert_path",
Value: "",
Usage: "Path to the Certificates filefile. " + azBlobAuthMsg(azblobproxy.AuthMethodClientCertificate),
Usage: "Path to the certificates file. " + azBlobAuthMsg(azblobproxy.AuthMethodClientCertificate),
EnvVars: []string{"BAZEL_REMOTE_AZBLOB_CERT_PATH", "AZURE_CLIENT_CERTIFICATE_PATH"},
},

Expand Down

0 comments on commit 4e1cc1e

Please sign in to comment.