mirror of
https://github.com/drone-plugins/drone-docker.git
synced 2026-06-04 18:24:24 +08:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f202de1604 | |||
| 60929c782c | |||
| 90280c9c7b | |||
| 531cc5cb7a | |||
| a5459e5f20 | |||
| 6ae21e3d8c | |||
| bee421255a | |||
| a6b3e2f7b5 | |||
| 6cade1e98b | |||
| 0a2f635d57 | |||
| 74d5558af0 | |||
| 5639b702aa | |||
| 9a4e1ba483 | |||
| ced9875ed0 | |||
| e9b38c94b4 | |||
| 292ebe06de | |||
| 49e9dde7a7 | |||
| e7e8dd882d | |||
| 11015f0ddb | |||
| a946428e81 |
+35
-35
@@ -12,7 +12,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: vet
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- go vet ./...
|
||||
environment:
|
||||
@@ -22,7 +22,7 @@ steps:
|
||||
path: /go
|
||||
|
||||
- name: test
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- go test -cover ./...
|
||||
environment:
|
||||
@@ -55,7 +55,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: go build
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
commands:
|
||||
@@ -162,7 +162,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: go build
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
commands:
|
||||
@@ -172,7 +172,7 @@ steps:
|
||||
- go build -o release/windows/amd64/drone-acr.exe ./cmd/drone-acr
|
||||
- go build -o release/windows/amd64/drone-gar.exe ./cmd/drone-gar
|
||||
- name: build docker plugin
|
||||
image: plugins/docker
|
||||
image: plugins/docker@sha256:f0233d950ae87ee6cb5500b2d5497fe02aa338201c0bdce2619f443fd174cfa4
|
||||
settings:
|
||||
dockerfile: docker/docker/Dockerfile.windows.amd64.ltsc2022
|
||||
repo: plugins/docker
|
||||
@@ -186,7 +186,7 @@ steps:
|
||||
when:
|
||||
event: [push, tag]
|
||||
- name: build ecr plugin
|
||||
image: plugins/docker
|
||||
image: plugins/docker@sha256:f0233d950ae87ee6cb5500b2d5497fe02aa338201c0bdce2619f443fd174cfa4
|
||||
settings:
|
||||
dockerfile: docker/ecr/Dockerfile.windows.amd64.ltsc2022
|
||||
repo: plugins/ecr
|
||||
@@ -200,7 +200,7 @@ steps:
|
||||
when:
|
||||
event: [push, tag]
|
||||
- name: build gcr plugin
|
||||
image: plugins/docker
|
||||
image: plugins/docker@sha256:f0233d950ae87ee6cb5500b2d5497fe02aa338201c0bdce2619f443fd174cfa4
|
||||
settings:
|
||||
dockerfile: docker/gcr/Dockerfile.windows.amd64.ltsc2022
|
||||
repo: plugins/gcr
|
||||
@@ -214,7 +214,7 @@ steps:
|
||||
when:
|
||||
event: [push, tag]
|
||||
- name: build acr plugin
|
||||
image: plugins/docker
|
||||
image: plugins/docker@sha256:f0233d950ae87ee6cb5500b2d5497fe02aa338201c0bdce2619f443fd174cfa4
|
||||
settings:
|
||||
dockerfile: docker/acr/Dockerfile.windows.amd64.ltsc2022
|
||||
repo: plugins/acr
|
||||
@@ -228,7 +228,7 @@ steps:
|
||||
when:
|
||||
event: [push, tag]
|
||||
- name: build gar plugin
|
||||
image: plugins/docker
|
||||
image: plugins/docker@sha256:f0233d950ae87ee6cb5500b2d5497fe02aa338201c0bdce2619f443fd174cfa4
|
||||
settings:
|
||||
dockerfile: docker/gar/Dockerfile.windows.amd64.ltsc2022
|
||||
repo: plugins/gar
|
||||
@@ -264,7 +264,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/amd64/drone-docker ./cmd/drone-docker'
|
||||
environment:
|
||||
@@ -275,7 +275,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/amd64/drone-docker ./cmd/drone-docker'
|
||||
environment:
|
||||
@@ -285,7 +285,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: executable
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- ./release/linux/amd64/drone-docker --help
|
||||
|
||||
@@ -329,7 +329,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/arm64/drone-docker ./cmd/drone-docker'
|
||||
environment:
|
||||
@@ -340,7 +340,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/arm64/drone-docker ./cmd/drone-docker'
|
||||
environment:
|
||||
@@ -350,7 +350,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: executable
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- ./release/linux/arm64/drone-docker --help
|
||||
|
||||
@@ -429,7 +429,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/amd64/drone-gcr ./cmd/drone-gcr'
|
||||
environment:
|
||||
@@ -440,7 +440,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/amd64/drone-gcr ./cmd/drone-gcr'
|
||||
environment:
|
||||
@@ -488,7 +488,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/arm64/drone-gcr ./cmd/drone-gcr'
|
||||
environment:
|
||||
@@ -499,7 +499,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/arm64/drone-gcr ./cmd/drone-gcr'
|
||||
environment:
|
||||
@@ -582,7 +582,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/amd64/drone-gar ./cmd/drone-gar'
|
||||
environment:
|
||||
@@ -593,7 +593,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/amd64/drone-gar ./cmd/drone-gar'
|
||||
environment:
|
||||
@@ -641,7 +641,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/arm64/drone-gar ./cmd/drone-gar'
|
||||
environment:
|
||||
@@ -652,7 +652,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/arm64/drone-gar ./cmd/drone-gar'
|
||||
environment:
|
||||
@@ -734,7 +734,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/amd64/drone-ecr ./cmd/drone-ecr'
|
||||
environment:
|
||||
@@ -744,7 +744,7 @@ steps:
|
||||
exclude:
|
||||
- tag
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/amd64/drone-ecr ./cmd/drone-ecr'
|
||||
environment:
|
||||
@@ -792,7 +792,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/arm64/drone-ecr ./cmd/drone-ecr'
|
||||
environment:
|
||||
@@ -802,7 +802,7 @@ steps:
|
||||
exclude:
|
||||
- tag
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/arm64/drone-ecr ./cmd/drone-ecr'
|
||||
environment:
|
||||
@@ -885,7 +885,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/amd64/drone-heroku ./cmd/drone-heroku'
|
||||
environment:
|
||||
@@ -895,7 +895,7 @@ steps:
|
||||
exclude:
|
||||
- tag
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/amd64/drone-heroku ./cmd/drone-heroku'
|
||||
environment:
|
||||
@@ -944,7 +944,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/linux/arm64/drone-heroku ./cmd/drone-heroku'
|
||||
environment:
|
||||
@@ -954,7 +954,7 @@ steps:
|
||||
exclude:
|
||||
- tag
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/linux/arm64/drone-heroku ./cmd/drone-heroku'
|
||||
environment:
|
||||
@@ -1035,7 +1035,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.build=${DRONE_BUILD_NUMBER}" -a -tags netgo -o release/linux/amd64/drone-acr ./cmd/drone-acr'
|
||||
environment:
|
||||
@@ -1045,7 +1045,7 @@ steps:
|
||||
exclude:
|
||||
- tag
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}" -a -tags netgo -o release/linux/amd64/drone-acr ./cmd/drone-acr'
|
||||
environment:
|
||||
@@ -1093,7 +1093,7 @@ platform:
|
||||
|
||||
steps:
|
||||
- name: build-push
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.build=${DRONE_BUILD_NUMBER}" -a -tags netgo -o release/linux/arm64/drone-acr ./cmd/drone-acr'
|
||||
environment:
|
||||
@@ -1104,7 +1104,7 @@ steps:
|
||||
- tag
|
||||
|
||||
- name: build-tag
|
||||
image: golang:1.22
|
||||
image: golang:1.22.4
|
||||
commands:
|
||||
- 'go build -v -ldflags "-X main.version=${DRONE_TAG##v} -X main.build=${DRONE_BUILD_NUMBER}" -a -tags netgo -o release/linux/arm64/drone-acr ./cmd/drone-acr'
|
||||
environment:
|
||||
|
||||
@@ -222,6 +222,21 @@ func main() {
|
||||
Usage: "docker password",
|
||||
EnvVar: "PLUGIN_PASSWORD,DOCKER_PASSWORD",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "docker.baseimageusername",
|
||||
Usage: "Docker username for base image registry",
|
||||
EnvVar: "PLUGIN_DOCKER_USERNAME,PLUGIN_BASE_IMAGE_USERNAME,DOCKER_BASE_IMAGE_USERNAME",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "docker.baseimagepassword",
|
||||
Usage: "Docker password for base image registry",
|
||||
EnvVar: "PLUGIN_DOCKER_PASSWORD,PLUGIN_BASE_IMAGE_PASSWORD,DOCKER_BASE_IMAGE_PASSWORD",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "docker.baseimageregistry",
|
||||
Usage: "Docker registry for base image registry",
|
||||
EnvVar: "PLUGIN_DOCKER_REGISTRY,PLUGIN_BASE_IMAGE_REGISTRY,DOCKER_BASE_IMAGE_REGISTRY",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "docker.email",
|
||||
Usage: "docker email",
|
||||
@@ -367,6 +382,9 @@ func run(c *cli.Context) error {
|
||||
Experimental: c.Bool("daemon.experimental"),
|
||||
RegistryType: registryType,
|
||||
},
|
||||
BaseImageRegistry: c.String("docker.baseimageregistry"),
|
||||
BaseImageUsername: c.String("docker.baseimageusername"),
|
||||
BaseImagePassword: c.String("docker.baseimagepassword"),
|
||||
}
|
||||
|
||||
if c.Bool("tags.auto") {
|
||||
|
||||
+18
-3
@@ -42,7 +42,7 @@ func main() {
|
||||
assumeRole = getenv("PLUGIN_ASSUME_ROLE")
|
||||
externalId = getenv("PLUGIN_EXTERNAL_ID")
|
||||
scanOnPush = parseBoolOrDefault(false, getenv("PLUGIN_SCAN_ON_PUSH"))
|
||||
idToken = os.Getenv("PLUGIN_OIDC_TOKEN_ID")
|
||||
idToken = os.Getenv("PLUGIN_OIDC_TOKEN_ID")
|
||||
)
|
||||
|
||||
// set the region
|
||||
@@ -218,9 +218,24 @@ func getECRClient(sess *session.Session, role string, externalId string, idToken
|
||||
if role == "" {
|
||||
return ecr.New(sess)
|
||||
}
|
||||
// Use STS AssumeRoleWithWebIdentity when idToken is provided
|
||||
|
||||
if idToken != "" {
|
||||
creds := stscreds.NewWebIdentityCredentials(sess, role, "", idToken)
|
||||
tempFile, err := os.CreateTemp("/tmp", "idToken-*.jwt")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create temporary file: %v", err)
|
||||
}
|
||||
defer tempFile.Close()
|
||||
|
||||
if err := os.Chmod(tempFile.Name(), 0600); err != nil {
|
||||
log.Fatalf("Failed to set file permissions: %v", err)
|
||||
}
|
||||
|
||||
if _, err := tempFile.WriteString(idToken); err != nil {
|
||||
log.Fatalf("Failed to write ID token to temporary file: %v", err)
|
||||
}
|
||||
|
||||
// Create credentials using the path to the ID token file
|
||||
creds := stscreds.NewWebIdentityCredentials(sess, role, "", tempFile.Name())
|
||||
return ecr.New(sess, &aws.Config{Credentials: creds})
|
||||
} else if externalId != "" {
|
||||
return ecr.New(sess, &aws.Config{
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/drone-plugins/drone-docker/internal/docker"
|
||||
"github.com/drone-plugins/drone-plugin-lib/drone"
|
||||
)
|
||||
|
||||
@@ -75,13 +76,16 @@ type (
|
||||
|
||||
// Plugin defines the Docker plugin parameters.
|
||||
Plugin struct {
|
||||
Login Login // Docker login configuration
|
||||
Build Build // Docker build configuration
|
||||
Daemon Daemon // Docker daemon configuration
|
||||
Dryrun bool // Docker push is skipped
|
||||
Cleanup bool // Docker purge is enabled
|
||||
CardPath string // Card path to write file to
|
||||
ArtifactFile string // Artifact path to write file to
|
||||
Login Login // Docker login configuration
|
||||
Build Build // Docker build configuration
|
||||
Daemon Daemon // Docker daemon configuration
|
||||
Dryrun bool // Docker push is skipped
|
||||
Cleanup bool // Docker purge is enabled
|
||||
CardPath string // Card path to write file to
|
||||
ArtifactFile string // Artifact path to write file to
|
||||
BaseImageRegistry string // Docker registry to pull base image
|
||||
BaseImageUsername string // Docker registry username to pull base image
|
||||
BaseImagePassword string // Docker registry password to pull base image
|
||||
}
|
||||
|
||||
Card []struct {
|
||||
@@ -160,6 +164,29 @@ func (p Plugin) Exec() error {
|
||||
}
|
||||
}
|
||||
|
||||
// instead of writing to config file directly, using docker's login func
|
||||
// is better to integrate with various credential helpers,
|
||||
// it also handles different registry specific logic in a better way,
|
||||
// as opposed to config write where different registries need to be addressed differently.
|
||||
// It handles any changes in the authentication process across different Docker versions.
|
||||
|
||||
if p.BaseImagePassword != "" {
|
||||
var baseConnectorLogin Login
|
||||
baseConnectorLogin.Registry = p.BaseImageRegistry
|
||||
baseConnectorLogin.Username = p.BaseImageUsername
|
||||
baseConnectorLogin.Password = p.BaseImagePassword
|
||||
|
||||
cmd := commandLogin(baseConnectorLogin)
|
||||
|
||||
raw, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
out := string(raw)
|
||||
out = strings.Replace(out, "WARNING! Using --password via the CLI is insecure. Use --password-stdin.", "", -1)
|
||||
fmt.Println(out)
|
||||
return fmt.Errorf("Error authenticating base connector: exit status 1")
|
||||
}
|
||||
}
|
||||
|
||||
// login to the Docker registry
|
||||
if p.Login.Password != "" {
|
||||
cmd := commandLogin(p.Login)
|
||||
@@ -270,6 +297,35 @@ func (p Plugin) Exec() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// helper function to set the credentials
|
||||
func setDockerAuth(username, password, registry, baseImageUsername,
|
||||
baseImagePassword, baseImageRegistry string) ([]byte, error) {
|
||||
var credentials []docker.RegistryCredentials
|
||||
// add only docker registry to the config
|
||||
dockerConfig := docker.NewConfig()
|
||||
if password != "" {
|
||||
pushToRegistryCreds := docker.RegistryCredentials{
|
||||
Registry: registry,
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
// push registry auth
|
||||
credentials = append(credentials, pushToRegistryCreds)
|
||||
}
|
||||
|
||||
if baseImageRegistry != "" {
|
||||
pullFromRegistryCreds := docker.RegistryCredentials{
|
||||
Registry: baseImageRegistry,
|
||||
Username: baseImageUsername,
|
||||
Password: baseImagePassword,
|
||||
}
|
||||
// base image registry auth
|
||||
credentials = append(credentials, pullFromRegistryCreds)
|
||||
}
|
||||
// Creates docker config for both the registries used for authentication
|
||||
return dockerConfig.CreateDockerConfigJson(credentials)
|
||||
}
|
||||
|
||||
// helper function to create the docker login command.
|
||||
func commandLogin(login Login) *exec.Cmd {
|
||||
if login.Email != "" {
|
||||
|
||||
@@ -9,6 +9,7 @@ require (
|
||||
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/stretchr/testify v1.8.1
|
||||
github.com/urfave/cli v1.22.2
|
||||
golang.org/x/oauth2 v0.13.0
|
||||
)
|
||||
@@ -17,6 +18,7 @@ require (
|
||||
cloud.google.com/go/compute v1.23.1 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.2.3 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/google/s2a-go v0.1.7 // indirect
|
||||
@@ -24,6 +26,7 @@ require (
|
||||
github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
@@ -39,4 +42,4 @@ require (
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
go 1.22
|
||||
go 1.22.4
|
||||
|
||||
@@ -90,6 +90,7 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
|
||||
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
v2HubRegistryURL string = "https://registry.hub.docker.com/v2/"
|
||||
v1RegistryURL string = "https://index.docker.io/v1/" // Default registry
|
||||
v2RegistryURL string = "https://index.docker.io/v2/" // v2 registry is not supported
|
||||
)
|
||||
|
||||
type (
|
||||
Auth struct {
|
||||
Auth string `json:"auth"`
|
||||
}
|
||||
|
||||
Config struct {
|
||||
Auths map[string]Auth `json:"auths"`
|
||||
CredHelpers map[string]string `json:"credHelpers,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
type RegistryCredentials struct {
|
||||
Registry string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
Auths: make(map[string]Auth),
|
||||
CredHelpers: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) SetAuth(registry, username, password string) {
|
||||
authBytes := []byte(username + ":" + password)
|
||||
encodedString := base64.StdEncoding.EncodeToString(authBytes)
|
||||
c.Auths[registry] = Auth{Auth: encodedString}
|
||||
}
|
||||
|
||||
func (c *Config) SetCredHelper(registry, helper string) {
|
||||
c.CredHelpers[registry] = helper
|
||||
}
|
||||
|
||||
func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]byte, error) {
|
||||
for _, cred := range credentials {
|
||||
if cred.Registry != "" {
|
||||
|
||||
if cred.Username == "" {
|
||||
return nil, fmt.Errorf("Username must be specified for registry: %s", cred.Registry)
|
||||
}
|
||||
if cred.Password == "" {
|
||||
return nil, fmt.Errorf("Password must be specified for registry: %s", cred.Registry)
|
||||
}
|
||||
c.SetAuth(cred.Registry, cred.Username, cred.Password)
|
||||
}
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to serialize docker config json")
|
||||
}
|
||||
|
||||
return jsonBytes, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
RegistryV1 string = "https://index.docker.io/v1/"
|
||||
RegistryV2 string = "https://index.docker.io/v2/"
|
||||
RegistryECRPublic string = "public.ecr.aws"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
c := NewConfig()
|
||||
assert.NotNil(t, c.Auths)
|
||||
assert.NotNil(t, c.CredHelpers)
|
||||
|
||||
c.SetAuth(RegistryV1, "test", "password")
|
||||
expectedAuth := Auth{Auth: "dGVzdDpwYXNzd29yZA=="}
|
||||
assert.Equal(t, expectedAuth, c.Auths[RegistryV1])
|
||||
|
||||
c.SetCredHelper(RegistryECRPublic, "ecr-login")
|
||||
assert.Equal(t, "ecr-login", c.CredHelpers[RegistryECRPublic])
|
||||
|
||||
tempDir, err := ioutil.TempDir("", "docker-config-test")
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
credentials := []RegistryCredentials{
|
||||
{
|
||||
Registry: "https://index.docker.io/v1/",
|
||||
Username: "user1",
|
||||
Password: "pass1",
|
||||
},
|
||||
{
|
||||
Registry: "gcr.io",
|
||||
Username: "user2",
|
||||
Password: "pass2",
|
||||
},
|
||||
}
|
||||
|
||||
jsonBytes, err := c.CreateDockerConfigJson(credentials)
|
||||
assert.NoError(t, err)
|
||||
|
||||
configPath := filepath.Join(tempDir, "config.json")
|
||||
err = ioutil.WriteFile(configPath, jsonBytes, 0644)
|
||||
assert.NoError(t, err)
|
||||
|
||||
data, err := ioutil.ReadFile(configPath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var configFromFile Config
|
||||
err = json.Unmarshal(data, &configFromFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, c.Auths, configFromFile.Auths)
|
||||
assert.Equal(t, c.CredHelpers, configFromFile.CredHelpers)
|
||||
}
|
||||
Reference in New Issue
Block a user