-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_protobuf.go
More file actions
274 lines (273 loc) · 8.91 KB
/
example_protobuf.go
File metadata and controls
274 lines (273 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package securelog
// Example: Protocol Buffer Transport for Secure Logging
//
// This example demonstrates using Protocol Buffers for communication between
// the logger (U) and the trusted server (T).
//
// Why Protocol Buffers?
// 1. Language-agnostic: Trusted server T can be written in any language
// 2. Compact binary format: More efficient than JSON
// 3. Schema evolution: Backward/forward compatible updates
// 4. Fast encoding/decoding: Better performance than text formats
//
// Use case: Production deployments where the trusted server is a separate
// service, potentially written in a different language (Java, Python, Rust, etc.)
//
// Architecture:
//
// ┌─────────────────┐ ┌─────────────────┐
// │ Logger U │ │ Trusted Server T│
// │ (Go) │ │ (Any Language) │
// ├─────────────────┤ ├─────────────────┤
// │ RemoteLogger │ │ REST API │
// │ │ HTTP + Protobuf │ │
// │ ProtoTransport ├───────────────────────>│ /api/init │
// │ │ │ /api/open │
// │ FileStore │ │ /api/close │
// │ /var/log/app/ │ │ /api/verify │
// └─────────────────┘ └─────────────────┘
//
// Protocol Messages (defined in proto/securelog.proto):
//
// 1. InitCommitment - Initial commitment from logger to server
// - log_id: Unique identifier for the log
// - start_time: When logging begins
// - key_a0: Initial key for V-chain (A_0)
// - key_b0: Initial key for T-chain (B_0)
// - update_freq: Anchor interval
//
// 2. OpenMessage - First record notification
// - log_id: Log identifier
// - open_time: When first record was written
// - first_index: Index of first entry (usually 2)
// - first_tag_v: μ_V,1 for V-chain
// - first_tag_t: μ_T,1 for T-chain
//
// 3. CloseMessage - Log closure notification
// - log_id: Log identifier
// - close_time: When log was closed
// - final_index: Index of last entry
// - final_tag_v: μ_V,f for V-chain
// - final_tag_t: μ_T,f for T-chain
//
// 4. Record - Individual log entry
// - index: Sequence number
// - ts: Timestamp (protobuf.Timestamp)
// - msg: Log message bytes
// - tag_v: μ_V,i for V-chain
// - tag_t: μ_T,i for T-chain
//
// 5. VerifyRequest - Batch verification request
// - log_id: Log identifier
// - records: Array of Record messages
//
// 6. VerifyResponse - Verification result
// - verified: Boolean result
// - error_message: Description if verification fails
//
//
// Usage Example:
//
// // ===== Logger U (Go) =====
//
// import (
// "securelog"
// "time"
// )
//
// // Create Protocol Buffer transport
// transport := securelog.NewProtoHTTPTransport("https://trust.example.com")
//
// // Create file store for local log storage
// store, _ := securelog.OpenFileStore("/var/log/myapp")
//
// // Create remote logger (sends InitCommitment via protobuf)
// logger, _ := securelog.NewRemoteLogger(
// securelog.Config{AnchorEvery: 100},
// store,
// transport,
// "app-log-001",
// )
//
// // Append entries
// logger.Append([]byte("user login: alice"), time.Now())
// logger.Append([]byte("file access: /etc/passwd"), time.Now())
//
// // Close log (sends CloseMessage via protobuf)
// logger.Close()
//
//
// // ===== Trusted Server T (Python example) =====
//
// from flask import Flask, request
// from proto import securelog_pb2
//
// app = Flask(__name__)
// trusted_server = TrustedServer()
//
// @app.route('/api/init', methods=['POST'])
// def handle_init():
// # Decode protobuf message
// commit = securelog_pb2.InitCommitment()
// commit.ParseFromString(request.data)
//
// # Store commitment
// trusted_server.register_log(
// commit.log_id,
// commit.key_a0,
// commit.key_b0,
// commit.start_time
// )
// return b"OK"
//
// @app.route('/api/close', methods=['POST'])
// def handle_close():
// # Decode protobuf message
// close = securelog_pb2.CloseMessage()
// close.ParseFromString(request.data)
//
// # Accept closure
// trusted_server.accept_closure(
// close.log_id,
// close.final_index,
// close.final_tag_t
// )
// return b"OK"
//
// @app.route('/api/verify', methods=['POST'])
// def handle_verify():
// # Decode protobuf message
// req = securelog_pb2.VerifyRequest()
// req.ParseFromString(request.data)
//
// # Verify T-chain
// verified = trusted_server.verify_records(
// req.log_id,
// req.records
// )
//
// # Return protobuf response
// resp = securelog_pb2.VerifyResponse()
// resp.verified = verified
// return resp.SerializeToString()
//
//
// Size Comparison (1000 log entries with 100-byte messages):
//
// Format Size Notes
// ─────────────────────────────────────────────────────────
// Gob ~180 KB Go-only, not portable
// Protobuf ~175 KB Language-agnostic, portable
// JSON ~231 KB 31% larger, human-readable
//
//
// Performance Benefits:
//
// ✓ Cross-language compatibility (Go logger + Python/Java/Rust server)
// ✓ Compact binary format (~24% smaller than JSON)
// ✓ Fast serialization/deserialization (2-10x faster than JSON)
// ✓ Strong typing with schema validation
// ✓ Forward/backward compatibility for protocol evolution
// ✓ Built-in timestamp handling (google.protobuf.Timestamp)
//
//
// Security Considerations:
//
// 1. TLS Required: Always use HTTPS for protobuf transport
// - Prevents man-in-the-middle attacks
// - Protects keys during transmission
//
// 2. Authentication: Implement auth tokens or mutual TLS
// - Verify logger identity before accepting commits
// - Prevent unauthorized log registration
//
// 3. Rate Limiting: Protect trusted server from DoS
// - Limit requests per log ID
// - Cap maximum record batch size
//
// 4. Input Validation: Parse protobuf messages carefully
// - Check log_id format and length
// - Validate timestamp ranges
// - Verify tag lengths (must be 32 bytes)
//
//
// Deployment Example (Docker Compose):
//
// version: '3.8'
// services:
// logger:
// image: myapp:latest
// environment:
// - SECURELOG_TRANSPORT=proto
// - SECURELOG_SERVER=https://trusted-server:8443
// volumes:
// - /var/log/myapp:/logs
//
// trusted-server:
// image: trusted-server:latest
// ports:
// - "8443:8443"
// volumes:
// - /var/securelog:/data
// environment:
// - TLS_CERT=/certs/server.crt
// - TLS_KEY=/certs/server.key
//
//
// Generating Protobuf Code for Other Languages:
//
// # Python
// protoc --python_out=. proto/securelog.proto
//
// # Java
// protoc --java_out=. proto/securelog.proto
//
// # Rust
// protoc --rust_out=. proto/securelog.proto
//
// # C++
// protoc --cpp_out=. proto/securelog.proto
//
// # C#
// protoc --csharp_out=. proto/securelog.proto
//
//
// Migration from Gob to Protobuf:
//
// If you're currently using the default Gob transport and want to migrate:
//
// // Before (Gob-based):
// transport := securelog.NewHTTPTransport("https://trust.example.com")
//
// // After (Protobuf-based):
// transport := securelog.NewProtoHTTPTransport("https://trust.example.com")
//
// The rest of your logger code remains unchanged!
//
//
// Wire Format Example:
//
// InitCommitment message (hexdump):
// 0a 0c 61 70 70 2d 6c 6f 67 2d 30 30 31 12 0b 08 |..app-log-001...|
// ef c7 cb 93 06 10 00 1a 20 a1 b2 c3 d4 e5 f6 a1 |........ .......|
// b2 c3 d4 e5 f6 a1 b2 c3 d4 e5 f6 a1 b2 c3 d4 e5 |................|
// [32 bytes for key_a0, 32 bytes for key_b0...]
//
//
// Advantages over Gob Transport:
//
// Gob Transport (Default):
// ✓ No protobuf dependency
// ✓ Slightly simpler setup
// ✓ Good for Go-only deployments
// - Cannot interop with non-Go servers
// - Gob format not standardized
//
// Protobuf Transport (This Example):
// ✓ Language-agnostic (Go, Python, Java, Rust, etc.)
// ✓ Industry-standard format
// ✓ Better documentation and tooling
// ✓ More compact than JSON
// - Requires protoc compiler
// - Extra dependency (google.golang.org/protobuf)
//