yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/loadbalancercert.go (about) 1 // Copyright 2019 Yunion 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azure 16 17 import ( 18 "bytes" 19 "crypto/sha1" 20 "crypto/x509" 21 "encoding/pem" 22 "fmt" 23 "strings" 24 "time" 25 26 "github.com/pkg/errors" 27 28 "yunion.io/x/jsonutils" 29 30 api "yunion.io/x/cloudmux/pkg/apis/compute" 31 "yunion.io/x/cloudmux/pkg/cloudprovider" 32 "yunion.io/x/cloudmux/pkg/multicloud" 33 ) 34 35 type SLoadbalancerCert struct { 36 multicloud.SResourceBase 37 lb *SLoadbalancer 38 cert *x509.Certificate 39 Name string `json:"name"` 40 ID string `json:"id"` 41 PublicKey string `json:"public_key"` 42 } 43 44 func (self *SLoadbalancerCert) GetId() string { 45 return self.ID 46 } 47 48 func (self *SLoadbalancerCert) GetName() string { 49 return self.Name 50 } 51 52 func (self *SLoadbalancerCert) GetGlobalId() string { 53 return strings.ToLower(self.GetId()) 54 } 55 56 func (self *SLoadbalancerCert) GetStatus() string { 57 return api.LB_STATUS_ENABLED 58 } 59 60 func (self *SLoadbalancerCert) Refresh() error { 61 cert, err := self.lb.GetILoadBalancerCertificateById(self.GetId()) 62 if err != nil { 63 return errors.Wrap(err, "GetILoadBalancerCertificateById") 64 } 65 66 err = jsonutils.Update(self, cert) 67 if err != nil { 68 return errors.Wrap(err, "Update") 69 } 70 71 return nil 72 } 73 74 func (self *SLoadbalancerCert) IsEmulated() bool { 75 return false 76 } 77 78 func (self *SLoadbalancerCert) GetSysTags() map[string]string { 79 return nil 80 } 81 82 func (self *SLoadbalancerCert) GetTags() (map[string]string, error) { 83 return nil, nil 84 } 85 86 func (self *SLoadbalancerCert) SetTags(tags map[string]string, replace bool) error { 87 return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") 88 } 89 90 func (self *SLoadbalancerCert) GetProjectId() string { 91 return getResourceGroup(self.GetId()) 92 } 93 94 func (self *SLoadbalancerCert) Sync(name, privateKey, publickKey string) error { 95 return errors.Wrap(cloudprovider.ErrNotImplemented, "Sync") 96 } 97 98 func (self *SLoadbalancerCert) Delete() error { 99 return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete") 100 } 101 102 func (self *SLoadbalancerCert) ParsePublicKey() (*x509.Certificate, error) { 103 if self.cert != nil { 104 return self.cert, nil 105 } 106 107 publicKey := self.GetPublickKey() 108 if len(publicKey) == 0 { 109 return nil, fmt.Errorf("SElbCertificate ParsePublicKey public key is empty") 110 } 111 112 block, _ := pem.Decode([]byte(publicKey)) 113 cert, err := x509.ParseCertificate(block.Bytes) 114 if err != nil { 115 return nil, errors.Wrap(err, "ParseCertificate") 116 } 117 118 self.cert = cert 119 return cert, nil 120 } 121 122 func (self *SLoadbalancerCert) GetCommonName() string { 123 cert, err := self.ParsePublicKey() 124 if err != nil { 125 return "" 126 } 127 128 return cert.Issuer.CommonName 129 } 130 131 func (self *SLoadbalancerCert) GetSubjectAlternativeNames() string { 132 _, err := self.ParsePublicKey() 133 if err != nil { 134 return "" 135 } 136 137 return "" 138 } 139 140 func (self *SLoadbalancerCert) GetFingerprint() string { 141 publicKey := self.GetPublickKey() 142 if len(publicKey) == 0 { 143 return "" 144 } 145 146 _fp := sha1.Sum([]byte(publicKey)) 147 fp := fmt.Sprintf("sha1:% x", _fp) 148 return strings.Replace(fp, " ", ":", -1) 149 } 150 151 func (self *SLoadbalancerCert) GetExpireTime() time.Time { 152 cert, err := self.ParsePublicKey() 153 if err != nil { 154 return time.Time{} 155 } 156 157 return cert.NotAfter 158 } 159 160 func (self *SLoadbalancerCert) GetPublickKey() string { 161 if len(self.PublicKey) > 0 { 162 var pk bytes.Buffer 163 pk.WriteString("-----BEGIN CERTIFICATE-----\r\n") 164 content := bytes.NewBufferString(self.PublicKey) 165 for { 166 l := content.Next(64) 167 if len(l) == 64 { 168 pk.WriteString(fmt.Sprintf("%s\r\n", l)) 169 } else { 170 pk.WriteString(fmt.Sprintf("%s\r\n", l)) 171 break 172 } 173 } 174 pk.WriteString("-----END CERTIFICATE-----") 175 return pk.String() 176 } 177 178 return "" 179 } 180 181 func (self *SLoadbalancerCert) GetPrivateKey() string { 182 return "" 183 }