github.com/vmware/govmomi@v0.37.2/govc/library/trust/ls.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  	"fmt"
    25  	"io"
    26  	"text/tabwriter"
    27  
    28  	"github.com/vmware/govmomi/govc/cli"
    29  	"github.com/vmware/govmomi/govc/flags"
    30  	"github.com/vmware/govmomi/vapi/library"
    31  )
    32  
    33  type ls struct {
    34  	*flags.ClientFlag
    35  	*flags.OutputFlag
    36  }
    37  
    38  func init() {
    39  	cli.Register("library.trust.ls", &ls{})
    40  }
    41  
    42  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    44  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  	cmd.OutputFlag.Register(ctx, f)
    47  }
    48  
    49  func (cmd *ls) Process(ctx context.Context) error {
    50  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }
    55  
    56  func (cmd *ls) Description() string {
    57  	return `List trusted certificates for content libraries.
    58  
    59  Examples:
    60    govc library.trust.ls
    61    govc library.trust.ls -json`
    62  }
    63  
    64  type lsResultsWriter struct {
    65  	TrustedCertificates []library.TrustedCertificateSummary `json:"certificates,omitempty"`
    66  }
    67  
    68  func (r lsResultsWriter) Write(w io.Writer) error {
    69  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    70  
    71  	for _, cert := range r.TrustedCertificates {
    72  		block, _ := pem.Decode([]byte(cert.Text))
    73  		x, err := x509.ParseCertificate(block.Bytes)
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  		x.Subject.Names = nil // trim x.Subject.String() output
    79  
    80  		fmt.Fprintf(tw, "%s\t%s\n", cert.ID, x.Subject)
    81  	}
    82  
    83  	return tw.Flush()
    84  }
    85  
    86  func (r lsResultsWriter) Dump() interface{} {
    87  	return r.TrustedCertificates
    88  }
    89  
    90  func (cmd *ls) Run(ctx context.Context, _ *flag.FlagSet) error {
    91  	c, err := cmd.RestClient()
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	certs, err := library.NewManager(c).ListTrustedCertificates(ctx)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	return cmd.WriteResult(&lsResultsWriter{certs})
   102  }