method.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package provisioner
  2. import (
  3. "context"
  4. )
  5. // Method indicates the action to action that we will perform, it's used as part
  6. // of the context in the call to authorize. It defaults to Sing.
  7. type Method int
  8. // The key to save the Method in the context.
  9. type methodKey struct{}
  10. const (
  11. // SignMethod is the method used to sign X.509 certificates.
  12. SignMethod Method = iota
  13. // RevokeMethod is the method used to revoke X.509 certificates.
  14. RevokeMethod
  15. // SignSSHMethod is the method used to sign SSH certificates.
  16. SignSSHMethod
  17. // RenewSSHMethod is the method used to renew SSH certificates.
  18. RenewSSHMethod
  19. // RevokeSSHMethod is the method used to revoke SSH certificates.
  20. RevokeSSHMethod
  21. // RekeySSHMethod is the method used to rekey SSH certificates.
  22. RekeySSHMethod
  23. )
  24. // String returns a string representation of the context method.
  25. func (m Method) String() string {
  26. switch m {
  27. case SignMethod:
  28. return "sign-method"
  29. case RevokeMethod:
  30. return "revoke-method"
  31. case SignSSHMethod:
  32. return "sign-ssh-method"
  33. case RenewSSHMethod:
  34. return "renew-ssh-method"
  35. case RevokeSSHMethod:
  36. return "revoke-ssh-method"
  37. case RekeySSHMethod:
  38. return "rekey-ssh-method"
  39. default:
  40. return "unknown"
  41. }
  42. }
  43. // NewContextWithMethod creates a new context from ctx and attaches method to
  44. // it.
  45. func NewContextWithMethod(ctx context.Context, method Method) context.Context {
  46. return context.WithValue(ctx, methodKey{}, method)
  47. }
  48. // MethodFromContext returns the Method saved in ctx. Returns Sign if the given
  49. // context has no Method associated with it.
  50. func MethodFromContext(ctx context.Context) Method {
  51. m, _ := ctx.Value(methodKey{}).(Method)
  52. return m
  53. }