github.com/vmware/govmomi@v0.37.1/govc/host/cert/install.go (about)

     1  /*
     2  Copyright (c) 2016 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 cert
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"flag"
    23  	"io"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  )
    30  
    31  type install struct {
    32  	*flags.HostSystemFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("host.cert.import", &install{})
    37  }
    38  
    39  func (cmd *install) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    41  	cmd.HostSystemFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *install) Usage() string {
    45  	return "FILE"
    46  }
    47  
    48  func (cmd *install) Description() string {
    49  	return `Install SSL certificate FILE on HOST.
    50  
    51  If FILE name is "-", read certificate from stdin.`
    52  }
    53  
    54  func (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  
    61  func (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 := os.ReadFile(filepath.Clean(name))
    83  		if err != nil {
    84  			return err
    85  		}
    86  		cert = string(b)
    87  	}
    88  
    89  	return m.InstallServerCertificate(ctx, cert)
    90  }