1/* 2Copyright (c) 2016 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package cert 18 19import ( 20 "bytes" 21 "context" 22 "flag" 23 "io" 24 "io/ioutil" 25 "os" 26 27 "github.com/vmware/govmomi/govc/cli" 28 "github.com/vmware/govmomi/govc/flags" 29) 30 31type install struct { 32 *flags.HostSystemFlag 33} 34 35func init() { 36 cli.Register("host.cert.import", &install{}) 37} 38 39func (cmd *install) Register(ctx context.Context, f *flag.FlagSet) { 40 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 41 cmd.HostSystemFlag.Register(ctx, f) 42} 43 44func (cmd *install) Usage() string { 45 return "FILE" 46} 47 48func (cmd *install) Description() string { 49 return `Install SSL certificate FILE on HOST. 50 51If FILE name is "-", read certificate from stdin.` 52} 53 54func (cmd *install) Process(ctx context.Context) error { 55 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 56 return err 57 } 58 return nil 59} 60 61func (cmd *install) Run(ctx context.Context, f *flag.FlagSet) error { 62 host, err := cmd.HostSystem() 63 if err != nil { 64 return err 65 } 66 67 m, err := host.ConfigManager().CertificateManager(ctx) 68 if err != nil { 69 return err 70 } 71 72 var cert string 73 74 name := f.Arg(0) 75 if name == "-" || name == "" { 76 var buf bytes.Buffer 77 if _, err := io.Copy(&buf, os.Stdin); err != nil { 78 return err 79 } 80 cert = buf.String() 81 } else { 82 b, err := ioutil.ReadFile(name) 83 if err != nil { 84 return err 85 } 86 cert = string(b) 87 } 88 89 return m.InstallServerCertificate(ctx, cert) 90} 91