golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/rendezvous/fake_rendezvous.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rendezvous 6 7 import ( 8 "context" 9 "log" 10 "net/http" 11 "time" 12 13 "golang.org/x/build/buildlet" 14 ) 15 16 type rendezvousServer interface { 17 DeregisterInstance(ctx context.Context, id string) 18 HandleReverse(w http.ResponseWriter, r *http.Request) 19 RegisterInstance(ctx context.Context, id string, wait time.Duration) 20 WaitForInstance(ctx context.Context, id string) (buildlet.Client, error) 21 } 22 23 var _ rendezvousServer = (*FakeRendezvous)(nil) 24 var _ rendezvousServer = (*Rendezvous)(nil) 25 26 // FakeRendezvous is a fake rendezvous implementation intended for use in testing. 27 type FakeRendezvous struct { 28 validator TokenValidator 29 } 30 31 // NewFake creates a Fake Rendezvous instance. 32 func NewFake(ctx context.Context, validator TokenValidator) *FakeRendezvous { 33 rdv := &FakeRendezvous{ 34 validator: validator, 35 } 36 return rdv 37 } 38 39 // RegisterInstance is a fake implementation. 40 func (rdv *FakeRendezvous) RegisterInstance(ctx context.Context, id string, wait time.Duration) { 41 // do nothing 42 } 43 44 // DeregisterInstance is a fake implementation. 45 func (rdv *FakeRendezvous) DeregisterInstance(ctx context.Context, id string) { 46 // do nothing 47 } 48 49 // WaitForInstance is a fake implementation. 50 func (rdv *FakeRendezvous) WaitForInstance(ctx context.Context, id string) (buildlet.Client, error) { 51 return &buildlet.FakeClient{}, nil 52 } 53 54 // HandleReverse is a fake implementation of the handler. 55 func (rdv *FakeRendezvous) HandleReverse(w http.ResponseWriter, r *http.Request) { 56 if r.TLS == nil { 57 http.Error(w, "buildlet registration requires SSL", http.StatusInternalServerError) 58 return 59 } 60 var ( 61 id = r.Header.Get(HeaderID) 62 authToken = r.Header.Get(HeaderToken) 63 hostname = r.Header.Get(HeaderHostname) 64 ) 65 if hostname == "" { 66 http.Error(w, "missing X-Go-Hostname header", http.StatusBadRequest) 67 return 68 } 69 if id == "" { 70 http.Error(w, "missing X-Go-Gomote-ID header", http.StatusBadRequest) 71 return 72 } 73 if authToken == "" { 74 http.Error(w, "missing X-Go-Swarming-Auth-Token header", http.StatusBadRequest) 75 return 76 } 77 if !rdv.validator(r.Context(), authToken) { 78 log.Printf("rendezvous: Unable to validate authentication token id=%s", id) 79 http.Error(w, "invalid authentication Token", http.StatusPreconditionFailed) 80 return 81 } 82 }