vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
149 stars 53 forks source link

3.0 RC: PostgreSQL connection fails when supplying password and always fails in release builds #22

Closed 4np closed 6 years ago

4np commented 6 years ago

Connecting to PostgreSQL always fails in release builds, and it always fails in debug builds when you supply a password.

Steps to reproduce

1. Create user + database:

createuser bertandernie
createdb bigbird
psql
alter user bertandernie with encrypted password 'sesamestreet';
grant all privileges on database bigbird to bertandernie;
\q

2. Update configuration Update configure.swift to have the following configuration:

    // Configure PostgreSQL Database
    let postgresConfig: PostgreSQLDatabaseConfig
    if env.name == "production" {
        // here you would probably have production credentials
        print("Using PostgreSQLDatabaseConfig as database user with supplying password")
        postgresConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "bertandernie", database: "bigbird", password: "sesamestreet")
    } else {
        // here you would have development credentials
        print("Using PostgreSQLDatabaseConfig as database user without supplying password")
        postgresConfig = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "bertandernie", database: "bigbird")
    }
    let postgresDatabase = PostgreSQLDatabase(config: postgresConfig)
    databases.add(database: postgresDatabase, as: .psql)
    services.register(databases)

3. Build and run

$ swift build
Compile Swift Module 'App' (15 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/Run
 $ .build/x86_64-apple-macosx10.10/debug/Run
Running development build on macOS (non-release, debug mode)
Using PostgreSQLDatabaseConfig as database user without supplying password
Migrating psql DB
Migrations complete
Server starting on http://127.0.0.1:8080
^C $ .build/x86_64-apple-macosx10.10/debug/Run --env prod
Running production build on macOS (release, debug mode)
Using PostgreSQLDatabaseConfig as database user with supplying password
Migrating psql DB
Fatal error: Error raised at top level: ⚠️ PostgreSQL Error: No password is required
- id: PostgreSQLError.trust
: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-902.0.41/src/swift/stdlib/public/core/ErrorType.swift, line 191
Illegal instruction: 4
$ 

Observe that running the development build will connect properly when you don't supply a password, but it fails with PostgreSQL Error: No password is required when you do supply a password.

4. Build and run for release

$ swift build --configuration release
...
$ .build/x86_64-apple-macosx10.10/release/Run
Running development build on macOS (non-release, release mode)
Using PostgreSQLDatabaseConfig as database user without supplying password
Migrating psql DB
[PostgreSQL] dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot initialize PostgreSQLAuthenticationType from invalid Int32 value 8", underlyingError: nil))
^C $ .build/x86_64-apple-macosx10.10/release/Run --env prod
Running production build on macOS (release, release mode)
Using PostgreSQLDatabaseConfig as database user with supplying password
Migrating psql DB
[PostgreSQL] dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot initialize PostgreSQLAuthenticationType from invalid Int32 value 8", underlyingError: nil))
^C $ 

Observe that the release build will always fails with Cannot initialize PostgreSQLAuthenticationType from invalid Int32 value 8, regardless of whether or not you supply a password.

4np commented 6 years ago

On Ubuntu Linux (16.04 LTS) I see different behaviour:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial
$ uname -a
Linux ripcurl 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ swift --version
Swift version 4.1-dev (LLVM 1c8b50929b, Clang 8be462d5b8, Swift f2049df523)
Target: x86_64-unknown-linux-gnu
$ 

1. Build and run

$ swift build
...
$ .build/x86_64-unknown-linux/debug/Run
Running development build on linux (non-release, debug mode)
Using PostgreSQLDatabaseConfig without password
Migrating psql DB
Fatal error: Error raised at top level: ⚠️ PostgreSQL Error: Password is required
- id: PostgreSQLError.password
: file /home/buildnode/jenkins/workspace/oss-swift-4.1-package-linux-ubuntu-16_04/swift/stdlib/public/core/ErrorType.swift, line 191
Current stack trace:
0    libswiftCore.so                    0x00007fb657da7080 _swift_stdlib_reportFatalErrorInFile + 221
1    libswiftCore.so                    0x00007fb657b1634c <unavailable> + 1368908
2    libswiftCore.so                    0x00007fb657d4fd32 <unavailable> + 3702066
3    libswiftCore.so                    0x00007fb657b15a46 <unavailable> + 1366598
4    libswiftCore.so                    0x00007fb657d4fc37 <unavailable> + 3701815
5    libswiftCore.so                    0x00007fb657b15a46 <unavailable> + 1366598
6    libswiftCore.so                    0x00007fb657c82c89 <unavailable> + 2862217
7    libswiftCore.so                    0x00007fb657b53850 swift_errorInMain + 318
8    Run                                0x00005589f548c531 <unavailable> + 4400433
9    libc.so.6                          0x00007fb65558e740 __libc_start_main + 240
10   Run                                0x00005589f50cc239 <unavailable> + 467513
Illegal instruction (core dumped)
$ .build/x86_64-unknown-linux/debug/Run --env prod
Running production build on linux (release, debug mode)
Using PostgreSQLDatabaseConfig with password
Migrating psql DB
Migrations complete
[ WARNING ] Debug build mode detected while configured for release environment: production. (Application.swift:60)
[ INFO ] Compile your application with `-c release` to enable code optimizations. (Application.swift:61)
Server starting on http://127.0.0.1:8080
^C $

Observe that when building on Ubuntu, the connection only succeeds if you do supply a password...

2. Build for release and run

$ swift build --configuration release
...
$ .build/x86_64-unknown-linux/release/Run
Running development build on linux (non-release, release mode)
Using PostgreSQLDatabaseConfig without password
Migrating psql DB
[PostgreSQL] dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot initialize PostgreSQLAuthenticationType from invalid Int32 value 12", underlyingError: nil))
^C $ .build/x86_64-unknown-linux/release/Run --env prod
Running production build on linux (release, release mode)
Using PostgreSQLDatabaseConfig with password
Migrating psql DB
[PostgreSQL] dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "Cannot initialize PostgreSQLAuthenticationType from invalid Int32 value 12", underlyingError: nil))
^C $ 

Observe that on Ubuntu Linux the release build will also always fail, like on macOS

tanner0101 commented 6 years ago

I was also seeing this bug on master branch, haven't seen it since refactor on nio branch. 👍 thanks!

nhatlee commented 5 years ago

This still happen with vapor 3 Thread 1: Fatal error: Error raised at top level: ⚠️ PostgreSQL Error: No password is required

tanner0101 commented 5 years ago

@nhatlee try setting the password to nil in your config.