Sign Run requests with observed platform clock
This commit is contained in:
@@ -136,6 +136,47 @@ func TestPlatformClientSignsRunChannelRequestsWithUniqueNonce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlatformClientSignsWithObservedServerClock(t *testing.T) {
|
||||
activeSessionToken := "session-token"
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/run/control/hello":
|
||||
writeTestJSON(t, w, protocol.RunHelloResponse{Accepted: true, RunEndpointID: "run-local", SessionToken: activeSessionToken, ServerTime: time.Now().UTC().Add(12 * time.Hour), HeartbeatIntervalSeconds: 15})
|
||||
case "/api/v1/run/control/heartbeat":
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("read heartbeat body: %v", err)
|
||||
}
|
||||
verifyRunRequestSignature(t, r, body, "run-local", activeSessionToken)
|
||||
timestamp, err := strconv.ParseInt(r.Header.Get("X-Run-Timestamp"), 10, 64)
|
||||
if err != nil {
|
||||
t.Fatalf("parse signed timestamp: %v", err)
|
||||
}
|
||||
signedAt := time.Unix(timestamp, 0).UTC()
|
||||
serverNow := time.Now().UTC().Add(12 * time.Hour)
|
||||
if delta := signedAt.Sub(serverNow); delta < -5*time.Second || delta > 5*time.Second {
|
||||
t.Fatalf("expected signed timestamp to follow observed server clock, signedAt=%s serverNow=%s delta=%s", signedAt.Format(time.RFC3339), serverNow.Format(time.RFC3339), delta)
|
||||
}
|
||||
writeTestJSON(t, w, protocol.RunHeartbeatResponse{Accepted: true, RunEndpointID: "run-local", NextHeartbeatSeconds: 15, ServerTime: serverNow})
|
||||
default:
|
||||
t.Fatalf("unexpected request path %s", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewPlatformClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("new client: %v", err)
|
||||
}
|
||||
hello, err := client.Hello(context.Background(), validRunHelloRequest())
|
||||
if err != nil {
|
||||
t.Fatalf("hello: %v", err)
|
||||
}
|
||||
if _, err := client.Heartbeat(context.Background(), validRunHeartbeatRequest(hello.SessionToken)); err != nil {
|
||||
t.Fatalf("heartbeat: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlatformClientStreamsSignedControlEvents(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost || r.URL.Path != "/api/v1/run/control/events" {
|
||||
|
||||
Reference in New Issue
Block a user