Skip to content

Commit

Permalink
Remove VPN feature flag checks (#979)
Browse files Browse the repository at this point in the history
Required:

Task/Issue URL: https://app.asana.com/0/414235014887631/1208254974939835/f
iOS PR: duckduckgo/iOS#3334
macOS PR: duckduckgo/macos-browser#3232
What kind of version bump will this require?: Major

Description:

This PR cleans up VPN feature flag checks.
  • Loading branch information
samsymons authored Sep 13, 2024
1 parent 0464f4e commit f9134f8
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 146 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public protocol NetworkProtectionTokenStore {
public final class NetworkProtectionKeychainTokenStore: NetworkProtectionTokenStore {
private let keychainStore: NetworkProtectionKeychainStore
private let errorEvents: EventMapping<NetworkProtectionError>?
private let isSubscriptionEnabled: Bool
private let useAccessTokenProvider: Bool
public typealias AccessTokenProvider = () -> String?
private let accessTokenProvider: AccessTokenProvider

Expand All @@ -59,13 +59,13 @@ public final class NetworkProtectionKeychainTokenStore: NetworkProtectionTokenSt
public init(keychainType: KeychainType,
serviceName: String = Defaults.tokenStoreService,
errorEvents: EventMapping<NetworkProtectionError>?,
isSubscriptionEnabled: Bool,
useAccessTokenProvider: Bool,
accessTokenProvider: @escaping AccessTokenProvider) {
keychainStore = NetworkProtectionKeychainStore(label: Defaults.tokenStoreEntryLabel,
serviceName: serviceName,
keychainType: keychainType)
self.errorEvents = errorEvents
self.isSubscriptionEnabled = isSubscriptionEnabled
self.useAccessTokenProvider = useAccessTokenProvider
self.accessTokenProvider = accessTokenProvider
}

Expand All @@ -84,7 +84,7 @@ public final class NetworkProtectionKeychainTokenStore: NetworkProtectionTokenSt
}

public func fetchToken() throws -> String? {
if isSubscriptionEnabled {
if useAccessTokenProvider {
return accessTokenProvider().map { makeToken(from: $0) }
}

Expand Down
20 changes: 6 additions & 14 deletions Sources/NetworkProtection/NetworkProtectionDeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,24 @@ public actor NetworkProtectionDeviceManager: NetworkProtectionDeviceManagement {

private let errorEvents: EventMapping<NetworkProtectionError>?

private let isSubscriptionEnabled: Bool

public init(environment: VPNSettings.SelectedEnvironment,
tokenStore: NetworkProtectionTokenStore,
keyStore: NetworkProtectionKeyStore,
errorEvents: EventMapping<NetworkProtectionError>?,
isSubscriptionEnabled: Bool) {
self.init(networkClient: NetworkProtectionBackendClient(environment: environment, isSubscriptionEnabled: isSubscriptionEnabled),
errorEvents: EventMapping<NetworkProtectionError>?) {
self.init(networkClient: NetworkProtectionBackendClient(environment: environment),
tokenStore: tokenStore,
keyStore: keyStore,
errorEvents: errorEvents,
isSubscriptionEnabled: isSubscriptionEnabled)
errorEvents: errorEvents)
}

init(networkClient: NetworkProtectionClient,
tokenStore: NetworkProtectionTokenStore,
keyStore: NetworkProtectionKeyStore,
errorEvents: EventMapping<NetworkProtectionError>?,
isSubscriptionEnabled: Bool) {
errorEvents: EventMapping<NetworkProtectionError>?) {
self.networkClient = networkClient
self.tokenStore = tokenStore
self.keyStore = keyStore
self.errorEvents = errorEvents
self.isSubscriptionEnabled = isSubscriptionEnabled
}

/// Requests a new server list from the backend and updates it locally.
Expand Down Expand Up @@ -348,10 +342,8 @@ public actor NetworkProtectionDeviceManager: NetworkProtectionDeviceManagement {
private func handleAccessRevoked(_ error: NetworkProtectionClientError) throws {
switch error {
case .accessDenied, .invalidAuthToken:
if isSubscriptionEnabled {
errorEvents?.fire(.vpnAccessRevoked)
throw NetworkProtectionError.vpnAccessRevoked
}
errorEvents?.fire(.vpnAccessRevoked)
throw NetworkProtectionError.vpnAccessRevoked
default:
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,8 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
}()

private let endpointURL: URL
private let isSubscriptionEnabled: Bool

init(environment: VPNSettings.SelectedEnvironment = .default, isSubscriptionEnabled: Bool) {
self.isSubscriptionEnabled = isSubscriptionEnabled
init(environment: VPNSettings.SelectedEnvironment = .default) {
self.endpointURL = environment.endpointURL
}

Expand Down Expand Up @@ -405,7 +403,7 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
responseData = data
case 401:
return .failure(.invalidAuthToken)
case 403 where isSubscriptionEnabled:
case 403:
return .failure(.accessDenied)
default:
throw RegisterError.unexpectedStatus(status: response.statusCode)
Expand Down
26 changes: 10 additions & 16 deletions Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
let locationRepository = NetworkProtectionLocationListCompositeRepository(
environment: settings.selectedEnvironment,
tokenStore: tokenStore,
errorEvents: debugEvents,
isSubscriptionEnabled: isSubscriptionEnabled
errorEvents: debugEvents
)
return VPNServerSelectionResolver(locationListRepository: locationRepository, vpnSettings: settings)
}()
Expand Down Expand Up @@ -420,19 +419,15 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
environment: self.settings.selectedEnvironment,
tokenStore: self.tokenStore,
keyStore: self.keyStore,
errorEvents: self.debugEvents,
isSubscriptionEnabled: self.isSubscriptionEnabled
errorEvents: self.debugEvents
)

private lazy var tunnelFailureMonitor = NetworkProtectionTunnelFailureMonitor(handshakeReporter: adapter)

public lazy var latencyMonitor = NetworkProtectionLatencyMonitor()
public lazy var entitlementMonitor = NetworkProtectionEntitlementMonitor()
public lazy var serverStatusMonitor = NetworkProtectionServerStatusMonitor(
networkClient: NetworkProtectionBackendClient(
environment: self.settings.selectedEnvironment,
isSubscriptionEnabled: true
),
networkClient: NetworkProtectionBackendClient(environment: self.settings.selectedEnvironment),
tokenStore: self.tokenStore
)

Expand All @@ -453,8 +448,6 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
private let keychainType: KeychainType
private let debugEvents: EventMapping<NetworkProtectionError>
private let providerEvents: EventMapping<Event>

public let isSubscriptionEnabled: Bool
public let entitlementCheck: (() async -> Result<Bool, Error>)?

public init(notificationsPresenter: NetworkProtectionNotificationsPresenter,
Expand All @@ -469,7 +462,6 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
providerEvents: EventMapping<Event>,
settings: VPNSettings,
defaults: UserDefaults,
isSubscriptionEnabled: Bool,
entitlementCheck: (() async -> Result<Bool, Error>)?) {
Logger.networkProtectionMemory.debug("[+] PacketTunnelProvider")

Expand All @@ -485,8 +477,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
self.wireGuardInterface = wireGuardInterface
self.settings = settings
self.defaults = defaults
self.isSubscriptionEnabled = isSubscriptionEnabled
self.entitlementCheck = isSubscriptionEnabled ? entitlementCheck : nil
self.entitlementCheck = entitlementCheck

super.init()

Expand Down Expand Up @@ -1021,7 +1012,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
regenerateKey: regenerateKey
)
} catch {
if isSubscriptionEnabled, let error = error as? NetworkProtectionError, case .vpnAccessRevoked = error {
if let error = error as? NetworkProtectionError, case .vpnAccessRevoked = error {
throw TunnelError.vpnAccessRevoked
}

Expand Down Expand Up @@ -1513,7 +1504,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
await latencyMonitor.stop()
}

if isSubscriptionEnabled, await isEntitlementInvalid() {
if await isEntitlementInvalid() {
return
}

Expand All @@ -1532,7 +1523,10 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
await entitlementMonitor.stop()
}

guard isSubscriptionEnabled, let entitlementCheck else { return }
guard let entitlementCheck else {
assertionFailure("Expected entitlement check but didn't find one")
return
}

await entitlementMonitor.start(entitlementCheck: entitlementCheck) { [weak self] result in
/// Attempt tunnel shutdown & show messaging iff the entitlement is verified to be invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,23 @@ final public class NetworkProtectionLocationListCompositeRepository: NetworkProt
private let client: NetworkProtectionClient
private let tokenStore: NetworkProtectionTokenStore
private let errorEvents: EventMapping<NetworkProtectionError>
private let isSubscriptionEnabled: Bool

convenience public init(environment: VPNSettings.SelectedEnvironment,
tokenStore: NetworkProtectionTokenStore,
errorEvents: EventMapping<NetworkProtectionError>,
isSubscriptionEnabled: Bool) {
errorEvents: EventMapping<NetworkProtectionError>) {
self.init(
client: NetworkProtectionBackendClient(environment: environment, isSubscriptionEnabled: isSubscriptionEnabled),
client: NetworkProtectionBackendClient(environment: environment),
tokenStore: tokenStore,
errorEvents: errorEvents,
isSubscriptionEnabled: isSubscriptionEnabled
errorEvents: errorEvents
)
}

init(client: NetworkProtectionClient,
tokenStore: NetworkProtectionTokenStore,
errorEvents: EventMapping<NetworkProtectionError>,
isSubscriptionEnabled: Bool) {
errorEvents: EventMapping<NetworkProtectionError>) {
self.client = client
self.tokenStore = tokenStore
self.errorEvents = errorEvents
self.isSubscriptionEnabled = isSubscriptionEnabled
}

@MainActor
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class NetworkProtectionClientTests: XCTestCase {

override func setUp() {
super.setUp()
client = NetworkProtectionBackendClient(environment: .production, isSubscriptionEnabled: false)
client = NetworkProtectionBackendClient(environment: .production)
}

override class func setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ final class NetworkProtectionDeviceManagerTests: XCTestCase {
networkClient: networkClient,
tokenStore: tokenStore,
keyStore: keyStore,
errorEvents: nil,
isSubscriptionEnabled: false
errorEvents: nil
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class NetworkProtectionLocationListCompositeRepositoryTests: XCTestCase {
tokenStore: tokenStore,
errorEvents: .init { [weak self] event, _, _, _ in
self?.verifyErrorEvent?(event)
},
isSubscriptionEnabled: false)
})
}

@MainActor
Expand Down

0 comments on commit f9134f8

Please sign in to comment.