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

Try to install PostgreSQL extensions automatically (backport #1483) #1507

Merged
merged 5 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Add standard info elem fields for NVTs in get_info [#1426](https://github.com/greenbone/gvmd/pull/1426)
- Add --ldap-debug option [#1439](https://github.com/greenbone/gvmd/pull/1439)
- Add check if PostgreSQL extensions are installed [#1444](https://github.com/greenbone/gvmd/pull/1444)
- Try to install PostgreSQL extensions automatically [#1444](https://github.com/greenbone/gvmd/pull/1444) [#1483](https://github.com/greenbone/gvmd/pull/1483)
- Add auto retry on scanner connection lost during a running task [#1452](https://github.com/greenbone/gvmd/pull/1452)
- Add --feed-lock-timeout option [#1472](https://github.com/greenbone/gvmd/pull/1472)

Expand Down
15 changes: 3 additions & 12 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,13 @@ Certificates`.
grant dba to mattm; # mattm is the user created in step 3
```

5. Create DB extensions (also necessary when the database got dropped).

```sh
sudo -u postgres bash # if you logged out after step 4
psql gvmd
create extension "uuid-ossp";
create extension "pgcrypto";
```

6. Make Postgres aware of the gvm libraries if not installed
5. Make Postgres aware of the gvm libraries if not installed
in a ld-aware directory. For example create file `/etc/ld.so.conf.d/gvm.conf`
with appropriate path and then run `ldconfig`.

7. Run Manager as usual.
6. Run Manager as usual.

8. To run SQL on the database.
7. To run SQL on the database.

```sh
psql gvmd
Expand Down
60 changes: 37 additions & 23 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
*/
#define G_LOG_DOMAIN "md manage"

/**
* @brief Database superuser role
*/
#define DB_SUPERUSER_ROLE "dba"


/* Headers */
int
check_db_extensions ();


/* Session. */

Expand Down Expand Up @@ -186,17 +196,12 @@ manage_create_sql_functions ()
if (created)
return 0;

if (sql_int ("SELECT count (*) FROM pg_available_extensions"
" WHERE name = 'uuid-ossp' AND installed_version IS NOT NULL;")
== 0)
{
g_warning ("%s: PostgreSQL extension uuid-ossp required", __func__);
return -1;
}
if (check_db_extensions ())
return -1;

/* Functions in C. */

sql ("SET role dba;");
sql ("SET ROLE \"%s\";", DB_SUPERUSER_ROLE);

sql ("CREATE OR REPLACE FUNCTION hosts_contains (text, text)"
" RETURNS boolean"
Expand Down Expand Up @@ -2912,47 +2917,56 @@ check_db_sequences ()
}

/**
* @brief Check if an extension is installed.
* @brief Check if an extension is available and can be installed.
*
* @param[in] extname Name of the extension to check.
* @param[in] name Name of the extension to check.
*
* @return TRUE extension is installed, FALSE otherwise.
* @return TRUE extension is available, FALSE otherwise.
*/
static gboolean
db_extension_installed (const char *extname)
db_extension_available (const char *name)
{
if (sql_int ("SELECT count(*) FROM pg_extension WHERE extname = '%s'",
extname))
if (sql_int ("SELECT count(*) FROM pg_available_extensions"
" WHERE name = '%s'",
name))
{
g_debug ("%s: Extension '%s' is installed.",
__func__, extname);
g_debug ("%s: Extension '%s' is available.",
__func__, name);
return TRUE;
}
else
{
g_message ("%s: Extension '%s' is not installed.",
__func__, extname);
g_message ("%s: Extension '%s' is not available.",
__func__, name);
return FALSE;
}
}

/**
* @brief Check if all extensions are installed.
* @brief Ensure all extensions are installed.
*
* @return 0 success, 1 extension missing.
*/
int
check_db_extensions ()
{
if (db_extension_installed ("uuid-ossp")
&& db_extension_installed ("pgcrypto"))
if (db_extension_available ("uuid-ossp")
&& db_extension_available ("pgcrypto"))
{
g_debug ("%s: All required extensions are installed.", __func__);
g_debug ("%s: All required extensions are available.", __func__);

// Switch to superuser role and try to install extensions.
sql ("SET ROLE \"%s\";", DB_SUPERUSER_ROLE);

sql ("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"");
sql ("CREATE EXTENSION IF NOT EXISTS \"pgcrypto\"");

sql ("RESET ROLE;");
return 0;
}
else
{
g_warning ("%s: A required extension is not installed.", __func__);
g_warning ("%s: A required extension is not available.", __func__);
return 1;
}
}
Expand Down