Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: local event data encrypt #322

Open
wants to merge 4 commits into
base: base/3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#import <XCTest/XCTest.h>

#import "GrowingTrackerCore/Utils/GrowingKeyChainWrapper.h"
#import "GrowingULKeyChainWrapper.h"

@interface GrowingKeyChainTest : XCTestCase

Expand All @@ -36,17 +36,13 @@ - (void)tearDown {
}

- (void)testGrowingKeyChainWrapper {
[GrowingKeyChainWrapper setKeychainObject:@"KeyChainTest" forKey:@"KeyChainTestKey"];
[GrowingULKeyChainWrapper setKeychainObject:@"KeyChainTest" forKey:@"KeyChainTestKey"];

NSString *obj = [GrowingKeyChainWrapper keyChainObjectForKey:@"KeyChainTestKey"];
NSString *obj = [GrowingULKeyChainWrapper keyChainObjectForKey:@"KeyChainTestKey"];
XCTAssertTrue([obj isEqualToString:@"KeyChainTest"]);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
[GrowingKeyChainWrapper performSelector:@selector(removeKeyChainObjectForKey:) withObject:@"KeyChainTestKey"];
#pragma clang diagnostic pop

NSString *obj2 = [GrowingKeyChainWrapper keyChainObjectForKey:@"KeyChainTestKey"];
[GrowingULKeyChainWrapper removeKeyChainObjectForKey:@"KeyChainTestKey"];
NSString *obj2 = [GrowingULKeyChainWrapper keyChainObjectForKey:@"KeyChainTestKey"];
XCTAssertTrue(obj2 == nil);
}

Expand Down
4 changes: 2 additions & 2 deletions GrowingAnalytics.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和
end

s.subspec 'TrackerCore' do |trackerCore|
trackerCore.dependency 'GrowingUtils/TrackerCore', '~> 1.2.3'
trackerCore.dependency 'GrowingUtils/TrackerCore', '~> 1.3.0'
trackerCore.source_files = 'GrowingTrackerCore/**/*{.h,.m,.c,.cpp,.mm}'
trackerCore.exclude_files = 'GrowingTrackerCore/Utils/UserIdentifier/GrowingUserIdentifier_NoIDFA.m'
trackerCore.public_header_files = 'GrowingTrackerCore/Public/*.h'
Expand All @@ -51,7 +51,7 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和

s.subspec 'AutotrackerCore' do |autotrackerCore|
autotrackerCore.ios.deployment_target = '10.0'
autotrackerCore.dependency 'GrowingUtils/AutotrackerCore', '~> 1.2.3'
autotrackerCore.dependency 'GrowingUtils/AutotrackerCore', '~> 1.3.0'
autotrackerCore.source_files = 'GrowingAutotrackerCore/**/*{.h,.m,.c,.cpp,.mm}'
autotrackerCore.public_header_files = 'GrowingAutotrackerCore/Public/*.h'
autotrackerCore.dependency 'GrowingAnalytics/TrackerCore', s.version.to_s
Expand Down
5 changes: 4 additions & 1 deletion GrowingTrackerCore/Event/GrowingEventManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,10 @@ - (void)prettyLogForEvents:(NSArray<id<GrowingEventPersistenceProtocol>> *)event
withChannel:(GrowingEventChannel *)channel {
NSMutableArray *arrayM = [NSMutableArray array];
for (id<GrowingEventPersistenceProtocol> event in events) {
[arrayM addObject:event.toJSONObject];
id jsonObject = event.toJSONObject;
if (jsonObject) {
[arrayM addObject:jsonObject];
}
}
GIOLogVerbose(@"Send events = %@\n", arrayM);
}
Expand Down
2 changes: 2 additions & 0 deletions GrowingTrackerCore/GrowingTrackConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ - (instancetype)initWithProjectId:(NSString *)projectId {
_urlScheme = nil;
_encryptEnabled = NO;
_requestTimeout = 30;
_localEventEncryptEnabled = NO;

// Advert
_ASAEnabled = NO;
Expand Down Expand Up @@ -91,6 +92,7 @@ - (id)copyWithZone:(NSZone *)zone {
configuration->_urlScheme = _urlScheme;
configuration->_encryptEnabled = _encryptEnabled;
configuration->_requestTimeout = _requestTimeout;
configuration->_localEventEncryptEnabled = _localEventEncryptEnabled;

// Advert
configuration->_ASAEnabled = _ASAEnabled;
Expand Down
1 change: 1 addition & 0 deletions GrowingTrackerCore/Public/GrowingTrackConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ FOUNDATION_EXPORT NSString *const kGrowingDefaultDataCollectionServerHost;
@property (nonatomic, copy) NSString *urlScheme;
@property (nonatomic, assign) BOOL encryptEnabled;
@property (nonatomic, assign) NSTimeInterval requestTimeout;
@property (nonatomic, assign) BOOL localEventEncryptEnabled;

- (instancetype)initWithProjectId:(NSString *)projectId;

Expand Down
8 changes: 4 additions & 4 deletions GrowingTrackerCore/Utils/GrowingDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h"
#import "GrowingTrackerCore/Thread/GrowingDispatchManager.h"
#import "GrowingTrackerCore/Utils/GrowingInternalMacros.h"
#import "GrowingTrackerCore/Utils/GrowingKeyChainWrapper.h"
#import "GrowingTrackerCore/Utils/UserIdentifier/GrowingUserIdentifier.h"
#import "GrowingULAppLifecycle.h"
#import "GrowingULKeyChainWrapper.h"

static NSString *kGrowingUrlScheme = nil;
NSString *const kGrowingKeychainUserIdKey = @"kGrowingIOKeychainUserIdKey";
static NSString *const kGrowingKeychainUserIdKey = @"kGrowingIOKeychainUserIdKey";

@interface GrowingDeviceInfo () <GrowingULAppLifecycleDelegate>

Expand Down Expand Up @@ -124,15 +124,15 @@ - (NSString *)getCurrentUrlScheme {

- (NSString *)getDeviceIdString {
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
NSString *deviceIdString = [GrowingKeyChainWrapper keyChainObjectForKey:kGrowingKeychainUserIdKey];
NSString *deviceIdString = [GrowingULKeyChainWrapper keyChainObjectForKey:kGrowingKeychainUserIdKey];
if ([deviceIdString growingHelper_isValidU]) {
return deviceIdString;
}
#endif

NSString *uuid = [GrowingUserIdentifier getUserIdentifier];
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
[GrowingKeyChainWrapper setKeychainObject:uuid forKey:kGrowingKeychainUserIdKey];
[GrowingULKeyChainWrapper setKeychainObject:uuid forKey:kGrowingKeychainUserIdKey];
#endif
return uuid;
}
Expand Down
42 changes: 0 additions & 42 deletions GrowingTrackerCore/Utils/GrowingKeyChainWrapper.h

This file was deleted.

79 changes: 0 additions & 79 deletions GrowingTrackerCore/Utils/GrowingKeyChainWrapper.m

This file was deleted.

59 changes: 47 additions & 12 deletions Modules/Protobuf/GrowingEventProtobufDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// limitations under the License.

#import "Modules/Protobuf/GrowingEventProtobufDatabase.h"
#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h"
#import "GrowingULEncryptor.h"
#import "GrowingULTimeUtil.h"
#import "Modules/Protobuf/GrowingEventProtobufPersistence.h"
#import "Services/Database/FMDB/GrowingFMDB.h"
Expand Down Expand Up @@ -156,12 +158,22 @@ - (BOOL)insertEvent:(GrowingEventProtobufPersistence *)event {
self.databaseError = error;
return;
}
result = [db executeUpdate:@"INSERT INTO namedcachetable(key,value,createAt,type,policy) VALUES(?,?,?,?,?)",
event.eventUUID,
((GrowingEventProtobufPersistence *)event).data,
@([GrowingULTimeUtil currentTimeMillis]),
event.eventType,
@(event.policy)];
BOOL encryptEnabled = GrowingConfigurationManager.sharedInstance.trackConfiguration.localEventEncryptEnabled;
NSData *value = [NSData data];
NSData *enc_value = [NSData data];
if (encryptEnabled) {
enc_value = [[GrowingULEncryptor encryptor] aesEncrypt:event.data];
} else {
value = event.data;
}
result = [db
executeUpdate:@"INSERT INTO namedcachetable(key,value,enc_value,createAt,type,policy) VALUES(?,?,?,?,?,?)",
event.eventUUID,
value,
enc_value,
@([GrowingULTimeUtil currentTimeMillis]),
event.eventType,
@(event.policy)];

if (!result) {
self.databaseError = [self writeErrorInDatabase:db];
Expand All @@ -184,12 +196,24 @@ - (BOOL)insertEvents:(NSArray<GrowingEventProtobufPersistence *> *)events {
}
for (int i = 0; i < events.count; i++) {
GrowingEventProtobufPersistence *event = (GrowingEventProtobufPersistence *)events[i];
result = [db executeUpdate:@"INSERT INTO namedcachetable(key,value,createAt,type,policy) VALUES(?,?,?,?,?)",
event.eventUUID,
event.data,
@([GrowingULTimeUtil currentTimeMillis]),
event.eventType,
@(event.policy)];
BOOL encryptEnabled =
GrowingConfigurationManager.sharedInstance.trackConfiguration.localEventEncryptEnabled;
NSData *value = [NSData data];
NSData *enc_value = [NSData data];
if (encryptEnabled) {
enc_value = [[GrowingULEncryptor encryptor] aesEncrypt:event.data];
} else {
value = event.data;
}
result =
[db executeUpdate:
@"INSERT INTO namedcachetable(key,value,enc_value,createAt,type,policy) VALUES(?,?,?,?,?,?)",
event.eventUUID,
value,
enc_value,
@([GrowingULTimeUtil currentTimeMillis]),
event.eventType,
@(event.policy)];

if (!result) {
self.databaseError = [self writeErrorInDatabase:db];
Expand Down Expand Up @@ -308,6 +332,13 @@ - (BOOL)initDB {
self.databaseError = [self createDBErrorInDatabase:db];
return;
}
NSString *sqlCreateEncValue = @"ALTER TABLE namedcachetable ADD enc_value BLOB";
if (![db columnExists:@"enc_value" inTableWithName:@"namedcachetable"]) {
if (![db executeUpdate:sqlCreateEncValue]) {
self.databaseError = [self createDBErrorInDatabase:db];
return;
}
}
result = YES;
}];

Expand Down Expand Up @@ -394,6 +425,10 @@ - (void)enumerateKeysAndValuesUsingBlock:
while (!stop && [set next]) {
NSString *key = [set stringForColumn:@"key"];
NSData *value = [set dataForColumn:@"value"];
NSData *enc_value = [set dataForColumn:@"enc_value"];
if (enc_value && enc_value.length > 0) {
value = [[GrowingULEncryptor encryptor] aesDecrypt:enc_value];
}
NSString *type = [set stringForColumn:@"type"];
NSUInteger policy = [set intForColumn:@"policy"];
block(key, value, type, policy, &stop);
Expand Down
10 changes: 5 additions & 5 deletions Modules/V2AdapterTrackOnly/GrowingV2Adapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#import "Modules/V2AdapterTrackOnly/Public/GrowingV2Adapter.h"
#import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h"
#import "GrowingTrackerCore/Helpers/GrowingHelpers.h"
#import "GrowingTrackerCore/Utils/GrowingKeyChainWrapper.h"
#import "GrowingULKeyChainWrapper.h"

static NSString *kGrowingUserdefault_2xto3x = @"growingio.userdefault.2xto3x";
static NSString *const kGrowingKeychainUserIdKey = @"kGrowingIOKeychainUserIdKey";
Expand Down Expand Up @@ -55,13 +55,13 @@ + (void)upgrade {

// deviceId
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
NSString *deviceId = [GrowingKeyChainWrapper keyChainObjectForKey:@"GROWINGIO_CUSTOM_U_KEY"];
NSString *deviceId = [GrowingULKeyChainWrapper keyChainObjectForKey:@"GROWINGIO_CUSTOM_U_KEY"];
if ([deviceId growingHelper_isValidU]) {
[GrowingKeyChainWrapper setKeychainObject:deviceId forKey:kGrowingKeychainUserIdKey];
[GrowingULKeyChainWrapper setKeychainObject:deviceId forKey:kGrowingKeychainUserIdKey];
} else {
deviceId = [GrowingKeyChainWrapper keyChainObjectForKey:@"GROWINGIO_KEYCHAIN_KEY"];
deviceId = [GrowingULKeyChainWrapper keyChainObjectForKey:@"GROWINGIO_KEYCHAIN_KEY"];
if ([deviceId growingHelper_isValidU]) {
[GrowingKeyChainWrapper setKeychainObject:deviceId forKey:kGrowingKeychainUserIdKey];
[GrowingULKeyChainWrapper setKeychainObject:deviceId forKey:kGrowingKeychainUserIdKey];
}
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/growingio/growingio-sdk-ios-utilities.git",
"1.2.3" ..< "1.3.0"
"1.3.0" ..< "1.4.0"
),
.package(
url: "https://github.com/growingio/growingio-sdk-ios-performance-ext.git",
Expand Down
Loading
Loading