| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/hostbox" |
| 11 | "github.com/carlosframework/platform/internal/hostguard" |
| 12 | ) |
| 13 | |
| 14 | func rootFenceViewClient(call hostbox.JournalCall) hostguard.FenceViewProvider { |
| 15 | return func(ctx context.Context, host string, c hostguard.Captured) (hostguard.FenceView, error) { |
| 16 | if call == nil { |
| 17 | return hostguard.FenceView{}, os.ErrPermission |
| 18 | } |
| 19 | ctx, cancel := context.WithTimeout(ctx, time.Second) |
| 20 | defer cancel() |
| 21 | q := hostbox.JournalRequest{V: 1, Op: "fence-view", Target: hostguard.FenceTarget{Host: host, Incarnation: c.Incarnation, Stamp: c.Stamp}} |
| 22 | out, err := call(ctx, q) |
| 23 | if ctx.Err() != nil { |
| 24 | return hostguard.FenceView{}, ctx.Err() |
| 25 | } |
| 26 | if err != nil { |
| 27 | return hostguard.FenceView{}, err |
| 28 | } |
| 29 | if out.V != 1 || out.Error != "" || out.FenceView == nil || out.FenceView.Schema != 1 || out.FenceView.Host != host || out.FenceView.Captured != c { |
| 30 | return hostguard.FenceView{}, os.ErrPermission |
| 31 | } |
| 32 | return *out.FenceView, nil |
| 33 | } |
| 34 | } |
| 35 | func (h ClaimRootHTTP) serveFenceView(w http.ResponseWriter, r *http.Request, q hostbox.JournalRequest) { |
| 36 | root := h.Stop.Coordinator.Guard.Root |
| 37 | if q.V != 1 || root == nil || q.Target.Incarnation == "" || q.Target.Stamp == 0 { |
| 38 | w.WriteHeader(http.StatusForbidden) |
| 39 | return |
| 40 | } |
| 41 | v, err := root.View(r.Context(), q.Target.Host, hostguard.Captured{Incarnation: q.Target.Incarnation, Stamp: q.Target.Stamp}) |
| 42 | if err != nil { |
| 43 | w.WriteHeader(http.StatusConflict) |
| 44 | return |
| 45 | } |
| 46 | w.Header().Set("Content-Type", "application/json") |
| 47 | _ = json.NewEncoder(w).Encode(hostbox.JournalResponse{V: 1, ID: q.ID, FenceView: &v}) |
| 48 | } |
| 49 | |