0
0
mirror of https://github.com/thegeeklab/wp-docker-buildx.git synced 2024-11-25 09:10:39 +00:00

fix: fix broken write to docker config.json

This commit is contained in:
Robert Kaussow 2024-11-13 23:11:33 +01:00
parent 81e822e883
commit b305bfc823
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
6 changed files with 84 additions and 10 deletions

4
go.mod
View File

@ -5,6 +5,7 @@ go 1.23.3
require ( require (
github.com/cenkalti/backoff/v4 v4.3.0 github.com/cenkalti/backoff/v4 v4.3.0
github.com/rs/zerolog v1.33.0 github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
github.com/thegeeklab/wp-plugin-go/v3 v3.0.9 github.com/thegeeklab/wp-plugin-go/v3 v3.0.9
github.com/urfave/cli/v2 v2.27.5 github.com/urfave/cli/v2 v2.27.5
) )
@ -15,6 +16,7 @@ require (
github.com/Masterminds/semver/v3 v3.3.0 // indirect github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/Masterminds/sprig/v3 v3.3.0 // indirect github.com/Masterminds/sprig/v3 v3.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect github.com/huandu/xstrings v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/joho/godotenv v1.5.1 // indirect
@ -22,6 +24,7 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect github.com/spf13/cast v1.7.0 // indirect
@ -29,4 +32,5 @@ require (
golang.org/x/crypto v0.29.0 // indirect golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect golang.org/x/sys v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

1
go.sum
View File

@ -68,6 +68,7 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"path/filepath"
"time" "time"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
@ -129,7 +130,12 @@ func (p *Plugin) Execute() error {
} }
if p.Settings.Registry.Config != "" { if p.Settings.Registry.Config != "" {
if err := WriteDockerConf(homeDir, p.Settings.Registry.Config); err != nil { path := filepath.Join(homeDir, ".docker", "config.json")
if err := os.MkdirAll(filepath.Dir(path), strictFilePerm); err != nil {
return err
}
if err := WriteDockerConf(path, p.Settings.Registry.Config); err != nil {
return fmt.Errorf("error writing docker config: %w", err) return fmt.Errorf("error writing docker config: %w", err)
} }
} }

View File

@ -333,9 +333,9 @@ func Flags(settings *Settings, category string) []cli.Flag {
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "docker.config", Name: "registry.config",
EnvVars: []string{"PLUGIN_CONFIG", "DOCKER_PLUGIN_CONFIG"}, EnvVars: []string{"PLUGIN_REGISTRY_CONFIG", "DOCKER_REGISTRY_CONFIG"},
Usage: "content of the docker daemon json config", Usage: "content of the registry credentials store file",
Destination: &settings.Registry.Config, Destination: &settings.Registry.Config,
DefaultText: "$DOCKER_PLUGIN_CONFIG", DefaultText: "$DOCKER_PLUGIN_CONFIG",
Category: category, Category: category,

View File

@ -1,11 +1,14 @@
package plugin package plugin
import ( import (
"encoding/json"
"fmt"
"net" "net"
"os" "os"
"path/filepath"
) )
var errInvalidDockerConfig = fmt.Errorf("invalid docker config")
func GetContainerIP() (string, error) { func GetContainerIP() (string, error) {
netInterfaceAddrList, err := net.InterfaceAddrs() netInterfaceAddrList, err := net.InterfaceAddrs()
if err != nil { if err != nil {
@ -23,13 +26,17 @@ func GetContainerIP() (string, error) {
} }
func WriteDockerConf(path, conf string) error { func WriteDockerConf(path, conf string) error {
confPath := filepath.Join(path, ".docker", "config.json") var jsonData interface{}
if err := json.Unmarshal([]byte(conf), &jsonData); err != nil {
if err := os.MkdirAll(confPath, strictFilePerm); err != nil { return fmt.Errorf("%w: %w", errInvalidDockerConfig, err)
return err
} }
err := os.WriteFile(path, []byte(conf), strictFilePerm) jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return fmt.Errorf("%w: %w", errInvalidDockerConfig, err)
}
err = os.WriteFile(path, jsonBytes, strictFilePerm)
if err != nil { if err != nil {
return err return err
} }

56
plugin/util_test.go Normal file
View File

@ -0,0 +1,56 @@
package plugin
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWriteDockerConf(t *testing.T) {
tests := []struct {
name string
conf string
wantErr bool
}{
{
name: "valid json config",
conf: `{"auths":{"registry.example.com":{"auth":"dXNlcjpwYXNz"}}}`,
wantErr: false,
},
{
name: "invalid json config",
conf: `{"auths":invalid}`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "config.json")
err := WriteDockerConf(tmpFile, tt.conf)
if tt.wantErr {
assert.ErrorAs(t, err, &errInvalidDockerConfig)
return
}
assert.NoError(t, err)
content, err := os.ReadFile(tmpFile)
assert.NoError(t, err, "Failed to read config file")
var got, want interface{}
err = json.Unmarshal(content, &got)
assert.NoError(t, err, "Failed to parse written config")
err = json.Unmarshal([]byte(tt.conf), &want)
assert.NoError(t, err, "Failed to parse test config")
assert.Equal(t, want, got, "Written config does not match expected")
})
}
}