carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package hostfence
2
3import (
4 "context"
5 "crypto/sha256"
6 "encoding/hex"
7 "encoding/json"
8 "testing"
9
10 "github.com/aws/aws-sdk-go-v2/service/ec2/types"
11 "github.com/carlosframework/platform/internal/awsinv"
12 "github.com/carlosframework/platform/internal/hostclaim"
13)
14
15func TestC7FenceReaderAuthenticatesRosterAndCloud(t *testing.T) {
16 for _, tc := range []string{"valid", "enrollment", "account", "cloud-region", "instance", "reporter", "nonterminal", "signature", "self-reporter"} {
17 t.Run(tc, func(t *testing.T) {
18 ctx := context.Background()
19 w, s := terminationFixture(t)
20 w.Reader = func(context.Context, string) (awsinv.ExactEC2API, error) {
21 return terminatedEC2{state: types.InstanceStateNameTerminated, owner: "123"}, nil
22 }
23 got, err := w.Describe(ctx, "dead")
24 if err != nil {
25 t.Fatal(err)
26 }
27 ref := ClaimFenceRef(got)
28 raw, etag, err := s.Get(ctx, ref.Key)
29 if err != nil {
30 t.Fatal(err)
31 }
32 var f Termination
33 if err = json.Unmarshal(raw, &f); err != nil {
34 t.Fatal(err)
35 }
36 switch tc {
37 case "enrollment":
38 f.Enrollment = "cccccccccccccccccccccccccccccccc"
39 case "account":
40 f.Cloud.Account = "456"
41 case "cloud-region":
42 f.Cloud.Region = "eu-west-2"
43 case "instance":
44 f.Cloud.InstanceID = "i-other"
45 case "reporter":
46 f.WrittenBy.Enrollment = "cccccccccccccccccccccccccccccccc"
47 case "nonterminal":
48 f.State = "shutting-down"
49 case "self-reporter":
50 f.WrittenBy.Box = f.Box
51 f.WrittenBy.Enrollment = f.Enrollment
52 case "signature":
53 w.Directory.PublicKey = make([]byte, 32)
54 }
55 raw, err = json.Marshal(f)
56 if err != nil {
57 t.Fatal(err)
58 }
59 // Deliberately replace the object in this local adversarial fixture and
60 // recompute its digest: a digest match alone must not grant authority.
61 if _, err = s.Put(ctx, ref.Key, raw, etag); err != nil {
62 t.Fatal(err)
63 }
64 sum := sha256.Sum256(raw)
65 ref.SHA256 = hex.EncodeToString(sum[:])
66 _, err = hostclaim.VerifyFence(ctx, s, ClaimFenceAuthority{Directory: w.Directory, Now: w.Now}, ref)
67 if (err == nil) != (tc == "valid") {
68 t.Fatalf("%s: %v", tc, err)
69 }
70 })
71 }
72}
73