-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add database grants and connectivity check #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a30dc66
docs: improve database grants documentation with SQL example and impo…
konradmichalik acbf11d
feat: add database grants and connectivity check for Root and Simple …
konradmichalik 35dc66c
docs: add database grants check documentation
konradmichalik e0334a7
Merge branch 'feature/requirements-typo3-alignment' into feature/requ…
konradmichalik cb289f3
Merge remote-tracking branch 'origin/feature/requirements-typo3-align…
konradmichalik f613d31
Merge remote-tracking branch 'origin/main' into feature/requirements-…
konradmichalik 06e6196
fix: resolve host/port from .env for remote database connections
konradmichalik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Deployer; | ||
|
|
||
| use Deployer\Exception\RunException; | ||
|
|
||
| task('requirements:check:database_grants', function (): void { | ||
| if (!get('requirements_check_database_grants_enabled')) { | ||
| return; | ||
| } | ||
|
|
||
| $managerType = has('database_manager_type') ? get('database_manager_type') : null; | ||
|
|
||
| if ($managerType === null) { | ||
| addRequirementRow('Database grants', REQUIREMENT_SKIP, 'No database_manager_type configured'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($managerType === 'mittwald_api') { | ||
| addRequirementRow('Database grants', REQUIREMENT_SKIP, 'Managed via Mittwald API'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($managerType === 'simple') { | ||
| checkSimplePoolConnectivity(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Root / default mode | ||
| checkRootGrants(); | ||
| })->hidden(); | ||
|
|
||
| function checkRootGrants(): void | ||
| { | ||
| $credentials = resolveDatabaseCredentials(); | ||
|
|
||
| if ($credentials === null) { | ||
| addRequirementRow( | ||
| 'Database grants', | ||
| REQUIREMENT_SKIP, | ||
| 'No credentials available (set database_user/database_password or DEPLOYER_CONFIG_DATABASE_PASSWORD)' | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $mysqlBin = has('mysql') ? get('mysql') : 'mysql'; | ||
| $connectInfo = sprintf('%s@%s:%d', $credentials['user'], $credentials['host'], $credentials['port']); | ||
|
|
||
| try { | ||
| $output = run(sprintf( | ||
| '%s --connect-timeout=5 -u %s -p%s -h %s -P %d -N -e %s 2>&1', | ||
| escapeshellarg($mysqlBin), | ||
| escapeshellarg($credentials['user']), | ||
| "'%secret%'", | ||
| escapeshellarg($credentials['host']), | ||
| $credentials['port'], | ||
| escapeshellarg('SHOW GRANTS FOR CURRENT_USER()') | ||
| ), secret: $credentials['password']); | ||
| } catch (RunException) { | ||
| addRequirementRow('Database: connectivity', REQUIREMENT_FAIL, "Cannot connect as $connectInfo"); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| addRequirementRow('Database: connectivity', REQUIREMENT_OK, "Connected as $connectInfo"); | ||
|
|
||
| $result = parseGlobalGrants($output); | ||
|
|
||
| if ($result['ok']) { | ||
| addRequirementRow('Database: global grants', REQUIREMENT_OK, 'All required grants on *.*'); | ||
| } else { | ||
| addRequirementRow( | ||
| 'Database: global grants', | ||
| REQUIREMENT_FAIL, | ||
| 'Missing on *.*: ' . implode(', ', $result['missing']) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function checkSimplePoolConnectivity(): void | ||
| { | ||
| if (!has('database_pool')) { | ||
| addRequirementRow('Database pool', REQUIREMENT_FAIL, 'database_pool not configured'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $pool = get('database_pool'); | ||
|
|
||
| if (empty($pool)) { | ||
| addRequirementRow('Database pool', REQUIREMENT_FAIL, 'database_pool is empty'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| addRequirementRow('Database pool', REQUIREMENT_OK, sprintf('%d database(s) configured', count($pool))); | ||
|
|
||
| $mysqlBin = has('mysql') ? get('mysql') : 'mysql'; | ||
|
|
||
| foreach ($pool as $alias => $config) { | ||
| $requiredKeys = ['database_user', 'database_password', 'database_name']; | ||
| $missingKeys = array_diff($requiredKeys, array_keys(array_filter($config, 'is_string'))); | ||
|
|
||
| if (!empty($missingKeys)) { | ||
| addRequirementRow( | ||
| "Database pool: $alias", | ||
| REQUIREMENT_FAIL, | ||
| 'Missing config: ' . implode(', ', $missingKeys) | ||
| ); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| $host = $config['database_host'] ?? '127.0.0.1'; | ||
| $port = (int) ($config['database_port'] ?? 3306); | ||
| $password = $config['database_password']; | ||
|
|
||
| // Resolve env var references (DEPLOYER_CONFIG_* pattern) | ||
| if (str_starts_with($password, 'DEPLOYER_CONFIG_')) { | ||
| $envValue = getenv($password); | ||
| $password = is_string($envValue) && $envValue !== '' ? $envValue : ''; | ||
| } | ||
|
|
||
| if ($password === '') { | ||
| addRequirementRow("Database pool: $alias", REQUIREMENT_SKIP, 'Password not resolvable'); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| run(sprintf( | ||
| '%s --connect-timeout=5 -u %s -p%s -h %s -P %d -e %s %s 2>&1', | ||
| escapeshellarg($mysqlBin), | ||
| escapeshellarg($config['database_user']), | ||
| "'%secret%'", | ||
| escapeshellarg($host), | ||
| $port, | ||
| escapeshellarg('SELECT 1'), | ||
| escapeshellarg($config['database_name']) | ||
| ), secret: $password); | ||
|
|
||
| addRequirementRow("Database pool: $alias", REQUIREMENT_OK, sprintf( | ||
| '%s@%s:%d/%s', | ||
| $config['database_user'], | ||
| $host, | ||
| $port, | ||
| $config['database_name'] | ||
| )); | ||
| } catch (RunException) { | ||
| addRequirementRow("Database pool: $alias", REQUIREMENT_FAIL, sprintf( | ||
| 'Cannot connect as %s@%s:%d/%s', | ||
| $config['database_user'], | ||
| $host, | ||
| $port, | ||
| $config['database_name'] | ||
| )); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$hoststays127.0.0.1when both credentials are pre-resolved before reaching the.envblock.The guard at line 423 (
$user === '' || $password === '') prevents.envfrom being read when both credentials are already satisfied — for example,database_userset in the Deployer config anddatabase_passwordsupplied viaDEPLOYER_CONFIG_DATABASE_PASSWORD. In that casegetSharedEnvVars()is never called, so the TYPO3/Symfony host resolution on lines 438–464 is never reached and$hostremains127.0.0.1. For projects where the DB host is only in the shared.env(remote RDS/MariaDB endpoint), the grants check will silently attempt to connect to the wrong host.The TYPO3/Symfony host-from-env logic added inside the block already handles the case where at least one credential is missing (addressing the earlier review). The remaining gap is when credentials are fully pre-resolved but
database_hostis not explicitly configured.🛠️ Proposed fix
Decouple host/port resolution from the credential guard by checking whether defaults are still in place independently:
🤖 Prompt for AI Agents