scan.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package scan
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. "github.com/cloudflare/cfssl/api"
  7. "github.com/cloudflare/cfssl/errors"
  8. "github.com/cloudflare/cfssl/log"
  9. "github.com/cloudflare/cfssl/scan"
  10. )
  11. // scanHandler is an HTTP handler that accepts GET parameters for host (required)
  12. // family and scanner, and uses these to perform scans, returning a JSON blob result.
  13. func scanHandler(w http.ResponseWriter, r *http.Request) error {
  14. if err := r.ParseForm(); err != nil {
  15. log.Warningf("failed to parse body: %v", err)
  16. return errors.NewBadRequest(err)
  17. }
  18. family := r.Form.Get("family")
  19. scanner := r.Form.Get("scanner")
  20. ip := r.Form.Get("ip")
  21. timeoutStr := r.Form.Get("timeout")
  22. var timeout time.Duration
  23. var err error
  24. if timeoutStr != "" {
  25. if timeout, err = time.ParseDuration(timeoutStr); err != nil {
  26. return errors.NewBadRequest(err)
  27. }
  28. if timeout < time.Second || timeout > 5*time.Minute {
  29. return errors.NewBadRequestString("invalid timeout given")
  30. }
  31. } else {
  32. timeout = time.Minute
  33. }
  34. host := r.Form.Get("host")
  35. if host == "" {
  36. log.Warningf("no host given")
  37. return errors.NewBadRequestString("no host given")
  38. }
  39. results, err := scan.Default.RunScans(host, ip, family, scanner, timeout)
  40. if err != nil {
  41. return errors.NewBadRequest(err)
  42. }
  43. return json.NewEncoder(w).Encode(api.NewSuccessResponse(results))
  44. }
  45. // NewHandler returns a new http.Handler that handles a scan request.
  46. func NewHandler(caBundleFile string) (http.Handler, error) {
  47. return api.HTTPHandler{
  48. Handler: api.HandlerFunc(scanHandler),
  49. Methods: []string{"GET"},
  50. }, scan.LoadRootCAs(caBundleFile)
  51. }
  52. // scanInfoHandler is an HTTP handler that returns a JSON blob result describing
  53. // the possible families and scans to be run.
  54. func scanInfoHandler(w http.ResponseWriter, r *http.Request) error {
  55. log.Info("setting up scaninfo handler")
  56. response := api.NewSuccessResponse(scan.Default)
  57. enc := json.NewEncoder(w)
  58. return enc.Encode(response)
  59. }
  60. // NewInfoHandler returns a new http.Handler that handles a request for scan info.
  61. func NewInfoHandler() http.Handler {
  62. return api.HTTPHandler{
  63. Handler: api.HandlerFunc(scanInfoHandler),
  64. Methods: []string{"GET"},
  65. }
  66. }