github.com/vmware/govmomi@v0.43.0/object/host_certificate_manager.go (about)

     1  /*
     2  Copyright (c) 2016-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 object
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/vmware/govmomi/property"
    23  	"github.com/vmware/govmomi/vim25"
    24  	"github.com/vmware/govmomi/vim25/methods"
    25  	"github.com/vmware/govmomi/vim25/mo"
    26  	"github.com/vmware/govmomi/vim25/soap"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  // HostCertificateManager provides helper methods around the HostSystem.ConfigManager.CertificateManager
    31  type HostCertificateManager struct {
    32  	Common
    33  	Host *HostSystem
    34  }
    35  
    36  // NewHostCertificateManager creates a new HostCertificateManager helper
    37  func NewHostCertificateManager(c *vim25.Client, ref types.ManagedObjectReference, host types.ManagedObjectReference) *HostCertificateManager {
    38  	return &HostCertificateManager{
    39  		Common: NewCommon(c, ref),
    40  		Host:   NewHostSystem(c, host),
    41  	}
    42  }
    43  
    44  // CertificateInfo wraps the host CertificateManager certificateInfo property with the HostCertificateInfo helper.
    45  // The ThumbprintSHA1 field is set to HostSystem.Summary.Config.SslThumbprint if the host system is managed by a vCenter.
    46  func (m HostCertificateManager) CertificateInfo(ctx context.Context) (*HostCertificateInfo, error) {
    47  	var hs mo.HostSystem
    48  	var cm mo.HostCertificateManager
    49  
    50  	pc := property.DefaultCollector(m.Client())
    51  
    52  	err := pc.RetrieveOne(ctx, m.Reference(), []string{"certificateInfo"}, &cm)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	_ = pc.RetrieveOne(ctx, m.Host.Reference(), []string{"summary.config.sslThumbprint"}, &hs)
    58  
    59  	return &HostCertificateInfo{
    60  		HostCertificateManagerCertificateInfo: cm.CertificateInfo,
    61  		ThumbprintSHA1:                        hs.Summary.Config.SslThumbprint,
    62  	}, nil
    63  }
    64  
    65  // GenerateCertificateSigningRequest requests the host system to generate a certificate-signing request (CSR) for itself.
    66  // The CSR is then typically provided to a Certificate Authority to sign and issue the SSL certificate for the host system.
    67  // Use InstallServerCertificate to import this certificate.
    68  func (m HostCertificateManager) GenerateCertificateSigningRequest(ctx context.Context, useIPAddressAsCommonName bool) (string, error) {
    69  	req := types.GenerateCertificateSigningRequest{
    70  		This:                     m.Reference(),
    71  		UseIpAddressAsCommonName: useIPAddressAsCommonName,
    72  	}
    73  
    74  	res, err := methods.GenerateCertificateSigningRequest(ctx, m.Client(), &req)
    75  	if err != nil {
    76  		return "", err
    77  	}
    78  
    79  	return res.Returnval, nil
    80  }
    81  
    82  // GenerateCertificateSigningRequestByDn requests the host system to generate a certificate-signing request (CSR) for itself.
    83  // Alternative version similar to GenerateCertificateSigningRequest but takes a Distinguished Name (DN) as a parameter.
    84  func (m HostCertificateManager) GenerateCertificateSigningRequestByDn(ctx context.Context, distinguishedName string) (string, error) {
    85  	req := types.GenerateCertificateSigningRequestByDn{
    86  		This:              m.Reference(),
    87  		DistinguishedName: distinguishedName,
    88  	}
    89  
    90  	res, err := methods.GenerateCertificateSigningRequestByDn(ctx, m.Client(), &req)
    91  	if err != nil {
    92  		return "", err
    93  	}
    94  
    95  	return res.Returnval, nil
    96  }
    97  
    98  // InstallServerCertificate imports the given SSL certificate to the host system.
    99  func (m HostCertificateManager) InstallServerCertificate(ctx context.Context, cert string) error {
   100  	req := types.InstallServerCertificate{
   101  		This: m.Reference(),
   102  		Cert: cert,
   103  	}
   104  
   105  	_, err := methods.InstallServerCertificate(ctx, m.Client(), &req)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	// NotifyAffectedService is internal, not exposing as we don't have a use case other than with InstallServerCertificate
   111  	// Without this call, hostd needs to be restarted to use the updated certificate
   112  	// Note: using Refresh as it has the same struct/signature, we just need to use different xml name tags
   113  	body := struct {
   114  		Req *types.Refresh         `xml:"urn:vim25 NotifyAffectedServices,omitempty"`
   115  		Res *types.RefreshResponse `xml:"urn:vim25 NotifyAffectedServicesResponse,omitempty"`
   116  		methods.RefreshBody
   117  	}{
   118  		Req: &types.Refresh{This: m.Reference()},
   119  	}
   120  
   121  	err = m.Client().RoundTrip(ctx, &body, &body)
   122  	if err != nil && soap.IsSoapFault(err) {
   123  		if _, ok := soap.ToSoapFault(err).VimFault().(types.MethodNotFound); ok {
   124  			return nil
   125  		}
   126  	}
   127  	return err
   128  }
   129  
   130  // ListCACertificateRevocationLists returns the SSL CRLs of Certificate Authorities that are trusted by the host system.
   131  func (m HostCertificateManager) ListCACertificateRevocationLists(ctx context.Context) ([]string, error) {
   132  	req := types.ListCACertificateRevocationLists{
   133  		This: m.Reference(),
   134  	}
   135  
   136  	res, err := methods.ListCACertificateRevocationLists(ctx, m.Client(), &req)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	return res.Returnval, nil
   142  }
   143  
   144  // ListCACertificates returns the SSL certificates of Certificate Authorities that are trusted by the host system.
   145  func (m HostCertificateManager) ListCACertificates(ctx context.Context) ([]string, error) {
   146  	req := types.ListCACertificates{
   147  		This: m.Reference(),
   148  	}
   149  
   150  	res, err := methods.ListCACertificates(ctx, m.Client(), &req)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	return res.Returnval, nil
   156  }
   157  
   158  // ReplaceCACertificatesAndCRLs replaces the trusted CA certificates and CRL used by the host system.
   159  // These determine whether the server can verify the identity of an external entity.
   160  func (m HostCertificateManager) ReplaceCACertificatesAndCRLs(ctx context.Context, caCert []string, caCrl []string) error {
   161  	req := types.ReplaceCACertificatesAndCRLs{
   162  		This:   m.Reference(),
   163  		CaCert: caCert,
   164  		CaCrl:  caCrl,
   165  	}
   166  
   167  	_, err := methods.ReplaceCACertificatesAndCRLs(ctx, m.Client(), &req)
   168  	return err
   169  }