github.com/vmware/govmomi@v0.43.0/simulator/host_certificate_manager.go (about) 1 /* 2 Copyright (c) 2024-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 simulator 18 19 import ( 20 "bytes" 21 "crypto/rand" 22 "crypto/rsa" 23 "crypto/x509" 24 "encoding/pem" 25 "net" 26 27 "github.com/vmware/govmomi/object" 28 "github.com/vmware/govmomi/vim25/methods" 29 "github.com/vmware/govmomi/vim25/mo" 30 "github.com/vmware/govmomi/vim25/soap" 31 "github.com/vmware/govmomi/vim25/types" 32 ) 33 34 type HostCertificateManager struct { 35 mo.HostCertificateManager 36 37 Host *mo.HostSystem 38 } 39 40 func (m *HostCertificateManager) init(r *Registry) { 41 for _, obj := range r.objects { 42 if h, ok := obj.(*HostSystem); ok { 43 if h.ConfigManager.CertificateManager.Value == m.Self.Value { 44 m.Host = &h.HostSystem 45 } 46 } 47 } 48 } 49 50 func NewHostCertificateManager(h *mo.HostSystem) *HostCertificateManager { 51 m := &HostCertificateManager{Host: h} 52 53 _ = m.InstallServerCertificate(SpoofContext(), &types.InstallServerCertificate{ 54 Cert: string(m.Host.Config.Certificate), 55 }) 56 57 return m 58 } 59 60 func (m *HostCertificateManager) InstallServerCertificate(ctx *Context, req *types.InstallServerCertificate) soap.HasFault { 61 body := new(methods.InstallServerCertificateBody) 62 63 var info object.HostCertificateInfo 64 cert := []byte(req.Cert) 65 _, err := info.FromPEM(cert) 66 if err != nil { 67 body.Fault_ = Fault(err.Error(), new(types.HostConfigFault)) 68 return body 69 } 70 71 m.CertificateInfo = info.HostCertificateManagerCertificateInfo 72 73 m.Host.Config.Certificate = cert 74 75 body.Res = new(types.InstallServerCertificateResponse) 76 77 return body 78 } 79 80 func (m *HostCertificateManager) GenerateCertificateSigningRequest(ctx *Context, req *types.GenerateCertificateSigningRequest) soap.HasFault { 81 block, _ := pem.Decode(m.Host.Config.Certificate) 82 cert, err := x509.ParseCertificate(block.Bytes) 83 if err != nil { 84 panic(err) 85 } 86 87 csr := x509.CertificateRequest{ 88 Subject: cert.Subject, 89 SignatureAlgorithm: x509.SHA256WithRSA, 90 } 91 92 if req.UseIpAddressAsCommonName { 93 csr.IPAddresses = []net.IP{net.ParseIP(m.Host.Summary.ManagementServerIp)} 94 } else { 95 csr.DNSNames = []string{m.Host.Name} 96 } 97 98 key, _ := rsa.GenerateKey(rand.Reader, 2048) 99 der, _ := x509.CreateCertificateRequest(rand.Reader, &csr, key) 100 var buf bytes.Buffer 101 err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: der}) 102 if err != nil { 103 panic(err) 104 } 105 106 return &methods.GenerateCertificateSigningRequestBody{ 107 Res: &types.GenerateCertificateSigningRequestResponse{ 108 Returnval: buf.String(), 109 }, 110 } 111 }