Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #38749, postgresql GRANT user's permission after createDatabase, … #38750

Merged
merged 2 commits into from
Aug 16, 2024

Conversation

whlsxl
Copy link
Contributor

@whlsxl whlsxl commented Jun 12, 2023

@szaimen szaimen added this to the Nextcloud 28 milestone Jun 12, 2023
@szaimen szaimen added the 3. to review Waiting for reviews label Jun 12, 2023
@szaimen szaimen requested review from icewind1991, skjnldsv, a team, ArtificialOwl, Fenn-CS and come-nc and removed request for a team June 12, 2023 11:29
@come-nc
Copy link
Contributor

come-nc commented Jun 12, 2023

I’m not familiar enough with databases to review this

@come-nc come-nc removed their request for review June 12, 2023 12:39
Copy link
Contributor

@kesselb kesselb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your pull 👍

Could you please provide us some more information about your change?

I'm not familiar with PostgreSQL. Grant create does not work, before creating the database because the schema public does not exist?

lib/private/Setup/PostgreSQL.php Outdated Show resolved Hide resolved
lib/private/Setup/PostgreSQL.php Outdated Show resolved Hide resolved
@kesselb
Copy link
Contributor

kesselb commented Jun 12, 2023

I think we should remove the code to create database tables and users.

@whlsxl
Copy link
Contributor Author

whlsxl commented Jun 12, 2023

Thank you very much for your pull 👍

Could you please provide us some more information about your change?

I'm not familiar with PostgreSQL. Grant create does not work, before creating the database because the schema public does not exist?

Sorry, not familiar with PHP.

This is the docker-compose.yml to reproduce this issue.

version: "3.5"

services:
  postgres:
    container_name: postgres
    image: postgres:15.3-alpine
    restart: unless-stopped
    environment:
      - "POSTGRES_USER=postgres"
      - "POSTGRES_PASSWORD=U8PNOtHPefQ"
      - "POSTGRES_HOST_AUTH_METHOD=trust"
  nextcloud:
    container_name: nextcloud
    image: nextcloud:26.0.2-fpm-alpine
    restart: unless-stopped

After container up, enter the container docker exec -it nextcloud sh & install nextcloud with
php occ maintenance:install --database pgsql --database-host postgres:5432 --database-name nextcloud --database-user postgres --database-pass U8PNOtHPefQ --admin-user admin --admin-pass U8PNOtHPefQ --data-dir /var/www/html/data/

got error:
PostgreSQL username and/or password not valid

@whlsxl
Copy link
Contributor Author

whlsxl commented Jun 12, 2023

I traced this bug.
The pull request #34645 is to fix the #34617, but I tried many ways, can't reproduce the problem.

I think we should remove the code to create database tables and users.

In my opinion, create users & database & tables are very good functions, because most users are newbies, they don't have the ability to manage the database.

But this function should be refactor, for example

private function createDatabase(Connection $connection) {
if (!$this->databaseExists($connection)) {
//The database does not exists... let's create it
$query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER \"" . addslashes($this->dbUser) . '"');
try {
$query->execute();
} catch (DatabaseException $e) {
$this->logger->error('Error while trying to create database', [
'exception' => $e,
]);
}
} else {
$query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC");
try {
$query->execute();
} catch (DatabaseException $e) {
$this->logger->error('Error while trying to restrict database permissions', [
'exception' => $e,
]);
}
}
}
if the database exists, the superuser should resign the owner to nextcloud database user and copy all the permission nextcloud database user.

I can do this job, but still try to figure out how to write the TEST & execute it.

@kesselb
Copy link
Contributor

kesselb commented Jun 12, 2023

In my opinion, create users & database & tables are very good functions, because most users are newbies, they don't have the ability to manage the database.

I would prefer that newbies learn how to create a database and database user ;)

We have automated tests for PostgreSQL.

https://github.com/nextcloud/.github/blob/dc0440094e086858cb960e0054a29ca565c5fbbd/workflow-templates/phpunit-pgsql.yml#L49-L52

The notable difference:

  • PostgreSQL 14 vs 15
  • POSTGRES_DB (we ask the container to create a database named nextcloud)

That means in our automated tests, we do not run the code to create a database table. This code path is apparently broken.

Previous: Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] FATAL:  database "nextcloud" does not exist
Trace: #0 /var/www/html/lib/private/Setup/PostgreSQL.php(114): OC\DB\Connection->connect()
#1 /var/www/html/lib/private/Setup.php(353): OC\Setup\PostgreSQL->setupDatabase('admin')

The stack trace is a bit misleading.

$connectionMainDatabase = $this->connect();

Here is the problem. connect() uses this->dbName by default. That means we tell doctrine/pdo to connect to a non-existing database.

$connectionMainDatabase = $this->connect([
    'dbname' => 'postgres'
]);

We need to overwrite the dbname (like for $connection a few lines above) and hope that the postgres table will be there.

@whlsxl
Copy link
Contributor Author

whlsxl commented Nov 13, 2023

This bug still exists, wish someone could review it.

@whlsxl
Copy link
Contributor Author

whlsxl commented Mar 22, 2024

Can you please adjust your commits to comply with conventional commits? Thank you :)

Are you referring to the commit message? can you tell me more specifically?

@susnux
Copy link
Contributor

susnux commented Mar 22, 2024

Are you referring to the commit message? can you tell me more specifically?

Yes the commit message, we try to follow the conventional commits spec: https://www.conventionalcommits.org/

Some examples:

  • fix: Something that was wrong
  • fix(DB): Something that was wrong in database
  • feat: Something new

@Altahrim Altahrim mentioned this pull request Mar 25, 2024
@skjnldsv skjnldsv mentioned this pull request Mar 28, 2024
81 tasks
This was referenced Apr 4, 2024
@blizzz blizzz modified the milestones: Nextcloud 29, Nextcloud 30 Apr 8, 2024
@kesselb kesselb requested review from kesselb and removed request for kesselb June 20, 2024 13:29
This was referenced Jul 30, 2024
This was referenced Aug 5, 2024
@skjnldsv skjnldsv mentioned this pull request Aug 13, 2024
@skjnldsv skjnldsv closed this Aug 14, 2024
@skjnldsv skjnldsv removed this from the Nextcloud 30 milestone Aug 14, 2024
@skjnldsv skjnldsv reopened this Aug 16, 2024
@skjnldsv skjnldsv added 4. to release Ready to be released and/or waiting for tests to finish and removed 3. to review Waiting for reviews labels Aug 16, 2024
@skjnldsv skjnldsv self-assigned this Aug 16, 2024
@skjnldsv
Copy link
Member

Rebased and reviewed!
this issue was a bit old and staled, but this should be simple enough to get merged 🤞

Thanks for the hard work @whlsxl 💪

@skjnldsv skjnldsv merged commit 13a72d0 into nextcloud:master Aug 16, 2024
155 of 161 checks passed
Copy link

welcome bot commented Aug 16, 2024

Thanks for your first pull request and welcome to the community! Feel free to keep them coming! If you are looking for issues to tackle then have a look at this selection: https://github.com/nextcloud/server/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22

@skjnldsv skjnldsv added this to the Nextcloud 31 milestone Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4. to release Ready to be released and/or waiting for tests to finish bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: fresh install with postgresql, occ maintenance:install error
8 participants