diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 7d21184..e2ff6f8 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -47,6 +47,14 @@ jobs: - run: sh -n install.sh - run: cargo fmt --all -- --check - run: cargo clippy --workspace --all-targets --locked -- -D warnings + - name: Put PostgreSQL server binaries on PATH + run: | + if [ "$RUNNER_OS" = macOS ]; then + brew install postgresql@17 + echo "$(brew --prefix postgresql@17)/bin" >> "$GITHUB_PATH" + else + ls -d /usr/lib/postgresql/*/bin | tail -n 1 >> "$GITHUB_PATH" + fi - run: cargo test --workspace --locked - name: Install and run the executable run: | diff --git a/Cargo.lock b/Cargo.lock index 76ae171..0fe09fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" dependencies = [ "cfg-if", "cipher", - "cpufeatures", + "cpufeatures 0.2.17", ] [[package]] @@ -455,6 +455,12 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + [[package]] name = "bitflags" version = "1.3.2" @@ -476,6 +482,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "block-padding" version = "0.3.3" @@ -504,6 +519,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.12.1" @@ -562,6 +583,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core 0.10.1", +] + [[package]] name = "chrono" version = "0.4.45" @@ -579,10 +611,16 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" dependencies = [ - "crypto-common", + "crypto-common 0.1.7", "inout", ] +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + [[package]] name = "compact_str" version = "0.8.2" @@ -606,6 +644,18 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "const-oid" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" + [[package]] name = "const-random" version = "0.1.18" @@ -661,6 +711,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + [[package]] name = "crossbeam-utils" version = "0.8.23" @@ -708,6 +767,24 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "ctutils" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03bb0e1cc970d482d121d9a1744999169b69a07470b3d644a7894e53fcaf4574" +dependencies = [ + "cmov", +] + [[package]] name = "darling" version = "0.20.11" @@ -769,10 +846,33 @@ dependencies = [ "num", "once_cell", "openssl", - "sha2", + "sha2 0.10.9", + "zeroize", +] + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid 0.9.6", + "der_derive", + "flagset", "zeroize", ] +[[package]] +name = "der_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8034092389675178f570469e6c3b0465d3d30b4505c294a6550db47f3c17ad18" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "derive-where" version = "1.7.0" @@ -790,11 +890,23 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", "subtle", ] +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid 0.10.2", + "crypto-common 0.2.2", + "ctutils", +] + [[package]] name = "downcast-rs" version = "1.2.1" @@ -847,7 +959,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -870,6 +982,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + [[package]] name = "fallible-iterator" version = "0.3.0" @@ -905,6 +1023,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" +[[package]] +name = "flagset" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7ac824320a75a52197e8f2d787f6a38b6718bb6897a35142d749af3c0e8f4fe" + [[package]] name = "flatbuffers" version = "25.12.19" @@ -976,7 +1100,7 @@ dependencies = [ "quote", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "syn 2.0.119", "toml", ] @@ -1136,7 +1260,7 @@ checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.1+wasi-snapshot-preview1", ] [[package]] @@ -1147,10 +1271,22 @@ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", - "r-efi", + "r-efi 5.3.0", "wasip2", ] +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", + "rand_core 0.10.1", +] + [[package]] name = "grid" version = "0.15.0" @@ -1238,7 +1374,7 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" dependencies = [ - "hmac", + "hmac 0.12.1", ] [[package]] @@ -1247,7 +1383,16 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" dependencies = [ - "digest", + "digest 0.10.7", +] + +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest 0.11.3", ] [[package]] @@ -1301,6 +1446,15 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.11.1" @@ -1601,6 +1755,15 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "libc", +] + [[package]] name = "libsqlite3-sys" version = "0.31.0" @@ -1654,6 +1817,16 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" +[[package]] +name = "md-5" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69b6441f590336821bb897fb28fc622898ccceb1d6cea3fde5ea86b090c4de98" +dependencies = [ + "cfg-if", + "digest 0.11.3", +] + [[package]] name = "memchr" version = "2.8.3" @@ -1683,7 +1856,7 @@ checksum = "69d83b0086dc8ecf3ce9ae2874b2d1290252e2a30720bea58a5c6639b0092873" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.1+wasi-snapshot-preview1", "windows-sys 0.61.2", ] @@ -1785,6 +1958,24 @@ dependencies = [ "libm", ] +[[package]] +name = "objc2-core-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "objc2-system-configuration" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7216bd11cbda54ccabcab84d523dc93b858ec75ecfb3a7d89513fa22464da396" +dependencies = [ + "objc2-core-foundation", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -1895,6 +2086,25 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_shared", + "serde", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.13" @@ -1973,6 +2183,35 @@ dependencies = [ "winreg", ] +[[package]] +name = "postgres-protocol" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08808e3c483c46e999108051c78334f473d5adb59d78bb80a1268c7e6aa6c514" +dependencies = [ + "base64", + "byteorder", + "bytes", + "fallible-iterator 0.2.0", + "hmac 0.13.0", + "md-5", + "memchr", + "rand 0.10.3", + "sha2 0.11.1", + "stringprep", +] + +[[package]] +name = "postgres-types" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "851ca9db4932932d69f3ea811b1abe63087a0f740a47692619dd40d4899b68be" +dependencies = [ + "bytes", + "fallible-iterator 0.2.0", + "postgres-protocol", +] + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -2047,6 +2286,12 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + [[package]] name = "rand" version = "0.8.8" @@ -2055,7 +2300,18 @@ checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core 0.10.1", ] [[package]] @@ -2065,7 +2321,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -2077,6 +2333,12 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + [[package]] name = "ratatui" version = "0.29.0" @@ -2157,7 +2419,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c6d5e5acb6f6129fe3f7ba0a7fc77bca1942cb568535e18e7bc40262baf3110" dependencies = [ "bitflags 2.13.2", - "fallible-iterator", + "fallible-iterator 0.3.0", "fallible-streaming-iterator", "hashlink", "libsqlite3-sys", @@ -2183,7 +2445,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -2196,7 +2458,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2286,9 +2548,9 @@ dependencies = [ "hkdf", "num", "once_cell", - "rand", + "rand 0.8.8", "serde", - "sha2", + "sha2 0.10.9", "zbus", ] @@ -2416,8 +2678,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.2.17", + "digest 0.10.7", ] [[package]] @@ -2427,8 +2689,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", - "cpufeatures", - "digest", + "cpufeatures 0.2.17", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -2484,6 +2757,12 @@ dependencies = [ "libc", ] +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + [[package]] name = "slab" version = "0.4.12" @@ -2515,6 +2794,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "sql-bomb" version = "0.1.0" @@ -2534,6 +2823,8 @@ dependencies = [ "portable-pty", "prost", "rusqlite", + "rustls", + "rustls-native-certs", "semver", "serde", "serde_json", @@ -2541,6 +2832,8 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", + "tokio-postgres", + "tokio-postgres-rustls", "tokio-stream", "tonic", "vt100", @@ -2567,6 +2860,17 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + [[package]] name = "strsim" version = "0.11.1" @@ -2651,7 +2955,7 @@ dependencies = [ "getrandom 0.3.4", "once_cell", "rustix 1.1.4", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2703,6 +3007,33 @@ dependencies = [ "crunchy", ] +[[package]] +name = "tinyvec" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" + +[[package]] +name = "tls_codec" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de2e01245e2bb89d6f05801c564fa27624dbd7b1846859876c7dad82e90bf6b" +dependencies = [ + "tls_codec_derive", + "zeroize", +] + +[[package]] +name = "tls_codec_derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2e76690929402faae40aebdda620a2c0e25dd6d3b9afe48867dfd95991f4bd" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "tokio" version = "1.48.0" @@ -2729,6 +3060,46 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tokio-postgres" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a528f7d280f6d5b9cd149635c8705b0dd049754bc67d81d31fa25169a93809d3" +dependencies = [ + "async-trait", + "byteorder", + "bytes", + "fallible-iterator 0.2.0", + "futures-channel", + "futures-util", + "log", + "parking_lot", + "percent-encoding", + "phf", + "pin-project-lite", + "postgres-protocol", + "postgres-types", + "rand 0.10.3", + "socket2", + "tokio", + "tokio-util", + "whoami", +] + +[[package]] +name = "tokio-postgres-rustls" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2ad44aa0ae96db89c4742212ed41645b2f597311ff6e1945542a4d9fadc2fb" +dependencies = [ + "rustls", + "sha2 0.11.1", + "tokio", + "tokio-postgres", + "tokio-rustls", + "x509-cert", +] + [[package]] name = "tokio-rustls" version = "0.26.6" @@ -2992,12 +3363,33 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + [[package]] name = "unicode-ident" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" +[[package]] +name = "unicode-normalization" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d" + [[package]] name = "unicode-segmentation" version = "1.12.0" @@ -3099,6 +3491,15 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasi" +version = "0.14.7+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "883478de20367e224c0090af9cf5f9fa85bed63a95c1abf3afc5c083ebc06e8c" +dependencies = [ + "wasip2", +] + [[package]] name = "wasip2" version = "1.0.4+wasi-0.2.12" @@ -3108,6 +3509,15 @@ dependencies = [ "wit-bindgen", ] +[[package]] +name = "wasite" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66fe902b4a6b8028a753d5424909b764ccf79b7a209eac9bf97e59cda9f71a42" +dependencies = [ + "wasi 0.14.7+wasi-0.2.4", +] + [[package]] name = "wasm-bindgen" version = "0.2.129" @@ -3153,6 +3563,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "web-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "web-time" version = "1.1.0" @@ -3163,6 +3583,19 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "whoami" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626c4bac6755d76ffc12cb01b2eac751db1996b9e0041de9aa02c8c211ddc82c" +dependencies = [ + "libc", + "libredox", + "objc2-system-configuration", + "wasite", + "web-sys", +] + [[package]] name = "winapi" version = "0.3.9" @@ -3368,6 +3801,18 @@ version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" +[[package]] +name = "x509-cert" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1301e935010a701ae5f8655edc0ad17c44bad3ac5ce8c39185f75453b720ae94" +dependencies = [ + "const-oid 0.9.6", + "der", + "spki", + "tls_codec", +] + [[package]] name = "xdg-home" version = "1.3.0" @@ -3396,7 +3841,7 @@ dependencies = [ "hex", "nix 0.29.0", "ordered-stream", - "rand", + "rand 0.8.8", "serde", "serde_repr", "sha1", diff --git a/Cargo.toml b/Cargo.toml index 9d688d0..b324915 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,12 +43,16 @@ keyring = { version = "=3.6.3", features = [ "apple-native", "sync-secret-service", "crypto-rust", "vendored", ] } rusqlite = { version = "=0.33.0", features = ["bundled"] } +rustls = { version = "=0.23.45", default-features = false, features = ["ring", "std"] } +rustls-native-certs = "=0.8.4" semver = "=1.0.28" serde = { version = "=1.0.229", features = ["derive"] } serde_json = "=1.0.151" sql-bomb-completion = { path = "crates/completion", version = "=0.1.0" } thiserror.workspace = true tokio.workspace = true +tokio-postgres = "=0.7.18" +tokio-postgres-rustls = { version = "=0.14.0", features = ["ring"] } tonic = { version = "=0.14.6", features = ["tls-ring", "tls-native-roots"] } [dependencies.fusor] diff --git a/README.md b/README.md index 0479d73..5df1d0d 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,13 @@ To check which version you're running, use `boom --version`. ### Connect to a server 1. Run `boom` and choose a client from the chooser. -2. Enter your server URL, including its scheme and port, such as - `http://localhost:50051`. This example requires a server already running at that - address. Use `https://` for a server with TLS encryption. -3. If the server requires authentication, enter its request headers, such as - `authorization: Bearer YOUR_TOKEN`. Otherwise, leave the headers empty. +2. Enter your server's connection address, such as `http://localhost:50051` for + Flight SQL or `postgres://postgres@localhost:5432/postgres` for PostgreSQL. These + examples require a server already running at that address. The + [client reference](#clients) describes each client's addresses and TLS options. +3. If the server requires authentication, enter its request headers for Flight SQL, + such as `authorization: Bearer YOUR_TOKEN`, or its PostgreSQL password. + Otherwise, leave the field empty. 4. Select **Open workspace**. Type a query in the editor and press Ctrl+R, or click **Run**: @@ -224,7 +226,7 @@ The Menu lists actions and their shortcuts. The full reference is below. | Ctrl+S | Star or unstar the editor's query / selected library query | | Enter in library search or list | Load the selected query into the editor | | Up / Down, then Enter in Connections | Choose and open a saved connection | -| Space on a checkbox | Toggle saving or remembering headers | +| Space on a checkbox | Toggle saving or remembering credentials | | Enter in SQL | Insert a newline | | Ctrl+A in SQL | Select the entire query | | Tab / Shift+Tab | Move between controls and panes | @@ -301,6 +303,64 @@ The optional `QueryClient::tables()` API uses `CommandGetTables` with schemas included. It returns each table's catalog, database schema, name, type, and Arrow column schema. +### PostgreSQL + +Choose **PostgreSQL** in the client chooser, or use its flag. Adding a connection +address also skips the form, so use it for servers that need no password: + +```sh +boom --postgres +boom --postgres postgres://postgres@localhost:5432/postgres +``` + +The address is a PostgreSQL URL with the user, host, port, and database, such as +`postgres://postgres@localhost:5432/postgres`. Connection parameters, such as +`sslmode` or `connect_timeout`, go in its query string. Key-value strings, such as +`host=localhost user=postgres dbname=postgres`, are also accepted. + +Addresses are saved in plaintext with your query history, so they cannot contain a +password. Enter it in the **Password** field, or leave the field empty for servers +that do not ask for one. **Remember password securely** stores it in macOS +Keychain or Linux Secret Service, as described for Flight SQL headers; otherwise, +reopening a saved connection asks for the password again. + +`sslmode` follows psql's rules. The default, `prefer`, uses TLS when the server +offers it and connects unencrypted otherwise; `require` refuses unencrypted +connections; `disable` never uses TLS. These modes encrypt without checking the +server's certificate, so servers with their own certificate authority, such as +Supabase, connect without setup, but an attacker could impersonate the server. +`verify-ca` also checks that a certificate authority trusted by your operating +system issued the certificate, and `verify-full` also checks that the certificate +names the host. The `allow` mode and `sslrootcert` are not supported. + +Each query opens a connection, prepares one statement, and streams its rows as +they arrive. Run one statement at a time; statements with parameters such as `$1` +are not supported. Each query runs on its own connection, so session settings and +transactions do not carry over to the next query. + +| PostgreSQL type | Arrow type | +| --- | --- | +| `boolean` | Boolean | +| `smallint`, `integer`, `bigint` | Int16, Int32, Int64 | +| `oid` | UInt32 | +| `real`, `double precision` | Float32, Float64 | +| `text`, `varchar`, `char`, `name`, `citext` | Utf8 | +| `numeric` | Utf8, with every digit the server returns | +| `uuid`, `json`, `jsonb` | Utf8 | +| `bytea` | Binary | +| `date` | Date32 | +| `time` | Time64 in microseconds | +| `timestamp`, `timestamptz` | Timestamp in microseconds; `timestamptz` in UTC | + +A query that returns another type, such as an array, `interval`, or enum, fails +with an error naming the column. Cast the column to text, as in `tags::text`. +Infinite dates and timestamps also need a text cast. + +SQL suggestions include tables, views, materialized views, and foreign tables +outside `pg_catalog`, `information_schema`, and other `pg_` schemas, qualified by +schema. Columns of the built-in types above are suggested; `citext` comes from an +extension, so its columns are not. + ## How it works - **Pluggable clients.** The public [QueryClient trait](src/lib.rs) returns a @@ -358,6 +418,10 @@ The Flight SQL integration test checks delivery before completion, multiple endpoints, request headers, nullable Unicode values, empty results, and errors. It also launches `boom` in a terminal to verify metadata discovery, Tab and mouse completion, caret placement, dismissal, and editing after a metadata failure. +The PostgreSQL integration test starts a temporary TLS server with `initdb`, +`pg_ctl`, and `openssl`, so those must be on your PATH. It checks password +authentication, every supported column type, results spanning batches, table +discovery, `sslmode` handling, and errors. UI tests cover client selection, saved connections, credential prompts, history, stars, search, menu navigation, responsive layout, and draft recovery. Storage tests check that profiles exclude secrets and failed saves preserve credentials. diff --git a/docs/flight-sql.md b/docs/flight-sql.md index 76b0033..d6daff2 100644 --- a/docs/flight-sql.md +++ b/docs/flight-sql.md @@ -60,3 +60,65 @@ Without that option, reopening a saved connection that used headers asks for them again. Renaming the connection without entering headers keeps that prompt. Credential-store failures appear in the form. Headers are never saved in query history or written to a plaintext fallback file. + +## PostgreSQL + +Choose **PostgreSQL** in the client chooser, or use its flag. Adding a connection +address also skips the form, so use it for servers that need no password: + +```sh +boom --postgres +boom --postgres postgres://postgres@localhost:5432/postgres +``` + +### Connection address + +The address is a PostgreSQL URL with the user, host, port, and database, such as +`postgres://postgres@localhost:5432/postgres`. Connection parameters, such as +`sslmode` or `connect_timeout`, go in its query string. Key-value strings, such as +`host=localhost user=postgres dbname=postgres`, are also accepted. + +Addresses are saved in plaintext with your query history, so they cannot contain a +password. Enter it in the **Password** field, or leave the field empty for servers +that do not ask for one. **Remember password securely** stores it in macOS +Keychain or Linux Secret Service, as described for Flight SQL headers; otherwise, +reopening a saved connection asks for the password again. + +`sslmode` follows psql's rules. The default, `prefer`, uses TLS when the server +offers it and connects unencrypted otherwise; `require` refuses unencrypted +connections; `disable` never uses TLS. These modes encrypt without checking the +server's certificate, so servers with their own certificate authority, such as +Supabase, connect without setup, but an attacker could impersonate the server. +`verify-ca` also checks that a certificate authority trusted by your operating +system issued the certificate, and `verify-full` also checks that the certificate +names the host. The `allow` mode and `sslrootcert` are not supported. + +### Queries and types + +Each query opens a connection, prepares one statement, and streams its rows as +they arrive. Run one statement at a time; statements with parameters such as `$1` +are not supported. Each query runs on its own connection, so session settings and +transactions do not carry over to the next query. + +| PostgreSQL type | Arrow type | +| --- | --- | +| `boolean` | Boolean | +| `smallint`, `integer`, `bigint` | Int16, Int32, Int64 | +| `oid` | UInt32 | +| `real`, `double precision` | Float32, Float64 | +| `text`, `varchar`, `char`, `name`, `citext` | Utf8 | +| `numeric` | Utf8, with every digit the server returns | +| `uuid`, `json`, `jsonb` | Utf8 | +| `bytea` | Binary | +| `date` | Date32 | +| `time` | Time64 in microseconds | +| `timestamp`, `timestamptz` | Timestamp in microseconds; `timestamptz` in UTC | + +A query that returns another type, such as an array, `interval`, or enum, fails +with an error naming the column. Cast the column to text, as in `tags::text`. +Infinite dates and timestamps also need a text cast. + +SQL suggestions include tables, views, materialized views, and foreign tables +outside `pg_catalog`, `information_schema`, and other `pg_` schemas, qualified by +schema. Columns of the built-in types above are suggested; `citext` comes from an +extension, so its columns are not. diff --git a/docs/get-started.md b/docs/get-started.md index 1d58e1b..963084d 100644 --- a/docs/get-started.md +++ b/docs/get-started.md @@ -55,11 +55,13 @@ To check which version you're running, use `boom --version`. ## Connect to a server 1. Run `boom` and choose a client from the chooser. -2. Enter your server URL, including its scheme and port, such as - `http://localhost:50051`. This example requires a server already running at that - address. Use `https://` for a server with TLS encryption. -3. If the server requires authentication, enter its request headers, such as - `authorization: Bearer YOUR_TOKEN`. Otherwise, leave the headers empty. +2. Enter your server's connection address, such as `http://localhost:50051` for + Flight SQL or `postgres://postgres@localhost:5432/postgres` for PostgreSQL. These + examples require a server already running at that address. The + [client reference](flight-sql.md) describes each client's addresses and TLS options. +3. If the server requires authentication, enter its request headers for Flight SQL, + such as `authorization: Bearer YOUR_TOKEN`, or its PostgreSQL password. + Otherwise, leave the field empty. 4. Select **Open workspace**. Type a query in the editor and press Ctrl+R, or click **Run**: diff --git a/docs/workspace.md b/docs/workspace.md index f721dbd..0c56315 100644 --- a/docs/workspace.md +++ b/docs/workspace.md @@ -102,7 +102,7 @@ The Menu lists actions and their shortcuts. The full reference is below. | Ctrl+S | Star or unstar the editor's query / selected library query | | Enter in library search or list | Load the selected query into the editor | | Up / Down, then Enter in Connections | Choose and open a saved connection | -| Space on a checkbox | Toggle saving or remembering headers | +| Space on a checkbox | Toggle saving or remembering credentials | | Enter in SQL | Insert a newline | | Ctrl+A in SQL | Select the entire query | | Tab or Shift+Tab | Move between controls and panes | diff --git a/src/client.rs b/src/client.rs index 6d4d88b..b026e1b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,11 +1,12 @@ -use sql_bomb::{Error, FlightSql, QueryClient}; +use sql_bomb::{Error, FlightSql, Postgres, QueryClient}; use std::rc::Rc; -pub(crate) const CLIENTS: &[Client] = &[Client::FlightSql]; +pub(crate) const CLIENTS: &[Client] = &[Client::FlightSql, Client::Postgres]; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub(crate) enum Client { FlightSql, + Postgres, } pub(crate) struct ClientProfile { @@ -14,13 +15,21 @@ pub(crate) struct ClientProfile { pub(crate) description: &'static str, pub(crate) default_address: &'static str, pub(crate) address_hint: &'static str, - pub(crate) supports_headers: bool, + pub(crate) secret: SecretField, +} + +pub(crate) struct SecretField { + pub(crate) label: &'static str, + pub(crate) noun: &'static str, + pub(crate) multiline: bool, + pub(crate) placeholder: &'static str, + pub(crate) hint: &'static str, } pub(crate) struct Connection { pub(crate) kind: Client, pub(crate) address: String, - pub(crate) headers: String, + pub(crate) secret: String, pub(crate) client: Rc, pub(crate) saved: Option>, } @@ -34,23 +43,44 @@ impl Client { description: "Stream Arrow results over gRPC", default_address: "http://localhost:50051", address_hint: "Use http://host:port or https://host:port for TLS.", - supports_headers: true, + secret: SecretField { + label: "Request headers", + noun: "headers", + multiline: true, + placeholder: "authorization: Bearer …", + hint: "One name: value per line. Leave empty if not needed.", + }, + }, + Self::Postgres => &ClientProfile { + name: "PostgreSQL", + flag: "--postgres", + description: "Query a PostgreSQL server", + default_address: "postgres://postgres@localhost:5432/postgres", + address_hint: "Use postgres://user@host:port/database. sslmode works as in psql.", + secret: SecretField { + label: "Password", + noun: "password", + multiline: false, + placeholder: "", + hint: "Leave empty if the server does not ask for one.", + }, }, } } - pub(crate) fn connect(self, address: String, headers: String) -> Result { + pub(crate) fn connect(self, address: String, secret: String) -> Result { let client: Rc = match self { Self::FlightSql => { let mut client = FlightSql::new(&address)?; - client.set_headers(&headers)?; + client.set_headers(&secret)?; Rc::new(client) } + Self::Postgres => Rc::new(Postgres::new(&address, &secret)?), }; Ok(Connection { kind: self, address, - headers, + secret, client, saved: None, }) diff --git a/src/clients.rs b/src/clients.rs index f223bf0..64b3f82 100644 --- a/src/clients.rs +++ b/src/clients.rs @@ -1,3 +1,5 @@ mod flight_sql; +mod postgres; pub use flight_sql::FlightSql; +pub use postgres::Postgres; diff --git a/src/clients/postgres.rs b/src/clients/postgres.rs new file mode 100644 index 0000000..c1e0714 --- /dev/null +++ b/src/clients/postgres.rs @@ -0,0 +1,87 @@ +mod columns; +mod tables; +mod tls; +mod values; + +use crate::{Error, Query, QueryClient, Table}; +use columns::Columns; +use futures_util::{StreamExt, future::BoxFuture}; +use std::{iter, pin::pin}; +use tokio::task::JoinSet; +use tokio_postgres::{Client, Config, types::ToSql}; +use tokio_postgres_rustls::MakeRustlsConnect; + +const BATCH_ROWS: usize = 1024; + +pub struct Postgres { + config: Config, + tls: MakeRustlsConnect, +} + +struct Session { + client: Client, + // Dropping the set aborts the connection task, closing the socket when a query stream is + // dropped. Connection failures surface as closed-connection errors on the pending request. + _connection: JoinSet>, +} + +impl Postgres { + /// An empty `password` sends none. + pub fn new(address: &str, password: &str) -> Result { + let (address, tls) = tls::connector(address)?; + let mut config: Config = address.parse().map_err(Error::PostgresAddress)?; + if config.get_password().is_some() { + return Err(Error::PostgresPassword); + } + if !password.is_empty() { + config.password(password); + } + Ok(Self { config, tls }) + } + + async fn connect(&self) -> Result { + let (client, connection) = self.config.connect(self.tls.clone()).await?; + let mut tasks = JoinSet::new(); + tasks.spawn(connection); + Ok(Session { + client, + _connection: tasks, + }) + } +} + +impl QueryClient for Postgres { + fn query(&self, sql: String) -> BoxFuture<'_, Result> { + Box::pin(async move { + let session = self.connect().await?; + let statement = session.client.prepare(&sql).await?; + let columns = Columns::new(statement.columns())?; + let schema = columns.schema.as_ref().clone(); + let rows = session + .client + .query_raw(&statement, iter::empty::<&dyn ToSql>()) + .await?; + let batches = async_stream::try_stream! { + let mut chunks = pin!(rows.ready_chunks(BATCH_ROWS)); + while let Some(chunk) = chunks.next().await { + let rows = chunk.into_iter().collect::, _>>()?; + yield columns.batch(&rows)?; + } + // The stream owns the session so the connection stays open until rows end. + drop(session); + }; + Ok(Query { + schema, + batches: Box::pin(batches), + }) + }) + } + + fn tables(&self) -> Option, Error>>> { + Some(Box::pin(async move { + let session = self.connect().await?; + let rows = session.client.query(tables::QUERY, &[]).await?; + rows.iter().map(tables::table).collect() + })) + } +} diff --git a/src/clients/postgres/columns.rs b/src/clients/postgres/columns.rs new file mode 100644 index 0000000..2499f14 --- /dev/null +++ b/src/clients/postgres/columns.rs @@ -0,0 +1,155 @@ +use super::values::{Days, Json, Microseconds, Numeric, Uuid}; +use crate::Error; +use arrow_array::{ + ArrayRef, BinaryArray, BooleanArray, Date32Array, Float32Array, Float64Array, Int16Array, + Int32Array, Int64Array, RecordBatch, StringArray, Time64MicrosecondArray, + TimestampMicrosecondArray, UInt32Array, +}; +use arrow_schema::{DataType, Field, Schema, SchemaRef, TimeUnit}; +use std::sync::Arc; +use tokio_postgres::{ + Column, Row, + types::{FromSql, Type}, +}; + +const UTC: &str = "+00:00"; + +pub(super) struct Columns { + pub(super) schema: SchemaRef, + types: Vec, +} + +#[derive(Clone, Copy)] +pub(super) enum ColumnType { + Boolean, + Int16, + Int32, + Int64, + Oid, + Float32, + Float64, + Text, + Bytes, + Numeric, + Uuid, + Json, + Date, + Time, + Timestamp, + TimestampTz, +} + +impl Columns { + pub(super) fn new(columns: &[Column]) -> Result { + let mut fields = Vec::with_capacity(columns.len()); + let mut types = Vec::with_capacity(columns.len()); + for column in columns { + let column_type = + ColumnType::new(column.type_()).ok_or_else(|| Error::PostgresType { + column: column.name().to_owned(), + type_name: column.type_().name().to_owned(), + })?; + fields.push(Field::new(column.name(), column_type.data_type(), true)); + types.push(column_type); + } + Ok(Self { + schema: Arc::new(Schema::new(fields)), + types, + }) + } + + pub(super) fn batch(&self, rows: &[Row]) -> Result { + let arrays = self + .types + .iter() + .enumerate() + .map(|(index, column_type)| column_type.array(rows, index)) + .collect::, _>>()?; + Ok(RecordBatch::try_new(self.schema.clone(), arrays)?) + } +} + +impl ColumnType { + pub(super) fn new(postgres_type: &Type) -> Option { + Some(match *postgres_type { + Type::BOOL => Self::Boolean, + Type::INT2 => Self::Int16, + Type::INT4 => Self::Int32, + Type::INT8 => Self::Int64, + Type::OID => Self::Oid, + Type::FLOAT4 => Self::Float32, + Type::FLOAT8 => Self::Float64, + Type::BYTEA => Self::Bytes, + Type::NUMERIC => Self::Numeric, + Type::UUID => Self::Uuid, + Type::JSON | Type::JSONB => Self::Json, + Type::DATE => Self::Date, + Type::TIME => Self::Time, + Type::TIMESTAMP => Self::Timestamp, + Type::TIMESTAMPTZ => Self::TimestampTz, + ref text if <&str as FromSql>::accepts(text) => Self::Text, + _ => return None, + }) + } + + pub(super) fn data_type(self) -> DataType { + match self { + Self::Boolean => DataType::Boolean, + Self::Int16 => DataType::Int16, + Self::Int32 => DataType::Int32, + Self::Int64 => DataType::Int64, + Self::Oid => DataType::UInt32, + Self::Float32 => DataType::Float32, + Self::Float64 => DataType::Float64, + Self::Text | Self::Numeric | Self::Uuid | Self::Json => DataType::Utf8, + Self::Bytes => DataType::Binary, + Self::Date => DataType::Date32, + Self::Time => DataType::Time64(TimeUnit::Microsecond), + Self::Timestamp => DataType::Timestamp(TimeUnit::Microsecond, None), + Self::TimestampTz => DataType::Timestamp(TimeUnit::Microsecond, Some(UTC.into())), + } + } + + fn array(self, rows: &[Row], index: usize) -> Result { + Ok(match self { + Self::Boolean => Arc::new(values::(rows, index)?), + Self::Int16 => Arc::new(values::(rows, index)?), + Self::Int32 => Arc::new(values::(rows, index)?), + Self::Int64 => Arc::new(values::(rows, index)?), + Self::Oid => Arc::new(values::(rows, index)?), + Self::Float32 => Arc::new(values::(rows, index)?), + Self::Float64 => Arc::new(values::(rows, index)?), + Self::Text => Arc::new(values::(rows, index)?), + Self::Bytes => Arc::new(values::(rows, index)?), + Self::Numeric => Arc::new(values::(rows, index)?), + Self::Uuid => Arc::new(values::(rows, index)?), + Self::Json => Arc::new(values::(rows, index)?), + Self::Date => Arc::new(values::(rows, index)?), + Self::Time => Arc::new(values::( + rows, index, + )?), + Self::Timestamp => Arc::new(timestamps(rows, index)?), + Self::TimestampTz => Arc::new(timestamps(rows, index)?.with_timezone(UTC)), + }) + } +} + +fn timestamps( + rows: &[Row], + index: usize, +) -> Result { + values::<_, Microseconds, i64>(rows, index) +} + +fn values<'a, Output, Value, Element>( + rows: &'a [Row], + index: usize, +) -> Result +where + Output: FromIterator>, + Value: FromSql<'a> + Into, +{ + rows.iter() + .map(|row| Ok(row.try_get::<_, Option>(index)?.map(Into::into))) + .collect() +} diff --git a/src/clients/postgres/tables.rs b/src/clients/postgres/tables.rs new file mode 100644 index 0000000..ba7a70b --- /dev/null +++ b/src/clients/postgres/tables.rs @@ -0,0 +1,36 @@ +use super::columns::ColumnType; +use crate::{Error, Table}; +use arrow_schema::{Field, Schema}; +use tokio_postgres::{Row, types::Type}; + +pub(super) const QUERY: &str = include_str!("tables.sql"); + +const SCHEMA_NAME: &str = "schema_name"; +const TABLE_NAME: &str = "table_name"; +const TABLE_TYPE: &str = "table_type"; +const COLUMN_NAMES: &str = "column_names"; +const COLUMN_TYPES: &str = "column_types"; +const NULLABLE: &str = "nullable"; + +pub(super) fn table(row: &Row) -> Result { + let names: Vec<&str> = row.try_get(COLUMN_NAMES)?; + let types: Vec = row.try_get(COLUMN_TYPES)?; + let nullable: Vec = row.try_get(NULLABLE)?; + // Queries fail on columns of unsupported types, so discovery leaves them out. + let fields: Vec = names + .into_iter() + .zip(types) + .zip(nullable) + .filter_map(|((name, oid), nullable)| { + let column_type = ColumnType::new(&Type::from_oid(oid)?)?; + Some(Field::new(name, column_type.data_type(), nullable)) + }) + .collect(); + Ok(Table { + catalog: None, + database_schema: Some(row.try_get(SCHEMA_NAME)?), + name: row.try_get(TABLE_NAME)?, + table_type: row.try_get(TABLE_TYPE)?, + schema: Schema::new(fields), + }) +} diff --git a/src/clients/postgres/tables.sql b/src/clients/postgres/tables.sql new file mode 100644 index 0000000..192ea0e --- /dev/null +++ b/src/clients/postgres/tables.sql @@ -0,0 +1,22 @@ +SELECT + namespace.nspname AS schema_name, + class.relname AS table_name, + CASE class.relkind + WHEN 'v' THEN 'VIEW' + WHEN 'm' THEN 'MATERIALIZED VIEW' + WHEN 'f' THEN 'FOREIGN TABLE' + ELSE 'TABLE' + END AS table_type, + array_agg(attribute.attname ORDER BY attribute.attnum) AS column_names, + array_agg(attribute.atttypid ORDER BY attribute.attnum) AS column_types, + array_agg(NOT attribute.attnotnull ORDER BY attribute.attnum) AS nullable +FROM pg_catalog.pg_class AS class +JOIN pg_catalog.pg_namespace AS namespace ON namespace.oid = class.relnamespace +JOIN pg_catalog.pg_attribute AS attribute ON attribute.attrelid = class.oid +WHERE class.relkind IN ('r', 'p', 'v', 'm', 'f') + AND attribute.attnum > 0 + AND NOT attribute.attisdropped + AND namespace.nspname NOT IN ('pg_catalog', 'information_schema') + AND namespace.nspname NOT LIKE 'pg\_%' +GROUP BY namespace.nspname, class.relname, class.relkind +ORDER BY namespace.nspname, class.relname diff --git a/src/clients/postgres/tls.rs b/src/clients/postgres/tls.rs new file mode 100644 index 0000000..193a82b --- /dev/null +++ b/src/clients/postgres/tls.rs @@ -0,0 +1,140 @@ +use crate::Error; +use rustls::{ + ClientConfig, DigitallySignedStruct, RootCertStore, SignatureScheme, + client::{ + danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}, + verify_server_cert_signed_by_trust_anchor, verify_server_name, + }, + crypto::{WebPkiSupportedAlgorithms, ring, verify_tls12_signature, verify_tls13_signature}, + pki_types::{CertificateDer, ServerName, UnixTime}, + server::ParsedCertificate, +}; +use std::{ops::Range, sync::Arc}; +use tokio_postgres_rustls::MakeRustlsConnect; + +const REQUIRE: &str = "sslmode=require"; +const VERIFYING_MODES: [(&str, TrustRoots); 2] = [ + ("sslmode=verify-ca", Trust::Authority), + ("sslmode=verify-full", Trust::AuthorityAndHost), +]; + +type TrustRoots = fn(RootCertStore) -> Trust; + +// As in psql, `prefer` and `require` encrypt without checking the server's certificate. +#[derive(Debug)] +enum Trust { + Any, + Authority(RootCertStore), + AuthorityAndHost(RootCertStore), +} + +#[derive(Debug)] +struct ServerCertificate { + trust: Trust, + algorithms: WebPkiSupportedAlgorithms, +} + +// tokio-postgres only parses `disable`, `prefer`, and `require`, so a verifying `sslmode` is +// returned in the address as `require`, with the check moved into the connector. +pub(super) fn connector(address: &str) -> Result<(String, MakeRustlsConnect), Error> { + let verifying_mode = VERIFYING_MODES + .iter() + .find_map(|(parameter, trust)| Some((parameter_range(address, parameter)?, trust))); + let mut address = address.to_owned(); + let trust = match verifying_mode { + Some((range, trust)) => { + address.replace_range(range, REQUIRE); + trust(native_roots()?) + } + None => Trust::Any, + }; + let provider = Arc::new(ring::default_provider()); + let verifier = ServerCertificate { + trust, + algorithms: provider.signature_verification_algorithms, + }; + let config = ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .expect("the ring provider supports the default TLS versions") + .dangerous() + .with_custom_certificate_verifier(Arc::new(verifier)) + .with_no_client_auth(); + Ok((address, MakeRustlsConnect::new(config))) +} + +// Other spellings, such as quoted values, reach tokio-postgres unchanged and fail to parse. +fn parameter_range(address: &str, parameter: &str) -> Option> { + let is_separator = + |character: char| matches!(character, '?' | '&') || character.is_whitespace(); + address + .match_indices(parameter) + .map(|(start, _)| start..start + parameter.len()) + .find(|range| { + address[..range.start] + .chars() + .next_back() + .is_none_or(is_separator) + && address[range.end..].chars().next().is_none_or(is_separator) + }) +} + +fn native_roots() -> Result { + let native = rustls_native_certs::load_native_certs(); + if native.certs.is_empty() { + return Err(Error::Certificates); + } + let mut roots = RootCertStore::empty(); + // Certificates that fail to parse are skipped; the remaining roots still verify servers. + roots.add_parsable_certificates(native.certs); + Ok(roots) +} + +impl ServerCertVerifier for ServerCertificate { + fn verify_server_cert( + &self, + end_entity: &CertificateDer<'_>, + intermediates: &[CertificateDer<'_>], + server_name: &ServerName<'_>, + _ocsp_response: &[u8], + now: UnixTime, + ) -> Result { + let roots = match &self.trust { + Trust::Any => return Ok(ServerCertVerified::assertion()), + Trust::Authority(roots) | Trust::AuthorityAndHost(roots) => roots, + }; + let certificate = ParsedCertificate::try_from(end_entity)?; + verify_server_cert_signed_by_trust_anchor( + &certificate, + roots, + intermediates, + now, + self.algorithms.all, + )?; + if matches!(self.trust, Trust::AuthorityAndHost(_)) { + verify_server_name(&certificate, server_name)?; + } + Ok(ServerCertVerified::assertion()) + } + + fn verify_tls12_signature( + &self, + message: &[u8], + certificate: &CertificateDer<'_>, + signature: &DigitallySignedStruct, + ) -> Result { + verify_tls12_signature(message, certificate, signature, &self.algorithms) + } + + fn verify_tls13_signature( + &self, + message: &[u8], + certificate: &CertificateDer<'_>, + signature: &DigitallySignedStruct, + ) -> Result { + verify_tls13_signature(message, certificate, signature, &self.algorithms) + } + + fn supported_verify_schemes(&self) -> Vec { + self.algorithms.supported_schemes() + } +} diff --git a/src/clients/postgres/values.rs b/src/clients/postgres/values.rs new file mode 100644 index 0000000..89e7e0c --- /dev/null +++ b/src/clients/postgres/values.rs @@ -0,0 +1,192 @@ +use std::{error::Error, fmt::Write}; +use tokio_postgres::types::{FromSql, Type}; + +type DecodeError = Box; + +const POSTGRES_EPOCH_DAYS: i32 = 10_957; +const MICROSECONDS_PER_DAY: i64 = 86_400_000_000; +const POSTGRES_EPOCH_MICROSECONDS: i64 = POSTGRES_EPOCH_DAYS as i64 * MICROSECONDS_PER_DAY; +const OUT_OF_RANGE: &str = + "infinite or out-of-range value has no Arrow equivalent; cast it to text"; +const JSONB_VERSION: u8 = 1; +const UNKNOWN_JSONB: &str = "unsupported jsonb format version; cast the value to text"; +const UUID_GROUP_STARTS: [usize; 4] = [4, 6, 8, 10]; +const UUID_BYTES: usize = 16; +const NUMERIC_WORD_BYTES: usize = 2; +const NUMERIC_WORD_DIGITS: usize = 4; +const NUMERIC_NEGATIVE: u16 = 0x4000; +const NUMERIC_NAN: u16 = 0xC000; +const NUMERIC_INFINITY: u16 = 0xD000; +const NUMERIC_NEGATIVE_INFINITY: u16 = 0xF000; +const MALFORMED_NUMERIC: &str = "malformed numeric value"; + +// Days since the Unix epoch. +pub(super) struct Days(i32); + +// Microseconds since midnight for times, or since the Unix epoch for timestamps. +pub(super) struct Microseconds(i64); + +pub(super) struct Numeric(String); + +pub(super) struct Uuid(String); + +pub(super) struct Json<'a>(&'a str); + +impl From for i32 { + fn from(days: Days) -> Self { + days.0 + } +} + +impl From for i64 { + fn from(microseconds: Microseconds) -> Self { + microseconds.0 + } +} + +impl From for String { + fn from(numeric: Numeric) -> Self { + numeric.0 + } +} + +impl From for String { + fn from(uuid: Uuid) -> Self { + uuid.0 + } +} + +impl<'a> From> for &'a str { + fn from(json: Json<'a>) -> Self { + json.0 + } +} + +impl FromSql<'_> for Days { + fn from_sql(_: &Type, raw: &[u8]) -> Result { + match i32::from_be_bytes(raw.try_into()?) { + i32::MIN | i32::MAX => Err(OUT_OF_RANGE.into()), + days => Ok(Self( + days.checked_add(POSTGRES_EPOCH_DAYS).ok_or(OUT_OF_RANGE)?, + )), + } + } + + fn accepts(postgres_type: &Type) -> bool { + *postgres_type == Type::DATE + } +} + +impl FromSql<'_> for Microseconds { + fn from_sql(postgres_type: &Type, raw: &[u8]) -> Result { + let microseconds = i64::from_be_bytes(raw.try_into()?); + if *postgres_type == Type::TIME { + return Ok(Self(microseconds)); + } + match microseconds { + i64::MIN | i64::MAX => Err(OUT_OF_RANGE.into()), + since_postgres_epoch => Ok(Self( + since_postgres_epoch + .checked_add(POSTGRES_EPOCH_MICROSECONDS) + .ok_or(OUT_OF_RANGE)?, + )), + } + } + + fn accepts(postgres_type: &Type) -> bool { + matches!( + *postgres_type, + Type::TIME | Type::TIMESTAMP | Type::TIMESTAMPTZ + ) + } +} + +impl FromSql<'_> for Numeric { + fn from_sql(_: &Type, raw: &[u8]) -> Result { + let (words, []) = raw.as_chunks::() else { + return Err(MALFORMED_NUMERIC.into()); + }; + let words: Vec = words.iter().map(|word| u16::from_be_bytes(*word)).collect(); + let [count, weight, sign, scale, digits @ ..] = words.as_slice() else { + return Err(MALFORMED_NUMERIC.into()); + }; + if digits.len() != usize::from(*count) { + return Err(MALFORMED_NUMERIC.into()); + } + let mut text = match *sign { + NUMERIC_NAN => return Ok(Self("NaN".into())), + NUMERIC_INFINITY => return Ok(Self("Infinity".into())), + NUMERIC_NEGATIVE_INFINITY => return Ok(Self("-Infinity".into())), + NUMERIC_NEGATIVE => String::from("-"), + _ => String::new(), + }; + let weight = i32::from(weight.cast_signed()); + // Postgres omits zero digit words before and after the stored ones. + let digit = |position: i32| { + usize::try_from(position) + .ok() + .and_then(|index| digits.get(index)) + .map_or(0, |digit| *digit) + }; + if weight < 0 { + text.push('0'); + } else { + write!(text, "{}", digit(0))?; + for position in 1..=weight { + write!(text, "{:0NUMERIC_WORD_DIGITS$}", digit(position))?; + } + } + let scale = usize::from(*scale); + if scale > 0 { + text.push('.'); + let fraction_end = text.len() + scale; + let mut position = weight + 1; + while text.len() < fraction_end { + write!(text, "{:0NUMERIC_WORD_DIGITS$}", digit(position))?; + position += 1; + } + text.truncate(fraction_end); + } + Ok(Self(text)) + } + + fn accepts(postgres_type: &Type) -> bool { + *postgres_type == Type::NUMERIC + } +} + +impl FromSql<'_> for Uuid { + fn from_sql(_: &Type, raw: &[u8]) -> Result { + let bytes: [u8; UUID_BYTES] = raw.try_into()?; + let mut text = String::new(); + for (index, byte) in bytes.iter().enumerate() { + if UUID_GROUP_STARTS.contains(&index) { + text.push('-'); + } + write!(text, "{byte:02x}")?; + } + Ok(Self(text)) + } + + fn accepts(postgres_type: &Type) -> bool { + *postgres_type == Type::UUID + } +} + +impl<'a> FromSql<'a> for Json<'a> { + fn from_sql(postgres_type: &Type, raw: &'a [u8]) -> Result { + let text = if *postgres_type == Type::JSONB { + match raw.split_first() { + Some((&JSONB_VERSION, text)) => text, + _ => return Err(UNKNOWN_JSONB.into()), + } + } else { + raw + }; + Ok(Self(std::str::from_utf8(text)?)) + } + + fn accepts(postgres_type: &Type) -> bool { + matches!(*postgres_type, Type::JSON | Type::JSONB) + } +} diff --git a/src/lib.rs b/src/lib.rs index 4c2b7c9..796ba75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use arrow_array::RecordBatch; use arrow_schema::Schema; use futures_util::{future::BoxFuture, stream::BoxStream}; -pub use clients::FlightSql; +pub use clients::{FlightSql, Postgres}; pub trait QueryClient { fn query(&self, sql: String) -> BoxFuture<'_, Result>; @@ -49,4 +49,33 @@ pub enum Error { TableMetadata(&'static str), #[error("Server returned an endpoint without a ticket; check the Flight SQL server")] MissingTicket, + #[error( + "Invalid PostgreSQL connection address; use postgres://user@host:port/database: {}", + with_causes(.0) + )] + PostgresAddress(tokio_postgres::Error), + #[error("Remove the password from the connection address; enter it in the Password field")] + PostgresPassword, + #[error("Cannot load trusted TLS certificates; check the operating system's certificate store")] + Certificates, + #[error( + "PostgreSQL request failed; check the query, credentials, and server: {}", + with_causes(.0) + )] + Postgres(#[from] tokio_postgres::Error), + #[error( + "Column {column} has unsupported PostgreSQL type {type_name}; cast it, as in {column}::text" + )] + PostgresType { column: String, type_name: String }, +} + +// tokio-postgres keeps details such as the server's message in `source`, not in `Display`. +fn with_causes(error: &tokio_postgres::Error) -> String { + let causes = std::iter::successors(Some(error as &dyn std::error::Error), |cause| { + cause.source() + }); + causes + .map(ToString::to_string) + .collect::>() + .join(": ") } diff --git a/src/library.rs b/src/library.rs index 3449d3d..341157b 100644 --- a/src/library.rs +++ b/src/library.rs @@ -30,7 +30,7 @@ pub(crate) enum Error { #[error("Cannot read or save the query library; check its database and file permissions: {0}")] Database(#[from] rusqlite::Error), #[error( - "Cannot access saved credentials; unlock your OS credential store or use session-only headers" + "Cannot access saved credentials; unlock your OS credential store or use session-only credentials" )] Credentials(#[from] keyring::Error), #[error( diff --git a/src/library/connections.rs b/src/library/connections.rs index 762e1ab..b0e1d4a 100644 --- a/src/library/connections.rs +++ b/src/library/connections.rs @@ -118,7 +118,7 @@ impl Library { ], )?; let change = if credentials == Credentials::Keyring { - CredentialChange::Store(&connection.headers) + CredentialChange::Store(&connection.secret) } else if previous.as_deref() == Some(Credentials::Keyring.stored()) { CredentialChange::Forget } else { @@ -134,7 +134,7 @@ impl Library { })) } - pub(crate) fn connection_headers( + pub(crate) fn connection_secret( &self, profile: &SavedConnection, ) -> Result, Error> { diff --git a/src/library/credentials.rs b/src/library/credentials.rs index 1d5b048..f3476e5 100644 --- a/src/library/credentials.rs +++ b/src/library/credentials.rs @@ -25,13 +25,13 @@ impl Library { let entry = (self.credentials)(account)?; let previous = read(&entry)?; match change { - CredentialChange::Store(headers) => entry.set_password(headers)?, + CredentialChange::Store(secret) => entry.set_password(secret)?, CredentialChange::Forget => forget(&entry)?, CredentialChange::Keep => unreachable!("unchanged credentials commit without a store"), } if let Err(error) = transaction.commit() { let restored = match previous { - Some(headers) => entry.set_password(&headers), + Some(secret) => entry.set_password(&secret), None => forget(&entry), }; restored.map_err(|_| Error::CredentialRollback)?; @@ -43,7 +43,7 @@ impl Library { pub(super) fn read(entry: &keyring::Entry) -> keyring::Result> { match entry.get_password() { - Ok(headers) => Ok(Some(headers)), + Ok(secret) => Ok(Some(secret)), // Credentials can be removed independently in the OS credential store. Err(keyring::Error::NoEntry) => Ok(None), Err(error) => Err(error), diff --git a/src/terminal.rs b/src/terminal.rs index 2f916ba..8ec50ac 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -49,7 +49,7 @@ enum Execution { pub(crate) struct Terminal { connection: Signal>, address: Signal, - request_headers: Signal, + secret: Signal, connection_error: Signal, catalog: ConnectionCatalog, draft: ConnectionDraft, @@ -95,7 +95,7 @@ impl FromInputs for Terminal { }; Ok(Self { address: signal(address), - request_headers: signal(String::new()), + secret: signal(String::new()), connection: signal(connection), connection_error: signal(String::new()), catalog, diff --git a/src/terminal/connection.rs b/src/terminal/connection.rs index 2a5d573..38b01e8 100644 --- a/src/terminal/connection.rs +++ b/src/terminal/connection.rs @@ -59,26 +59,29 @@ impl Terminal { } pub(super) fn connect(&self) -> Result<(), Error> { - let headers_missing = self - .request_headers - .with(|headers| headers.trim().is_empty()); - if self.saving_connection() && self.draft.remember.get() && headers_missing { - self.connection_error - .set("Enter headers to remember, or uncheck Remember headers.".into()); - return Ok(()); - } - if matches!(self.screen.get(), Screen::Credentials(_)) && headers_missing { - self.connection_error - .set("Enter the request headers for this connection.".into()); - return Ok(()); - } let kind = match self.screen.get() { Screen::Connection(kind) => kind, Screen::Credentials(profile) => profile.kind, _ => return Ok(()), }; + let secret = &kind.profile().secret; + let secret_missing = self.secret.with(|value| value.trim().is_empty()); + if self.saving_connection() && self.draft.remember.get() && secret_missing { + self.connection_error.set(format!( + "Enter {0} to remember, or uncheck Remember {0}.", + secret.noun + )); + return Ok(()); + } + if matches!(self.screen.get(), Screen::Credentials(_)) && secret_missing { + self.connection_error.set(format!( + "Enter the {} for this connection.", + secret.label.to_lowercase() + )); + return Ok(()); + } let address = self.address.with(|address| address.trim().to_owned()); - let connection = kind.connect(address, self.request_headers.get()); + let connection = kind.connect(address, self.secret.get()); match connection { Ok(connection) => { if let Err(error) = self.submit_connection(connection) { @@ -100,7 +103,7 @@ impl Terminal { }; self.suggest_connection_name(); let name = self.draft.name.with(|name| name.trim().to_owned()); - let credentials = if connection.headers.trim().is_empty() { + let credentials = if connection.secret.trim().is_empty() { original .as_ref() .filter(|profile| profile.credentials == Credentials::Session) @@ -176,7 +179,7 @@ impl Terminal { self.connection.with(|connection| { if let Some(connection) = connection { self.address.set(connection.address.clone()); - self.request_headers.set(connection.headers.clone()); + self.secret.set(connection.secret.clone()); self.draft.name.set(String::new()); self.draft.save.set(true); self.draft.remember.set(false); diff --git a/src/terminal/connections.rs b/src/terminal/connections.rs index 4195c2b..547d8ed 100644 --- a/src/terminal/connections.rs +++ b/src/terminal/connections.rs @@ -42,7 +42,7 @@ impl Terminal { self.catalog.search.set(String::new()); self.search_connections()?; self.address.set(String::new()); - self.request_headers.set(String::new()); + self.secret.set(String::new()); self.draft.name.set(String::new()); self.draft.save.set(false); self.draft.remember.set(false); @@ -82,8 +82,8 @@ impl Terminal { pub(super) fn open_saved_connection(&self, profile: Rc) -> Result<(), Error> { self.connection_error.set(String::new()); - match self.library.connection_headers(&profile) { - Ok(Some(headers)) => match profile.kind.connect(profile.address.clone(), headers) { + match self.library.connection_secret(&profile) { + Ok(Some(secret)) => match profile.kind.connect(profile.address.clone(), secret) { Ok(mut connection) => { connection.saved = Some(profile); self.activate_connection(connection)?; @@ -96,7 +96,7 @@ impl Terminal { .purpose .set(ConnectionPurpose::Reopen(profile.clone())); self.screen.set(Screen::Credentials(profile)); - self.focus("request-headers"); + self.focus("connection-secret"); } Err(error) => self.connection_error.set(error.to_string()), } @@ -114,9 +114,9 @@ impl Terminal { let Some(profile) = self.selected_connection() else { return Ok(()); }; - match self.library.connection_headers(&profile) { - Ok(headers) => { - self.prepare_saved_connection(&profile, headers.unwrap_or_default()); + match self.library.connection_secret(&profile) { + Ok(secret) => { + self.prepare_saved_connection(&profile, secret.unwrap_or_default()); self.screen.set(Screen::Connection(profile.kind)); self.focus("connection-name"); } @@ -125,9 +125,9 @@ impl Terminal { Ok(()) } - fn prepare_saved_connection(&self, profile: &Rc, headers: String) { + fn prepare_saved_connection(&self, profile: &Rc, secret: String) { self.address.set(profile.address.clone()); - self.request_headers.set(headers); + self.secret.set(secret); self.draft.name.set(profile.name.clone()); self.draft.save.set(true); self.draft diff --git a/src/terminal/view.rs b/src/terminal/view.rs index f4ecb34..02dc1c3 100644 --- a/src/terminal/view.rs +++ b/src/terminal/view.rs @@ -5,12 +5,16 @@ use crate::library::Collection; use std::rc::Rc; macro_rules! terminal_views { - ($($name:ident => $template:literal),+ $(,)?) => { + ($($name:ident $({ $input:ident: $type:ty })? => $template:literal),+ $(,)?) => { $( #[derive(fusor::FromInputs)] struct $name { #[input] terminal: Rc, + $( + #[input] + $input: $type, + )? } fusor::template!(backend = "hypercmd", $template); @@ -20,8 +24,9 @@ macro_rules! terminal_views { terminal_views! { SavedConnections => "ui/connections/saved.html", - ConnectionHeaders => "ui/connections/headers.html", - ConnectionCredentials => "ui/connections/credentials.html", + ConnectionForm { client: Client } => "ui/connections/form.html", + ConnectionSecret { client: Client } => "ui/connections/secret.html", + ConnectionCredentials { client: Client } => "ui/connections/credentials.html", RemoveConnection => "ui/connections/remove.html", ClientChooser => "ui/connections/clients.html", QueryWorkspace => "ui/workspace.html", @@ -31,13 +36,4 @@ terminal_views! { CommandMenu => "ui/menu.html", } -#[derive(fusor::FromInputs)] -struct ConnectionForm { - #[input] - terminal: Rc, - #[input] - client: Client, -} - -fusor::template!(backend = "hypercmd", "ui/connections/form.html"); fusor::template!(backend = "hypercmd", "ui/app.html"); diff --git a/src/tests.rs b/src/tests.rs index d117e70..b83f79f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -14,6 +14,7 @@ use std::{ const ENDPOINT: &str = "http://localhost:50051"; const OTHER_ENDPOINT: &str = "http://localhost:50052"; +const POSTGRES_ENDPOINT: &str = "postgres://postgres@localhost:5432/postgres"; const SAVED_SQL: &str = "-- café\nSELECT '東京_100%';"; const DRAFT: &str = "SELECT 'unfinished draft';"; const CONTROL: Modifiers = Modifiers { @@ -290,6 +291,28 @@ fn client_selection_and_configuration_precede_the_workspace() { fs::remove_dir_all(directory).unwrap(); } +#[test] +fn postgres_password_is_one_line_so_enter_opens_the_workspace() { + let directory = library_directory("postgres"); + let crate::cli::Command::Start(startup) = + crate::cli::parse(["--postgres".to_owned()].into_iter()).unwrap() + else { + panic!("A client flag starts the application"); + }; + let mut session = TerminalSession::start(Library::open(&directory).unwrap(), startup); + let root = session.scope.root(); + assert_eq!(root.find("address").unwrap().value(), POSTGRES_ENDPOINT); + session.click("connection-secret"); + session.input(Input::Paste("password".into())); + session.key(Key::Enter, Modifiers::default()); + assert_eq!(session.controls.focus(), root.find("query")); + assert_eq!( + root.find("connection-label").unwrap().text(), + format!("PostgreSQL · {POSTGRES_ENDPOINT}") + ); + fs::remove_dir_all(directory).unwrap(); +} + fn choose_client_card(session: &mut TerminalSession, arguments: &[&str]) { let root = session.scope.root(); let cards = root.find("client-cards").unwrap(); @@ -297,7 +320,7 @@ fn choose_client_card(session: &mut TerminalSession, arguments: &[&str]) { .descendants() .filter(|node| node.tag() == "button") .collect(); - assert_eq!(buttons.len(), 1); + assert_eq!(buttons.len(), 2); assert_eq!(session.controls.focus().as_ref(), buttons.first()); assert_eq!( root.find("connection-label").unwrap().text(), @@ -336,27 +359,27 @@ fn rejects_invalid_connection_settings(session: &mut TerminalSession) { session.click("address"); session.key(Key::Char('a'), CONTROL); session.input(Input::Paste(ENDPOINT.into())); - session.click("request-headers"); + session.click("connection-secret"); session.input(Input::Paste("not-a-header".into())); session.click("connect"); assert_eq!( root.find("connection-error").unwrap().text(), "Invalid request headers; use one ASCII name: value per line (no binary headers)" ); - session.click("request-headers"); + session.click("connection-secret"); session.key(Key::Char('a'), CONTROL); session.input(Input::Paste("authorization: Bearer example".into())); session.key(Key::Enter, Modifiers::default()); session.input(Input::Paste("x-tenant: example".into())); assert_eq!( - root.find("request-headers").unwrap().value(), + root.find("connection-secret").unwrap().value(), "authorization: Bearer example\nx-tenant: example" ); session.key(Key::Escape, Modifiers::default()); session.key(Key::Enter, Modifiers::default()); assert_eq!(session.controls.focus(), root.find("address")); assert_eq!( - root.find("request-headers").unwrap().value(), + root.find("connection-secret").unwrap().value(), "authorization: Bearer example\nx-tenant: example" ); session.size = (40, 20); diff --git a/src/tests/connections.rs b/src/tests/connections.rs index b9291a3..131f6c2 100644 --- a/src/tests/connections.rs +++ b/src/tests/connections.rs @@ -58,7 +58,7 @@ fn saved_connections_reopen_with_credentials_and_support_editing_and_removal() { session.key(Key::Enter, Modifiers::default()); assert_eq!( session.controls.focus(), - session.scope.root().find("request-headers") + session.scope.root().find("connection-secret") ); session.click("connect"); assert_eq!( @@ -70,7 +70,7 @@ fn saved_connections_reopen_with_credentials_and_support_editing_and_removal() { .text(), "Enter the request headers for this connection." ); - fill_field(&mut session, "request-headers", HEADERS); + fill_field(&mut session, "connection-secret", HEADERS); session.click("remember-credentials"); session.click("connect"); drop(session); @@ -105,7 +105,7 @@ fn create_session_only_connection(session: &mut TerminalSession) { let root = session.scope.root(); assert!(!root.find("save-connection").unwrap().checked()); assert_eq!(root.find("connection-name"), None); - fill_field(session, "request-headers", HEADERS); + fill_field(session, "connection-secret", HEADERS); session.click("save-connection"); assert_eq!(root.find("connection-name").unwrap().value(), "localhost"); assert!(!root.find("remember-credentials").unwrap().checked()); @@ -139,7 +139,7 @@ fn rename_session_only_connection(session: &mut TerminalSession) { session .scope .root() - .find("request-headers") + .find("connection-secret") .unwrap() .value(), "" @@ -169,7 +169,7 @@ fn edit_saved_connection( session .scope .root() - .find("request-headers") + .find("connection-secret") .unwrap() .value(), HEADERS @@ -208,7 +208,7 @@ fn edit_saved_connection( } fn reject_empty_remembered_headers(session: &mut TerminalSession) { - fill_field(session, "request-headers", ""); + fill_field(session, "connection-secret", ""); session.click("connect"); assert_eq!( session @@ -219,7 +219,7 @@ fn reject_empty_remembered_headers(session: &mut TerminalSession) { .text(), "Enter headers to remember, or uncheck Remember headers." ); - fill_field(session, "request-headers", HEADERS); + fill_field(session, "connection-secret", HEADERS); } fn remove_saved_connection( @@ -299,7 +299,7 @@ fn profiles_keep_secrets_out_of_sqlite_and_preserve_credentials_when_saving_fail .save_connection(None, "Original", &connection, Credentials::Keyring) .unwrap(); assert_eq!( - library.connection_headers(&profile).unwrap(), + library.connection_secret(&profile).unwrap(), Some(HEADERS.into()) ); let second = library @@ -323,7 +323,7 @@ fn profiles_keep_secrets_out_of_sqlite_and_preserve_credentials_when_saving_fail let saved = reopened.connections("original").unwrap(); assert_eq!(saved[0].address, ENDPOINT); assert_eq!( - reopened.connection_headers(&saved[0]).unwrap(), + reopened.connection_secret(&saved[0]).unwrap(), Some(HEADERS.into()) ); for file in fs::read_dir(&directory).unwrap() { @@ -335,7 +335,7 @@ fn profiles_keep_secrets_out_of_sqlite_and_preserve_credentials_when_saving_fail ); } reopened.remove_connection(&saved[0]).unwrap(); - assert_eq!(reopened.connection_headers(&saved[0]).unwrap(), None); + assert_eq!(reopened.connection_secret(&saved[0]).unwrap(), None); assert_eq!(reopened.connections("").unwrap().len(), 1); drop((library, reopened)); fs::remove_dir_all(directory).unwrap(); @@ -368,11 +368,11 @@ fn credential_failure_keeps_profile( .unwrap(); assert_eq!( error.to_string(), - "Cannot access saved credentials; unlock your OS credential store or use session-only headers" + "Cannot access saved credentials; unlock your OS credential store or use session-only credentials" ); assert_eq!(library.connections("original").unwrap()[0].name, "Original"); assert_eq!( - library.connection_headers(profile).unwrap(), + library.connection_secret(profile).unwrap(), Some(HEADERS.into()) ); } @@ -411,7 +411,7 @@ fn locked_database_restores_credentials( reader.execute_batch("ROLLBACK").unwrap(); assert_eq!(library.connections("original").unwrap()[0].name, "Original"); assert_eq!( - library.connection_headers(profile).unwrap(), + library.connection_secret(profile).unwrap(), Some(HEADERS.into()) ); } diff --git a/tests/postgres.rs b/tests/postgres.rs new file mode 100644 index 0000000..717c290 --- /dev/null +++ b/tests/postgres.rs @@ -0,0 +1,344 @@ +use arrow_array::{ + ArrayRef, BinaryArray, BooleanArray, Date32Array, Float32Array, Float64Array, Int16Array, + Int32Array, Int64Array, RecordBatch, StringArray, Time64MicrosecondArray, + TimestampMicrosecondArray, UInt32Array, cast::AsArray, types::Int32Type, +}; +use arrow_schema::{DataType, Field, Schema}; +use futures_util::TryStreamExt; +use sql_bomb::{Error, Postgres, QueryClient, Table}; +use std::{ + fs, net::TcpListener, os::unix::fs::PermissionsExt, path::PathBuf, process::Command, sync::Arc, +}; +use tempfile::TempDir; +use tokio_postgres::error::SqlState; + +const USER: &str = "sql_bomb"; +const PASSWORD: &str = "fixture password"; +const SETUP: &[&str] = &[ + "CREATE SCHEMA travel", + "CREATE TABLE travel.samples ( + id int4 NOT NULL, + boolean bool, + int16 int2, + int64 int8, + oid oid, + float32 float4, + float64 float8, + text varchar(10), + bytes bytea, + uuid uuid, + json json, + jsonb jsonb, + date date, + time time, + timestamp timestamp, + timestamptz timestamptz, + tags text[] +)", + r#"INSERT INTO travel.samples VALUES ( + 1, true, -2, 9000000000, 4, 1.5, -2.25, 'café', '\x00ff', + 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', '{"b": 1, "a": [true]}', '{"b": 1, "a": [true]}', + '1970-01-02', '12:34:56.789', '2000-01-01 00:00:01', '2000-01-01 00:00:00+01', '{x}' +)"#, + "CREATE VIEW travel.arrivals AS SELECT text AS city FROM travel.samples", +]; +const SAMPLES: &str = "SELECT id, boolean, int16, int64, oid, float32, float64, text, bytes, + uuid, json, jsonb, date, time, timestamp, timestamptz +FROM travel.samples"; +const NUMBERS: &str = "SELECT value +FROM (VALUES (-1234.5600::numeric), (0.00012), (10000), ('NaN'), (NULL)) AS numbers(value)"; +const UUID: &str = "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"; +const JSON: &str = r#"{"b": 1, "a": [true]}"#; +const JSONB: &str = r#"{"a": [true], "b": 1}"#; +const SERIES: &str = "SELECT generate_series(1, 3000) AS number"; +const SERIES_LENGTH: i32 = 3000; +const MISSING_TABLE_QUERY: &str = "SELECT * FROM missing_table"; +const PRIVATE_KEY_MODE: u32 = 0o600; +const RSA_KEY: &str = "rsa:2048"; +const SERVER_EXTENSIONS: &str = "basicConstraints = CA:FALSE +subjectAltName = DNS:localhost +"; +const ENCRYPTED_QUERY: &str = "SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()"; +const UNTRUSTED_CERTIFICATE: &str = "PostgreSQL request failed; check the query, credentials, and \ + server: error performing TLS handshake: invalid peer certificate: UnknownIssuer"; +const ARRAY_QUERY: &str = "SELECT tags FROM travel.samples"; + +struct Server { + directory: TempDir, + address: String, +} + +impl Server { + fn start() -> Self { + let directory = tempfile::tempdir().expect("a temporary directory is available"); + let password = directory.path().join("password"); + fs::write(&password, PASSWORD).expect("the temporary directory is writable"); + run(Command::new("initdb") + .arg("--pgdata") + .arg(data(&directory)) + .args(["--username", USER, "--auth", "scram-sha-256", "--no-locale"]) + .arg("--pwfile") + .arg(&password)); + issue_server_certificate(&directory); + let port = TcpListener::bind("127.0.0.1:0") + .and_then(|listener| listener.local_addr()) + .expect("a loopback port is available") + .port(); + let options = format!( + "-p {port} -c listen_addresses=127.0.0.1 -c unix_socket_directories='' -c ssl=on" + ); + run(pg_ctl(&directory) + .args(["--wait", "--options", &options, "--log"]) + .arg(directory.path().join("server.log")) + .arg("start")); + Self { + directory, + address: format!("postgres://{USER}@127.0.0.1:{port}/postgres"), + } + } +} + +impl Drop for Server { + fn drop(&mut self) { + run(pg_ctl(&self.directory).args(["--mode", "immediate", "stop"])); + } +} + +fn issue_server_certificate(directory: &TempDir) { + let path = |name: &str| directory.path().join(name); + let key = data(directory).join("server.key"); + fs::write(path("server.ext"), SERVER_EXTENSIONS).expect("the temporary directory is writable"); + run(Command::new("openssl") + .args(["req", "-x509", "-newkey", RSA_KEY, "-nodes", "-days", "1"]) + .args(["-subj", "/CN=sql-bomb test CA", "-keyout"]) + .arg(path("ca.key")) + .arg("-out") + .arg(path("ca.crt"))); + run(Command::new("openssl") + .args([ + "req", + "-new", + "-newkey", + RSA_KEY, + "-nodes", + "-subj", + "/CN=localhost", + ]) + .arg("-keyout") + .arg(&key) + .arg("-out") + .arg(path("server.csr"))); + run(Command::new("openssl") + .args(["x509", "-req", "-days", "1", "-set_serial", "1", "-in"]) + .arg(path("server.csr")) + .arg("-CA") + .arg(path("ca.crt")) + .arg("-CAkey") + .arg(path("ca.key")) + .arg("-extfile") + .arg(path("server.ext")) + .arg("-out") + .arg(data(directory).join("server.crt"))); + fs::set_permissions(&key, fs::Permissions::from_mode(PRIVATE_KEY_MODE)) + .expect("the server key is writable"); +} + +fn data(directory: &TempDir) -> PathBuf { + directory.path().join("data") +} + +fn pg_ctl(directory: &TempDir) -> Command { + let mut command = Command::new("pg_ctl"); + // PostgreSQL on macOS refuses to start without a valid locale in the environment. + command + .env("LC_ALL", "C") + .arg("--pgdata") + .arg(data(directory)); + command +} + +fn run(command: &mut Command) { + let output = command + .output() + .expect("PostgreSQL server binaries (initdb, pg_ctl) and openssl must be on PATH"); + assert!( + output.status.success(), + "{command:?} failed: {}", + String::from_utf8_lossy(&output.stderr) + ); +} + +async fn rows(client: &dyn QueryClient, sql: &str) -> (Schema, Vec) { + let query = client + .query(sql.into()) + .await + .expect("the fixture SQL runs"); + let batches = query.batches.try_collect().await; + (query.schema, batches.expect("the fixture rows decode")) +} + +#[tokio::test] +async fn postgres_streams_typed_rows_discovers_tables_and_reports_errors() { + let server = Server::start(); + let client = Postgres::new(&server.address, PASSWORD).unwrap(); + let client: &dyn QueryClient = &client; + for statement in SETUP { + assert_eq!(rows(client, statement).await.1, vec![]); + } + let samples = assert_typed_rows(client).await; + assert_numeric_text(client).await; + assert_rows_span_batches(client).await; + assert_table_discovery(client, &samples).await; + assert_errors(client, &server.address).await; + assert_ssl_modes(&server.address).await; +} + +async fn assert_typed_rows(client: &dyn QueryClient) -> Schema { + let columns: Vec<(&str, ArrayRef)> = vec![ + ("id", Arc::new(Int32Array::from(vec![1]))), + ("boolean", Arc::new(BooleanArray::from(vec![true]))), + ("int16", Arc::new(Int16Array::from(vec![-2]))), + ("int64", Arc::new(Int64Array::from(vec![9_000_000_000]))), + ("oid", Arc::new(UInt32Array::from(vec![4]))), + ("float32", Arc::new(Float32Array::from(vec![1.5]))), + ("float64", Arc::new(Float64Array::from(vec![-2.25]))), + ("text", Arc::new(StringArray::from(vec!["café"]))), + ( + "bytes", + Arc::new(BinaryArray::from(vec![[0, 255].as_slice()])), + ), + ("uuid", Arc::new(StringArray::from(vec![UUID]))), + ("json", Arc::new(StringArray::from(vec![JSON]))), + ("jsonb", Arc::new(StringArray::from(vec![JSONB]))), + ("date", Arc::new(Date32Array::from(vec![1]))), + ( + "time", + Arc::new(Time64MicrosecondArray::from(vec![45_296_789_000])), + ), + ( + "timestamp", + Arc::new(TimestampMicrosecondArray::from(vec![946_684_801_000_000])), + ), + ( + "timestamptz", + Arc::new( + TimestampMicrosecondArray::from(vec![946_681_200_000_000]).with_timezone("+00:00"), + ), + ), + ]; + let expected = columns + .into_iter() + .map(|(name, values)| (name, values, true)); + let expected = RecordBatch::try_from_iter_with_nullable(expected) + .expect("fixture columns have one row each"); + let (schema, batches) = rows(client, SAMPLES).await; + assert_eq!(schema, *expected.schema()); + assert_eq!(batches, vec![expected]); + schema +} + +async fn assert_numeric_text(client: &dyn QueryClient) { + let numbers = StringArray::from(vec![ + Some("-1234.5600"), + Some("0.00012"), + Some("10000"), + Some("NaN"), + None, + ]); + let numbers = RecordBatch::try_from_iter([("value", Arc::new(numbers) as ArrayRef)]) + .expect("one column forms a batch"); + assert_eq!(rows(client, NUMBERS).await.1, vec![numbers]); +} + +async fn assert_rows_span_batches(client: &dyn QueryClient) { + let streamed: Vec = rows(client, SERIES) + .await + .1 + .iter() + .flat_map(|batch| { + batch + .column(0) + .as_primitive::() + .values() + .to_vec() + }) + .collect(); + assert_eq!(streamed, (1..=SERIES_LENGTH).collect::>()); +} + +async fn assert_table_discovery(client: &dyn QueryClient, samples: &Schema) { + let tables = client + .tables() + .expect("PostgreSQL supports table discovery") + .await + .expect("table discovery returns the fixture tables"); + let mut sample_fields: Vec<_> = samples.fields().to_vec(); + sample_fields[0] = Arc::new(Field::new("id", DataType::Int32, false)); + let expected = [ + ( + "arrivals", + "VIEW", + vec![Arc::new(Field::new("city", DataType::Utf8, true))], + ), + ("samples", "TABLE", sample_fields), + ] + .into_iter() + .map(|(name, table_type, fields)| Table { + catalog: None, + database_schema: Some("travel".into()), + name: name.into(), + table_type: table_type.into(), + schema: Schema::new(fields), + }) + .collect::>(); + assert_eq!(tables, expected); +} + +async fn assert_errors(client: &dyn QueryClient, address: &str) { + let unsupported = client.query(ARRAY_QUERY.into()).await.err(); + let unsupported = unsupported.expect("text[] columns are unsupported"); + assert_eq!( + unsupported.to_string(), + "Column tags has unsupported PostgreSQL type _text; cast it, as in tags::text" + ); + let missing = client.query(MISSING_TABLE_QUERY.into()).await.err(); + assert_eq!( + missing.expect("the table does not exist").to_string(), + "PostgreSQL request failed; check the query, credentials, and server: \ + db error: ERROR: relation \"missing_table\" does not exist" + ); + let wrong_password = Postgres::new(address, "wrong").expect("the fixture address is valid"); + let Some(Error::Postgres(failure)) = wrong_password.query(SERIES.into()).await.err() else { + panic!("Expected the server's authentication error"); + }; + assert_eq!(failure.code(), Some(&SqlState::INVALID_PASSWORD)); + let url_password = address.replace(USER, &format!("{USER}:{PASSWORD}")); + assert!(matches!( + Postgres::new(&url_password, ""), + Err(Error::PostgresPassword) + )); +} + +async fn assert_ssl_modes(address: &str) { + for (parameters, encrypted) in [ + ("", true), + ("?sslmode=require", true), + ("?sslmode=disable", false), + ] { + let client = Postgres::new(&format!("{address}{parameters}"), PASSWORD) + .expect("the fixture address is valid"); + let (_, batches) = rows(&client, ENCRYPTED_QUERY).await; + assert_eq!(batches[0].column(0).as_boolean().value(0), encrypted); + } + for parameters in ["?sslmode=verify-ca", "?sslmode=verify-full"] { + let client = Postgres::new(&format!("{address}{parameters}"), PASSWORD) + .expect("the fixture address is valid"); + let failure = client.query(ENCRYPTED_QUERY.into()).await.err(); + assert_eq!( + failure + .expect("the self-signed certificate is untrusted") + .to_string(), + UNTRUSTED_CERTIFICATE + ); + } +} diff --git a/ui/app.html b/ui/app.html index 29a2b93..173ae90 100644 --- a/ui/app.html +++ b/ui/app.html @@ -19,8 +19,9 @@

›_ sql-bomb

- - + + diff --git a/ui/connections/credentials.html b/ui/connections/credentials.html index 19cc5f9..05965a1 100644 --- a/ui/connections/credentials.html +++ b/ui/connections/credentials.html @@ -3,8 +3,9 @@

{{ state.terminal.draft.name.get() }}

{{ state.terminal.address.get() }}

-

Enter the headers for this connection.

- +

Enter the {{ state.client.profile().secret.noun }} for this connection.

+

{{ state.terminal.connection_error.get() }}