Skip to content

Commit b45aa1f

Browse files
committed
add option to disable db user creation trough environment variable
Signed-off-by: Robin Appelman <[email protected]>
1 parent 41b2e17 commit b45aa1f

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

lib/private/Setup/AbstractDatabase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ abstract class AbstractDatabase {
5757
protected $logger;
5858
/** @var ISecureRandom */
5959
protected $random;
60+
/** @var bool */
61+
protected $tryCreateDbUser;
6062

6163
public function __construct(IL10N $trans, SystemConfig $config, LoggerInterface $logger, ISecureRandom $random) {
6264
$this->trans = $trans;
@@ -88,6 +90,8 @@ public function initialize($config) {
8890
$dbPort = !empty($config['dbport']) ? $config['dbport'] : '';
8991
$dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
9092

93+
$this->tryCreateDbUser = getenv("SETUP_CREATE_DB_USER") !== "false";
94+
9195
$this->config->setValues([
9296
'dbname' => $dbName,
9397
'dbhost' => $dbHost,

lib/private/Setup/MySQL.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ public function setupDatabase($username) {
4949
$connection = $this->connect(['dbname' => null]);
5050
}
5151

52-
$this->createSpecificUser($username, new ConnectionAdapter($connection));
52+
if ($this->tryCreateDbUser) {
53+
$this->createSpecificUser($username, new ConnectionAdapter($connection));
54+
}
55+
56+
$this->config->setValues([
57+
'dbuser' => $this->dbUser,
58+
'dbpassword' => $this->dbPassword,
59+
]);
5360

5461
//create the database
5562
$this->createDatabase($connection);
@@ -147,8 +154,7 @@ private function createSpecificUser($username, $connection): void {
147154
. $this->random->generate(2, ISecureRandom::CHAR_UPPER)
148155
. $this->random->generate(2, ISecureRandom::CHAR_LOWER)
149156
. $this->random->generate(2, ISecureRandom::CHAR_DIGITS)
150-
. $this->random->generate(2, $saveSymbols)
151-
;
157+
. $this->random->generate(2, $saveSymbols);
152158
$this->dbPassword = str_shuffle($password);
153159

154160
try {
@@ -196,10 +202,5 @@ private function createSpecificUser($username, $connection): void {
196202
$this->dbUser = $rootUser;
197203
$this->dbPassword = $rootPassword;
198204
}
199-
200-
$this->config->setValues([
201-
'dbuser' => $this->dbUser,
202-
'dbpassword' => $this->dbPassword,
203-
]);
204205
}
205206
}

lib/private/Setup/PostgreSQL.php

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,44 @@ public function setupDatabase($username) {
4545
$connection = $this->connect([
4646
'dbname' => 'postgres'
4747
]);
48-
//check for roles creation rights in postgresql
49-
$builder = $connection->getQueryBuilder();
50-
$builder->automaticTablePrefix(false);
51-
$query = $builder
52-
->select('rolname')
53-
->from('pg_roles')
54-
->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE')))
55-
->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
56-
57-
try {
58-
$result = $query->execute();
59-
$canCreateRoles = $result->rowCount() > 0;
60-
} catch (DatabaseException $e) {
61-
$canCreateRoles = false;
62-
}
63-
64-
if ($canCreateRoles) {
65-
$connectionMainDatabase = $this->connect();
66-
//use the admin login data for the new database user
67-
68-
//add prefix to the postgresql user name to prevent collisions
69-
$this->dbUser = 'oc_' . strtolower($username);
70-
//create a new password so we don't need to store the admin config in the config file
71-
$this->dbPassword = \OC::$server->getSecureRandom()->generate(30, ISecureRandom::CHAR_ALPHANUMERIC);
72-
73-
$this->createDBUser($connection);
74-
75-
// Go to the main database and grant create on the public schema
76-
// The code below is implemented to make installing possible with PostgreSQL version 15:
77-
// https://www.postgresql.org/docs/release/15.0/
78-
// From the release notes: For new databases having no need to defend against insider threats, granting CREATE permission will yield the behavior of prior releases
79-
// Therefore we assume that the database is only used by one user/service which is Nextcloud
80-
// Additional services should get installed in a separate database in order to stay secure
81-
// Also see https://www.postgresql.org/docs/15/ddl-schemas.html#DDL-SCHEMAS-PATTERNS
82-
$connectionMainDatabase->executeQuery('GRANT CREATE ON SCHEMA public TO ' . addslashes($this->dbUser));
83-
$connectionMainDatabase->close();
48+
if ($this->tryCreateDbUser) {
49+
//check for roles creation rights in postgresql
50+
$builder = $connection->getQueryBuilder();
51+
$builder->automaticTablePrefix(false);
52+
$query = $builder
53+
->select('rolname')
54+
->from('pg_roles')
55+
->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE')))
56+
->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser)));
57+
58+
try {
59+
$result = $query->execute();
60+
$canCreateRoles = $result->rowCount() > 0;
61+
} catch (DatabaseException $e) {
62+
$canCreateRoles = false;
63+
}
64+
65+
if ($canCreateRoles) {
66+
$connectionMainDatabase = $this->connect();
67+
//use the admin login data for the new database user
68+
69+
//add prefix to the postgresql user name to prevent collisions
70+
$this->dbUser = 'oc_' . strtolower($username);
71+
//create a new password so we don't need to store the admin config in the config file
72+
$this->dbPassword = \OC::$server->getSecureRandom()->generate(30, ISecureRandom::CHAR_ALPHANUMERIC);
73+
74+
$this->createDBUser($connection);
75+
76+
// Go to the main database and grant create on the public schema
77+
// The code below is implemented to make installing possible with PostgreSQL version 15:
78+
// https://www.postgresql.org/docs/release/15.0/
79+
// From the release notes: For new databases having no need to defend against insider threats, granting CREATE permission will yield the behavior of prior releases
80+
// Therefore we assume that the database is only used by one user/service which is Nextcloud
81+
// Additional services should get installed in a separate database in order to stay secure
82+
// Also see https://www.postgresql.org/docs/15/ddl-schemas.html#DDL-SCHEMAS-PATTERNS
83+
$connectionMainDatabase->executeQuery('GRANT CREATE ON SCHEMA public TO ' . addslashes($this->dbUser));
84+
$connectionMainDatabase->close();
85+
}
8486
}
8587

8688
$this->config->setValues([

0 commit comments

Comments
 (0)