sslcertificate.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package huawei
  2. import (
  3. "bytes"
  4. "crypto/sha1"
  5. "crypto/x509"
  6. "encoding/pem"
  7. "fmt"
  8. "net/url"
  9. "time"
  10. "yunion.io/x/cloudmux/pkg/apis"
  11. "yunion.io/x/cloudmux/pkg/multicloud"
  12. "yunion.io/x/pkg/errors"
  13. )
  14. type SSSLCertificate struct {
  15. multicloud.SCertificateBase
  16. HuaweiTags
  17. client *SHuaweiClient
  18. Id string // 证书ID
  19. Name string // 证书名称
  20. Domain string // 证书绑定的域名
  21. Sans string // 证书的SAN(Subject Alternative Name)扩展属性,表示证书关联的其他域名、IP地址等
  22. SignatureAlgorithm string // 证书签名算法
  23. DeploySupport bool // 是否支持部署
  24. Type string // 证书类型 取值如下: DV_SSL_CERT、DV_SSL_CERT_BASIC、EV_SSL_CERT、EV_SSL_CERT_PRO、OV_SSL_CERT、OV_SSL_CERT_PRO
  25. Brand string // 证书品牌 取值如下:GLOBALSIGN、SYMANTEC、GEOTRUST、CFCA
  26. //ExpireTime time.Time // 证书过期时间
  27. ExpireTime string // 证书过期时间
  28. DomainType string // 域名类型。取值如下: SINGLE_DOMAIN:单域名 WILDCARD:通配符 MULTI_DOMAIN:多域名
  29. ValidityPeriod int // 证书有效期,单位为月
  30. Status string // 证书状态。取值如下: PAID:证书已支付;待申请证书 ISSUED:证书已签发 CHECKING:证书申请审核中 CANCELCHECKING:取消证书申请审核中 UNPASSED:证书申请未通过 EXPIRED:证书已过期 REVOKING:证书吊销申请审核中 CANCLEREVOKING:证书取消吊销申请审核中 REVOKED:证书已吊销 UPLOAD:证书托管中 SUPPLEMENTCHECKING:多域名证书新增附加域名审核中 CANCELSUPPLEMENTING:取消新增附加域名审核中
  31. DomainCount int // 证书绑定的域名数量
  32. WildcardCount int // 证书绑定的通配符域名数量
  33. Description string // 证书描述
  34. EnterpriseProjectId string // 企业项目ID 默认为“0”
  35. // certificate details
  36. detailsInitd bool
  37. Certificate string `json:"certificate"` // 证书内容
  38. PrivateKey string `json:"private_key"` // 证书私钥
  39. }
  40. func (s *SSSLCertificate) GetSans() string {
  41. return s.Sans
  42. }
  43. func (s *SSSLCertificate) GetStartDate() time.Time {
  44. return s.GetEndDate().AddDate(0, -s.ValidityPeriod, 0)
  45. }
  46. func (s *SSSLCertificate) GetProvince() string {
  47. return ""
  48. }
  49. func (s *SSSLCertificate) GetCommon() string {
  50. return s.Domain
  51. }
  52. func (s *SSSLCertificate) GetCountry() string {
  53. return ""
  54. }
  55. func (s *SSSLCertificate) GetIssuer() string {
  56. return s.Brand
  57. }
  58. func (s *SSSLCertificate) GetEndDate() time.Time {
  59. t, _ := time.Parse("2006-01-02 15:04:05", s.ExpireTime)
  60. return t
  61. }
  62. func (s *SSSLCertificate) GetFingerprint() string {
  63. var buf bytes.Buffer
  64. s.GetDetails()
  65. certBlock, _ := pem.Decode([]byte(s.Certificate))
  66. if certBlock == nil {
  67. return ""
  68. }
  69. cert, _ := x509.ParseCertificate(certBlock.Bytes)
  70. fingerprint := sha1.Sum(cert.Raw)
  71. for _, f := range fingerprint {
  72. fmt.Fprintf(&buf, "%02X", f)
  73. }
  74. return buf.String()
  75. }
  76. func (s *SSSLCertificate) GetCity() string {
  77. return ""
  78. }
  79. func (s *SSSLCertificate) GetOrgName() string {
  80. return ""
  81. }
  82. func (s *SSSLCertificate) GetId() string {
  83. return s.Id
  84. }
  85. func (s *SSSLCertificate) GetName() string {
  86. return s.Name
  87. }
  88. func (s *SSSLCertificate) GetGlobalId() string {
  89. return s.Id
  90. }
  91. func (s *SSSLCertificate) GetStatus() string {
  92. return apis.STATUS_AVAILABLE
  93. }
  94. func (s *SSSLCertificate) GetIsUpload() bool {
  95. if s.Status == "UPLOAD" {
  96. return true
  97. }
  98. return false
  99. }
  100. func (s *SSSLCertificate) GetCert() string {
  101. s.GetDetails()
  102. return s.Certificate
  103. }
  104. func (s *SSSLCertificate) GetKey() string {
  105. s.GetDetails()
  106. return s.PrivateKey
  107. }
  108. func (s *SSSLCertificate) GetDetails() (*SSSLCertificate, error) {
  109. if !s.detailsInitd {
  110. cert, err := s.client.GetSSLCertificate(s.GetId())
  111. if err != nil {
  112. return nil, err
  113. }
  114. s.detailsInitd = true
  115. s.Certificate = cert.Certificate
  116. s.PrivateKey = cert.PrivateKey
  117. }
  118. return s, nil
  119. }
  120. func (r *SHuaweiClient) GetSSLCertificates() ([]SSSLCertificate, error) {
  121. params := url.Values{}
  122. params.Set("sort_key", "certExpiredTime")
  123. params.Set("sort_dir", "DESC")
  124. ret := make([]SSSLCertificate, 0)
  125. for {
  126. resp, err := r.list(SERVICE_SCM, "", "scm/certificates", params)
  127. if err != nil {
  128. return nil, errors.Wrapf(err, "list certificates")
  129. }
  130. part := struct {
  131. Certificates []SSSLCertificate
  132. TotalCount int
  133. }{}
  134. err = resp.Unmarshal(&part)
  135. if err != nil {
  136. return nil, err
  137. }
  138. ret = append(ret, part.Certificates...)
  139. if len(ret) >= part.TotalCount || len(part.Certificates) == 0 {
  140. break
  141. }
  142. params.Set("offset", fmt.Sprintf("%d", len(ret)))
  143. }
  144. return ret, nil
  145. }
  146. func (r *SHuaweiClient) GetSSLCertificate(certId string) (*SSSLCertificate, error) {
  147. resource := fmt.Sprintf("scm/certificates/%s/export", certId)
  148. resp, err := r.post(SERVICE_SCM, "", resource, nil)
  149. if err != nil {
  150. return nil, errors.Wrap(err, "DescribeCertificateDetail")
  151. }
  152. cert := &SSSLCertificate{}
  153. err = resp.Unmarshal(cert)
  154. if err != nil {
  155. return nil, errors.Wrap(err, "Unmarshal")
  156. }
  157. cert.client = r
  158. return cert, nil
  159. }