github.com/vmware/govmomi@v0.37.2/govc/library/trust/info.go (about)

     1  /*
     2  Copyright (c) 2022-2022 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 trust
    18  
    19  import (
    20  	"context"
    21  	"crypto/x509"
    22  	"encoding/pem"
    23  	"flag"
    24  	"io"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/object"
    29  	"github.com/vmware/govmomi/vapi/library"
    30  )
    31  
    32  type info struct {
    33  	*flags.ClientFlag
    34  	*flags.OutputFlag
    35  }
    36  
    37  func init() {
    38  	cli.Register("library.trust.info", &info{})
    39  }
    40  
    41  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    43  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    44  	cmd.ClientFlag.Register(ctx, f)
    45  	cmd.OutputFlag.Register(ctx, f)
    46  }
    47  
    48  func (cmd *info) Process(ctx context.Context) error {
    49  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  func (cmd *info) Usage() string {
    56  	return "ID"
    57  }
    58  
    59  func (cmd *info) Description() string {
    60  	return `Display trusted certificate info.
    61  
    62  Examples:
    63    govc library.trust.info vmware_signed`
    64  }
    65  
    66  type infoResultsWriter struct {
    67  	TrustedCertificateInfo *library.TrustedCertificate `json:"info,omitempty"`
    68  }
    69  
    70  func (r infoResultsWriter) Write(w io.Writer) error {
    71  	block, _ := pem.Decode([]byte(r.TrustedCertificateInfo.Text))
    72  	x, err := x509.ParseCertificate(block.Bytes)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	var info object.HostCertificateInfo
    78  	info.FromCertificate(x)
    79  
    80  	return info.Write(w)
    81  }
    82  
    83  func (r infoResultsWriter) Dump() interface{} {
    84  	return r.TrustedCertificateInfo
    85  }
    86  
    87  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    88  	if f.NArg() != 1 {
    89  		return flag.ErrHelp
    90  	}
    91  
    92  	c, err := cmd.RestClient()
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	cert, err := library.NewManager(c).GetTrustedCertificate(ctx, f.Arg(0))
    98  	if err != nil {
    99  		return err
   100  	}
   101  	return cmd.WriteResult(&infoResultsWriter{cert})
   102  }