cas.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package cas
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/cas/apiv1"
  7. // Enable default implementation
  8. _ "github.com/smallstep/certificates/cas/softcas"
  9. )
  10. // CertificateAuthorityService is the interface implemented by all the CAS.
  11. type CertificateAuthorityService = apiv1.CertificateAuthorityService
  12. // CertificateAuthorityCreator is the interface implemented by all CAS that can create a new authority.
  13. type CertificateAuthorityCreator = apiv1.CertificateAuthorityCreator
  14. // New creates a new CertificateAuthorityService using the given options.
  15. func New(ctx context.Context, opts apiv1.Options) (CertificateAuthorityService, error) {
  16. if err := opts.Validate(); err != nil {
  17. return nil, err
  18. }
  19. t := apiv1.Type(strings.ToLower(opts.Type))
  20. if t == apiv1.DefaultCAS {
  21. t = apiv1.SoftCAS
  22. }
  23. fn, ok := apiv1.LoadCertificateAuthorityServiceNewFunc(t)
  24. if !ok {
  25. return nil, errors.Errorf("unsupported cas type '%s'", t)
  26. }
  27. return fn(ctx, opts)
  28. }
  29. // NewCreator creates a new CertificateAuthorityCreator using the given options.
  30. func NewCreator(ctx context.Context, opts apiv1.Options) (CertificateAuthorityCreator, error) {
  31. opts.IsCreator = true
  32. t := apiv1.Type(strings.ToLower(opts.Type))
  33. if t == apiv1.DefaultCAS {
  34. t = apiv1.SoftCAS
  35. }
  36. svc, err := New(ctx, opts)
  37. if err != nil {
  38. return nil, err
  39. }
  40. creator, ok := svc.(CertificateAuthorityCreator)
  41. if !ok {
  42. return nil, errors.Errorf("cas type '%s' does not implements CertificateAuthorityCreator", t)
  43. }
  44. return creator, nil
  45. }