chore: update docker-credential-gcr to use v2 (#3026)

This commit is contained in:
Aaron Prindle
2024-02-26 10:54:32 -08:00
committed by GitHub
parent 6c3f0ec32a
commit 65f8ec4caf
35 changed files with 92 additions and 45 deletions
@@ -19,7 +19,7 @@ gazelle(
go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2",
visibility = ["//visibility:private"],
deps = [
"//cli:go_default_library",
@@ -57,7 +57,7 @@ The program in this repository is written with the Go programming language and c
You can download the source code, compile the binary, and put it in your `$GOPATH` with `go get`.
```shell
go get -u github.com/GoogleCloudPlatform/docker-credential-gcr
go get -u github.com/GoogleCloudPlatform/docker-credential-gcr/v2
```
If `$GOPATH/bin` is in your system `$PATH`, this will also automatically install the compiled binary. You can confirm using `which docker-credential-gcr` and continue to the [section on Configuration and Usage](#configuration-and-usage).
@@ -3,7 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["login.go"],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/auth",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/auth",
visibility = ["//visibility:public"],
deps = [
"//config:go_default_library",
@@ -31,7 +31,7 @@ import (
"os"
"strings"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
"github.com/toqueteos/webbrowser"
"golang.org/x/oauth2"
)
@@ -12,7 +12,7 @@ go_library(
"gcr-logout.go",
"version.go",
],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/cli",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/cli",
visibility = ["//visibility:public"],
deps = [
"//auth:go_default_library",
@@ -20,7 +20,7 @@ import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/store"
"github.com/google/subcommands"
)
@@ -22,7 +22,7 @@ import (
"os"
"strings"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
"github.com/google/subcommands"
)
@@ -24,7 +24,7 @@ import (
"path/filepath"
"strings"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
"github.com/google/subcommands"
@@ -20,9 +20,9 @@ import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/credhelper"
"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/credhelper"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/store"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/google/subcommands"
)
@@ -20,8 +20,8 @@ import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/docker-credential-gcr/auth"
"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/auth"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/store"
"github.com/google/subcommands"
)
@@ -20,7 +20,7 @@ import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/store"
"github.com/google/subcommands"
)
@@ -20,7 +20,7 @@ import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
"github.com/google/subcommands"
)
@@ -6,7 +6,7 @@ go_library(
"const.go",
"file.go",
],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/config",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config",
visibility = ["//visibility:public"],
deps = [
"//util:go_default_library",
@@ -18,6 +18,7 @@ package config
import (
"context"
"fmt"
"regexp"
"runtime/debug"
"strings"
@@ -37,16 +38,21 @@ const (
)
// Version can be set via:
// -ldflags="-X 'github.com/GoogleCloudPlatform/docker-credential-gcr/config.Version=$TAG'"
// -ldflags="-X 'github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config.Version=$TAG'"
var Version string
func init() {
if Version == "" {
i, ok := debug.ReadBuildInfo()
if !ok {
return
if i, ok := debug.ReadBuildInfo(); ok {
Version = i.Main.Version
}
Version = i.Main.Version
}
Version = strings.TrimPrefix(Version, "v")
re := regexp.MustCompile(`^[0-9]+(?:[\._][0-9]+)*$`)
if re.MatchString(Version) {
GcrOAuth2Username = fmt.Sprintf("_dcgcr_%s_token", strings.ReplaceAll(Version, ".", "_"))
} else {
GcrOAuth2Username = "_dcgcr_0_0_0_token"
}
}
@@ -63,6 +69,7 @@ var DefaultGCRRegistries = [...]string{
// Registry. If the --include-artifact-registry flag is supplied then these
// are added in addition to the GCR Registries.
var DefaultARRegistries = [...]string{
"africa-south1-docker.pkg.dev",
"asia-docker.pkg.dev",
"asia-east1-docker.pkg.dev",
"asia-east2-docker.pkg.dev",
@@ -86,8 +93,11 @@ var DefaultARRegistries = [...]string{
"europe-west6-docker.pkg.dev",
"europe-west8-docker.pkg.dev",
"europe-west9-docker.pkg.dev",
"europe-west10-docker.pkg.dev",
"europe-west12-docker.pkg.dev",
"me-central1-docker.pkg.dev",
"me-central2-docker.pkg.dev",
"docker.me-central2.rep.pkg.dev",
"me-west1-docker.pkg.dev",
"northamerica-northeast1-docker.pkg.dev",
"northamerica-northeast2-docker.pkg.dev",
@@ -103,6 +113,7 @@ var DefaultARRegistries = [...]string{
"us-west2-docker.pkg.dev",
"us-west3-docker.pkg.dev",
"us-west4-docker.pkg.dev",
"us-west8-docker.pkg.dev",
}
// SupportedGCRTokenSources maps config keys to plain english explanations for
@@ -124,4 +135,4 @@ var GCRScopes = []string{"https://www.googleapis.com/auth/devstorage.read_write"
var OAuthHTTPContext = context.Background()
// GcrOAuth2Username is the Basic auth username accompanying Docker requests to GCR.
var GcrOAuth2Username = fmt.Sprintf("_dcgcr_%s_token", strings.ReplaceAll(Version, ".", "_"))
var GcrOAuth2Username string
@@ -21,7 +21,7 @@ import (
"path/filepath"
"strings"
"github.com/GoogleCloudPlatform/docker-credential-gcr/util"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/util"
)
const (
@@ -3,7 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["helper.go"],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/credhelper",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/credhelper",
visibility = ["//visibility:public"],
deps = [
"//config:go_default_library",
@@ -19,15 +19,19 @@ for GCR authentication.
package credhelper
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
"github.com/GoogleCloudPlatform/docker-credential-gcr/util/cmd"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/auth"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/store"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/util/cmd"
"github.com/docker/docker-credential-helpers/credentials"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
@@ -82,7 +86,34 @@ func (ch *gcrCredHelper) Get(serverURL string) (string, string, error) {
func (ch *gcrCredHelper) gcrCreds() (string, string, error) {
accessToken, err := ch.getGCRAccessToken()
if err != nil {
return "", "", helperErr("could not retrieve GCR's access token", err)
if rerr, ok := err.(*oauth2.RetrieveError); ok {
var resp struct {
Error string `json:"error"`
ErrorSubtype string `json:"error_subtype"`
}
if err := json.Unmarshal(rerr.Body, &resp); err == nil &&
resp.Error == "invalid_grant" &&
resp.ErrorSubtype == "invalid_rapt" {
fmt.Fprintln(os.Stderr, "Reauth required; opening a browser to proceed...")
tok, err := (&auth.GCRLoginAgent{}).PerformLogin()
if err != nil {
return "", "", fmt.Errorf("unable to authenticate user: %v", err)
}
if err = ch.store.SetGCRAuth(tok); err != nil {
return "", "", fmt.Errorf("unable to persist access token: %v", err)
}
fmt.Fprintln(os.Stderr, "Reauth successful!")
// Attempt the refresh dance again, using the new token.
if accessToken, err := ch.getGCRAccessToken(); err != nil {
return "", "", err
} else {
return config.GcrOAuth2Username, accessToken, nil
}
}
}
if err != nil {
return "", "", helperErr("could not retrieve GCR's access token", err)
}
}
return config.GcrOAuth2Username, accessToken, nil
}
@@ -25,7 +25,7 @@ import (
"flag"
"os"
"github.com/GoogleCloudPlatform/docker-credential-gcr/cli"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/cli"
"github.com/google/subcommands"
)
@@ -3,7 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["store.go"],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/store",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/store",
visibility = ["//visibility:public"],
deps = [
"//config:go_default_library",
@@ -28,8 +28,8 @@ import (
"strings"
"time"
"github.com/GoogleCloudPlatform/docker-credential-gcr/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/util"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/config"
"github.com/GoogleCloudPlatform/docker-credential-gcr/v2/util"
"github.com/docker/docker-credential-helpers/credentials"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
@@ -3,6 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["util.go"],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/util",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/util",
visibility = ["//visibility:public"],
)
@@ -3,6 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["exec.go"],
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/util/cmd",
importpath = "github.com/GoogleCloudPlatform/docker-credential-gcr/v2/util/cmd",
visibility = ["//visibility:public"],
)