X Tutup
Skip to content

Commit 4615c92

Browse files
committed
Fix warning in docker CLI when swarm ca --ca-cert, etc. flags are passed,
and add a test. Signed-off-by: Ying Li <ying.li@docker.com>
1 parent 6d74d8c commit 4615c92

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

cli/command/swarm/ca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func runCA(dockerCli command.Cli, flags *pflag.FlagSet, opts caOptions) error {
6060
}
6161

6262
if !opts.rotate {
63-
for _, f := range []string{flagCACert, flagCAKey, flagCACert, flagExternalCA} {
63+
for _, f := range []string{flagCACert, flagCAKey, flagCertExpiry, flagExternalCA} {
6464
if flags.Changed(f) {
6565
return fmt.Errorf("`--%s` flag requires the `--rotate` flag to update the CA", f)
6666
}

cli/command/swarm/ca_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package swarm
22

33
import (
44
"bytes"
5+
"io/ioutil"
6+
"os"
57
"testing"
68
"time"
79

10+
"github.com/docker/cli/cli/internal/test"
811
"github.com/docker/docker/api/types/swarm"
12+
"github.com/docker/docker/pkg/testutil"
913
"github.com/stretchr/testify/assert"
1014
"github.com/stretchr/testify/require"
1115
)
@@ -34,6 +38,69 @@ func TestDisplayTrustRootNoRoot(t *testing.T) {
3438
assert.EqualError(t, err, "No CA information available")
3539
}
3640

41+
func TestDisplayTrustRootInvalidFlags(t *testing.T) {
42+
// we need an actual PEMfile to test
43+
tmpfile, err := ioutil.TempFile("", "pemfile")
44+
assert.NoError(t, err)
45+
defer os.Remove(tmpfile.Name())
46+
tmpfile.Write([]byte(`
47+
-----BEGIN CERTIFICATE-----
48+
MIIBajCCARCgAwIBAgIUe0+jYWhxN8fFOByC7yveIYgvx1kwCgYIKoZIzj0EAwIw
49+
EzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMTcwNjI3MTUxNDAwWhcNMzcwNjIyMTUx
50+
NDAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH
51+
A0IABGgbOZLd7b4b262+6m4ignIecbAZKim6djNiIS1Kl5IHciXYn7gnSpsayjn7
52+
GQABpgkdPeM9TEQowmtR1qSnORujQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB
53+
Af8EBTADAQH/MB0GA1UdDgQWBBQ6Rtcn823/fxRZyheRDFpDzuBMpTAKBggqhkjO
54+
PQQDAgNIADBFAiEAqD3Kb2rgsy6NoTk+zEgcUi/aGBCsvQDG3vML1PXN8j0CIBjj
55+
4nDj+GmHXcnKa8wXx70Z8OZEpRQIiKDDLmcXuslp
56+
-----END CERTIFICATE-----
57+
`))
58+
tmpfile.Close()
59+
60+
errorTestCases := [][]string{
61+
{
62+
"--ca-cert=" + tmpfile.Name(),
63+
},
64+
{
65+
"--ca-key=" + tmpfile.Name(),
66+
},
67+
{ // to make sure we're not erroring because we didn't provide a CA key along with the CA cert
68+
69+
"--ca-cert=" + tmpfile.Name(),
70+
"--ca-key=" + tmpfile.Name(),
71+
},
72+
{
73+
"--cert-expiry=2160h0m0s",
74+
},
75+
{
76+
"--external-ca=protocol=cfssl,url=https://some.com/https/url",
77+
},
78+
{ // to make sure we're not erroring because we didn't provide a CA cert and external CA
79+
80+
"--ca-cert=" + tmpfile.Name(),
81+
"--external-ca=protocol=cfssl,url=https://some.com/https/url",
82+
},
83+
}
84+
85+
for _, args := range errorTestCases {
86+
cmd := newCACommand(
87+
test.NewFakeCli(&fakeClient{
88+
swarmInspectFunc: func() (swarm.Swarm, error) {
89+
return swarm.Swarm{
90+
ClusterInfo: swarm.ClusterInfo{
91+
TLSInfo: swarm.TLSInfo{
92+
TrustRoot: "root",
93+
},
94+
},
95+
}, nil
96+
},
97+
}))
98+
assert.NoError(t, cmd.Flags().Parse(args))
99+
cmd.SetOutput(ioutil.Discard)
100+
testutil.ErrorContains(t, cmd.Execute(), "flag requires the `--rotate` flag to update the CA")
101+
}
102+
}
103+
37104
func TestDisplayTrustRoot(t *testing.T) {
38105
buffer := new(bytes.Buffer)
39106
trustRoot := "trustme"

0 commit comments

Comments
 (0)
X Tutup