provisioner_validate.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "github.com/smallstep/linkedca"
  6. "go.step.sm/crypto/jose"
  7. "github.com/smallstep/certificates/authority/admin"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. )
  10. // detailsType returns the provisioner type implied by the populated details
  11. // oneof arm. ok is false for arms unknown to this package, in which case no
  12. // type/details matching is enforced.
  13. func detailsType(data any) (linkedca.Provisioner_Type, bool) {
  14. switch data.(type) {
  15. case *linkedca.ProvisionerDetails_JWK:
  16. return linkedca.Provisioner_JWK, true
  17. case *linkedca.ProvisionerDetails_OIDC:
  18. return linkedca.Provisioner_OIDC, true
  19. case *linkedca.ProvisionerDetails_GCP:
  20. return linkedca.Provisioner_GCP, true
  21. case *linkedca.ProvisionerDetails_AWS:
  22. return linkedca.Provisioner_AWS, true
  23. case *linkedca.ProvisionerDetails_Azure:
  24. return linkedca.Provisioner_AZURE, true
  25. case *linkedca.ProvisionerDetails_ACME:
  26. return linkedca.Provisioner_ACME, true
  27. case *linkedca.ProvisionerDetails_X5C:
  28. return linkedca.Provisioner_X5C, true
  29. case *linkedca.ProvisionerDetails_K8SSA:
  30. return linkedca.Provisioner_K8SSA, true
  31. case *linkedca.ProvisionerDetails_SSHPOP:
  32. return linkedca.Provisioner_SSHPOP, true
  33. case *linkedca.ProvisionerDetails_SCEP:
  34. return linkedca.Provisioner_SCEP, true
  35. case *linkedca.ProvisionerDetails_Nebula:
  36. return linkedca.Provisioner_NEBULA, true
  37. default:
  38. return linkedca.Provisioner_NOOP, false
  39. }
  40. }
  41. // anyNonEmpty reports whether at least one entry in bs has content.
  42. func anyNonEmpty(bs [][]byte) bool {
  43. for _, b := range bs {
  44. if len(b) > 0 {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. // validateProvisioner validates a provisioner sent to the create or update
  51. // endpoints before it is stored. It enforces that the details oneof matches
  52. // the declared type and that per-type required fields are present and
  53. // parseable, returning structured bad-request errors instead of the internal
  54. // errors the downstream translation layer would produce. Deeper semantic
  55. // validation remains in provisioner.Interface.Init.
  56. func validateProvisioner(p *linkedca.Provisioner) error {
  57. if p.GetName() == "" {
  58. return admin.NewError(admin.ErrorBadRequestType, "provisioner name is required")
  59. }
  60. if p.GetType() == linkedca.Provisioner_NOOP {
  61. return admin.NewError(admin.ErrorBadRequestType, "provisioner type is required")
  62. }
  63. data := p.GetDetails().GetData()
  64. if data == nil {
  65. return admin.NewError(admin.ErrorBadRequestType, "provisioner details are required")
  66. }
  67. if typ, ok := detailsType(data); ok && typ != p.GetType() {
  68. return admin.NewError(admin.ErrorBadRequestType,
  69. "provisioner details (%s) do not match provisioner type (%s)", typ, p.GetType())
  70. }
  71. switch d := data.(type) {
  72. case *linkedca.ProvisionerDetails_JWK:
  73. if len(d.JWK.GetPublicKey()) == 0 {
  74. return admin.NewError(admin.ErrorBadRequestType, "jwk.publicKey is required")
  75. }
  76. var key jose.JSONWebKey
  77. if err := json.Unmarshal(d.JWK.GetPublicKey(), &key); err != nil {
  78. return admin.NewError(admin.ErrorBadRequestType, "jwk.publicKey is not a valid JWK")
  79. }
  80. case *linkedca.ProvisionerDetails_OIDC:
  81. switch {
  82. case d.OIDC.GetClientId() == "":
  83. return admin.NewError(admin.ErrorBadRequestType, "oidc.clientId is required")
  84. case d.OIDC.GetConfigurationEndpoint() == "":
  85. return admin.NewError(admin.ErrorBadRequestType, "oidc.configurationEndpoint is required")
  86. }
  87. if _, err := url.Parse(d.OIDC.GetConfigurationEndpoint()); err != nil {
  88. return admin.NewError(admin.ErrorBadRequestType, "oidc.configurationEndpoint is not a valid URL")
  89. }
  90. case *linkedca.ProvisionerDetails_AWS:
  91. if age := d.AWS.GetInstanceAge(); age != "" {
  92. if _, err := provisioner.NewDuration(age); err != nil {
  93. return admin.NewError(admin.ErrorBadRequestType, "aws.instanceAge is invalid")
  94. }
  95. }
  96. case *linkedca.ProvisionerDetails_GCP:
  97. if age := d.GCP.GetInstanceAge(); age != "" {
  98. if _, err := provisioner.NewDuration(age); err != nil {
  99. return admin.NewError(admin.ErrorBadRequestType, "gcp.instanceAge is invalid")
  100. }
  101. }
  102. case *linkedca.ProvisionerDetails_Azure:
  103. if d.Azure.GetTenantId() == "" {
  104. return admin.NewError(admin.ErrorBadRequestType, "azure.tenantId is required")
  105. }
  106. case *linkedca.ProvisionerDetails_X5C:
  107. if !anyNonEmpty(d.X5C.GetRoots()) {
  108. return admin.NewError(admin.ErrorBadRequestType, "x5c.roots is required")
  109. }
  110. case *linkedca.ProvisionerDetails_K8SSA:
  111. if !anyNonEmpty(d.K8SSA.GetPublicKeys()) {
  112. return admin.NewError(admin.ErrorBadRequestType, "k8sSA.publicKeys is required")
  113. }
  114. case *linkedca.ProvisionerDetails_Nebula:
  115. if !anyNonEmpty(d.Nebula.GetRoots()) {
  116. return admin.NewError(admin.ErrorBadRequestType, "nebula.roots is required")
  117. }
  118. }
  119. return nil
  120. }