api.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package authorization
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "encoding/pem"
  6. )
  7. const (
  8. // AuthZApiRequest is the url for daemon request authorization
  9. AuthZApiRequest = "AuthZPlugin.AuthZReq"
  10. // AuthZApiResponse is the url for daemon response authorization
  11. AuthZApiResponse = "AuthZPlugin.AuthZRes"
  12. // AuthZApiImplements is the name of the interface all AuthZ plugins implement
  13. AuthZApiImplements = "authz"
  14. )
  15. // PeerCertificate is a wrapper around x509.Certificate which provides a sane
  16. // encoding/decoding to/from PEM format and JSON.
  17. type PeerCertificate x509.Certificate
  18. // MarshalJSON returns the JSON encoded pem bytes of a PeerCertificate.
  19. func (pc *PeerCertificate) MarshalJSON() ([]byte, error) {
  20. b := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: pc.Raw})
  21. return json.Marshal(b)
  22. }
  23. // UnmarshalJSON populates a new PeerCertificate struct from JSON data.
  24. func (pc *PeerCertificate) UnmarshalJSON(b []byte) error {
  25. var buf []byte
  26. if err := json.Unmarshal(b, &buf); err != nil {
  27. return err
  28. }
  29. derBytes, _ := pem.Decode(buf)
  30. c, err := x509.ParseCertificate(derBytes.Bytes)
  31. if err != nil {
  32. return err
  33. }
  34. *pc = PeerCertificate(*c)
  35. return nil
  36. }
  37. // Request holds data required for authZ plugins
  38. type Request struct {
  39. // User holds the user extracted by AuthN mechanism
  40. User string `json:"User,omitempty"`
  41. // UserAuthNMethod holds the mechanism used to extract user details (e.g., krb)
  42. UserAuthNMethod string `json:"UserAuthNMethod,omitempty"`
  43. // RequestMethod holds the HTTP method (GET/POST/PUT)
  44. RequestMethod string `json:"RequestMethod,omitempty"`
  45. // RequestUri holds the full HTTP uri (e.g., /v1.21/version)
  46. RequestURI string `json:"RequestUri,omitempty"`
  47. // RequestBody stores the raw request body sent to the docker daemon
  48. RequestBody []byte `json:"RequestBody,omitempty"`
  49. // RequestHeaders stores the raw request headers sent to the docker daemon
  50. RequestHeaders map[string]string `json:"RequestHeaders,omitempty"`
  51. // RequestPeerCertificates stores the request's TLS peer certificates in PEM format
  52. RequestPeerCertificates []*PeerCertificate `json:"RequestPeerCertificates,omitempty"`
  53. // ResponseStatusCode stores the status code returned from docker daemon
  54. ResponseStatusCode int `json:"ResponseStatusCode,omitempty"`
  55. // ResponseBody stores the raw response body sent from docker daemon
  56. ResponseBody []byte `json:"ResponseBody,omitempty"`
  57. // ResponseHeaders stores the response headers sent to the docker daemon
  58. ResponseHeaders map[string]string `json:"ResponseHeaders,omitempty"`
  59. }
  60. // Response represents authZ plugin response
  61. type Response struct {
  62. // Allow indicating whether the user is allowed or not
  63. Allow bool `json:"Allow"`
  64. // Msg stores the authorization message
  65. Msg string `json:"Msg,omitempty"`
  66. // Err stores a message in case there's an error
  67. Err string `json:"Err,omitempty"`
  68. }