(ctx context.Context, dockerCLI command.Cli, opts unlockKeyOptions)
| 43 | } |
| 44 | |
| 45 | func runUnlockKey(ctx context.Context, dockerCLI command.Cli, opts unlockKeyOptions) error { |
| 46 | apiClient := dockerCLI.Client() |
| 47 | |
| 48 | if opts.rotate { |
| 49 | res, err := apiClient.SwarmInspect(ctx, client.SwarmInspectOptions{}) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | if !res.Swarm.Spec.EncryptionConfig.AutoLockManagers { |
| 55 | return errors.New("cannot rotate because autolock is not turned on") |
| 56 | } |
| 57 | |
| 58 | _, err = apiClient.SwarmUpdate(ctx, client.SwarmUpdateOptions{ |
| 59 | Version: res.Swarm.Version, |
| 60 | Spec: res.Swarm.Spec, |
| 61 | |
| 62 | RotateManagerUnlockKey: true, |
| 63 | }) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | if !opts.quiet { |
| 69 | _, _ = fmt.Fprintln(dockerCLI.Out(), "Successfully rotated manager unlock key.") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | resp, err := apiClient.SwarmGetUnlockKey(ctx) |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("could not fetch unlock key: %w", err) |
| 76 | } |
| 77 | |
| 78 | if resp.Key == "" { |
| 79 | return errors.New("no unlock key is set") |
| 80 | } |
| 81 | |
| 82 | if opts.quiet { |
| 83 | _, _ = fmt.Fprintln(dockerCLI.Out(), resp.Key) |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | printUnlockCommand(dockerCLI.Out(), resp.Key) |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func printUnlockCommand(out io.Writer, unlockKey string) { |
| 92 | if len(unlockKey) > 0 { |
no test coverage detected
searching dependent graphs…