host_certificate_manager.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package object
  14. import (
  15. "context"
  16. "github.com/vmware/govmomi/property"
  17. "github.com/vmware/govmomi/vim25"
  18. "github.com/vmware/govmomi/vim25/methods"
  19. "github.com/vmware/govmomi/vim25/mo"
  20. "github.com/vmware/govmomi/vim25/types"
  21. )
  22. // HostCertificateManager provides helper methods around the HostSystem.ConfigManager.CertificateManager
  23. type HostCertificateManager struct {
  24. Common
  25. Host *HostSystem
  26. }
  27. // NewHostCertificateManager creates a new HostCertificateManager helper
  28. func NewHostCertificateManager(c *vim25.Client, ref types.ManagedObjectReference, host types.ManagedObjectReference) *HostCertificateManager {
  29. return &HostCertificateManager{
  30. Common: NewCommon(c, ref),
  31. Host: NewHostSystem(c, host),
  32. }
  33. }
  34. // CertificateInfo wraps the host CertificateManager certificateInfo property with the HostCertificateInfo helper.
  35. // The ThumbprintSHA1 field is set to HostSystem.Summary.Config.SslThumbprint if the host system is managed by a vCenter.
  36. func (m HostCertificateManager) CertificateInfo(ctx context.Context) (*HostCertificateInfo, error) {
  37. var hs mo.HostSystem
  38. var cm mo.HostCertificateManager
  39. pc := property.DefaultCollector(m.Client())
  40. err := pc.RetrieveOne(ctx, m.Reference(), []string{"certificateInfo"}, &cm)
  41. if err != nil {
  42. return nil, err
  43. }
  44. _ = pc.RetrieveOne(ctx, m.Host.Reference(), []string{"summary.config.sslThumbprint"}, &hs)
  45. return &HostCertificateInfo{
  46. HostCertificateManagerCertificateInfo: cm.CertificateInfo,
  47. ThumbprintSHA1: hs.Summary.Config.SslThumbprint,
  48. }, nil
  49. }
  50. // GenerateCertificateSigningRequest requests the host system to generate a certificate-signing request (CSR) for itself.
  51. // The CSR is then typically provided to a Certificate Authority to sign and issue the SSL certificate for the host system.
  52. // Use InstallServerCertificate to import this certificate.
  53. func (m HostCertificateManager) GenerateCertificateSigningRequest(ctx context.Context, useIPAddressAsCommonName bool) (string, error) {
  54. req := types.GenerateCertificateSigningRequest{
  55. This: m.Reference(),
  56. UseIpAddressAsCommonName: useIPAddressAsCommonName,
  57. }
  58. res, err := methods.GenerateCertificateSigningRequest(ctx, m.Client(), &req)
  59. if err != nil {
  60. return "", err
  61. }
  62. return res.Returnval, nil
  63. }
  64. // GenerateCertificateSigningRequestByDn requests the host system to generate a certificate-signing request (CSR) for itself.
  65. // Alternative version similar to GenerateCertificateSigningRequest but takes a Distinguished Name (DN) as a parameter.
  66. func (m HostCertificateManager) GenerateCertificateSigningRequestByDn(ctx context.Context, distinguishedName string) (string, error) {
  67. req := types.GenerateCertificateSigningRequestByDn{
  68. This: m.Reference(),
  69. DistinguishedName: distinguishedName,
  70. }
  71. res, err := methods.GenerateCertificateSigningRequestByDn(ctx, m.Client(), &req)
  72. if err != nil {
  73. return "", err
  74. }
  75. return res.Returnval, nil
  76. }
  77. // InstallServerCertificate imports the given SSL certificate to the host system.
  78. func (m HostCertificateManager) InstallServerCertificate(ctx context.Context, cert string) error {
  79. req := types.InstallServerCertificate{
  80. This: m.Reference(),
  81. Cert: cert,
  82. }
  83. _, err := methods.InstallServerCertificate(ctx, m.Client(), &req)
  84. if err != nil {
  85. return err
  86. }
  87. // NotifyAffectedService is internal, not exposing as we don't have a use case other than with InstallServerCertificate
  88. // Without this call, hostd needs to be restarted to use the updated certificate
  89. // Note: using Refresh as it has the same struct/signature, we just need to use different xml name tags
  90. body := struct {
  91. Req *types.Refresh `xml:"urn:vim25 NotifyAffectedServices,omitempty"`
  92. Res *types.RefreshResponse `xml:"urn:vim25 NotifyAffectedServicesResponse,omitempty"`
  93. methods.RefreshBody
  94. }{
  95. Req: &types.Refresh{This: m.Reference()},
  96. }
  97. return m.Client().RoundTrip(ctx, &body, &body)
  98. }
  99. // ListCACertificateRevocationLists returns the SSL CRLs of Certificate Authorities that are trusted by the host system.
  100. func (m HostCertificateManager) ListCACertificateRevocationLists(ctx context.Context) ([]string, error) {
  101. req := types.ListCACertificateRevocationLists{
  102. This: m.Reference(),
  103. }
  104. res, err := methods.ListCACertificateRevocationLists(ctx, m.Client(), &req)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return res.Returnval, nil
  109. }
  110. // ListCACertificates returns the SSL certificates of Certificate Authorities that are trusted by the host system.
  111. func (m HostCertificateManager) ListCACertificates(ctx context.Context) ([]string, error) {
  112. req := types.ListCACertificates{
  113. This: m.Reference(),
  114. }
  115. res, err := methods.ListCACertificates(ctx, m.Client(), &req)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return res.Returnval, nil
  120. }
  121. // ReplaceCACertificatesAndCRLs replaces the trusted CA certificates and CRL used by the host system.
  122. // These determine whether the server can verify the identity of an external entity.
  123. func (m HostCertificateManager) ReplaceCACertificatesAndCRLs(ctx context.Context, caCert []string, caCrl []string) error {
  124. req := types.ReplaceCACertificatesAndCRLs{
  125. This: m.Reference(),
  126. CaCert: caCert,
  127. CaCrl: caCrl,
  128. }
  129. _, err := methods.ReplaceCACertificatesAndCRLs(ctx, m.Client(), &req)
  130. return err
  131. }