| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net" |
| 8 |
"reflect" |
| 9 |
"regexp" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"time" |
| 14 |
|
| 15 |
logpb "cloud.google.com/go/logging/apiv2/loggingpb" |
| 16 |
tspb "github.com/golang/protobuf/ptypes/timestamp" |
| 17 |
"github.com/google/go-tpm-tools/verifier/util" |
| 18 |
"google.golang.org/grpc" |
| 19 |
) |
| 20 |
|
| 21 |
// The only IDs that WriteLogEntries will accept. |
| 22 |
const ( |
| 23 |
TestProjectID = "test-project" |
| 24 |
) |
| 25 |
|
| 26 |
// A fakeServer is an in-process gRPC server, listening on a system-chosen port on |
| 27 |
// the local loopback interface. Servers are for testing only and are not |
| 28 |
// intended to be used in production code. |
| 29 |
type fakeServer struct { |
| 30 |
Addr string |
| 31 |
Port int |
| 32 |
l net.Listener |
| 33 |
Gsrv *grpc.Server |
| 34 |
} |
| 35 |
|
| 36 |
// Start causes the server to start accepting incoming connections. |
| 37 |
// Call Start after registering handlers. |
| 38 |
func (s *fakeServer) Start() { |
| 39 |
go func() { |
| 40 |
if err := s.Gsrv.Serve(s.l); err != nil { |
| 41 |
log.Printf("fake_cloudlogging_server.fakeServer.Start: %v", err) |
| 42 |
} |
| 43 |
}() |
| 44 |
} |
| 45 |
|
| 46 |
// Close shuts down the server. |
| 47 |
func (s *fakeServer) Close() { |
| 48 |
s.Gsrv.Stop() |
| 49 |
s.l.Close() |
| 50 |
} |
| 51 |
|
| 52 |
// newFakeServer creates a new Server. The Server will be listening for gRPC connections |
| 53 |
// at the address named by the Addr field, without TLS. |
| 54 |
func newFakeServer(opts ...grpc.ServerOption) (*fakeServer, error) { |
| 55 |
return newFakeServerWithPort(0, opts...) |
| 56 |
} |
| 57 |
|
| 58 |
// newFakeServerWithPort creates a new Server at a specific port. The Server will be listening |
| 59 |
// for gRPC connections at the address named by the Addr field, without TLS. |
| 60 |
func newFakeServerWithPort(port int, opts ...grpc.ServerOption) (*fakeServer, error) { |
| 61 |
l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port)) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
s := &fakeServer{ |
| 66 |
Addr: l.Addr().String(), |
| 67 |
Port: parsePort(l.Addr().String()), |
| 68 |
l: l, |
| 69 |
Gsrv: grpc.NewServer(opts...), |
| 70 |
} |
| 71 |
return s, nil |
| 72 |
} |
| 73 |
|
| 74 |
var portParser = regexp.MustCompile(`:[0-9]+`) |
| 75 |
|
| 76 |
func parsePort(addr string) int { |
| 77 |
res := portParser.FindAllString(addr, -1) |
| 78 |
if len(res) == 0 { |
| 79 |
panic(fmt.Errorf("parsePort: found no numbers in %s", addr)) |
| 80 |
} |
| 81 |
stringPort := res[0][1:] // strip the : |
| 82 |
p, err := strconv.ParseInt(stringPort, 10, 32) |
| 83 |
if err != nil { |
| 84 |
panic(err) |
| 85 |
} |
| 86 |
return int(p) |
| 87 |
} |
| 88 |
|
| 89 |
type loggingHandler struct { |
| 90 |
logpb.LoggingServiceV2Server |
| 91 |
|
| 92 |
mu sync.Mutex |
| 93 |
logs map[string][]*logpb.LogEntry // indexed by log name |
| 94 |
} |
| 95 |
|
| 96 |
// WriteLogEntries writes log entries to Cloud Logging. All log entries in |
| 97 |
// Cloud Logging are written by this method. |
| 98 |
func (h *loggingHandler) WriteLogEntries(_ context.Context, req *logpb.WriteLogEntriesRequest) (*logpb.WriteLogEntriesResponse, error) { |
| 99 |
if !strings.HasPrefix(req.LogName, "projects/"+TestProjectID+"/") { |
| 100 |
return nil, fmt.Errorf("bad LogName: %q", req.LogName) |
| 101 |
} |
| 102 |
h.mu.Lock() |
| 103 |
defer h.mu.Unlock() |
| 104 |
for _, e := range req.Entries { |
| 105 |
// Assign timestamp if missing. |
| 106 |
if e.Timestamp == nil { |
| 107 |
e.Timestamp = &tspb.Timestamp{Seconds: time.Now().Unix(), Nanos: 0} |
| 108 |
} |
| 109 |
// Fill from common fields in request. |
| 110 |
if e.LogName == "" { |
| 111 |
e.LogName = req.LogName |
| 112 |
} |
| 113 |
if e.Resource == nil { |
| 114 |
e.Resource = req.Resource |
| 115 |
} |
| 116 |
for k, v := range req.Labels { |
| 117 |
if _, ok := e.Labels[k]; !ok { |
| 118 |
e.Labels[k] = v |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Store by log name. |
| 123 |
h.logs[e.LogName] = append(h.logs[e.LogName], e) |
| 124 |
} |
| 125 |
|
| 126 |
var logEntryPayload []map[string]interface{} |
| 127 |
logEntryPayload = append(logEntryPayload, map[string]interface{}{"aud": "test", "iat": float64(1709752525), "exp": float64(1919752525)}) |
| 128 |
logEntryPayload = append(logEntryPayload, map[string]interface{}{"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0IiwiaWF0IjoxNzA5NzUyNTI1LCJleHAiOjE5MTk3NTI1MjV9.EBLA2zX3c-Fu0l--J9Gey6LIXMO1TFRCoe3bzuPGc1k"}) |
| 129 |
logEntryPayload = append(logEntryPayload, map[string]interface{}{"Name": "projects/test-project/locations/us-central-1/challenges/" + util.FakeChallengeUUID, "Nonce": util.FakeTpmNonce, "ConnID": ""}) |
| 130 |
attestationMapFields := []string{"TeeAttestation", "ak_pub", "quotes", "event_log", "ak_cert"} |
| 131 |
for _, entry := range h.logs["projects/"+TestProjectID+"/logs/"+toolName] { |
| 132 |
payload := entry.GetJsonPayload().AsMap() |
| 133 |
foundMatch := false |
| 134 |
for _, m := range logEntryPayload { |
| 135 |
if reflect.DeepEqual(m, payload) { |
| 136 |
foundMatch = true |
| 137 |
} |
| 138 |
} |
| 139 |
if !foundMatch { |
| 140 |
for _, field := range attestationMapFields { |
| 141 |
if _, keyFound := payload[field]; !keyFound { |
| 142 |
return nil, fmt.Errorf("wrong log: %q", entry.GetJsonPayload().String()) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
return &logpb.WriteLogEntriesResponse{}, nil |
| 148 |
} |
| 149 |
|
| 150 |
// newMockCloudLoggingServer creates a new in-memory fake server implementing the logging service. |
| 151 |
// It returns the address of the server. |
| 152 |
func newMockCloudLoggingServer() (string, error) { |
| 153 |
srv, err := newFakeServer() |
| 154 |
if err != nil { |
| 155 |
return "", err |
| 156 |
} |
| 157 |
logpb.RegisterLoggingServiceV2Server(srv.Gsrv, &loggingHandler{ |
| 158 |
logs: make(map[string][]*logpb.LogEntry), |
| 159 |
}) |
| 160 |
|
| 161 |
srv.Start() |
| 162 |
return srv.Addr, nil |
| 163 |
} |