diff --git a/.gitignore b/.gitignore index 2e32ae7..bb460e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,121 +1,7 @@ -# Xcode -# -# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore - -## User settings +.DS_Store +/.build +/Packages +/*.xcodeproj xcuserdata/ - -## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) -*.xcscmblueprint -*.xccheckout - -## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) -build/ DerivedData/ -*.moved-aside -*.pbxuser -!default.pbxuser -*.mode1v3 -!default.mode1v3 -*.mode2v3 -!default.mode2v3 -*.perspectivev3 -!default.perspectivev3 - -## Obj-C/Swift specific -*.hmap - -## App packaging -*.ipa -*.dSYM.zip -*.dSYM - -## Playgrounds -timeline.xctimeline -playground.xcworkspace - -# Swift Package Manager -# -# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. -# Packages/ -# Package.pins -# Package.resolved -# *.xcodeproj -# -# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata -# hence it is not needed unless you have added a package configuration file to your project -# .swiftpm - -.build/ - -# CocoaPods -# -# We recommend against adding the Pods directory to your .gitignore. However -# you should judge for yourself, the pros and cons are mentioned at: -# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control -# -# Pods/ -# -# Add this line if you want to avoid checking in source code from the Xcode workspace -# *.xcworkspace - -# Carthage -# -# Add this line if you want to avoid checking in source code from Carthage dependencies. -# Carthage/Checkouts - -Carthage/Build/ - -# Accio dependency management -Dependencies/ -.accio/ - -# fastlane -# -# It is recommended to not store the screenshots in the git repo. -# Instead, use fastlane to re-generate the screenshots whenever they are needed. -# For more information about the recommended setup visit: -# https://docs.fastlane.tools/best-practices/source-control/#source-control - -fastlane/report.xml -fastlane/Preview.html -fastlane/screenshots/**/*.png -fastlane/test_output - -# Code Injection -# -# After new code Injection tools there's a generated folder /iOSInjectionProject -# https://github.com/johnno1962/injectionforxcode - -iOSInjectionProject/ - -# MacOS -# -# Taken from https://github.com/github/gitignore/blob/master/Global/macOS.gitignore - -## General -.DS_Store -.AppleDouble -.LSOverride - -## Icon must end with two \r -Icon - -## Thumbnails -._* - -## Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -## Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/Secrets.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/Secrets.xcscheme index df4102a..d5d030a 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/Secrets.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/Secrets.xcscheme @@ -1,6 +1,6 @@ - - - - + skipped = "NO" + parallelizable = "YES" + testExecutionOrdering = "random"> + + + + posthog + The posthog key here + bugsnag + The busnag key here + + +``` diff --git a/Sources/Delivery/Key.swift b/Sources/Delivery/Key.swift new file mode 100644 index 0000000..4e3c63e --- /dev/null +++ b/Sources/Delivery/Key.swift @@ -0,0 +1,47 @@ +// +// Key.swift +// +// +// Created by Martin Dutra on 17/2/22. +// + +import Foundation + +/// The Key enum list the possible keys that a client can use to retrieve a value +public enum Key: String { + /// PostHog Project API key + case posthog + + /// Bugsnag Notifier API key + case bugsnag + + /// Planetary Push token + case push + + /// Authy token + case authy + + /// Planetary Blob token + case blob + + /// Planetary Pub token + case pub + + /// Token used to authenticate with Planetary's cloud services. + case planetaryCloudAPISecret + + /// Token used to join the Planetary room + case planetaryRoomToken + + /// Token used to join the Twit room + case twitRoomToken + + /// Token used to join the Lorentz room + case lorentzRoomToken + + /// Zendesk App ID + case zendeskAppID + + /// Zendesk Client ID + case zendeskClientID +} diff --git a/Sources/Delivery/Keys.swift b/Sources/Delivery/Keys.swift new file mode 100644 index 0000000..c45918d --- /dev/null +++ b/Sources/Delivery/Keys.swift @@ -0,0 +1,43 @@ +// +// Secrets.swift +// +// +// Created by Martin Dutra on 24/11/21. +// + +import Foundation +import Logger + +/// The Keys class perovides a simple key-value storage (in a .plist file) utility that +/// can be used to retrieve secrets in a secure way +public class Keys { + + /// Singleton holding a Keys instance + /// + /// This instance should be used in most cases, it prevents reading from the + /// Secrets.plist file each time as it maintains its value in-memory + public static let shared = Keys() + + var service: SecretsService? + + /// Creates an instance of the Keys class + /// - parameter bundle: Bundle instance used for finding a Secrets.plist file + /// + /// Normally, the singleton should be used instead. This initializer is useful + /// for testing only. + public init(bundle: Bundle = .main) { + self.service = SecretsServiceAdapter(bundle: bundle) + } + + init(service: SecretsService) { + self.service = service + } + + /// Retrieve a value associated with a given key + /// + /// - parameter key: The key whose value is going to be retrieved + /// - returns: The value of the key. Returns nil if it doesn't exist or the .plist file couldn't be found + public func get(key: Key) -> String? { + service?.get(key: key.rawValue) + } +} diff --git a/Sources/Secrets/Domain/Services/SecretsService.swift b/Sources/Domain/SecretsService.swift similarity index 77% rename from Sources/Secrets/Domain/Services/SecretsService.swift rename to Sources/Domain/SecretsService.swift index be4a047..3d11819 100644 --- a/Sources/Secrets/Domain/Services/SecretsService.swift +++ b/Sources/Domain/SecretsService.swift @@ -8,5 +8,5 @@ import Foundation protocol SecretsService { - func get(key: Key) -> String? + func get(key: String) -> String? } diff --git a/Sources/Domain/SecretsServiceAdapter.swift b/Sources/Domain/SecretsServiceAdapter.swift new file mode 100644 index 0000000..f76b10e --- /dev/null +++ b/Sources/Domain/SecretsServiceAdapter.swift @@ -0,0 +1,31 @@ +// +// File.swift +// +// +// Created by Martin Dutra on 1/12/21. +// + +import Foundation +import Logger + +class SecretsServiceAdapter: SecretsService { + + var dictionary: NSDictionary? + + init(bundle: Bundle = .main) { + if let path = bundle.path(forResource: "Secrets", ofType: "plist") { + dictionary = NSDictionary(contentsOfFile: path) + } + } + + func get(key: String) -> String? { + let value = dictionary?[key] as? String + if let result = value, !result.isEmpty { + Log.debug("Key \(key) found.") + return result + } else { + Log.error("Key \(key) not found.") + return nil + } + } +} diff --git a/Sources/Secrets/Delivery/Mappers/KeyMapper.swift b/Sources/Secrets/Delivery/Mappers/KeyMapper.swift deleted file mode 100644 index 2ce4a55..0000000 --- a/Sources/Secrets/Delivery/Mappers/KeyMapper.swift +++ /dev/null @@ -1,21 +0,0 @@ -// -// KeyMapper.swift -// -// -// Created by Martin Dutra on 8/12/21. -// - -import Foundation - -class KeyMapper { - func map(key: Secrets.Key) -> Key? { - switch key { - case .posthog: - return Key.posthog - case .bugsnag: - return Key.bugsnag - case .push: - return Key.push - } - } -} diff --git a/Sources/Secrets/Delivery/Secrets.swift b/Sources/Secrets/Delivery/Secrets.swift deleted file mode 100644 index e94de4f..0000000 --- a/Sources/Secrets/Delivery/Secrets.swift +++ /dev/null @@ -1,40 +0,0 @@ -// -// Secrets.swift -// -// -// Created by Martin Dutra on 24/11/21. -// - -import Logger - -open class Secrets { - - public enum Key { - case posthog - case bugsnag - case push - } - - public static let shared = Secrets(service: SecretsServiceAdapter(bundleSecretsService: PlistService())) - - var service: SecretsService? - - public init() { - self.service = nil - } - - init(service: SecretsService) { - self.service = service - } - - open func get(key: Secrets.Key) -> String? { - let mapper = KeyMapper() - if let key = mapper.map(key: key) { - return service?.get(key: key) - } else { - Logger.shared.fatal(.incorrectValue, "KeyMapper couldn't map a key") - return nil - } - } - -} diff --git a/Sources/Secrets/Domain/Models/Key.swift b/Sources/Secrets/Domain/Models/Key.swift deleted file mode 100644 index a3d993f..0000000 --- a/Sources/Secrets/Domain/Models/Key.swift +++ /dev/null @@ -1,14 +0,0 @@ -// -// Key.swift -// -// -// Created by Martin Dutra on 6/12/21. -// - -import Foundation - -enum Key: String { - case posthog - case bugsnag - case push -} diff --git a/Sources/Secrets/Domain/Services/BundleSecretsService.swift b/Sources/Secrets/Domain/Services/BundleSecretsService.swift deleted file mode 100644 index 2e49b3c..0000000 --- a/Sources/Secrets/Domain/Services/BundleSecretsService.swift +++ /dev/null @@ -1,14 +0,0 @@ -// -// BundleSecretsService.swift -// -// -// Created by Martin Dutra on 6/12/21. -// - -import Foundation - -protocol BundleSecretsService { - - func get(key: String) -> String? - -} diff --git a/Sources/Secrets/Domain/Services/SecretsServiceAdapter.swift b/Sources/Secrets/Domain/Services/SecretsServiceAdapter.swift deleted file mode 100644 index eb504af..0000000 --- a/Sources/Secrets/Domain/Services/SecretsServiceAdapter.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// File.swift -// -// -// Created by Martin Dutra on 1/12/21. -// - -import Foundation -import Logger - -class SecretsServiceAdapter: SecretsService { - - var bundleSecretsService: BundleSecretsService - - init(bundleSecretsService: BundleSecretsService) { - self.bundleSecretsService = bundleSecretsService - } - - func get(key: Key) -> String? { - if let result = bundleSecretsService.get(key: key.rawValue) { - Logger.shared.debug("Key \(key) found.") - return result - } else { - Logger.shared.info("Key \(key) not found.") - return nil - } - } - -} diff --git a/Sources/Secrets/Infraestructure/Services/PlistService.swift b/Sources/Secrets/Infraestructure/Services/PlistService.swift deleted file mode 100644 index 7260e19..0000000 --- a/Sources/Secrets/Infraestructure/Services/PlistService.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// PlistService.swift -// -// -// Created by Martin Dutra on 24/11/21. -// - -import Foundation - -class PlistService: BundleSecretsService { - - var dictionary: NSDictionary? - - init(bundle: Bundle = .main) { - if let path = bundle.path(forResource: "Config", ofType: "plist") { - dictionary = NSDictionary(contentsOfFile: path) - } - } - - func get(key: String) -> String? { - return dictionary?[key] as? String - } - -} diff --git a/Tests/SecretsTests/Delivery/SecretsTests.swift b/Tests/Delivery/KeysTests.swift similarity index 60% rename from Tests/SecretsTests/Delivery/SecretsTests.swift rename to Tests/Delivery/KeysTests.swift index 8d52e74..04a6eb6 100644 --- a/Tests/SecretsTests/Delivery/SecretsTests.swift +++ b/Tests/Delivery/KeysTests.swift @@ -11,11 +11,11 @@ import XCTest final class SecretsTests: XCTestCase { private var service: SecretsServiceMock! - private var secrets: Secrets! + private var secrets: Keys! override func setUp() { service = SecretsServiceMock() - secrets = Secrets(service: service) + secrets = Keys(service: service) } func testGet() { @@ -24,4 +24,10 @@ final class SecretsTests: XCTestCase { XCTAssertEqual(secrets.get(key: .posthog), expectedValue) } + func testInit() { + secrets = Keys(bundle: .module) + let expectedValue = "posthog-key" + service.value = expectedValue + XCTAssertEqual(secrets.get(key: .posthog), expectedValue) + } } diff --git a/Tests/Domain/SecretsServiceAdapterTests.swift b/Tests/Domain/SecretsServiceAdapterTests.swift new file mode 100644 index 0000000..fe33690 --- /dev/null +++ b/Tests/Domain/SecretsServiceAdapterTests.swift @@ -0,0 +1,36 @@ +// +// SecretsServiceAdapterTests.swift +// +// +// Created by Martin Dutra on 8/12/21. +// + +import XCTest +@testable import Secrets + +final class SecretsServiceAdapterTests: XCTestCase { + + private var service: SecretsServiceAdapter! + + override func setUp() { + service = SecretsServiceAdapter(bundle: .module) + } + + func testGet() { + let expectedValue = "posthog-key" + XCTAssertEqual(service.get(key: "posthog"), expectedValue) + } + + func testGetWhenKeyIsNotFound() { + XCTAssertNil(service.get(key: "unknownkey")) + } + + func testGetWhenKeyIsFoundButEmpty() { + XCTAssertNil(service.get(key: "bugsnag")) + } + + func testGetWhenPlistDoesntExist() { + service = SecretsServiceAdapter(bundle: .main) + XCTAssertNil(service.get(key: "posthog")) + } +} diff --git a/Tests/SecretsTests/Mocks/SecretsServiceMock.swift b/Tests/Mocks/SecretsServiceMock.swift similarity index 77% rename from Tests/SecretsTests/Mocks/SecretsServiceMock.swift rename to Tests/Mocks/SecretsServiceMock.swift index 8d92b4e..c82e8b8 100644 --- a/Tests/SecretsTests/Mocks/SecretsServiceMock.swift +++ b/Tests/Mocks/SecretsServiceMock.swift @@ -12,8 +12,7 @@ class SecretsServiceMock: SecretsService { var value: String? - func get(key: Key) -> String? { - return value + func get(key: String) -> String? { + value } - } diff --git a/Tests/SecretsTests/Samples/Config.plist b/Tests/Samples/Secrets.plist similarity index 89% rename from Tests/SecretsTests/Samples/Config.plist rename to Tests/Samples/Secrets.plist index a339d95..3dbbd52 100644 --- a/Tests/SecretsTests/Samples/Config.plist +++ b/Tests/Samples/Secrets.plist @@ -5,6 +5,6 @@ posthog posthog-key bugsnag - bugsnag-key + diff --git a/Tests/SecretsTests/Delivery/Mappers/KeyMapperTests.swift b/Tests/SecretsTests/Delivery/Mappers/KeyMapperTests.swift deleted file mode 100644 index 1508a20..0000000 --- a/Tests/SecretsTests/Delivery/Mappers/KeyMapperTests.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// KeyMapperTests.swift -// -// -// Created by Martin Dutra on 8/12/21. -// - -import Foundation -@testable import Secrets -import XCTest - -final class KeyMapperTests: XCTestCase { - - private var mapper: KeyMapper! - - override func setUp() { - mapper = KeyMapper() - } - - func testAllKeys() { - XCTAssertNotNil(mapper.map(key: .posthog)) - XCTAssertNotNil(mapper.map(key: .bugsnag)) - XCTAssertNotNil(mapper.map(key: .push)) - } - -} diff --git a/Tests/SecretsTests/Domain/Services/SecretsServiceAdapterTests.swift b/Tests/SecretsTests/Domain/Services/SecretsServiceAdapterTests.swift deleted file mode 100644 index 554129c..0000000 --- a/Tests/SecretsTests/Domain/Services/SecretsServiceAdapterTests.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// SecretsServiceAdapterTests.swift -// -// -// Created by Martin Dutra on 8/12/21. -// - -import XCTest -@testable import Secrets - -final class SecretsServiceAdapterTests: XCTestCase { - - private var bundleSecretsService: BundleSecretsServiceMock! - private var service: SecretsServiceAdapter! - - override func setUp() { - bundleSecretsService = BundleSecretsServiceMock() - service = SecretsServiceAdapter(bundleSecretsService: bundleSecretsService) - } - - func testGet() { - let expectedValue = "tests" - bundleSecretsService.value = expectedValue - XCTAssertEqual(service.get(key: .posthog), expectedValue) - } - - func testGetWhenKeyIsNotFound() { - bundleSecretsService.value = nil - XCTAssertNil(service.get(key: .posthog)) - } - - -} diff --git a/Tests/SecretsTests/Infraestructure/Services/PlistServiceTests.swift b/Tests/SecretsTests/Infraestructure/Services/PlistServiceTests.swift deleted file mode 100644 index d65bbe1..0000000 --- a/Tests/SecretsTests/Infraestructure/Services/PlistServiceTests.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// File.swift -// -// -// Created by Martin Dutra on 6/12/21. -// - -import XCTest -@testable import Secrets - -final class PlistServiceTests: XCTestCase { - - private var service: PlistService! - - override func setUp() { - service = PlistService(bundle: Bundle.module) - } - - func testGet() { - XCTAssertEqual(service.get(key: "posthog"), "posthog-key") - } - - func testGetWhenKeyIsUnknown() { - XCTAssertNil(service.get(key: "unknownkey")) - } - -} diff --git a/Tests/SecretsTests/Mocks/BundleSecretsServiceMock.swift b/Tests/SecretsTests/Mocks/BundleSecretsServiceMock.swift deleted file mode 100644 index b283336..0000000 --- a/Tests/SecretsTests/Mocks/BundleSecretsServiceMock.swift +++ /dev/null @@ -1,19 +0,0 @@ -// -// BundleSecretsServiceMock.swift -// -// -// Created by Martin Dutra on 8/12/21. -// - -import Foundation -@testable import Secrets - -class BundleSecretsServiceMock: BundleSecretsService { - - var value: String? - - func get(key: String) -> String? { - return value - } - -} diff --git a/Tests/SecretsTests/Samples/.Config.plist.swp b/Tests/SecretsTests/Samples/.Config.plist.swp deleted file mode 100644 index 3258ff9..0000000 Binary files a/Tests/SecretsTests/Samples/.Config.plist.swp and /dev/null differ