Skip to content

Commit

Permalink
feat: revamped db encryption (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pallab Maiti authored Sep 19, 2023
1 parent 7a590f0 commit 0efaffa
Show file tree
Hide file tree
Showing 30 changed files with 604 additions and 263,915 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ED761A052727E28800B086F4 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = ED7619FA2727E28800B086F4 /* Images.xcassets */; };
ED761A062727E28800B086F4 /* CustomFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = ED7619FC2727E28800B086F4 /* CustomFactory.m */; };
ED761A072727E28800B086F4 /* _AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = ED7619FD2727E28800B086F4 /* _AppDelegate.m */; };
ED8738CE2AB363A80076D24A /* EncryptedDatabaseProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = ED8738CC2AB363A80076D24A /* EncryptedDatabaseProvider.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -55,6 +56,8 @@
ED7619FB2727E28800B086F4 /* CustomIntegration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomIntegration.h; sourceTree = "<group>"; };
ED7619FC2727E28800B086F4 /* CustomFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomFactory.m; sourceTree = "<group>"; };
ED7619FD2727E28800B086F4 /* _AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _AppDelegate.m; sourceTree = "<group>"; };
ED8738CA2AB363A80076D24A /* EncryptedDatabaseProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EncryptedDatabaseProvider.h; sourceTree = "<group>"; };
ED8738CC2AB363A80076D24A /* EncryptedDatabaseProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EncryptedDatabaseProvider.m; sourceTree = "<group>"; };
F928F8A942558010CC7088BF /* Pods-RudderSampleAppObjC.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RudderSampleAppObjC.debug.xcconfig"; path = "Target Support Files/Pods-RudderSampleAppObjC/Pods-RudderSampleAppObjC.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -139,6 +142,8 @@
ED7619FC2727E28800B086F4 /* CustomFactory.m */,
ED7619FB2727E28800B086F4 /* CustomIntegration.h */,
ED7619EF2727E28700B086F4 /* CustomIntegration.m */,
ED8738CA2AB363A80076D24A /* EncryptedDatabaseProvider.h */,
ED8738CC2AB363A80076D24A /* EncryptedDatabaseProvider.m */,
ED7619FA2727E28800B086F4 /* Images.xcassets */,
ED7619ED2727E28700B086F4 /* InfoPlist.strings */,
ED7619F32727E28700B086F4 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -234,12 +239,14 @@
"${BUILT_PRODUCTS_DIR}/MetricsReporter-iOS12.0/MetricsReporter.framework",
"${BUILT_PRODUCTS_DIR}/RudderKit-iOS12.0/RudderKit.framework",
"${BUILT_PRODUCTS_DIR}/Rudder-iOS/Rudder.framework",
"${BUILT_PRODUCTS_DIR}/SQLCipher/SQLCipher.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MetricsReporter.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RudderKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Rudder.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SQLCipher.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -277,6 +284,7 @@
files = (
ED0CA6DF2A7D049E00899C1C /* RudderConfig.swift in Sources */,
ED761A032727E28800B086F4 /* main.m in Sources */,
ED8738CE2AB363A80076D24A /* EncryptedDatabaseProvider.m in Sources */,
ED761A062727E28800B086F4 /* CustomFactory.m in Sources */,
ED761A002727E28800B086F4 /* CustomIntegration.m in Sources */,
ED761A072727E28800B086F4 /* _AppDelegate.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// EncryptedDatabaseProvider.h
// RudderDatabaseEncryption
//
// Created by Pallab Maiti on 14/09/23.
//

#import <Foundation/Foundation.h>
#import <Rudder/Rudder.h>

NS_ASSUME_NONNULL_BEGIN

@interface EncryptedDatabaseProvider : NSObject<RSDatabaseProvider>

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// EncryptedDatabaseProvider.m
// RudderDatabaseEncryption
//
// Created by Pallab Maiti on 14/09/23.
//

#import "EncryptedDatabaseProvider.h"
#import "sqlite3.h"

@interface RSEncryptedDatabase : NSObject <RSDatabase>

@end

@implementation RSEncryptedDatabase {
sqlite3 *db;
}

- (int)open_v2:(const char *)filename flags:(int)flags zVfs:(const char *)zVfs {
return sqlite3_open_v2(filename, &db, flags, zVfs);
}


- (int)exec:(const char *)zSql xCallback:(callback)xCallback pArg:(void *)pArg pzErrMsg:(char * _Nullable *)pzErrMsg {
return sqlite3_exec(db, zSql, xCallback, pArg, pzErrMsg);
}

- (int)close {
return sqlite3_close(db);
}

- (int)step:(void *)pStmt {
return sqlite3_step(pStmt);
}

- (int)finalize:(void *)pStmt {
return sqlite3_finalize(pStmt);
}

- (int)prepare_v2:(const char *)zSql nBytes:(int)nBytes ppStmt:(void **)ppStmt pzTail:(const char **)pzTail {
return sqlite3_prepare_v2(db, zSql, nBytes, (sqlite3_stmt **)(ppStmt), pzTail);
}

- (int)column_int:(void *)pStmt i:(int)i {
return sqlite3_column_int(pStmt, i);
}

- (const unsigned char *)column_text:(void *)pStmt i:(int)i {
return sqlite3_column_text(pStmt, i);
}

- (int)key:(const void *)pKey nKey:(int)nKey {
return sqlite3_key(db, pKey, nKey);
}

@end

@implementation EncryptedDatabaseProvider

- (id<RSDatabase>)getDatabase {
return [RSEncryptedDatabase new];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import <Rudder/Rudder.h>
#import <AdSupport/ASIdentifierManager.h>
#import "RudderSampleAppObjC-Swift.h"

#import "EncryptedDatabaseProvider.h"

static int userCount = 1;
static int eventCount = 1;
Expand All @@ -27,14 +27,17 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
if (path != nil) {
NSURL *url = [NSURL fileURLWithPath:path];
RudderConfig *rudderConfig = [RudderConfig createFrom:url];
NSLog(@"------%@------", NSHomeDirectory());
if (rudderConfig != nil) {
RSConfigBuilder *builder = [[RSConfigBuilder alloc] init];
[builder withLoglevel:RSLogLevelVerbose];
[builder withTrackLifecycleEvens:YES];
[builder withCollectDeviceId:NO];
[builder withRecordScreenViews:YES];
[builder withDataPlaneUrl:rudderConfig.DEV_DATA_PLANE_URL];
[builder withDBEncryption:[[RSDBEncryption alloc] initWithKey:@"test1234" enable:YES]];
[builder withControlPlaneUrl:rudderConfig.DEV_CONTROL_PLANE_URL];
[builder withSleepTimeOut:3000];
[builder withDBEncryption:[[RSDBEncryption alloc] initWithKey:@"test1234" enable:NO databaseProvider:[EncryptedDatabaseProvider new]]];
[RSClient getInstance:rudderConfig.WRITE_KEY config:[builder build]];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ED0CA7042A7D0B2B00899C1C /* RudderConfig.plist in Resources */ = {isa = PBXBuildFile; fileRef = ED0CA7012A7D0B2B00899C1C /* RudderConfig.plist */; };
ED0CA7052A7D0B2B00899C1C /* SampleRudderConfig.plist in Resources */ = {isa = PBXBuildFile; fileRef = ED0CA7022A7D0B2B00899C1C /* SampleRudderConfig.plist */; };
ED0CA7062A7D0B2B00899C1C /* RudderConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED0CA7032A7D0B2B00899C1C /* RudderConfig.swift */; };
ED8738D02AB36C230076D24A /* EncryptedDatabaseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED8738CF2AB36C230076D24A /* EncryptedDatabaseProvider.swift */; };
EDEAEBC7299CB30200D537C3 /* CustomFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDEAEBC6299CB30200D537C3 /* CustomFilter.swift */; };
/* End PBXBuildFile section */

Expand All @@ -38,6 +39,8 @@
ED0CA7012A7D0B2B00899C1C /* RudderConfig.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = RudderConfig.plist; sourceTree = "<group>"; };
ED0CA7022A7D0B2B00899C1C /* SampleRudderConfig.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = SampleRudderConfig.plist; sourceTree = "<group>"; };
ED0CA7032A7D0B2B00899C1C /* RudderConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RudderConfig.swift; sourceTree = "<group>"; };
ED8738CF2AB36C230076D24A /* EncryptedDatabaseProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncryptedDatabaseProvider.swift; sourceTree = "<group>"; };
ED8738D82AB41F520076D24A /* RudderSampleAppSwift-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RudderSampleAppSwift-Bridging-Header.h"; sourceTree = "<group>"; };
EDEAEBC6299CB30200D537C3 /* CustomFilter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomFilter.swift; sourceTree = "<group>"; };
FD4DAD4049ABA2A60254AD71 /* Pods-RudderSampleAppSwift.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RudderSampleAppSwift.debug.xcconfig"; path = "Target Support Files/Pods-RudderSampleAppSwift/Pods-RudderSampleAppSwift.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -82,10 +85,12 @@
06EABC8724665E470043D720 /* SceneDelegate.swift */,
ED00467128A64DE50007206F /* SessionViewController.swift */,
06EABC8924665E470043D720 /* ViewController.swift */,
ED8738CF2AB36C230076D24A /* EncryptedDatabaseProvider.swift */,
06EABC8B24665E470043D720 /* Main.storyboard */,
06EABC8E24665E480043D720 /* Assets.xcassets */,
06EABC9024665E480043D720 /* LaunchScreen.storyboard */,
06EABC9324665E480043D720 /* Info.plist */,
ED8738D82AB41F520076D24A /* RudderSampleAppSwift-Bridging-Header.h */,
);
path = RudderSampleAppSwift;
sourceTree = "<group>";
Expand Down Expand Up @@ -237,6 +242,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
ED8738D02AB36C230076D24A /* EncryptedDatabaseProvider.swift in Sources */,
06EABC8A24665E470043D720 /* ViewController.swift in Sources */,
ED00467228A64DE50007206F /* SessionViewController.swift in Sources */,
06EABC8624665E470043D720 /* AppDelegate.swift in Sources */,
Expand Down Expand Up @@ -398,8 +404,10 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.rudderstack.ios.test.swift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "RudderSampleAppSwift/RudderSampleAppSwift-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
WARNING_CFLAGS = "$(inherited)";
};
name = Debug;
};
Expand All @@ -417,8 +425,10 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.rudderstack.ios.test.swift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "RudderSampleAppSwift/RudderSampleAppSwift-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
WARNING_CFLAGS = "$(inherited)";
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

print(NSHomeDirectory())

let builder: RSConfigBuilder = RSConfigBuilder()
.withLoglevel(RSLogLevelDebug)
.withDataPlaneUrl(rudderConfig.DEV_DATA_PLANE_URL)
.withControlPlaneUrl(rudderConfig.DEV_CONTROL_PLANE_URL)
.withTrackLifecycleEvens(false)
.withRecordScreenViews(false)
.withSleepTimeOut(3600)
.withSleepTimeOut(10)
.withSessionTimeoutMillis(30000)
.withConsentFilter(CustomFilter())
.withDBEncryption(RSDBEncryption(key: "test1234", enable: true))
.withDBEncryption(RSDBEncryption(key: "test1234", enable: true, databaseProvider: EncryptedDatabaseProvider()))
RSClient.getInstance(rudderConfig.WRITE_KEY, config: builder.build())

return true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// EncryptedDatabaseProvider.swift
// RudderSampleAppSwift
//
// Created by Pallab Maiti on 14/09/23.
// Copyright © 2023 RudderStack. All rights reserved.
//

import Foundation
import Rudder

class EncryptedDatabase: RSDatabase {

private var db: OpaquePointer?

func open_v2(_ filename: UnsafePointer<CChar>?, flags: Int32, zVfs: UnsafePointer<CChar>?) -> Int32 {
return sqlite3_open_v2(filename, &db, flags, zVfs)
}

func exec(_ zSql: UnsafePointer<CChar>?, xCallback: callback?, pArg: UnsafeMutableRawPointer?, pzErrMsg: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?) -> Int32 {
return sqlite3_exec(db, zSql, xCallback, pArg, pzErrMsg)
}

func prepare_v2(_ zSql: UnsafePointer<CChar>?, nBytes: Int32, ppStmt: UnsafeMutablePointer<UnsafeMutableRawPointer?>?, pzTail: UnsafeMutablePointer<UnsafePointer<CChar>?>?) -> Int32 {
return sqlite3_prepare_v2(db, zSql, nBytes, UnsafeMutablePointer(OpaquePointer(ppStmt)), pzTail)
}

func close() -> Int32 {
return sqlite3_close(db)
}

func step(_ pStmt: UnsafeMutableRawPointer?) -> Int32 {
return sqlite3_step(OpaquePointer(pStmt))
}

func finalize(_ pStmt: UnsafeMutableRawPointer?) -> Int32 {
return sqlite3_finalize(OpaquePointer(pStmt))
}

func column_int(_ pStmt: UnsafeMutableRawPointer?, i: Int32) -> Int32 {
return sqlite3_column_int(OpaquePointer(pStmt), i)
}

func column_text(_ pStmt: UnsafeMutableRawPointer?, i: Int32) -> UnsafePointer<UInt8> {
return sqlite3_column_text(OpaquePointer(pStmt), i)
}

func key(_ pKey: UnsafeRawPointer?, nKey: Int32) -> Int32 {
return sqlite3_key(db, pKey, nKey)
}
}

class EncryptedDatabaseProvider: RSDatabaseProvider {
func getDatabase() -> RSDatabase {
return EncryptedDatabase()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// RudderSampleAppSwift-Bridging-Header.h
// RudderSampleAppSwift
//
// Created by Pallab Maiti on 15/09/23.
// Copyright © 2023 RudderStack. All rights reserved.
//

#ifndef RudderSampleAppSwift_Bridging_Header_h
#define RudderSampleAppSwift_Bridging_Header_h

#import "sqlite3.h"

#endif /* RudderSampleAppSwift_Bridging_Header_h */
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension SessionViewController: UITableViewDataSource, UITableViewDelegate {
case 2:
RSClient.sharedInstance()?.startSession(1234567890)
case 3:
RSClient.sharedInstance()?.reset()
RSClient.sharedInstance()?.reset(false)
case 4:
count += 1
RSClient.sharedInstance()?.track("track_\(count)")
Expand Down Expand Up @@ -132,7 +132,7 @@ extension SessionViewController: UITableViewDataSource, UITableViewDelegate {
} else if index % 9 == 7 {
RSClient.sharedInstance()?.endSession()
} else if index % 9 == 8 {
RSClient.sharedInstance()?.reset()
RSClient.sharedInstance()?.reset(false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ extension ViewController: UITableViewDataSource, UITableViewDelegate {
case 8:
RSClient.sharedInstance()?.screen("ViewController", properties: ["key_1": "value_1"])
case 9:
RSClient.sharedInstance()?.reset()
RSClient.sharedInstance()?.reset(false)
case 10:
RSClient.sharedInstance()?.group("test_group_id")
case 11:
Expand Down
6 changes: 1 addition & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ let package = Package(
sources: ["Classes/"],
publicHeadersPath: "Classes/Headers/Public/",
cSettings: [
.headerSearchPath("Classes/Headers/"),
.define("SQLITE_HAS_CODEC"),
.define("SQLITE_TEMP_STORE", to: "3"),
.define("SQLCIPHER_CRYPTO_CC"),
.define("NDEBUG")
.headerSearchPath("Classes/Headers/")
]
),
.testTarget(
Expand Down
2 changes: 2 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ target 'RudderSampleAppObjC' do
platform :ios, '12.0'
shared_pods
shared_utility_pods
pod 'SQLCipher', '~> 4.0'
end

target 'RudderSampleAppSwift' do
project 'Examples/RudderSampleAppSwift/RudderSampleAppSwift.xcodeproj'
platform :ios, '12.0'
shared_pods
shared_utility_pods
pod 'SQLCipher', '~> 4.0'
end

target 'RudderSampleApptvOSObjC' do
Expand Down
Loading

0 comments on commit 0efaffa

Please sign in to comment.