(w http.ResponseWriter, req *http.Request)
| 76 | } |
| 77 | |
| 78 | func dispatchRequest(w http.ResponseWriter, req *http.Request) { |
| 79 | if req.Method != "POST" { |
| 80 | fail(w, req, http.StatusMethodNotAllowed, 1, "only POST is permitted", "") |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | defer req.Body.Close() |
| 85 | body, err := io.ReadAll(req.Body) |
| 86 | if err != nil { |
| 87 | fail(w, req, http.StatusInternalServerError, 1, err.Error(), "while reading request body") |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | var authReq auth.AuthenticatedRequest |
| 92 | err = json.Unmarshal(body, &authReq) |
| 93 | if err != nil { |
| 94 | fail(w, req, http.StatusBadRequest, 1, err.Error(), "while unmarshaling request body") |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | var sigRequest signer.SignRequest |
| 99 | err = json.Unmarshal(authReq.Request, &sigRequest) |
| 100 | if err != nil { |
| 101 | fail(w, req, http.StatusBadRequest, 1, err.Error(), "while unmarshalling authenticated request") |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | if sigRequest.Label == "" { |
| 106 | sigRequest.Label = defaultLabel |
| 107 | } |
| 108 | |
| 109 | acl := whitelists[sigRequest.Label] |
| 110 | if acl != nil { |
| 111 | ip, err := whitelist.HTTPRequestLookup(req) |
| 112 | if err != nil { |
| 113 | fail(w, req, http.StatusInternalServerError, 1, err.Error(), "while getting request IP") |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | if !acl.Permitted(ip) { |
| 118 | fail(w, req, http.StatusForbidden, 1, "not authorised", "because IP is not whitelisted") |
| 119 | return |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | s, ok := signers[sigRequest.Label] |
| 124 | if !ok { |
| 125 | fail(w, req, http.StatusBadRequest, 1, "bad request", "request is for non-existent label "+sigRequest.Label) |
| 126 | return |
| 127 | } |
| 128 | requests.WithLabelValues(signOperation, sigRequest.Label).Inc() |
| 129 | // Sanity checks to ensure that we have a valid policy. This |
| 130 | // should have been checked in NewAuthSignHandler. |
| 131 | policy := s.Policy() |
| 132 | if policy == nil { |
| 133 | fail(w, req, http.StatusInternalServerError, 1, "invalid policy", "signer was initialised without a signing policy") |
| 134 | return |
| 135 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…