Skip to content
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
2 changes: 1 addition & 1 deletion apple/Packages/TableProOracle/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apple/Packages/TableProOracle/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/TableProApp/oracle-nio",
revision: "045a44674e9f9da3904e0f2b87995490a7cc88e4"
revision: "28d9d83b9b88440598862f04e43ca527dcac1d7d"
),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.81.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.29.0"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Foundation
import OracleNIO

// What a failed connect says to the user. The driver's error structs are not for display, as the query path's
// `mapQueryError` says: they can carry this device's host name and OS user, or a login packet in hex.
enum OracleConnectFailureMapping {
static func connectError(
code: String,
underlying: (any Error)?,
serverMessage: String?,
phase: String?,
nativeNetworkEncryptionEnabled: Bool
) -> OracleCoreError {
let detail = failureDetail(code: code, underlying: underlying, serverMessage: serverMessage)
// BoringSSL's text is what tells an untrusted certificate from a cipher mismatch, and a TLS failure at a
// redirect's target sits one level further down.
let nested = (underlying as? OracleRedirectError)?.underlying
for candidate in [detail] + [underlying, nested].compactMap({ tlsText($0) }) {
if let kind = OracleSSLClassifier.classifyTLSFailure(candidate) {
return .tlsHandshakeFailed(kind: kind, serverMessage: candidate)
}
}
if let redirect = underlying as? OracleRedirectError {
return redirectError(redirect)
}
if let underlying, OracleTransportFailure.isConnectionRefused(underlying) {
return .connectionRefused
}
let failure = OracleConnectErrorClassifier.classify(code)
if case .loginHandshakeTimedOut = failure {
return .loginHandshakeStalled(phase: phase)
}
if case .advancedNegotiationRequired = failure {
return .nativeEncryptionRequired
}
if OracleConnectErrorClassifier.isLikelyNativeEncryptionFailure(
failure: failure,
nativeNetworkEncryptionEnabled: nativeNetworkEncryptionEnabled,
timedOut: false
) {
return .nativeEncryptionFailed(detail: detail)
}
switch failure {
case .verifierUnsupported(let flag):
return .authVerifierUnsupported(flag: flag)
case .versionNotSupported:
return .authVersionNotSupported
case .connectionDropped:
return .authConnectionDropped(phase: phase)
case .advancedNegotiationFailed:
return .nativeEncryptionFailed(detail: detail)
case .advancedNegotiationRequired:
return .nativeEncryptionRequired
case .loginHandshakeTimedOut:
return .loginHandshakeStalled(phase: phase)
case .connectionFailed:
return .connectionFailed(detail)
}
}

// Only NIOSSL's own errors are read for TLS words. Any other description can hold what a listener sent, such as a
// REFUSE that echoes this device's host name and OS user next to an ORA-29024.
static func tlsText(_ error: (any Error)?) -> String? {
guard let error, String(reflecting: type(of: error)).hasPrefix("NIOSSL.") else { return nil }
return String(describing: error)
}

static func failureDetail(code: String, underlying: (any Error)?, serverMessage: String?) -> String {
if let refused = underlying as? OracleListenerRefusedError {
return OracleListenerRefusal.detail(code: refused.code)
}
if let serverMessage {
return serverMessage
}
if let underlying {
if let transport = OracleTransportFailure.detail(underlying) {
return transport
}
// The negotiation's reason is a plain sentence, the one underlying error that is.
if OracleConnectErrorClassifier.classify(code) == .advancedNegotiationFailed {
return String(describing: underlying)
}
}
return String(format: OracleCoreError.driverErrorFormat, code)
}

static func transportError(_ error: any Error) -> OracleCoreError {
if OracleTransportFailure.isConnectionRefused(error) {
return .connectionRefused
}
let detail = OracleTransportFailure.detail(error)
?? String(format: OracleCoreError.driverErrorFormat, String(describing: type(of: error)))
return .connectionFailed(detail)
}

static func redirectError(_ redirect: OracleRedirectError) -> OracleCoreError {
let target = redirect.host.map { host -> String in
let shown = host.contains(":") ? "[\(host)]" : host
return redirect.port.map { "\(shown):\($0)" } ?? shown
}
switch redirect.reason {
case .notFollowed:
// Following is turned off only when the dial is a tunnel's forward.
return .listenerRedirectFailed(.throughTunnel, target: target)
case .unreadable:
return .listenerRedirectFailed(.unreadable, target: nil)
case .transportMismatch:
return .listenerRedirectFailed(.needsTLS, target: target)
case .tooManyRedirects:
return .listenerRedirectFailed(.tooManyRedirects, target: target)
case .targetUnreachable:
let detail = redirect.underlying.flatMap(OracleTransportFailure.detail)
return .listenerRedirectFailed(.unreachable(detail: detail), target: target)
case .targetRefused:
let refusal = redirect.underlying as? OracleListenerRefusedError
return .listenerRedirectFailed(
.refused(detail: OracleListenerRefusal.detail(code: refusal?.code)), target: target
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public struct OracleConnectionOptions: Sendable, Equatable {
public var tls: OracleTLSDescription
public var networkEncryption: NetworkEncryption
public var loginTimeoutSeconds: Double
// Off when the dial is a tunnel's forward: the address a listener redirects to is one only the far side reaches.
public var followsListenerRedirects: Bool

public init(
host: String,
Expand All @@ -68,7 +70,8 @@ public struct OracleConnectionOptions: Sendable, Equatable {
role: Role = .normal,
tls: OracleTLSDescription = OracleTLSDescription(),
networkEncryption: NetworkEncryption = .accepted,
loginTimeoutSeconds: Double = OracleConnectionOptions.defaultLoginTimeoutSeconds
loginTimeoutSeconds: Double = OracleConnectionOptions.defaultLoginTimeoutSeconds,
followsListenerRedirects: Bool = true
) {
self.host = host
self.port = port
Expand All @@ -82,6 +85,7 @@ public struct OracleConnectionOptions: Sendable, Equatable {
self.tls = tls
self.networkEncryption = networkEncryption
self.loginTimeoutSeconds = loginTimeoutSeconds
self.followsListenerRedirects = followsListenerRedirects
}

public var identifier: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public final class OracleCoreConnection: @unchecked Sendable {
)
configuration.mode = authenticationMode(for: options.role)
configuration.nativeNetworkEncryption = encryptionLevel(for: options.networkEncryption)
configuration.followsListenerRedirects = options.followsListenerRedirects
// The driver owns the channel, so it is the only thing that can end a login the
// server has stopped answering, and it names the handshake phase when it does.
// The wrapper in connect() stays a grace period behind it as a backstop for a driver
Expand Down Expand Up @@ -248,10 +249,10 @@ public final class OracleCoreConnection: @unchecked Sendable {
} catch {
let detail = String(describing: error)
osLogger.error("Oracle connection failed: \(String(describing: type(of: error)), privacy: .public) \(detail, privacy: .private)")
if let kind = OracleSSLClassifier.classifyTLSFailure(detail) {
throw OracleCoreError.tlsHandshakeFailed(kind: kind, serverMessage: detail)
if let tls = OracleConnectFailureMapping.tlsText(error), let kind = OracleSSLClassifier.classifyTLSFailure(tls) {
throw OracleCoreError.tlsHandshakeFailed(kind: kind, serverMessage: tls)
}
throw OracleCoreError.connectionFailed(detail)
throw OracleConnectFailureMapping.transportError(error)
}
}

Expand Down Expand Up @@ -334,58 +335,19 @@ public final class OracleCoreConnection: @unchecked Sendable {
}

private func connectError(from sqlError: OracleSQLError) -> OracleCoreError {
let detail = Self.connectFailureDetail(sqlError)
let phase = sqlError.handshakePhase
let code = sqlError.code.description
let underlying = sqlError.underlying.map { String(describing: $0) } ?? ""
osLogger.error(
"Oracle connection failed at phase \(phase ?? "unknown", privacy: .public) (\(code, privacy: .public))"
"Oracle connection failed at phase \(phase ?? "unknown", privacy: .public) (\(code, privacy: .public)): \(underlying, privacy: .private)"
)
return OracleConnectFailureMapping.connectError(
code: code,
underlying: sqlError.underlying,
serverMessage: sqlError.serverInfo?.message,
phase: phase,
nativeNetworkEncryptionEnabled: options.networkEncryption != .rejected
)
if let kind = OracleSSLClassifier.classifyTLSFailure(detail) {
return .tlsHandshakeFailed(kind: kind, serverMessage: detail)
}
let failure = OracleConnectErrorClassifier.classify(sqlError.code.description)
if case .loginHandshakeTimedOut = failure {
return .loginHandshakeStalled(phase: phase)
}
if case .advancedNegotiationRequired = failure {
return .nativeEncryptionRequired
}
if OracleConnectErrorClassifier.isLikelyNativeEncryptionFailure(
failure: failure,
nativeNetworkEncryptionEnabled: options.networkEncryption != .rejected,
timedOut: false
) {
return .nativeEncryptionFailed(detail: detail)
}
switch failure {
case .verifierUnsupported(let flag):
return .authVerifierUnsupported(flag: flag)
case .versionNotSupported:
return .authVersionNotSupported
case .connectionDropped:
return .authConnectionDropped(phase: phase)
case .advancedNegotiationFailed:
return .nativeEncryptionFailed(detail: detail)
case .advancedNegotiationRequired:
return .nativeEncryptionRequired
case .loginHandshakeTimedOut:
return .loginHandshakeStalled(phase: phase)
case .connectionFailed:
return .connectionFailed(detail)
}
}

private static func connectFailureDetail(_ error: OracleSQLError) -> String {
if let refused = error.underlying as? OracleListenerRefusedError {
return OracleListenerRefusal.detail(code: refused.code)
}
if let serverMessage = error.serverInfo?.message {
return serverMessage
}
if let underlying = error.underlying {
return String(describing: underlying)
}
return String(format: OracleCoreError.driverErrorFormat, error.code.description)
}

public func disconnect(reason: OracleDisconnectReason = .userRequested) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public enum OracleCertificateField: Sendable, Equatable {
case clientKey
}

public enum OracleRedirectFailure: Sendable, Equatable {
case throughTunnel
case unreadable
case needsTLS
case tooManyRedirects
case unreachable(detail: String?)
case refused(detail: String)
}

public enum OracleCoreError: LocalizedError, Sendable, Equatable {
case notConnected
case connectionFailed(String)
Expand All @@ -34,6 +43,8 @@ public enum OracleCoreError: LocalizedError, Sendable, Equatable {
case certificateUnavailable(field: OracleCertificateField, path: String)
case certificateAuthorityRequired
case verifyIdentityNeedsHostName(address: String)
case listenerRedirectFailed(OracleRedirectFailure, target: String?)
case connectionRefused

public var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -88,6 +99,10 @@ public enum OracleCoreError: LocalizedError, Sendable, Equatable {
return String(format: Self.certificateUnavailableFormat(for: field), path)
case .certificateAuthorityRequired:
return String(localized: "Verify CA needs a CA certificate. Choose the CA certificate that signed the server's certificate.")
case .listenerRedirectFailed(let failure, let target):
return Self.redirectMessage(failure, target: target)
case .connectionRefused:
return String(localized: "The server refused the connection. Check the host and port, and that the Oracle listener is running.")
case .verifyIdentityNeedsHostName(let address):
return String(
format: String(localized: """
Expand All @@ -99,6 +114,56 @@ public enum OracleCoreError: LocalizedError, Sendable, Equatable {
}
}

private static func redirectMessage(_ failure: OracleRedirectFailure, target: String?) -> String {
let unreadable = String(localized: "The Oracle listener redirected the connection to an address TablePro could not read.")
guard let target else {
if case .tooManyRedirects = failure {
return String(localized: "The Oracle listeners redirected the connection too many times.")
}
return unreadable
}
switch failure {
case .throughTunnel:
return String(
format: String(localized: """
The Oracle listener redirected the connection to %@, which the tunnel does not forward. Enter that \
host and port as this connection's host and port so the tunnel reaches them.
"""),
target
)
case .unreadable:
return unreadable
case .needsTLS:
return String(
format: String(localized: "The Oracle listener redirected the connection to %@, which expects TLS. Set an SSL mode for this connection."),
target
)
case .tooManyRedirects:
return String(
format: String(localized: "The Oracle listeners redirected the connection too many times. The last address was %@."),
target
)
case .refused(let detail):
return String(
format: String(localized: "The Oracle listener redirected the connection to %1$@, and the listener there refused it. %2$@"),
target,
detail
)
case .unreachable(let detail):
guard let detail else {
return String(
format: String(localized: "The Oracle listener redirected the connection to %@, which could not be reached."),
target
)
}
return String(
format: String(localized: "The Oracle listener redirected the connection to %1$@, which could not be reached (%2$@)."),
target,
detail
)
}
}

/// What a driver failure with no server message behind it says to the user. OracleNIO's own
/// error description is a struct dump it documents as unfit to show, so the code's name is
/// what carries across.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation
import Network
import NIOCore

// A connect that fails below the TNS exchange throws Network.framework's or SwiftNIO's own error, whose description is
// a struct dump. These are the cases a user can act on, in words.
public enum OracleTransportFailure {
public static func isConnectionRefused(_ error: any Error) -> Bool {
posixCode(of: error) == ECONNREFUSED
}

public static func detail(_ error: any Error) -> String? {
if case .connectTimeout(let amount) = error as? ChannelError {
let seconds = max(1, Int((Double(amount.nanoseconds) / 1_000_000_000).rounded()))
return String(format: String(localized: "no answer within %lld seconds"), seconds)
}
if let nwError = error as? NWError, case .dns = nwError {
return String(localized: "the host name could not be resolved")
}
if let code = posixCode(of: error), let text = strerror(code) {
return String(cString: text)
}
return nil
}

private static func posixCode(of error: any Error) -> Int32? {
if let nwError = error as? NWError, case .posix(let code) = nwError {
return code.rawValue
}
if let ioError = error as? IOError {
return ioError.errnoCode
}
if let posixError = error as? POSIXError {
return posixError.code.rawValue
}
return nil
}
}
Loading
Loading