loadbalancercert.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. package azure
  15. import (
  16. "bytes"
  17. "crypto/sha1"
  18. "crypto/x509"
  19. "encoding/pem"
  20. "fmt"
  21. "net/url"
  22. "strings"
  23. "time"
  24. "yunion.io/x/cloudmux/pkg/apis"
  25. "yunion.io/x/cloudmux/pkg/cloudprovider"
  26. "yunion.io/x/cloudmux/pkg/multicloud"
  27. "yunion.io/x/pkg/errors"
  28. )
  29. type SLoadbalancerCert struct {
  30. multicloud.SResourceBase
  31. AzureTags
  32. region *SRegion
  33. Name string `json:"name"`
  34. Id string `json:"id"`
  35. Properties struct {
  36. PublicCertData string
  37. HttpListeners []struct {
  38. Id string
  39. }
  40. }
  41. }
  42. func (self *SLoadbalancerCert) GetId() string {
  43. return self.Id
  44. }
  45. func (self *SLoadbalancerCert) GetName() string {
  46. return self.Name
  47. }
  48. func (self *SLoadbalancerCert) GetGlobalId() string {
  49. return strings.ToLower(self.GetId())
  50. }
  51. func (self *SLoadbalancerCert) GetStatus() string {
  52. return apis.STATUS_AVAILABLE
  53. }
  54. func (self *SLoadbalancerCert) GetProjectId() string {
  55. return getResourceGroup(self.GetId())
  56. }
  57. func (self *SLoadbalancerCert) Delete() error {
  58. return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete")
  59. }
  60. func (self *SLoadbalancerCert) ParsePublicKey() (*x509.Certificate, error) {
  61. block, _ := pem.Decode([]byte(self.GetPublickKey()))
  62. cert, err := x509.ParseCertificate(block.Bytes)
  63. if err != nil {
  64. return nil, errors.Wrap(err, "ParseCertificate")
  65. }
  66. return cert, nil
  67. }
  68. func (self *SLoadbalancerCert) GetCommonName() string {
  69. cert, err := self.ParsePublicKey()
  70. if err != nil {
  71. return ""
  72. }
  73. return cert.Issuer.CommonName
  74. }
  75. func (self *SLoadbalancerCert) GetSubjectAlternativeNames() string {
  76. _, err := self.ParsePublicKey()
  77. if err != nil {
  78. return ""
  79. }
  80. return ""
  81. }
  82. func (self *SLoadbalancerCert) GetFingerprint() string {
  83. publicKey := self.GetPublickKey()
  84. if len(publicKey) == 0 {
  85. return ""
  86. }
  87. _fp := sha1.Sum([]byte(publicKey))
  88. fp := fmt.Sprintf("sha1:% x", _fp)
  89. return strings.Replace(fp, " ", ":", -1)
  90. }
  91. func (self *SLoadbalancerCert) GetExpireTime() time.Time {
  92. cert, err := self.ParsePublicKey()
  93. if err != nil {
  94. return time.Time{}
  95. }
  96. return cert.NotAfter
  97. }
  98. func (self *SLoadbalancerCert) GetPublickKey() string {
  99. if len(self.Properties.PublicCertData) > 0 {
  100. var pk bytes.Buffer
  101. pk.WriteString("-----BEGIN CERTIFICATE-----\r\n")
  102. content := bytes.NewBufferString(self.Properties.PublicCertData)
  103. for {
  104. l := content.Next(64)
  105. if len(l) == 64 {
  106. pk.WriteString(fmt.Sprintf("%s\r\n", l))
  107. } else {
  108. pk.WriteString(fmt.Sprintf("%s\r\n", l))
  109. break
  110. }
  111. }
  112. pk.WriteString("-----END CERTIFICATE-----")
  113. return pk.String()
  114. }
  115. return ""
  116. }
  117. func (self *SLoadbalancerCert) GetPrivateKey() string {
  118. return ""
  119. }
  120. func (self *SRegion) GetLoadbalancerCertificates() ([]SLoadbalancerCert, error) {
  121. params := url.Values{}
  122. resp, err := self.list_resources("Microsoft.Network/applicationGateways", "2023-09-01", params)
  123. if err != nil {
  124. return nil, err
  125. }
  126. ret := []SLoadbalancer{}
  127. err = resp.Unmarshal(&ret, "value")
  128. if err != nil {
  129. return nil, err
  130. }
  131. result := []SLoadbalancerCert{}
  132. for i := range ret {
  133. if ret[i].Location != self.Name {
  134. continue
  135. }
  136. result = append(result, ret[i].Properties.SSLCertificates...)
  137. }
  138. return result, nil
  139. }