sslcertificate.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package aliyun
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "yunion.io/x/cloudmux/pkg/multicloud"
  7. "yunion.io/x/pkg/errors"
  8. )
  9. type SSSLCertificate struct {
  10. multicloud.SCertificateBase
  11. AliyunTags
  12. client *SAliyunClient
  13. Sans string // 证书的SAN(Subject Alternative Name)扩展属性,表示证书关联的其他域名、IP地址等
  14. Id int // 证书ID
  15. StartDate time.Time // 证书签发日期
  16. Province string // 购买证书的用户所属的公司或组织所在的省
  17. Common string // 证书绑定的主域名
  18. Country string // 购买证书的用户所属的公司或组织所在的国家或地区
  19. Issuer string // 证书颁发机构
  20. BuyInAliyun bool // 是否在阿里云购买了证书
  21. Expired bool // 证书是否过期
  22. EndDate time.Time // 证书到期日期
  23. Name string // 证书名称
  24. Fingerprint string // 证书名称
  25. City string // 购买证书的用户所属的公司或组织所在的城市
  26. OrgName string // 购买证书的用户所属的公司或组织的名称
  27. // certificate details
  28. detailsInitd bool
  29. Cert string `json:"Cert"` // 证书内容
  30. Key string `json:"Key"` // 证书私钥
  31. }
  32. func (s *SSSLCertificate) GetSans() string {
  33. return s.Sans
  34. }
  35. func (s *SSSLCertificate) GetStartDate() time.Time {
  36. return s.StartDate
  37. }
  38. func (s *SSSLCertificate) GetProvince() string {
  39. return s.Province
  40. }
  41. func (s *SSSLCertificate) GetCommon() string {
  42. return s.Common
  43. }
  44. func (s *SSSLCertificate) GetCountry() string {
  45. return s.Country
  46. }
  47. func (s *SSSLCertificate) GetIssuer() string {
  48. return s.Issuer
  49. }
  50. func (s *SSSLCertificate) GetEndDate() time.Time {
  51. return s.EndDate
  52. }
  53. func (s *SSSLCertificate) GetFingerprint() string {
  54. return s.Fingerprint
  55. }
  56. func (s *SSSLCertificate) GetCity() string {
  57. return s.City
  58. }
  59. func (s *SSSLCertificate) GetOrgName() string {
  60. return s.OrgName
  61. }
  62. func (s *SSSLCertificate) GetId() string {
  63. return strconv.Itoa(s.Id)
  64. }
  65. func (s *SSSLCertificate) GetName() string {
  66. return s.Name
  67. }
  68. func (s *SSSLCertificate) GetGlobalId() string {
  69. return strconv.Itoa(s.Id)
  70. }
  71. func (s *SSSLCertificate) GetStatus() string {
  72. if s.Expired {
  73. return "expired"
  74. } else {
  75. return "normal"
  76. }
  77. }
  78. func (s *SSSLCertificate) GetIsUpload() bool {
  79. return false
  80. }
  81. func (s *SSSLCertificate) GetCert() string {
  82. s.GetDetails()
  83. return s.Cert
  84. }
  85. func (s *SSSLCertificate) GetKey() string {
  86. s.GetDetails()
  87. return s.Key
  88. }
  89. func (s *SSSLCertificate) GetDetails() (*SSSLCertificate, error) {
  90. if !s.detailsInitd {
  91. cert, err := s.client.GetISSLCertificate(s.GetId())
  92. if err != nil {
  93. return nil, err
  94. }
  95. s.detailsInitd = true
  96. _cert, ok := cert.(*SSSLCertificate)
  97. if !ok {
  98. return nil, errors.Wrapf(err, "cert.(*SSSLCertificate)")
  99. }
  100. s.Cert = _cert.Cert
  101. s.Key = _cert.Key
  102. }
  103. return s, nil
  104. }
  105. func (self *SAliyunClient) GetSSLCertificates(size, page int) ([]SSSLCertificate, int, error) {
  106. if size < 1 || size > 100 {
  107. size = 100
  108. }
  109. if page < 1 {
  110. page = 1
  111. }
  112. params := map[string]string{
  113. "ShowSize": fmt.Sprintf("%d", size),
  114. "CurrentPage": fmt.Sprintf("%d", page),
  115. }
  116. resp, err := self.scRequest("DescribeUserCertificateList", params)
  117. if err != nil {
  118. return nil, 0, errors.Wrapf(err, "DescribeUserCertificateList")
  119. }
  120. ret := make([]SSSLCertificate, 0)
  121. err = resp.Unmarshal(&ret, "CertificateList")
  122. if err != nil {
  123. return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
  124. }
  125. totalCount, _ := resp.Int("TotalCount")
  126. return ret, int(totalCount), nil
  127. }
  128. func (self *SAliyunClient) GetSSLCertificate(certId string) (*SSSLCertificate, error) {
  129. params := map[string]string{
  130. "CertId": fmt.Sprintf("%s", certId),
  131. }
  132. resp, err := self.scRequest("DescribeUserCertificateDetail", params)
  133. if err != nil {
  134. return nil, errors.Wrap(err, "DescribeUserCertificateDetail")
  135. }
  136. cert := &SSSLCertificate{}
  137. err = resp.Unmarshal(cert)
  138. if err != nil {
  139. return nil, errors.Wrap(err, "Unmarshal")
  140. }
  141. cert.client = self
  142. return cert, nil
  143. }