File
Blob: tun/client/waf.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "mime" |
| 8 | "net/http" |
| 9 | |
| 10 | "go.miragespace.co/specter/spec/protocol" |
| 11 | "go.miragespace.co/specter/spec/rpc" |
| 12 | "go.miragespace.co/specter/waf" |
| 13 | |
| 14 | "github.com/twitchtv/twirp" |
| 15 | ) |
| 16 | |
| 17 | const maxHostWAFRequestBytes = 64 << 10 |
| 18 | |
| 19 | func (c *Client) hostWAFCall(ctx context.Context, write bool, call func(context.Context) (*protocol.HostWAFResponse, error)) (*protocol.HostWAFResponse, error) { |
| 20 | nodes := c.getConnectedNodes() |
| 21 | if len(nodes) == 0 { |
| 22 | return nil, twirp.Unavailable.Error("no connected servers are available") |
| 23 | } |
| 24 | var last error |
| 25 | for _, node := range nodes { |
| 26 | if err := ctx.Err(); err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | callCtx, cancel := context.WithTimeout(rpc.WithNode(ctx, node), rpcTimeout) |
| 30 | response, err := call(callCtx) |
| 31 | cancel() |
| 32 | if err == nil { |
| 33 | policy, err := waf.ValidatePolicy(response.GetPolicy()) |
| 34 | if err != nil { |
| 35 | return nil, twirp.Unavailable.Errorf("server returned an unsupported WAF policy: %v", err) |
| 36 | } |
| 37 | response.Policy = policy |
| 38 | return response, nil |
| 39 | } |
| 40 | var rpcError twirp.Error |
| 41 | if errors.As(err, &rpcError) && (rpcError.Code() == twirp.BadRoute || rpcError.Code() == twirp.Unimplemented) { |
| 42 | continue |
| 43 | } |
| 44 | // Reads can use another connected server. An uncertain save keeps its |
| 45 | // original version guard and is left for the owner to refresh or retry. |
| 46 | if write || definite(err) { |
| 47 | return nil, err |
| 48 | } |
| 49 | last = err |
| 50 | } |
| 51 | if last != nil { |
| 52 | return nil, last |
| 53 | } |
| 54 | return nil, twirp.Unimplemented.Error("connected servers do not support web protection") |
| 55 | } |
| 56 | |
| 57 | func (c *Client) GetHostWAF(ctx context.Context, hostname string) (*protocol.HostWAFResponse, error) { |
| 58 | if hostname == "" { |
| 59 | return nil, twirp.InvalidArgument.Error("hostname is required") |
| 60 | } |
| 61 | return c.hostWAFCall(ctx, false, func(ctx context.Context) (*protocol.HostWAFResponse, error) { |
| 62 | return c.tunnelClient.GetHostWAF(ctx, &protocol.GetHostWAFRequest{Hostname: hostname}) |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | func (c *Client) SetHostWAF(ctx context.Context, hostname, expectedVersion string, policy *protocol.WAFPolicy) (*protocol.HostWAFResponse, error) { |
| 67 | if hostname == "" { |
| 68 | return nil, twirp.InvalidArgument.Error("hostname is required") |
| 69 | } |
| 70 | policy, err := waf.ValidatePolicy(policy) |
| 71 | if err != nil { |
| 72 | return nil, twirp.InvalidArgument.Error(err.Error()) |
| 73 | } |
| 74 | request := &protocol.SetHostWAFRequest{ |
| 75 | Hostname: hostname, ExpectedVersion: expectedVersion, |
| 76 | Policy: policy, |
| 77 | } |
| 78 | return c.hostWAFCall(ctx, true, func(ctx context.Context) (*protocol.HostWAFResponse, error) { |
| 79 | return c.tunnelClient.SetHostWAF(ctx, request) |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | func (c *Client) getHostWAFHandler(w http.ResponseWriter, r *http.Request) { |
| 84 | response, err := c.GetHostWAF(r.Context(), r.URL.Query().Get("hostname")) |
| 85 | if err != nil { |
| 86 | writeHostWAFError(w, err) |
| 87 | return |
| 88 | } |
| 89 | writeHostWAFResult(w, response) |
| 90 | } |
| 91 | |
| 92 | func (c *Client) setHostWAFHandler(w http.ResponseWriter, r *http.Request) { |
| 93 | mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| 94 | if err != nil || mediaType != "application/json" { |
| 95 | writeHostWAFError(w, twirp.InvalidArgument.Error("send WAF settings as application/json")) |
| 96 | return |
| 97 | } |
| 98 | var body protocol.SetHostWAFRequest |
| 99 | if err := waf.DecodeJSON(r.Body, &body); err != nil || body.Policy == nil { |
| 100 | writeHostWAFError(w, twirp.InvalidArgument.Error("invalid WAF settings request")) |
| 101 | return |
| 102 | } |
| 103 | response, err := c.SetHostWAF(r.Context(), r.URL.Query().Get("hostname"), body.ExpectedVersion, body.Policy) |
| 104 | if err != nil { |
| 105 | writeHostWAFError(w, err) |
| 106 | return |
| 107 | } |
| 108 | writeHostWAFResult(w, response) |
| 109 | } |
| 110 | |
| 111 | func writeHostWAFResult(w http.ResponseWriter, response *protocol.HostWAFResponse) { |
| 112 | data, err := waf.MarshalJSON(response) |
| 113 | if err != nil { |
| 114 | writeHostWAFError(w, twirp.InternalErrorWith(err)) |
| 115 | return |
| 116 | } |
| 117 | writeJSONResult(w, http.StatusOK, json.RawMessage(data)) |
| 118 | } |
| 119 | |
| 120 | func writeHostWAFError(w http.ResponseWriter, err error) { |
| 121 | status, code := http.StatusServiceUnavailable, twirp.Unavailable |
| 122 | var rpcError twirp.Error |
| 123 | if errors.As(err, &rpcError) { |
| 124 | code = rpcError.Code() |
| 125 | switch code { |
| 126 | case twirp.FailedPrecondition: |
| 127 | status = http.StatusConflict |
| 128 | case twirp.BadRoute: |
| 129 | status = http.StatusNotImplemented |
| 130 | case twirp.InvalidArgument, twirp.Malformed, twirp.PermissionDenied, twirp.Unauthenticated, |
| 131 | twirp.NotFound, twirp.AlreadyExists, twirp.Unimplemented, twirp.ResourceExhausted: |
| 132 | status = twirp.ServerHTTPStatusFromErrorCode(code) |
| 133 | } |
| 134 | } |
| 135 | writeJSONResult(w, status, struct { |
| 136 | Code string `json:"code"` |
| 137 | Error string `json:"error"` |
| 138 | }{string(code), err.Error()}) |
| 139 | } |