main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "time"
  9. )
  10. type start struct {
  11. File string
  12. }
  13. func main() {
  14. l, err := net.Listen("unix", "/run/docker/plugins/plugin.sock")
  15. if err != nil {
  16. panic(err)
  17. }
  18. mux := http.NewServeMux()
  19. mux.HandleFunc("/LogDriver.StartLogging", func(w http.ResponseWriter, req *http.Request) {
  20. startReq := &start{}
  21. if err := json.NewDecoder(req.Body).Decode(startReq); err != nil {
  22. http.Error(w, err.Error(), http.StatusBadRequest)
  23. return
  24. }
  25. f, err := os.OpenFile(startReq.File, os.O_RDONLY, 0o600)
  26. if err != nil {
  27. http.Error(w, err.Error(), http.StatusInternalServerError)
  28. return
  29. }
  30. // Close the file immediately, this allows us to test what happens in the daemon when the plugin has closed the
  31. // file or, for example, the plugin has crashed.
  32. f.Close()
  33. w.WriteHeader(http.StatusOK)
  34. fmt.Fprintln(w, `{}`)
  35. })
  36. server := http.Server{
  37. Addr: l.Addr().String(),
  38. Handler: mux,
  39. ReadHeaderTimeout: 2 * time.Second, // This server is not for production code; picked an arbitrary timeout to statisfy gosec (G112: Potential Slowloris Attack)
  40. }
  41. server.Serve(l)
  42. }