Skip to content

Commit

Permalink
fix: fix broken write to docker config.json (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Nov 13, 2024
1 parent 81e822e commit 13f95c9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/data/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ properties:
defaultValue: false
required: false

- name: config
- name: registry_config
description: |
Content of the docker daemon json config.
Content of the registry credentials store file.
type: string
defaultValue: $DOCKER_PLUGIN_CONFIG
defaultValue: $DOCKER_REGISTRY_CONFIG
required: false

- name: containerfile
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.3
require (
github.com/cenkalti/backoff/v4 v4.3.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/urfave/cli/v2 v2.27.5
)
Expand All @@ -15,18 +16,21 @@ require (
github.com/Masterminds/semver/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/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.2.0 // 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/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
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/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 7 additions & 1 deletion plugin/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"time"

"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -129,7 +130,12 @@ func (p *Plugin) Execute() error {
}

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)
}
}
Expand Down
8 changes: 4 additions & 4 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ func Flags(settings *Settings, category string) []cli.Flag {
Category: category,
},
&cli.StringFlag{
Name: "docker.config",
EnvVars: []string{"PLUGIN_CONFIG", "DOCKER_PLUGIN_CONFIG"},
Usage: "content of the docker daemon json config",
Name: "registry.config",
EnvVars: []string{"PLUGIN_REGISTRY_CONFIG", "DOCKER_REGISTRY_CONFIG"},
Usage: "content of the registry credentials store file",
Destination: &settings.Registry.Config,
DefaultText: "$DOCKER_PLUGIN_CONFIG",
DefaultText: "$DOCKER_REGISTRY_CONFIG",
Category: category,
},
&cli.BoolFlag{
Expand Down
17 changes: 12 additions & 5 deletions plugin/util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package plugin

import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
)

var errInvalidDockerConfig = fmt.Errorf("invalid docker config")

func GetContainerIP() (string, error) {
netInterfaceAddrList, err := net.InterfaceAddrs()
if err != nil {
Expand All @@ -23,13 +26,17 @@ func GetContainerIP() (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 {
return fmt.Errorf("%w: %w", errInvalidDockerConfig, err)
}

if err := os.MkdirAll(confPath, strictFilePerm); err != nil {
return err
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return fmt.Errorf("%w: %w", errInvalidDockerConfig, err)
}

err := os.WriteFile(path, []byte(conf), strictFilePerm)
err = os.WriteFile(path, jsonBytes, strictFilePerm)
if err != nil {
return err
}
Expand Down
56 changes: 56 additions & 0 deletions plugin/util_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
}

0 comments on commit 13f95c9

Please sign in to comment.