servicecertificates.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type SServiceCertificateManager struct {
  27. db.SStandaloneResourceBaseManager
  28. }
  29. var ServiceCertificateManager *SServiceCertificateManager
  30. func init() {
  31. ServiceCertificateManager = &SServiceCertificateManager{
  32. SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
  33. SServiceCertificate{},
  34. "servicecertificates_tbl",
  35. "servicecertificate",
  36. "servicecertificates",
  37. ),
  38. }
  39. ServiceCertificateManager.SetVirtualObject(ServiceCertificateManager)
  40. }
  41. type SServiceCertificate struct {
  42. db.SStandaloneResourceBase
  43. db.SCertificateResourceBase
  44. CaCertificate string `create:"optional" list:"admin"`
  45. CaPrivateKey string `create:"optional" list:"admin"`
  46. }
  47. func (man *SServiceCertificateManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
  48. v := validators.NewCertKeyValidator("certificate", "private_key")
  49. if err := v.Validate(ctx, data); err != nil {
  50. return nil, err
  51. }
  52. data = v.UpdateCertKeyInfo(ctx, data)
  53. if caCert, _ := data.GetString("ca_certificate"); len(caCert) > 0 {
  54. vc := validators.NewCertificateValidator("ca_certificate")
  55. if err := vc.Validate(ctx, data); err != nil {
  56. return nil, err
  57. }
  58. }
  59. if caPkey, _ := data.GetString("ca_private_key"); len(caPkey) > 0 {
  60. vp := validators.NewPrivateKeyValidator("ca_private_key")
  61. if err := vp.Validate(ctx, data); err != nil {
  62. return nil, err
  63. }
  64. }
  65. input := apis.StandaloneResourceCreateInput{}
  66. err := data.Unmarshal(&input)
  67. if err != nil {
  68. return nil, httperrors.NewInternalServerError("unmarshal StandaloneResourceCreateInput fail %s", err)
  69. }
  70. input, err = man.SStandaloneResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input)
  71. if err != nil {
  72. return nil, err
  73. }
  74. data.Update(jsonutils.Marshal(input))
  75. return data, nil
  76. }
  77. func (cert *SServiceCertificate) ValidateUpdateData(
  78. ctx context.Context, userCred mcclient.TokenCredential,
  79. query jsonutils.JSONObject, data *jsonutils.JSONDict,
  80. ) (*jsonutils.JSONDict, error) {
  81. v := validators.NewCertKeyValidator("certificate", "private_key")
  82. if err := v.Validate(ctx, data); err != nil {
  83. return nil, err
  84. }
  85. data = v.UpdateCertKeyInfo(ctx, data)
  86. if caCert, _ := data.GetString("ca_certificate"); len(caCert) > 0 {
  87. vc := validators.NewCertificateValidator("ca_certificate")
  88. if err := vc.Validate(ctx, data); err != nil {
  89. return nil, err
  90. }
  91. }
  92. if caPkey, _ := data.GetString("ca_private_key"); len(caPkey) > 0 {
  93. vp := validators.NewPrivateKeyValidator("ca_private_key")
  94. if err := vp.Validate(ctx, data); err != nil {
  95. return nil, err
  96. }
  97. }
  98. updateData := jsonutils.NewDict()
  99. if name, err := data.GetString("name"); err == nil {
  100. updateData.Set("name", jsonutils.NewString(name))
  101. }
  102. if desc, err := data.GetString("description"); err == nil {
  103. updateData.Set("description", jsonutils.NewString(desc))
  104. }
  105. input := apis.StandaloneResourceBaseUpdateInput{}
  106. err := updateData.Unmarshal(&input)
  107. if err != nil {
  108. return nil, errors.Wrap(err, "Unmarshal")
  109. }
  110. input, err = cert.SStandaloneResourceBase.ValidateUpdateData(ctx, userCred, query, input)
  111. if err != nil {
  112. return nil, errors.Wrap(err, "SVirtualResourceBase.ValidateUpdateData")
  113. }
  114. updateData.Update(jsonutils.Marshal(input))
  115. return updateData, nil
  116. }
  117. func (cert *SServiceCertificate) ToOutput() *api.CertificateDetails {
  118. return &api.CertificateDetails{
  119. SCertificateResourceBase: apis.SCertificateResourceBase{
  120. Certificate: cert.Certificate,
  121. PrivateKey: cert.PrivateKey,
  122. PublicKeyAlgorithm: cert.PublicKeyAlgorithm,
  123. PublicKeyBitLen: cert.PublicKeyBitLen,
  124. SignatureAlgorithm: cert.SignatureAlgorithm,
  125. Fingerprint: cert.Fingerprint,
  126. NotAfter: cert.NotAfter,
  127. NotBefore: cert.NotBefore,
  128. CommonName: cert.CommonName,
  129. SubjectAlternativeNames: cert.SubjectAlternativeNames,
  130. },
  131. CertName: cert.Name,
  132. CertId: cert.Id,
  133. CaCertificate: cert.CaCertificate,
  134. CaPrivateKey: cert.CaPrivateKey,
  135. }
  136. }