2017-04-23 22:06:55 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2022-01-04 02:24:25 -06:00
|
|
|
# Copyright (C) 2013 - 2022, The pgAdmin Development Team
|
2017-04-23 22:06:55 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
import os
|
2020-06-02 03:39:44 -05:00
|
|
|
import getpass
|
2021-05-03 05:40:45 -05:00
|
|
|
from pgadmin.utils.constants import KERBEROS
|
2017-04-23 22:06:55 -05:00
|
|
|
|
2020-09-29 04:38:14 -05:00
|
|
|
FAILED_CREATE_DIR = \
|
|
|
|
"ERROR : Failed to create the directory {}:\n {}"
|
|
|
|
|
2017-04-23 22:06:55 -05:00
|
|
|
|
|
|
|
def _create_directory_if_not_exists(_path):
|
2020-01-13 01:50:37 -06:00
|
|
|
if _path and not os.path.exists(_path):
|
2017-04-23 22:06:55 -05:00
|
|
|
os.mkdir(_path)
|
2022-04-11 03:07:39 -05:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2017-04-23 22:06:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
def create_app_data_directory(config):
|
|
|
|
"""
|
|
|
|
Create the required directories (if not present).
|
|
|
|
"""
|
|
|
|
# Create the directory containing the configuration file (if not present).
|
2022-04-11 03:07:39 -05:00
|
|
|
is_directory_created = False
|
2020-06-02 00:41:53 -05:00
|
|
|
try:
|
2022-04-11 03:07:39 -05:00
|
|
|
is_directory_created = _create_directory_if_not_exists(
|
|
|
|
os.path.dirname(config.SQLITE_PATH))
|
2020-06-02 00:41:53 -05:00
|
|
|
except PermissionError as e:
|
2020-09-29 04:38:14 -05:00
|
|
|
print(FAILED_CREATE_DIR.format(os.path.dirname(config.SQLITE_PATH), e))
|
2020-06-02 00:41:53 -05:00
|
|
|
print(
|
|
|
|
"HINT : Create the directory {}, ensure it is writeable by\n"
|
|
|
|
" '{}', and try again, or, create a config_local.py file\n"
|
|
|
|
" and override the SQLITE_PATH setting per\n"
|
|
|
|
" https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".
|
|
|
|
format(
|
|
|
|
os.path.dirname(config.SQLITE_PATH),
|
2020-06-02 03:39:44 -05:00
|
|
|
getpass.getuser(),
|
2020-06-02 00:41:53 -05:00
|
|
|
config.APP_VERSION))
|
|
|
|
exit(1)
|
|
|
|
|
2019-07-16 08:36:29 -05:00
|
|
|
# Try to set the permissions on the directory, but don't complain
|
2019-05-20 03:46:02 -05:00
|
|
|
# if we can't. This may be the case on a mounted directory, e.g. in
|
|
|
|
# OpenShift. We'll still secure the config database anyway.
|
2022-04-11 03:07:39 -05:00
|
|
|
if os.name != 'nt' and is_directory_created:
|
2019-05-20 07:53:08 -05:00
|
|
|
try:
|
|
|
|
os.chmod(os.path.dirname(config.SQLITE_PATH), 0o700)
|
|
|
|
except Exception as e:
|
|
|
|
# The flask app isn't setup yet, so we can't use the logger
|
|
|
|
print('WARNING: Failed to set ACL on the directory containing the '
|
2020-06-02 00:41:53 -05:00
|
|
|
'configuration database:\n {}'.format(e))
|
|
|
|
print("HINT : You may need to manually set the permissions on\n"
|
|
|
|
" {} to allow {} to write to it.".
|
|
|
|
format(os.path.dirname(config.SQLITE_PATH),
|
2020-06-02 03:39:44 -05:00
|
|
|
getpass.getuser()))
|
2017-04-23 22:06:55 -05:00
|
|
|
|
|
|
|
# Create the directory containing the log file (if not present).
|
2020-06-02 00:41:53 -05:00
|
|
|
try:
|
|
|
|
_create_directory_if_not_exists(os.path.dirname(config.LOG_FILE))
|
|
|
|
except PermissionError as e:
|
2020-09-29 04:38:14 -05:00
|
|
|
print(FAILED_CREATE_DIR.format(os.path.dirname(config.LOG_FILE), e))
|
2020-06-02 00:41:53 -05:00
|
|
|
print(
|
|
|
|
"HINT : Create the directory {}, ensure it is writeable by\n"
|
|
|
|
" '{}', and try again, or, create a config_local.py file\n"
|
|
|
|
" and override the LOG_FILE setting per\n"
|
|
|
|
" https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".
|
|
|
|
format(
|
|
|
|
os.path.dirname(config.LOG_FILE),
|
2020-06-02 03:39:44 -05:00
|
|
|
getpass.getuser(),
|
2020-06-02 00:41:53 -05:00
|
|
|
config.APP_VERSION))
|
|
|
|
exit(1)
|
2017-04-23 22:06:55 -05:00
|
|
|
|
|
|
|
# Create the session directory (if not present).
|
2020-06-02 00:41:53 -05:00
|
|
|
try:
|
2022-04-11 03:07:39 -05:00
|
|
|
is_directory_created = \
|
|
|
|
_create_directory_if_not_exists(config.SESSION_DB_PATH)
|
2020-06-02 00:41:53 -05:00
|
|
|
except PermissionError as e:
|
2020-09-29 04:38:14 -05:00
|
|
|
print(FAILED_CREATE_DIR.format(config.SESSION_DB_PATH, e))
|
2020-06-02 00:41:53 -05:00
|
|
|
print(
|
|
|
|
"HINT : Create the directory {}, ensure it is writeable by\n"
|
|
|
|
" '{}', and try again, or, create a config_local.py file\n"
|
|
|
|
" and override the SESSION_DB_PATH setting per\n"
|
|
|
|
" https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".
|
|
|
|
format(
|
|
|
|
config.SESSION_DB_PATH,
|
2020-06-02 03:39:44 -05:00
|
|
|
getpass.getuser(),
|
2020-06-02 00:41:53 -05:00
|
|
|
config.APP_VERSION))
|
|
|
|
exit(1)
|
|
|
|
|
2022-04-11 03:07:39 -05:00
|
|
|
if os.name != 'nt' and is_directory_created:
|
2019-05-21 06:14:36 -05:00
|
|
|
os.chmod(config.SESSION_DB_PATH, 0o700)
|
2017-04-23 22:06:55 -05:00
|
|
|
|
|
|
|
# Create the storage directory (if not present).
|
2020-06-02 00:41:53 -05:00
|
|
|
try:
|
|
|
|
_create_directory_if_not_exists(config.STORAGE_DIR)
|
|
|
|
except PermissionError as e:
|
2020-09-29 04:38:14 -05:00
|
|
|
print(FAILED_CREATE_DIR.format(config.STORAGE_DIR, e))
|
2020-06-02 00:41:53 -05:00
|
|
|
print(
|
|
|
|
"HINT : Create the directory {}, ensure it is writable by\n"
|
|
|
|
" '{}', and try again, or, create a config_local.py file\n"
|
|
|
|
" and override the STORAGE_DIR setting per\n"
|
|
|
|
" https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".
|
|
|
|
format(
|
|
|
|
config.STORAGE_DIR,
|
2020-06-02 03:39:44 -05:00
|
|
|
getpass.getuser(),
|
2020-06-02 00:41:53 -05:00
|
|
|
config.APP_VERSION))
|
|
|
|
exit(1)
|
2021-05-03 05:40:45 -05:00
|
|
|
|
2022-06-27 09:06:20 -05:00
|
|
|
# Create Azure Credential Cache directory (if not present).
|
|
|
|
try:
|
|
|
|
_create_directory_if_not_exists(config.AZURE_CREDENTIAL_CACHE_DIR)
|
|
|
|
except PermissionError as e:
|
|
|
|
print(FAILED_CREATE_DIR.format(config.AZURE_CREDENTIAL_CACHE_DIR, e))
|
|
|
|
print(
|
|
|
|
"HINT : Create the directory {}, ensure it is writable by\n"
|
|
|
|
"'{}', and try again, or, create a config_local.py file\n"
|
|
|
|
" and override the AZURE_CREDENTIAL_CACHE_DIR setting per\n"
|
|
|
|
" https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".
|
|
|
|
format(
|
|
|
|
config.AZURE_CREDENTIAL_CACHE_DIR,
|
|
|
|
getpass.getuser(),
|
|
|
|
config.APP_VERSION))
|
|
|
|
exit(1)
|
|
|
|
|
2021-05-03 05:40:45 -05:00
|
|
|
# Create Kerberos Credential Cache directory (if not present).
|
|
|
|
if config.SERVER_MODE and KERBEROS in config.AUTHENTICATION_SOURCES:
|
|
|
|
try:
|
|
|
|
_create_directory_if_not_exists(config.KERBEROS_CCACHE_DIR)
|
|
|
|
except PermissionError as e:
|
|
|
|
print(FAILED_CREATE_DIR.format(config.KERBEROS_CCACHE_DIR, e))
|
|
|
|
print(
|
|
|
|
"HINT : Create the directory {}, ensure it is writable by\n"
|
|
|
|
"'{}', and try again, or, create a config_local.py file\n"
|
|
|
|
" and override the KERBEROS_CCACHE_DIR setting per\n"
|
|
|
|
" https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html".
|
|
|
|
format(
|
|
|
|
config.KERBEROS_CCACHE_DIR,
|
|
|
|
getpass.getuser(),
|
|
|
|
config.APP_VERSION))
|
|
|
|
exit(1)
|