-
-
Notifications
You must be signed in to change notification settings - Fork 321
fix(connections): keep the username empty when a connection URL has none #1860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Plugins/ClickHouseDriverPlugin/ClickHouseCredentials.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // | ||
| // ClickHouseCredentials.swift | ||
| // ClickHouseDriverPlugin | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| internal enum ClickHouseCredentials { | ||
| internal static let defaultUsername = "default" | ||
|
|
||
| internal static func effectiveUsername(_ username: String) -> String { | ||
| username.isEmpty ? defaultUsername : username | ||
| } | ||
|
|
||
| internal static func basicAuthorizationHeader(username: String, password: String) -> String? { | ||
| let credentials = "\(effectiveUsername(username)):\(password)" | ||
| guard let data = credentials.data(using: .utf8) else { return nil } | ||
| return "Basic \(data.base64EncodedString())" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
TableProTests/Core/Utilities/ConnectionURLImportUsernameTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // | ||
| // ConnectionURLImportUsernameTests.swift | ||
| // TableProTests | ||
| // | ||
|
|
||
| import Foundation | ||
| import TableProPluginKit | ||
| import Testing | ||
|
|
||
| @testable import TablePro | ||
|
|
||
| @Suite("Connection URL Import Username") | ||
| @MainActor | ||
| struct ConnectionURLImportUsernameTests { | ||
| private func parse(_ urlString: String) throws -> ParsedConnectionURL { | ||
| guard case .success(let parsed) = ConnectionURLParser.parse(urlString) else { | ||
| throw ConnectionURLParseError.invalidURL | ||
| } | ||
| return parsed | ||
| } | ||
|
|
||
| @Test("Importing a MySQL URL with no user info keeps the username empty") | ||
| func mysqlURLWithoutUserKeepsUsernameEmpty() throws { | ||
| let parsed = try parse("mysql://localhost:3306/shop") | ||
| let connection = TransientConnectionFactory.build(from: parsed) | ||
| #expect(connection.username.isEmpty) | ||
| } | ||
|
|
||
| @Test("Importing a PostgreSQL URL with no user info keeps the username empty") | ||
| func postgresURLWithoutUserKeepsUsernameEmpty() throws { | ||
| let parsed = try parse("postgresql://db.example.com:5432/analytics") | ||
| let connection = TransientConnectionFactory.build(from: parsed) | ||
| #expect(connection.username.isEmpty) | ||
| } | ||
|
|
||
| @Test("A URL with user info still imports that username") | ||
| func urlWithUserInfoKeepsUsername() throws { | ||
| let parsed = try parse("mysql://admin:secret@localhost:3306/shop") | ||
| let connection = TransientConnectionFactory.build(from: parsed) | ||
| #expect(connection.username == "admin") | ||
| } | ||
|
|
||
| @Test("An imported connection with no username exports a URL with no user info") | ||
| func emptyUsernameRoundTripsThroughFormatter() throws { | ||
| let parsed = try parse("mysql://localhost:3306/shop") | ||
| let connection = TransientConnectionFactory.build(from: parsed) | ||
| let url = ConnectionURLFormatter.format(connection, password: "", sshPassword: nil) | ||
| #expect(!url.contains("@")) | ||
| #expect(url.contains("localhost")) | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
TableProTests/Core/Utilities/PgpassReaderUsernameTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // | ||
| // PgpassReaderUsernameTests.swift | ||
| // TableProTests | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import TablePro | ||
|
|
||
| @Suite("Pgpass Effective Username") | ||
| struct PgpassReaderUsernameTests { | ||
| @Test("A blank username matches ~/.pgpass as the operating system user") | ||
| func blankUsernameResolvesToOSUser() { | ||
| #expect(PgpassReader.effectiveUsername("") == NSUserName()) | ||
| } | ||
|
|
||
| @Test("An explicit username is matched as given") | ||
| func explicitUsernameIsPreserved() { | ||
| #expect(PgpassReader.effectiveUsername("analytics") == "analytics") | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
TableProTests/Models/Connection/DatabaseConnectionUsernameTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // | ||
| // DatabaseConnectionUsernameTests.swift | ||
| // TableProTests | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import TablePro | ||
|
|
||
| @Suite("Database Connection Username") | ||
| struct DatabaseConnectionUsernameTests { | ||
| @Test("Username defaults to empty, never a fabricated account name") | ||
| func usernameDefaultsToEmpty() { | ||
| let connection = DatabaseConnection(name: "Local") | ||
| #expect(connection.username.isEmpty) | ||
| } | ||
|
|
||
| @Test("Decoding a connection with no username key yields an empty username") | ||
| func decodingWithoutUsernameKeyYieldsEmpty() throws { | ||
| let json = """ | ||
| { | ||
| "id": "3F2504E0-4F89-11D3-9A0C-0305E82C3301", | ||
| "name": "No User", | ||
| "host": "localhost", | ||
| "port": 3306, | ||
| "type": "MySQL" | ||
| } | ||
| """ | ||
| let data = try #require(json.data(using: .utf8)) | ||
| let connection = try JSONDecoder().decode(DatabaseConnection.self, from: data) | ||
| #expect(connection.username.isEmpty) | ||
| } | ||
|
|
||
| @Test("Decoding preserves an explicit username") | ||
| func decodingPreservesExplicitUsername() throws { | ||
| let json = """ | ||
| { | ||
| "id": "3F2504E0-4F89-11D3-9A0C-0305E82C3302", | ||
| "name": "With User", | ||
| "host": "localhost", | ||
| "port": 3306, | ||
| "username": "admin", | ||
| "type": "MySQL" | ||
| } | ||
| """ | ||
| let data = try #require(json.data(using: .utf8)) | ||
| let connection = try JSONDecoder().decode(DatabaseConnection.self, from: data) | ||
| #expect(connection.username == "admin") | ||
| } | ||
|
|
||
| @Test("An empty username round-trips through encoding") | ||
| func emptyUsernameRoundTrips() throws { | ||
| let connection = DatabaseConnection(name: "Local", username: "") | ||
| let data = try JSONEncoder().encode(connection) | ||
| let decoded = try JSONDecoder().decode(DatabaseConnection.self, from: data) | ||
| #expect(decoded.username.isEmpty) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // | ||
| // ClickHouseCredentialsTests.swift | ||
| // TableProTests | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| @Suite("ClickHouse Credentials") | ||
| struct ClickHouseCredentialsTests { | ||
| @Test("A blank username resolves to the ClickHouse default user") | ||
| func blankUsernameResolvesToDefaultUser() { | ||
| #expect(ClickHouseCredentials.effectiveUsername("") == "default") | ||
| } | ||
|
|
||
| @Test("An explicit username is used as given") | ||
| func explicitUsernameIsPreserved() { | ||
| #expect(ClickHouseCredentials.effectiveUsername("analytics") == "analytics") | ||
| } | ||
|
|
||
| @Test("Basic authorization for a blank username authenticates as default") | ||
| func basicAuthorizationForBlankUsername() throws { | ||
| let header = try #require(ClickHouseCredentials.basicAuthorizationHeader(username: "", password: "secret")) | ||
| let encoded = header.replacingOccurrences(of: "Basic ", with: "") | ||
| let data = try #require(Data(base64Encoded: encoded)) | ||
| #expect(String(data: data, encoding: .utf8) == "default:secret") | ||
| } | ||
|
|
||
| @Test("Basic authorization keeps an explicit username") | ||
| func basicAuthorizationForExplicitUsername() throws { | ||
| let header = try #require(ClickHouseCredentials.basicAuthorizationHeader(username: "analytics", password: "")) | ||
| let encoded = header.replacingOccurrences(of: "Basic ", with: "") | ||
| let data = try #require(Data(base64Encoded: encoded)) | ||
| #expect(String(data: data, encoding: .utf8) == "analytics:") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the form check
~/.pgpasswith the OS username when the username field is blank, but the actual connection path still callsPgpassReader.resolve(... username: connection.username)inTablePro/Core/Database/DatabaseDriver.swift:547-552. For PostgreSQL connections using~/.pgpassthrough an SSH/Cloudflare/Cloud SQL tunnel, the app must resolve the password itself against the original host; with a blank saved username it now reports a match in the form, then looks up an empty username during connect and passes no password, so the connection can fail despite the green status.Useful? React with 👍 / 👎.