|
| 1 | +/* |
| 2 | +Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kms |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "os" |
| 25 | + "text/tabwriter" |
| 26 | + |
| 27 | + "github.com/vmware/govmomi/crypto" |
| 28 | + "github.com/vmware/govmomi/govc/cli" |
| 29 | + "github.com/vmware/govmomi/govc/flags" |
| 30 | + "github.com/vmware/govmomi/vim25/types" |
| 31 | +) |
| 32 | + |
| 33 | +type ls struct { |
| 34 | + *flags.ClientFlag |
| 35 | + *flags.OutputFlag |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + cli.Register("kms.ls", &ls{}) |
| 40 | +} |
| 41 | + |
| 42 | +func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { |
| 43 | + cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) |
| 44 | + cmd.ClientFlag.Register(ctx, f) |
| 45 | + |
| 46 | + cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) |
| 47 | + cmd.OutputFlag.Register(ctx, f) |
| 48 | +} |
| 49 | + |
| 50 | +func (cmd *ls) Process(ctx context.Context) error { |
| 51 | + if err := cmd.ClientFlag.Process(ctx); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + return cmd.OutputFlag.Process(ctx) |
| 55 | +} |
| 56 | + |
| 57 | +func (cmd *ls) Usage() string { |
| 58 | + return "NAME" |
| 59 | +} |
| 60 | + |
| 61 | +func (cmd *ls) Description() string { |
| 62 | + return `Display KMS info. |
| 63 | +
|
| 64 | +Examples: |
| 65 | + govc kms.ls |
| 66 | + govc kms.ls -json |
| 67 | + govc kms.ls - # default provider |
| 68 | + govc kms.ls ProviderName |
| 69 | + govc kms.ls -json ProviderName` |
| 70 | +} |
| 71 | + |
| 72 | +func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { |
| 73 | + c, err := cmd.Client() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + m, err := crypto.GetManagerKmip(c) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + info, err := m.ListKmipServers(ctx, nil) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + id := f.Arg(0) |
| 89 | + |
| 90 | + if id == "" { |
| 91 | + status, err := m.GetStatus(ctx, info...) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + return cmd.WriteResult(&clusterResult{Info: info, Status: status}) |
| 96 | + } |
| 97 | + |
| 98 | + if id == "-" { |
| 99 | + for _, s := range info { |
| 100 | + if s.UseAsDefault { |
| 101 | + id = s.ClusterId.Id |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + status, err := m.GetClusterStatus(ctx, id) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + format := &serverResult{Status: status} |
| 113 | + |
| 114 | + for _, s := range info { |
| 115 | + if s.ClusterId.Id == id { |
| 116 | + format.Info = s |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return cmd.WriteResult(format) |
| 121 | +} |
| 122 | + |
| 123 | +type serverResult struct { |
| 124 | + Info types.KmipClusterInfo `json:"info"` |
| 125 | + Status *types.CryptoManagerKmipClusterStatus `json:"status"` |
| 126 | +} |
| 127 | + |
| 128 | +func (r *serverResult) status(name string) types.ManagedEntityStatus { |
| 129 | + for _, server := range r.Status.Servers { |
| 130 | + if server.Name == name { |
| 131 | + return server.Status |
| 132 | + } |
| 133 | + } |
| 134 | + return types.ManagedEntityStatusGray |
| 135 | +} |
| 136 | + |
| 137 | +func (r *serverResult) Write(w io.Writer) error { |
| 138 | + tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) |
| 139 | + |
| 140 | + if r.Info.ManagementType == string(types.KmipClusterInfoKmsManagementTypeNativeProvider) { |
| 141 | + boolVal := func(v *bool) bool { |
| 142 | + if v == nil { |
| 143 | + return false |
| 144 | + } |
| 145 | + return *v |
| 146 | + } |
| 147 | + |
| 148 | + fmt.Fprintf(tw, "Key ID: %s\tHas Backup: %t\tTPM Required: %t\n", |
| 149 | + r.Info.KeyId, boolVal(r.Info.HasBackup), boolVal(r.Info.TpmRequired)) |
| 150 | + } else { |
| 151 | + for _, s := range r.Info.Servers { |
| 152 | + status := r.status(s.Name) |
| 153 | + fmt.Fprintf(tw, "%s\t%s:%d\t%s\n", s.Name, s.Address, s.Port, status) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return tw.Flush() |
| 158 | +} |
| 159 | + |
| 160 | +type clusterResult struct { |
| 161 | + Info []types.KmipClusterInfo `json:"info"` |
| 162 | + Status []types.CryptoManagerKmipClusterStatus `json:"status"` |
| 163 | +} |
| 164 | + |
| 165 | +func (r *clusterResult) status(id types.KeyProviderId) types.ManagedEntityStatus { |
| 166 | + for _, status := range r.Status { |
| 167 | + if status.ClusterId == id { |
| 168 | + return status.OverallStatus |
| 169 | + } |
| 170 | + } |
| 171 | + return types.ManagedEntityStatusGray |
| 172 | +} |
| 173 | + |
| 174 | +func kmsType(kind string) string { |
| 175 | + switch types.KmipClusterInfoKmsManagementType(kind) { |
| 176 | + case types.KmipClusterInfoKmsManagementTypeVCenter: |
| 177 | + return "Standard" |
| 178 | + case types.KmipClusterInfoKmsManagementTypeNativeProvider: |
| 179 | + return "Native" |
| 180 | + default: |
| 181 | + return kind |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +func (r *clusterResult) Write(w io.Writer) error { |
| 186 | + tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) |
| 187 | + |
| 188 | + for _, info := range r.Info { |
| 189 | + name := info.ClusterId.Id |
| 190 | + kind := kmsType(info.ManagementType) |
| 191 | + status := r.status(info.ClusterId) |
| 192 | + fmt.Fprintf(tw, "%s\t%s\t%s\n", name, kind, status) |
| 193 | + } |
| 194 | + |
| 195 | + return tw.Flush() |
| 196 | +} |
0 commit comments