Skip to content

Commit

Permalink
RUMM-2203 Make the SDK directory name unique for client token and Dat…
Browse files Browse the repository at this point in the history
…adog site
  • Loading branch information
ncreated committed Jun 22, 2022
1 parent 7bfb5c0 commit 9fcd11a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Sources/Datadog/Core/Directories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ internal extension CoreDirectory {
/// - configuration: the configuration of SDK instance. It is used to determine unique path of the core
/// directory created for this instance of the SDK.
init(in osDirectory: Directory, from configuration: CoreConfiguration) throws {
let sdkInstanceUUID = configuration.clientToken // TODO: RUMM-2203 use hash(clientToken, DD site)
let clientToken = configuration.clientToken
let site = configuration.site?.rawValue ?? ""

let sdkInstanceUUID = sha256("\(clientToken)\(site)")
let path = "com.datadoghq/v2/\(sdkInstanceUUID)"

self.init(
Expand Down
4 changes: 4 additions & 0 deletions Sources/Datadog/Core/FeaturesConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import Foundation
/// unvalidated and unresolved inputs, it should never be passed to features. Instead, `FeaturesConfiguration` should be used.
internal struct FeaturesConfiguration {
struct Common {
/// [Datadog Site](https://docs.datadoghq.com/getting_started/site/) for data uploads. It can be `nil` in V1
/// if the SDK is configured using deprecated APIs: `set(logsEndpoint:)`, `set(tracesEndpoint:)` and `set(rumEndpoint:)`.
let site: DatadogSite?
let clientToken: String
let applicationName: String
let applicationVersion: String
Expand Down Expand Up @@ -149,6 +152,7 @@ extension FeaturesConfiguration {
}

let common = Common(
site: configuration.datadogEndpoint,
clientToken: try ifValid(clientToken: configuration.clientToken),
applicationName: appContext.bundleName ?? appContext.bundleType.rawValue,
applicationVersion: appContext.bundleVersion ?? "0.0.0",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Datadog/DatadogConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension Datadog {
case never
}

public enum DatadogEndpoint {
public enum DatadogEndpoint: String {
/// US based servers.
/// Sends data to [app.datadoghq.com](https://app.datadoghq.com/).
case us1
Expand Down
8 changes: 8 additions & 0 deletions Sources/Datadog/DatadogInternal/DatadogV1Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import Foundation

/// Datadog site that SDK sends data to.
/// See: https://docs.datadoghq.com/getting_started/site/
internal typealias DatadogSite = Datadog.Configuration.DatadogEndpoint

/// The SDK context for V1, until the V2 context is created.
///
/// V2-like context can be safely assembled from V1 core components. Unlike in V2, this requires more hassle and is less performant:
Expand Down Expand Up @@ -34,6 +38,10 @@ internal struct DatadogV1Context {
internal extension DatadogV1Context {
// MARK: - Datadog Specific

/// [Datadog Site](https://docs.datadoghq.com/getting_started/site/) for data uploads. It can be `nil` in V1
/// if the SDK is configured using deprecated APIs: `set(logsEndpoint:)`, `set(tracesEndpoint:)` and `set(rumEndpoint:)`.
var site: DatadogSite? { configuration.site }

/// The client token allowing for data uploads to [Datadog Site](https://docs.datadoghq.com/getting_started/site/).
var clientToken: String { configuration.clientToken }

Expand Down
43 changes: 42 additions & 1 deletion Tests/DatadogTests/Datadog/Core/DirectoriesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,53 @@ class DirectoriesTests: XCTestCase {
super.tearDown()
}

func testWhenCreatingCoreDirectory_thenItsNameIsUniqueForClientTokenAndSite() throws {
// Given
let fixtures: [(clientToken: String, site: DatadogSite?, expectedName: String)] = [
("abcdef", .us1, "d5f91716d9c17bc76cb9931e1f9ff37724a27d4c05f1eb7081f59ea34d44c777"),
("abcdef", .us3, "4a2e7e5b459af9976950e85463db2ba1e71500cdd77ead26b41559bf5a372dfb"),
("abcdef", .us5, "38028ebbeab2aa980eab9e5a8ee714f93e8118621472697dabe084e6a9c55cd1"),
("abcdef", .eu1, "ff203358d7d236d35dd6acbe6f74b2db17c5855c9a8c43d4f9c2d6869af413e9"),
("abcdef", .us1_fed, "2a69100a36ae68ad3b081daa4c254fcade6b804ec71eda9109b7ec4b8317940b"),
("abcdef", nil, "bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721"),
("ghijkl", .us1, "158931c9e9576ef6ed1576721227d29e641e3f0ec2083e4bff280684f6b7ca94"),
("ghijkl", .us3, "e098808a9b0e3695f6b876ff677e50aaf98034606369abeabd5df45bbe8bb739"),
("ghijkl", .us5, "6212ba431e02e4da2da2f36a5fe9d26b4c33641a63be75c22e81196acfde7d91"),
("ghijkl", .eu1, "16fbe70ae92694f96bb36021589ae2ae5f050872548c26fe320cde96eac81957"),
("ghijkl", .us1_fed, "1585291b515c607624ed20935382bde4438ffac64f190b20a064eb6c1b734c6b"),
("ghijkl", nil, "54f6ee81b58accbc57adbceb0f50264897626060071dc9e92f897e7b373deb93"),
]

// When
let coreDirectories = try fixtures.map { clientToken, site, _ in
try CoreDirectory(
in: temporaryDirectory,
from: .mockWith(site: site, clientToken: clientToken)
)
}
defer { coreDirectories.forEach { $0.delete() } }

// Then
zip(fixtures, coreDirectories).forEach { fixture, coreDirectory in
let directoryName = coreDirectory.coreDirectory.url.lastPathComponent
XCTAssertEqual(directoryName, fixture.expectedName)
XCTAssertFalse(
directoryName.contains(fixture.clientToken),
"The core directory name must not include client token"
)
}
}

func testGivenDifferentSDKConfigurations_whenCreatingCoreDirectories_thenEachDirectoryIsUnique() throws {
// Given
let sdkConfigurations: [CoreConfiguration] = (0..<50).map { index in
let randomSite: DatadogSite = .mockRandom()
let randomClientToken: String = .mockRandom(among: .alphanumerics, length: 31) + "\(index)"

return .mockWith(clientToken: randomClientToken)
return .mockWith(
site: randomSite,
clientToken: randomClientToken
)
}

// When
Expand Down
8 changes: 7 additions & 1 deletion Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ extension BundleType: CaseIterable {
public static var allCases: [Self] { [.iOSApp, iOSAppExtension] }
}

extension Datadog.Configuration.DatadogEndpoint: RandomMockable {
extension Datadog.Configuration.DatadogEndpoint: AnyMockable, RandomMockable {
static func mockAny() -> Datadog.Configuration.DatadogEndpoint {
return .us1
}

static func mockRandom() -> Self {
return [.us1, .us3, .eu1, .us1_fed].randomElement()!
}
Expand Down Expand Up @@ -191,6 +195,7 @@ extension FeaturesConfiguration.Common {
static func mockAny() -> Self { mockWith() }

static func mockWith(
site: DatadogSite? = .mockAny(),
clientToken: String = .mockAny(),
applicationName: String = .mockAny(),
applicationVersion: String = .mockAny(),
Expand All @@ -205,6 +210,7 @@ extension FeaturesConfiguration.Common {
encryption: DataEncryption? = nil
) -> Self {
return .init(
site: site,
clientToken: clientToken,
applicationName: applicationName,
applicationVersion: applicationVersion,
Expand Down

0 comments on commit 9fcd11a

Please sign in to comment.