Fix func to determine default login (#29)

only add default login if auth profided or no login entry exist

Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/29
Reviewed-by: qwerty287 <qwerty287@noreply.codeberg.org>
This commit is contained in:
6543 2022-10-11 11:38:36 +02:00
parent 5868a6e6a8
commit fca44ab7ae
6 changed files with 108 additions and 9 deletions

View File

@ -117,7 +117,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Name: "tags",
EnvVars: []string{"PLUGIN_TAG", "PLUGIN_TAGS"},
Usage: "sets repository tags to use for the image",
Value: cli.NewStringSlice([]string{"latest"}...),
Value: cli.NewStringSlice("latest"),
FilePath: ".tags",
Destination: &settings.Build.Tags,
},

5
go.mod
View File

@ -3,10 +3,12 @@ module codeberg.org/woodpecker-plugins/plugin-docker-buildx
go 1.18
require (
codeberg.org/6543/go-yaml2json v0.3.0
github.com/coreos/go-semver v0.3.0
github.com/drone-plugins/drone-plugin-lib v0.4.0
github.com/joho/godotenv v1.4.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.16.3
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
honnef.co/go/tools v0.3.3
@ -15,10 +17,13 @@ require (
require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

9
go.sum
View File

@ -1,3 +1,5 @@
codeberg.org/6543/go-yaml2json v0.3.0 h1:BlvjmY0Gous8P+rr8aBdgPYnIfUAqFepF8q7Tp0R5t8=
codeberg.org/6543/go-yaml2json v0.3.0/go.mod h1:mz61q14LWF4ZABrgMEDMmk3t9dPi6zgR1uBh2VKV2RQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
@ -27,9 +29,12 @@ github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
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 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk=
github.com/urfave/cli/v2 v2.16.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
@ -59,11 +64,13 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg=
golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
honnef.co/go/tools v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
honnef.co/go/tools v0.3.3/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw=

View File

@ -84,15 +84,18 @@ func (l Login) anonymous() bool {
// Init initialise plugin settings
func (p *Plugin) InitSettings() error {
if p.settings.LoginsRaw != "" {
if err := json.Unmarshal([]byte(p.settings.LoginsRaw), &p.settings.Logins); err != nil {
return fmt.Errorf("Could not unmarshal logins: %v", err)
}
}
p.settings.Build.Branch = p.pipeline.Repo.Branch
p.settings.Build.Ref = p.pipeline.Commit.Ref
if p.settings.DefaultLogin.anonymous() {
p.settings.Logins = append(p.settings.Logins, p.settings.DefaultLogin)
} else {
if len(p.settings.Logins) == 0 {
p.settings.Logins = []Login{p.settings.DefaultLogin}
} else if !p.settings.DefaultLogin.anonymous() {
p.settings.Logins = prepend(p.settings.Logins, p.settings.DefaultLogin)
}

77
plugin/impl_test.go Normal file
View File

@ -0,0 +1,77 @@
package plugin
import (
"testing"
"codeberg.org/6543/go-yaml2json"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)
var defaultSettings = Settings{
Daemon: Daemon{
StoragePath: "/var/lib/docker",
},
Build: Build{
Context: ".",
Tags: *cli.NewStringSlice("latest"),
LabelsAuto: true,
Pull: true,
},
DefaultLogin: Login{
Registry: "https://index.docker.io/v1/",
},
LoginsRaw: "[]",
Cleanup: true,
}
func TestDefaultLogin(t *testing.T) {
s := defaultSettings
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 1) {
assert.EqualValues(t, defaultSettings.DefaultLogin.Registry, s.Logins[0].Registry)
}
// only use login to auth to registrys
loginsRaw, err := yaml2json.Convert([]byte(`
- registry: https://index.docker.io/v1/
username: docker_username
password: docker_password
- registry: https://codeberg.org
username: cb_username
password: cb_password`))
assert.NoError(t, err)
s.LoginsRaw = string(loginsRaw)
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 2) {
assert.EqualValues(t, defaultSettings.DefaultLogin.Registry, s.Logins[0].Registry)
}
// mixed login settings ('logins' and 'username', 'password' are used)
s = defaultSettings
loginsRaw, err = yaml2json.Convert([]byte(`
- registry: https://codeberg.org
username: cb_username
password: cb_password`))
assert.NoError(t, err)
s.LoginsRaw = string(loginsRaw)
s.DefaultLogin.Username = "docker_username"
s.DefaultLogin.Password = "docker_password"
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 2) {
assert.EqualValues(t, defaultSettings.DefaultLogin.Registry, s.Logins[0].Registry)
}
// ignore default registry
s = defaultSettings
loginsRaw, err = yaml2json.Convert([]byte(`
- registry: https://codeberg.org
username: cb_username
password: cb_password`))
assert.NoError(t, err)
s.LoginsRaw = string(loginsRaw)
assert.NoError(t, newSettingsOnly(&s).Validate())
if assert.Len(t, s.Logins, 1) {
assert.EqualValues(t, "https://codeberg.org", s.Logins[0].Registry)
}
}

View File

@ -6,7 +6,7 @@ import (
// Plugin implements drone.Plugin to provide the plugin implementation.
type Plugin struct {
settings Settings
settings *Settings
pipeline drone.Pipeline
network drone.Network
}
@ -14,8 +14,15 @@ type Plugin struct {
// New initializes a plugin from the given Settings, Pipeline, and Network.
func New(settings Settings, pipeline drone.Pipeline, network drone.Network) drone.Plugin {
return &Plugin{
settings: settings,
settings: &settings,
pipeline: pipeline,
network: network,
}
}
// newSettingsOnly initializes a plugin from the given Settings only.
func newSettingsOnly(settings *Settings) drone.Plugin {
return &Plugin{
settings: settings,
}
}