| 1 | package hostfence |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostactors" |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/termfence" |
| 10 | ) |
| 11 | |
| 12 | // ClaimFenceAuthority authenticates current active, unfenced writer standing |
| 13 | // against the signed roster without a reverse hostclaim dependency. |
| 14 | type ClaimFenceAuthority struct { |
| 15 | Directory hostactors.Directory |
| 16 | Now func() time.Time |
| 17 | } |
| 18 | |
| 19 | var _ hostclaim.FenceAuthority = ClaimFenceAuthority{} |
| 20 | |
| 21 | func (a ClaimFenceAuthority) ValidateFence(ctx context.Context, f termfence.Record) (hostclaim.FenceBinding, error) { |
| 22 | if a.Now == nil || f.WrittenBy.Box == f.Box { |
| 23 | return hostclaim.FenceBinding{}, hostclaim.ErrFence |
| 24 | } |
| 25 | o, err := a.Directory.Read(ctx) |
| 26 | if err != nil { |
| 27 | return hostclaim.FenceBinding{}, err |
| 28 | } |
| 29 | if err := a.Directory.ValidateTerminationFenceAuthority(ctx, o, f.Box, f, a.Now()); err != nil { |
| 30 | return hostclaim.FenceBinding{}, err |
| 31 | } |
| 32 | return hostclaim.FenceBinding{FleetHomeRegion: o.Roster.HomeRegion, Region: o.Roster.Boxes[f.Box].Region, RosterRevision: o.Roster.Revision, RosterETag: o.ETag}, nil |
| 33 | } |
| 34 | |
| 35 | func ClaimFenceRef(ref EvidenceRef) hostclaim.FenceRef { |
| 36 | return hostclaim.FenceRef{Enrollment: ref.Enrollment, Key: ref.Key, SHA256: ref.SHA256} |
| 37 | } |
| 38 | |