syspolicy_api.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ts_omit_syspolicy
  4. package localapi
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "tailscale.com/util/httpm"
  11. "tailscale.com/util/syspolicy/rsop"
  12. "tailscale.com/util/syspolicy/setting"
  13. )
  14. func init() {
  15. Register("policy/", (*Handler).servePolicy)
  16. }
  17. func (h *Handler) servePolicy(w http.ResponseWriter, r *http.Request) {
  18. if !h.PermitRead {
  19. http.Error(w, "policy access denied", http.StatusForbidden)
  20. return
  21. }
  22. suffix, ok := strings.CutPrefix(r.URL.EscapedPath(), "/localapi/v0/policy/")
  23. if !ok {
  24. http.Error(w, "misconfigured", http.StatusInternalServerError)
  25. return
  26. }
  27. var scope setting.PolicyScope
  28. if suffix == "" {
  29. scope = setting.DefaultScope()
  30. } else if err := scope.UnmarshalText([]byte(suffix)); err != nil {
  31. http.Error(w, fmt.Sprintf("%q is not a valid scope", suffix), http.StatusBadRequest)
  32. return
  33. }
  34. policy, err := rsop.PolicyFor(scope)
  35. if err != nil {
  36. http.Error(w, err.Error(), http.StatusInternalServerError)
  37. return
  38. }
  39. var effectivePolicy *setting.Snapshot
  40. switch r.Method {
  41. case httpm.GET:
  42. effectivePolicy = policy.Get()
  43. case httpm.POST:
  44. effectivePolicy, err = policy.Reload()
  45. if err != nil {
  46. http.Error(w, err.Error(), http.StatusInternalServerError)
  47. return
  48. }
  49. default:
  50. http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
  51. return
  52. }
  53. w.Header().Set("Content-Type", "application/json")
  54. e := json.NewEncoder(w)
  55. e.SetIndent("", "\t")
  56. e.Encode(effectivePolicy)
  57. }