4
0

method.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // RenewMethod is the method used to renew X.509 certificates.
  16. RenewMethod
  17. // SSHSignMethod is the method used to sign SSH certificates.
  18. SSHSignMethod
  19. // SSHRenewMethod is the method used to renew SSH certificates.
  20. SSHRenewMethod
  21. // SSHRevokeMethod is the method used to revoke SSH certificates.
  22. SSHRevokeMethod
  23. // SSHRekeyMethod is the method used to rekey SSH certificates.
  24. SSHRekeyMethod
  25. )
  26. // String returns a string representation of the context method.
  27. func (m Method) String() string {
  28. switch m {
  29. case SignMethod:
  30. return "sign-method"
  31. case RevokeMethod:
  32. return "revoke-method"
  33. case RenewMethod:
  34. return "renew-method"
  35. case SSHSignMethod:
  36. return "ssh-sign-method"
  37. case SSHRenewMethod:
  38. return "ssh-renew-method"
  39. case SSHRevokeMethod:
  40. return "ssh-revoke-method"
  41. case SSHRekeyMethod:
  42. return "ssh-rekey-method"
  43. default:
  44. return "unknown"
  45. }
  46. }
  47. // NewContextWithMethod creates a new context from ctx and attaches method to
  48. // it.
  49. func NewContextWithMethod(ctx context.Context, method Method) context.Context {
  50. return context.WithValue(ctx, methodKey{}, method)
  51. }
  52. // MethodFromContext returns the Method saved in ctx. Returns Sign if the given
  53. // context has no Method associated with it.
  54. func MethodFromContext(ctx context.Context) Method {
  55. m, _ := ctx.Value(methodKey{}).(Method)
  56. return m
  57. }