Skip to content

RBS in encryption migration #638

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

Open
wants to merge 1 commit into
base: SDKS-9510_4
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
12 changes: 12 additions & 0 deletions Split/Common/Utils/DbCipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct DbCipher {
}
dbHelper.performAndWait {
updateSplits()
updateRuleBasedSegments()
updateSegments(type: .mySegment)
updateSegments(type: .myLargeSegment)
updateImpressions()
Expand All @@ -64,6 +65,17 @@ struct DbCipher {
}
}

private func updateRuleBasedSegments() {
let items = dbHelper.fetch(entity: .ruleBasedSegment).compactMap { return $0 as? RuleBasedSegmentEntity }
for item in items {
let name = fromCipher?.decrypt(item.name) ?? item.name
item.name = toCipher?.encrypt(name) ?? name

let body = fromCipher?.decrypt(item.body) ?? item.body
item.body = toCipher?.encrypt(body) ?? body
}
}

private func updateSegments(type entity: CoreDataEntity) {
let items = dbHelper.fetch(entity: entity).compactMap { return $0 as? MySegmentEntity }
for item in items {
Expand Down
11 changes: 10 additions & 1 deletion SplitTests/DbCipherTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class DbCipherTest: XCTestCase {
// Attributes
XCTAssertNotEqual(userKey, resultBefore.attributes.userKey)
XCTAssertFalse(resultBefore.attributes.attributes.contains("att1"))
// RBS
XCTAssertNotEqual("test_rbs", resultBefore.ruleBasedSegments.name)
XCTAssertFalse(resultBefore.ruleBasedSegments.body.contains("test_rbs"))

// Decrypted data
// Splits
Expand All @@ -80,6 +83,9 @@ class DbCipherTest: XCTestCase {
// Attributes
XCTAssertEqual(userKey, resultAfter.attributes.userKey)
XCTAssertTrue(resultAfter.attributes.attributes.contains("att1"))
// RBS
XCTAssertEqual("test_rbs", resultAfter.ruleBasedSegments.name)
XCTAssertTrue(resultAfter.ruleBasedSegments.body.contains("test_"))
}

struct DataResult {
Expand All @@ -91,6 +97,7 @@ class DbCipherTest: XCTestCase {
let impressionsCount: String
let uniqueKeys: (userKey: String, features: String)
let attributes: (userKey: String, attributes: String)
let ruleBasedSegments: (name: String, body: String)
}

private func loadData(dbHelper: CoreDataHelper) -> DataResult {
Expand All @@ -109,7 +116,8 @@ class DbCipherTest: XCTestCase {
uniqueKeys: dbHelper.fetch(entity: .uniqueKey).compactMap { $0 as? UniqueKeyEntity }.map { (userKey: $0.userKey,
features: $0.featureList) }[0],
attributes: dbHelper.fetch(entity: .attribute).compactMap { $0 as? AttributeEntity }.map { (userKey: $0.userKey!,
attributes: $0.attributes!) }[0]
attributes: $0.attributes!) }[0],
ruleBasedSegments: dbHelper.fetch(entity: .ruleBasedSegment).compactMap { $0 as? RuleBasedSegmentEntity }.map { (name: $0.name, body: $0.body )}[0]
)
}
return result!
Expand All @@ -129,6 +137,7 @@ class DbCipherTest: XCTestCase {
db.impressionsCountDao.insert(ImpressionsCountPerFeature(storageId: "id1", feature: "pepe", timeframe: 111111, count: 1) )
db.uniqueKeyDao.insert(UniqueKey(userKey: IntegrationHelper.dummyUserKey, features: ["split1"]))
db.attributesDao.update(userKey: userKey, attributes: ["att1": 1])
db.ruleBasedSegmentDao.insertOrUpdate(segment: TestingHelper.createRuleBasedSegment())
}

private func createDbHelper() -> CoreDataHelper {
Expand Down
7 changes: 5 additions & 2 deletions SplitTests/Helpers/TestingHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,11 @@ struct TestingHelper {
let msChange = SegmentChange(segments: ms, changeNumber: msCn)
let mlsChange = SegmentChange(segments: mls, changeNumber: mlsCn)
return try! Json.encodeToJson(newAllSegmentsChange(msChange: msChange, mlsChange: mlsChange))


}

static func createRuleBasedSegment(name: String = "test_rbs", trafficTypeName: String = "user", changeNumber: Int64 = 1000, status: Status = .active, conditions: [Condition]? = nil, excluded: Excluded? = nil) -> RuleBasedSegment {
let segment = RuleBasedSegment(name: name, trafficTypeName: trafficTypeName, changeNumber: changeNumber, status: status, conditions: conditions, excluded: excluded)
segment.isParsed = true
return segment
}
}