Skip to content

Commit

Permalink
sync: Allow storing allll the sync stages (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
russellhancox authored Feb 13, 2025
1 parent b8fe3ad commit a1043ea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
10 changes: 5 additions & 5 deletions Source/santasyncservice/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ objc_library(
}),
)

# This setting is used to enable storing all event uploads. See the
# -storeEventUploads method in SNTSyncEventUpload.mm for more details.
# This setting is used to enable storing all sync requests as JSON files on disk.
# This can be useful for debugging purposes.
config_setting(
name = "store_event_uploads",
values = {"define": "SANTA_STORE_EVENTUPLOADS=1"},
name = "store_json_uploads",
values = {"define": "SANTA_STORE_SYNC_JSON=1"},
)

objc_library(
Expand Down Expand Up @@ -78,7 +78,7 @@ objc_library(
],
hdrs = ["SNTSyncManager.h"],
defines = select({
":store_event_uploads": ["SANTA_STORE_EVENTUPLOADS"],
":store_json_uploads": ["SANTA_STORE_SYNC_JSON"],
"//conditions:default": [],
}),
sdk_dylibs = ["libz"],
Expand Down
24 changes: 3 additions & 21 deletions Source/santasyncservice/SNTSyncEventUpload.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ - (BOOL)uploadEvents:(NSArray<SNTStoredEvent *> *)events {
if (self.syncState.syncType == SNTSyncTypeNormal ||
[[SNTConfigurator configurator] enableCleanSyncEventUpload]) {
::pbv1::EventUploadResponse response;
NSURLRequest *urlReq = [self requestWithMessage:req];
#if defined(SANTA_STORE_EVENTUPLOADS) && defined(DEBUG)
[self storeEventUploads:urlReq];
#endif
NSError *err = [self performRequest:urlReq intoMessage:&response timeout:30];
NSError *err = [self performRequest:[self requestWithMessage:req]
intoMessage:&response
timeout:30];
if (err) {
SLOGE(@"Failed to upload events: %@", err);
return NO;
Expand Down Expand Up @@ -214,20 +212,4 @@ - (BOOL)uploadEvents:(NSArray<SNTStoredEvent *> *)events {
return *e;
}

#if defined(SANTA_STORE_EVENTUPLOADS) && defined(DEBUG)
// If Santa is built with both SANTA_STORE_EVENTUPLOADS and DEBUG defined,
// all event upload requests will be stored, in JSON form, to /var/db/santa/eventuploads.
// As santasyncservice runs as nobody, this directory must already exist and be owned by
// the nobody user.
- (void)storeEventUploads:(NSURLRequest *)request {
NSError *err;
NSString *path = [NSString
stringWithFormat:@"/var/db/santa/eventuploads/%f.json", [NSDate date].timeIntervalSince1970];
[request.HTTPBody writeToURL:[NSURL fileURLWithPath:path] options:NSDataWritingAtomic error:&err];
if (err) {
LOGE(@"Failed to write eventupload to file: %@", err);
}
}
#endif

@end
35 changes: 27 additions & 8 deletions Source/santasyncservice/SNTSyncStage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ - (NSString *)stageURL {
- (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message {
if (!message) return [self requestWithData:[NSData data] contentType:nil];

#ifndef SANTA_STORE_EVENTUPLOADS
#ifndef SANTA_STORE_SYNC_JSON
if ([[SNTConfigurator configurator] syncEnableProtoTransfer]) {
std::string data;
if (!message->SerializeToString(&data)) {
Expand All @@ -76,7 +76,7 @@ - (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message
return [self requestWithData:[NSData dataWithBytes:data.data() length:data.size()]
contentType:@"application/x-protobuf"];
}
#endif
#endif // SANTA_STORE_SYNC_JSON

google::protobuf::json::PrintOptions options{};
std::string json;
Expand All @@ -89,8 +89,13 @@ - (NSMutableURLRequest *)requestWithMessage:(google::protobuf::Message *)message
return nil;
}

return [self requestWithData:[NSData dataWithBytes:json.data() length:json.size()]
contentType:@"application/json"];
NSData *data = [NSData dataWithBytes:json.data() length:json.size()];

#ifdef SANTA_STORE_SYNC_JSON
[self storeJSON:data withMessageType:santa::StringToNSString(message->GetTypeName())];
#endif

return [self requestWithData:data contentType:@"application/json"];
}

- (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSString *)contentType {
Expand All @@ -102,7 +107,6 @@ - (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSSt
NSString *xsrfHeader = self.syncState.xsrfTokenHeader ?: kDefaultXSRFTokenHeader;
[req setValue:self.syncState.xsrfToken forHTTPHeaderField:xsrfHeader];

#ifndef SANTA_STORE_EVENTUPLOADS
NSData *compressed;
NSString *contentEncodingHeader;

Expand All @@ -125,7 +129,6 @@ - (NSMutableURLRequest *)requestWithData:(NSData *)requestBody contentType:(NSSt
requestBody = compressed;
[req setValue:contentEncodingHeader forHTTPHeaderField:@"Content-Encoding"];
}
#endif

[self addExtraRequestHeaders:req];

Expand Down Expand Up @@ -232,7 +235,7 @@ - (NSError *)performRequest:(NSURLRequest *)request
return nil;
}

#ifndef SANTA_STORE_EVENTUPLOADS
#ifndef SANTA_STORE_SYNC_JSON
if ([[SNTConfigurator configurator] syncEnableProtoTransfer]) {
if (!message->ParseFromString(std::string((const char *)data.bytes, data.length))) {
NSString *errStr = @"Failed to parse response proto into message";
Expand All @@ -243,7 +246,7 @@ - (NSError *)performRequest:(NSURLRequest *)request
}
return nil;
}
#endif
#endif // SANTA_STORE_SYNC_JSON

google::protobuf::json::ParseOptions options{
.ignore_unknown_fields = true,
Expand Down Expand Up @@ -342,4 +345,20 @@ - (BOOL)fetchXSRFToken {
return success;
}

#if defined(SANTA_STORE_SYNC_JSON) && defined(DEBUG)
// If Santa is built with both SANTA_STORE_SYNC_JSON and DEBUG defined,
// all sync requests will be stored, in JSON form, to /var/db/santa/storedsyncs.
// As santasyncservice runs as nobody, this directory must already exist and be
// owned by the nobody user.
- (void)storeJSON:(NSData *)json withMessageType:(NSString *)msgType {
NSError *err;
NSString *path = [NSString stringWithFormat:@"/var/db/santa/storedsyncs/%@.%f.json", msgType,
[NSDate date].timeIntervalSince1970];
[json writeToURL:[NSURL fileURLWithPath:path] options:NSDataWritingAtomic error:&err];
if (err) {
LOGE(@"Failed to write %@ to file: %@", msgType, err);
}
}
#endif

@end

0 comments on commit a1043ea

Please sign in to comment.