|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import '../postgres.dart'; |
| 5 | + |
| 6 | +({ |
| 7 | + Endpoint endpoint, |
| 8 | + String? applicationName, |
| 9 | + Duration? connectTimeout, |
| 10 | + Encoding? encoding, |
| 11 | + ReplicationMode? replicationMode, |
| 12 | + SecurityContext? securityContext, |
| 13 | + SslMode? sslMode, |
| 14 | +}) parseConnectionString(String connectionString) { |
| 15 | + final uri = Uri.parse(connectionString); |
| 16 | + |
| 17 | + if (uri.scheme != 'postgresql' && uri.scheme != 'postgres') { |
| 18 | + throw ArgumentError( |
| 19 | + 'Invalid connection string scheme: ${uri.scheme}. Expected "postgresql" or "postgres".'); |
| 20 | + } |
| 21 | + |
| 22 | + final host = uri.host.isEmpty ? 'localhost' : uri.host; |
| 23 | + final port = uri.port == 0 ? 5432 : uri.port; |
| 24 | + final database = uri.pathSegments.firstOrNull ?? 'postgres'; |
| 25 | + final username = uri.userInfo.isEmpty ? null : _parseUsername(uri.userInfo); |
| 26 | + final password = uri.userInfo.isEmpty ? null : _parsePassword(uri.userInfo); |
| 27 | + |
| 28 | + final validParams = { |
| 29 | + 'sslmode', |
| 30 | + 'sslcert', |
| 31 | + 'sslkey', |
| 32 | + 'sslrootcert', |
| 33 | + 'connect_timeout', |
| 34 | + 'application_name', |
| 35 | + 'client_encoding', |
| 36 | + 'replication' |
| 37 | + }; |
| 38 | + |
| 39 | + final params = uri.queryParameters; |
| 40 | + for (final key in params.keys) { |
| 41 | + if (!validParams.contains(key)) { |
| 42 | + throw ArgumentError('Unrecognized connection parameter: $key'); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + SslMode? sslMode; |
| 47 | + if (params.containsKey('sslmode')) { |
| 48 | + switch (params['sslmode']) { |
| 49 | + case 'disable': |
| 50 | + sslMode = SslMode.disable; |
| 51 | + break; |
| 52 | + case 'require': |
| 53 | + sslMode = SslMode.require; |
| 54 | + break; |
| 55 | + case 'verify-ca': |
| 56 | + case 'verify-full': |
| 57 | + sslMode = SslMode.verifyFull; |
| 58 | + break; |
| 59 | + default: |
| 60 | + throw ArgumentError( |
| 61 | + 'Invalid sslmode value: ${params['sslmode']}. Expected: disable, require, verify-ca, verify-full'); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + SecurityContext? securityContext; |
| 66 | + if (params.containsKey('sslcert') || |
| 67 | + params.containsKey('sslkey') || |
| 68 | + params.containsKey('sslrootcert')) { |
| 69 | + try { |
| 70 | + securityContext = _createSecurityContext( |
| 71 | + certPath: params['sslcert'], |
| 72 | + keyPath: params['sslkey'], |
| 73 | + caPath: params['sslrootcert'], |
| 74 | + ); |
| 75 | + } catch (e) { |
| 76 | + // re-throw with more context about connection string parsing |
| 77 | + throw ArgumentError('SSL configuration error in connection string: $e'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Duration? connectTimeout; |
| 82 | + if (params.containsKey('connect_timeout')) { |
| 83 | + final timeoutSeconds = int.tryParse(params['connect_timeout']!); |
| 84 | + if (timeoutSeconds == null || timeoutSeconds <= 0) { |
| 85 | + throw ArgumentError( |
| 86 | + 'Invalid connect_timeout value: ${params['connect_timeout']}. Expected positive integer.'); |
| 87 | + } |
| 88 | + connectTimeout = Duration(seconds: timeoutSeconds); |
| 89 | + } |
| 90 | + |
| 91 | + final applicationName = params['application_name']; |
| 92 | + |
| 93 | + Encoding? encoding; |
| 94 | + if (params.containsKey('client_encoding')) { |
| 95 | + switch (params['client_encoding']?.toUpperCase()) { |
| 96 | + case 'UTF8': |
| 97 | + case 'UTF-8': |
| 98 | + encoding = utf8; |
| 99 | + break; |
| 100 | + case 'LATIN1': |
| 101 | + case 'ISO-8859-1': |
| 102 | + encoding = latin1; |
| 103 | + break; |
| 104 | + default: |
| 105 | + throw ArgumentError( |
| 106 | + 'Unsupported client_encoding: ${params['client_encoding']}. Supported: UTF8, LATIN1'); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + ReplicationMode? replicationMode; |
| 111 | + if (params.containsKey('replication')) { |
| 112 | + switch (params['replication']) { |
| 113 | + case 'database': |
| 114 | + replicationMode = ReplicationMode.logical; |
| 115 | + break; |
| 116 | + case 'true': |
| 117 | + case 'physical': |
| 118 | + replicationMode = ReplicationMode.physical; |
| 119 | + break; |
| 120 | + case 'false': |
| 121 | + case 'no_select': |
| 122 | + replicationMode = ReplicationMode.none; |
| 123 | + break; |
| 124 | + default: |
| 125 | + throw ArgumentError( |
| 126 | + 'Invalid replication value: ${params['replication']}. Expected: database, true, physical, false, no_select'); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + final endpoint = Endpoint( |
| 131 | + host: host, |
| 132 | + port: port, |
| 133 | + database: database, |
| 134 | + username: username, |
| 135 | + password: password, |
| 136 | + ); |
| 137 | + |
| 138 | + return ( |
| 139 | + endpoint: endpoint, |
| 140 | + sslMode: sslMode, |
| 141 | + securityContext: securityContext, |
| 142 | + connectTimeout: connectTimeout, |
| 143 | + applicationName: applicationName, |
| 144 | + encoding: encoding, |
| 145 | + replicationMode: replicationMode, |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +String? _parseUsername(String userInfo) { |
| 150 | + final colonIndex = userInfo.indexOf(':'); |
| 151 | + if (colonIndex == -1) { |
| 152 | + return Uri.decodeComponent(userInfo); |
| 153 | + } |
| 154 | + return Uri.decodeComponent(userInfo.substring(0, colonIndex)); |
| 155 | +} |
| 156 | + |
| 157 | +String? _parsePassword(String userInfo) { |
| 158 | + final colonIndex = userInfo.indexOf(':'); |
| 159 | + if (colonIndex == -1) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + return Uri.decodeComponent(userInfo.substring(colonIndex + 1)); |
| 163 | +} |
| 164 | + |
| 165 | +SecurityContext _createSecurityContext({ |
| 166 | + String? certPath, |
| 167 | + String? keyPath, |
| 168 | + String? caPath, |
| 169 | +}) { |
| 170 | + final context = SecurityContext(); |
| 171 | + |
| 172 | + if (certPath != null) { |
| 173 | + try { |
| 174 | + context.useCertificateChain(certPath); |
| 175 | + } catch (e) { |
| 176 | + throw ArgumentError('Failed to load SSL certificate from $certPath: $e'); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if (keyPath != null) { |
| 181 | + try { |
| 182 | + context.usePrivateKey(keyPath); |
| 183 | + } catch (e) { |
| 184 | + throw ArgumentError('Failed to load SSL private key from $keyPath: $e'); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (caPath != null) { |
| 189 | + try { |
| 190 | + context.setTrustedCertificates(caPath); |
| 191 | + } catch (e) { |
| 192 | + throw ArgumentError( |
| 193 | + 'Failed to load SSL CA certificates from $caPath: $e'); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + return context; |
| 198 | +} |
0 commit comments