Skip to content

Commit a6030a5

Browse files
author
Aaron Lehmann
committed
Add unlock key rotation
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
1 parent 0f9fc54 commit a6030a5

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

api/server/router/swarm/cluster_routes.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ func (sr *swarmRouter) updateCluster(ctx context.Context, w http.ResponseWriter,
8787
flags.RotateManagerToken = rot
8888
}
8989

90+
if value := r.URL.Query().Get("rotateManagerUnlockKey"); value != "" {
91+
rot, err := strconv.ParseBool(value)
92+
if err != nil {
93+
return fmt.Errorf("invalid value for rotateManagerUnlockKey: %s", value)
94+
}
95+
96+
flags.RotateManagerUnlockKey = rot
97+
}
98+
9099
if err := sr.backend.Update(version, swarm, flags); err != nil {
91100
logrus.Errorf("Error configuring swarm: %v", err)
92101
return err

cli/command/swarm/unlock_key.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/spf13/cobra"
77

8+
"github.com/docker/docker/api/types/swarm"
89
"github.com/docker/docker/cli"
910
"github.com/docker/docker/cli/command"
1011
"github.com/pkg/errors"
@@ -23,14 +24,35 @@ func newUnlockKeyCommand(dockerCli *command.DockerCli) *cobra.Command {
2324
ctx := context.Background()
2425

2526
if rotate {
26-
// FIXME(aaronl)
27+
flags := swarm.UpdateFlags{RotateManagerUnlockKey: true}
28+
29+
swarm, err := client.SwarmInspect(ctx)
30+
if err != nil {
31+
return err
32+
}
33+
34+
if !swarm.Spec.EncryptionConfig.AutoLockManagers {
35+
return errors.New("cannot rotate because autolock is not turned on")
36+
}
37+
38+
err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, flags)
39+
if err != nil {
40+
return err
41+
}
42+
if !quiet {
43+
fmt.Fprintf(dockerCli.Out(), "Successfully rotated manager unlock key.\n\n")
44+
}
2745
}
2846

2947
unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
3048
if err != nil {
3149
return errors.Wrap(err, "could not fetch unlock key")
3250
}
3351

52+
if unlockKeyResp.UnlockKey == "" {
53+
return errors.New("no unlock key is set")
54+
}
55+
3456
if quiet {
3557
fmt.Fprintln(dockerCli.Out(), unlockKeyResp.UnlockKey)
3658
} else {

client/swarm_update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func (cli *Client) SwarmUpdate(ctx context.Context, version swarm.Version, swarm
1515
query.Set("version", strconv.FormatUint(version.Index, 10))
1616
query.Set("rotateWorkerToken", fmt.Sprintf("%v", flags.RotateWorkerToken))
1717
query.Set("rotateManagerToken", fmt.Sprintf("%v", flags.RotateManagerToken))
18+
query.Set("rotateManagerUnlockKey", fmt.Sprintf("%v", flags.RotateManagerUnlockKey))
1819
resp, err := cli.post(ctx, "/swarm/update", query, swarm, nil)
1920
ensureReaderClosed(resp)
2021
return err

daemon/cluster/cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ func (c *Cluster) GetUnlockKey() (string, error) {
558558
return "", err
559559
}
560560

561+
if len(r.UnlockKey) == 0 {
562+
// no key
563+
return "", nil
564+
}
565+
561566
return encryption.HumanReadableKey(r.UnlockKey), nil
562567
}
563568

0 commit comments

Comments
 (0)