diff --git a/conf/redis.conf b/conf/redis.conf index 75a1128..7d50bbc 100644 --- a/conf/redis.conf +++ b/conf/redis.conf @@ -100,7 +100,7 @@ tcp-keepalive 300 # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. -daemonize yes +daemonize no # If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: @@ -122,7 +122,7 @@ supervised no # # Creating a pid file is best effort: if Redis is not able to create it # nothing bad happens, the server will start and run normally. -pidfile /var/run/redis/redis-server__REDIS_PORT__.pid +pidfile __FINAL_PATH__/redis-server-__REDIS_PORT__.pid # Specify the server verbosity level. # This can be one of: @@ -135,7 +135,7 @@ loglevel notice # Specify the log file name. Also the empty string can be used to force # Redis to log on the standard output. Note that if you use standard # output for logging but daemonize, logs will be sent to /dev/null -logfile /var/log/redis/redis-server-__REDIS_PORT__.log +logfile /var/log/__APP__/redis-__REDIS_PORT__.log # Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT where diff --git a/conf/scrumblr_redis.service b/conf/scrumblr_redis.service index 70d6128..fc68505 100644 --- a/conf/scrumblr_redis.service +++ b/conf/scrumblr_redis.service @@ -7,41 +7,32 @@ Type=simple User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__/ -ExecStart=redis-server redis.conf -StandardOutput=syslog -StandardError=syslog -SyslogIdentifier=redis-__APP__ +ExecStart=/usr/bin/redis-server __FINALPATH__/redis.conf +ExecStop=/bin/kill -s TERM $MAINPID +PIDFile=__FINALPATH__/redis-server-__PORT__.pid +TimeoutStopSec=0 Restart=always -# Sandboxing options to harden security -# Depending on specificities of your service/app, you may need to tweak these -# .. but this should be a good baseline -# Details for these options: https://www.freedesktop.org/software/systemd/man/systemd.exec.html -NoNewPrivileges=yes +UMask=007 PrivateTmp=yes +LimitNOFILE=65535 PrivateDevices=yes -RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 -RestrictNamespaces=yes -RestrictRealtime=yes -DevicePolicy=closed -ProtectSystem=full -ProtectControlGroups=yes -ProtectKernelModules=yes -ProtectKernelTunables=yes -LockPersonality=yes -SystemCallFilter=~@clock @debug @module @mount @obsolete @reboot @setuid @swap +ProtectHome=yes +#ReadOnlyDirectories=/ +ReadWriteDirectories=-/var/lib/__APP__ +ReadWriteDirectories=-/var/log/__APP__ +ReadWriteDirectories=-__FINALPATH__ + +NoNewPrivileges=true +CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE +MemoryDenyWriteExecute=true +ProtectKernelModules=true +ProtectControlGroups=true +RestrictRealtime=true +RestrictNamespaces=true +RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX + -# Denying access to capabilities that should not be relevant for webapps -# Doc: https://man7.org/linux/man-pages/man7/capabilities.7.html -CapabilityBoundingSet=~CAP_RAWIO CAP_MKNOD -CapabilityBoundingSet=~CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_AUDIT_WRITE -CapabilityBoundingSet=~CAP_SYS_BOOT CAP_SYS_TIME CAP_SYS_MODULE CAP_SYS_PACCT -CapabilityBoundingSet=~CAP_LEASE CAP_LINUX_IMMUTABLE CAP_IPC_LOCK -CapabilityBoundingSet=~CAP_BLOCK_SUSPEND CAP_WAKE_ALARM -CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG -CapabilityBoundingSet=~CAP_MAC_ADMIN CAP_MAC_OVERRIDE -CapabilityBoundingSet=~CAP_NET_ADMIN CAP_NET_BROADCAST CAP_NET_RAW -CapabilityBoundingSet=~CAP_SYS_ADMIN CAP_SYS_PTRACE CAP_SYSLOG [Install] WantedBy=multi-user.target diff --git a/conf/systemd.service b/conf/systemd.service index 365e7ef..bff937c 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -1,6 +1,6 @@ [Unit] Description=Scrumblr: software to take note. -After=network.target +Requires=redis-__APP__.service [Service] Type=simple @@ -9,7 +9,7 @@ Group=__APP__ WorkingDirectory=__FINALPATH__ Environment="PATH=_PATH__" Environment="NODE_ENV=production" -ExecStart=__YNH_NODE__ server.js --port=__PORT__ --baseurl=__PATH__ --redis localhot:__REDIS_PORT__ +ExecStart=__YNH_NODE__ server.js --port=__PORT__ --baseurl=__PATH__ --redis localhost:__REDIS_PORT__ Restart=always # Sandboxing options to harden security diff --git a/scripts/backup b/scripts/backup index 08e8f9f..5ce4bc9 100644 --- a/scripts/backup +++ b/scripts/backup @@ -30,7 +30,7 @@ app=$YNH_APP_INSTANCE_NAME final_path=$(ynh_app_setting_get --app=$app --key=final_path) domain=$(ynh_app_setting_get --app=$app --key=domain) -db_name=$(ynh_app_setting_get --app=$app --key=db_name) +redis_port=$(ynh_app_setting_get --app=$app --key=redis_port) codename=$(ynh_app_setting_get $app codename) #================================================= @@ -38,6 +38,13 @@ codename=$(ynh_app_setting_get $app codename) #================================================= ynh_print_info --message="Declaring files to be backed up..." + +#================================================= +# EXPORT DATABASE +#================================================= + +redis-cli --rdb "$final_path/redis-$app.rdb" -p $redis_port + #================================================= # BACKUP THE APP MAIN DIR #================================================= @@ -63,6 +70,7 @@ ynh_backup --src_path="/etc/logrotate.d/$app" #================================================= ynh_backup --src_path="/etc/systemd/system/$app.service" +ynh_backup --src_path="/etc/systemd/system/redis-$app.service" #================================================= # END OF SCRIPT diff --git a/scripts/install b/scripts/install index 042f824..2ccb1ed 100644 --- a/scripts/install +++ b/scripts/install @@ -59,21 +59,6 @@ ynh_script_progression --message="Finding an available port..." --weight=1 port=$(ynh_find_port --port=8080) ynh_app_setting_set --app=$app --key=port --value=$port -#================================================== -# CONFIGURE REDIS SERVICE -#================================================== - -ynh_install_app_dependencies redis-server redis-tools - -# Find an available port for redis -redis_port=$(ynh_find_port --port=8081) -ynh_app_setting_set --app=$app --key=redis_port --value=$redis_port - -ynh_add_config --template="redis.conf" --destination="$final_path/redis.conf" - -redis_service="redis-$app$" -ynh_add_systemd_config --service $redis_service --template scrumblr_redis - #================================================= # INSTALL NODEJS #================================================= @@ -100,10 +85,25 @@ ynh_app_setting_set --app=$app --key=final_path --value=$final_path # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source --dest_dir="$final_path" -chmod 750 "$final_path" -chmod -R o-rwx "$final_path" -chown -R $app:www-data "$final_path" +#================================================== +# CONFIGURE REDIS SERVICE +#================================================== +ynh_install_app_dependencies redis-server redis-tools + +# Find an available port for redis +redis_port=$(ynh_find_port --port=8081) +ynh_app_setting_set --app=$app --key=redis_port --value=$redis_port + +ynh_add_config --template="redis.conf" --destination="$final_path/redis.conf" + +redis_service="redis-$app" +ynh_add_systemd_config --service $redis_service --template scrumblr_redis.service + + +chmod 770 "$final_path" +chmod -R o-rwx "$final_path" +chown -R $app:$app "$final_path" #================================================= # NGINX CONFIGURATION #================================================= @@ -122,6 +122,8 @@ ynh_script_progression --message="Configuring log rotation..." --weight=1 # Use logrotate to manage application logfile(s) ynh_use_logrotate +chown -R $app:$app "/var/log/$app/" + #================================================= # SETUP SYSTEMD #================================================= diff --git a/scripts/remove b/scripts/remove index 691fc90..2e3d9d2 100644 --- a/scripts/remove +++ b/scripts/remove @@ -41,6 +41,10 @@ ynh_script_progression --message="Stopping and removing the systemd service..." # Remove the dedicated systemd config ynh_remove_systemd_config +ynh_remove_systemd_config --service="redis-$app" + + + #================================================= # REMOVE LOGROTATE CONFIGURATION #================================================= diff --git a/scripts/restore b/scripts/restore index 663cf58..3ecc868 100644 --- a/scripts/restore +++ b/scripts/restore @@ -63,10 +63,6 @@ ynh_script_progression --message="Restoring the app main directory..." --weight= ynh_restore_file --origin_path="$final_path" -chmod 750 "$final_path" -chmod -R o-rwx "$final_path" -chown -R $app:www-data "$final_path" - #================================================= # SPECIFIC RESTORATION #================================================= @@ -92,6 +88,10 @@ ynh_script_progression --message="Restoring the redis configuration..." --weight ynh_restore_file --origin_path="/etc/systemd/system/redis-$app.service" systemctl enable "redis-$app.service" --quiet +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R $app:redis "$final_path" + #================================================= # RESTORE THE LOGROTATE CONFIGURATION #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index b15b0b2..b4d6c7d 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -54,9 +54,9 @@ ynh_script_progression --message="Stopping a systemd service..." --weight=1 ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" -if [ $(ynh_compare_current_package_version --comparison lt --version "0.2.0~ynh2") ] +if [ $(ynh_compare_current_package_version --comparison gt --version "0.2.0~ynh1") ] then - ynh_systemd_action --service_name=redis-$app --action="stop" --log_path="/var/log/$app/redis-$app.log"ls + ynh_systemd_action --service_name=redis-$app --action="stop" --log_path="/var/log/$app/redis-$app.log" fi #================================================= # ENSURE DOWNWARD COMPATIBILITY @@ -97,10 +97,6 @@ then ynh_setup_source --dest_dir="$final_path" fi -chmod 750 "$final_path" -chmod -R o-rwx "$final_path" -chown -R $app:www-data "$final_path" - if [ "$upgrade_type" == "UPGRADE_APP" ] then pushd $final_path @@ -114,6 +110,8 @@ fi #================================================= ynh_script_progression --message="migrating database... $(ynh_package_version --package=scrumblr)" --weight=1 +echo "$(ynh_package_version --package=srumblr)" + if [ $(ynh_compare_current_package_version --comparison lt --version "0.2.0~ynh2") ] @@ -139,6 +137,11 @@ ynh_add_config --template="redis.conf" --destination="$final_path/redis.conf" redis_service="redis-$app$" ynh_add_systemd_config --service $redis_service --template scrumblr_redis +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R $app:redis "$final_path" + + #================================================= # NGINX CONFIGURATION #=================================================