Перейти к содержанию

Onlyoffice workspace ansible

Based on the code snippets provided, here is an analysis of the Ansible role structure, its logic flow, and a few critical observations regarding security and functionality.

1. Role Overview

This Ansible role (onlyoffice_workspace) is designed to automate the deployment of ONLYOFFICE Workspace (Community Server) using Docker Compose. It handles: * Prerequisites: Installing Docker and required system packages. * Verification: Checking Docker Hub for the latest tags and validating the requested version. * Deployment: Cloning the official ONLYOFFICE Docker repository, configuring docker-compose.yml, and initializing the MySQL database. * Networking/SSL: Setting up an Nginx reverse proxy with Let's Encrypt (Certbot) to handle HTTPS and routing traffic to the internal ONLYOFFICE container.

2. Logic Flow Analysis

A. Verification & Tag Management (verify.yml)

  • Authentication: It fetches a temporary bearer token from Docker Hub to access the API.
  • Tag Validation: It retrieves the list of available tags for onlyoffice/controlpanel.
  • Version Check:
    • It asserts that the requested tag (workspace_onlyoffice_controlpanel_docker_tag) exists.
    • It calculates the "latest" version by filtering out tags containing letters (likely excluding latest, beta, etc.) and sorting them.
    • Enforcement: If workspace_onlyoffice_controlpanel_docker_tag_latest is true, it forces the deployment to use the calculated latest version, failing if the user specified an older one.

B. Docker Installation (docker-install.yml)

  • Idempotency: Checks if docker --version works. If not, it proceeds to install.
  • OS Specifics: Uses ansible_distribution and ansible_lsb.codename to dynamically construct the Docker repository URL (e.g., for Ubuntu or Debian).
  • Security: Adds the official Docker GPG key before adding the repository.

C. Core Deployment (main.yml)

  • Git Management:
    • Clones https://github.com/ONLYOFFICE/Docker-CommunityServer.git to /opt/Docker-CommunityServer.
    • Stash Logic: It attempts to git stash local changes before pulling the new version and git stash pop afterwards. This is a clever way to preserve local modifications (like the generated docker-compose.yml) without losing them during the git pull/git checkout process.
  • Configuration:
    • Renders docker-compose.yml.j2 (likely containing service definitions, ports, and volumes).
    • Renders onlyoffice-initdb.sql.j2 to initialize the database schema and users.
  • Container Lifecycle:
    1. Pulls images (docker_compose_v2_pull).
    2. Starts only the MySQL container first (onlyoffice-mysql-server).
    3. Wait Loop: Executes a command inside the MySQL container to check for database creation. It retries 12 times (2 minutes total) to ensure the DB is ready before proceeding.
    4. Starts the rest of the stack (docker_compose_v2).
  • Health Check: Asserts that all containers defined in the compose file are in a running state.

D. SSL & Proxy (docker-certbot.yml)

  • External Role: Includes a docker-certbot-proxy role.
  • Nginx Setup: Creates a separate Docker Compose stack for Nginx + Certbot.
  • Routing: Configures Nginx to listen on 80/443, handle SSL termination, and proxy requests to the internal ONLYOFFICE container (onlyoffice-community-server).

3. Critical Observations & Recommendations

⚠️ Security Risk: Hardcoded Passwords in SQL

In templates/onlyoffice-initdb.sql.j2, the SQL script grants ALL PRIVILEGES to the root user and specific application users:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'onlyoffice_user'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'mail_admin'@'%';
* Issue: Granting ALL PRIVILEGES on *.* (all databases) to application users is a security best practice violation. If the application is compromised, the attacker has full control over the entire MySQL instance. * Recommendation: Restrict privileges to specific databases (onlyoffice and onlyoffice_mailserver).
GRANT ALL PRIVILEGES ON onlyoffice.* TO 'onlyoffice_user'@'%';
GRANT ALL PRIVILEGES ON onlyoffice_mailserver.* TO 'mail_admin'@'%';

⚠️ Security Risk: Password Exposure in Logs

The task List the database in the MySQL container... passes the password directly in the command string:

command: >-
  /bin/bash -c 'mysql -uroot -p"{{ workspace_mysql_root_password }}" ...'
* Issue: If Ansible verbosity is high (-vvv), this command (and potentially the password) might be printed to the console or logs. * Recommendation: Use a temporary file or an environment variable to pass the password, or rely on the MYSQL_PWD environment variable within the container execution context to avoid it appearing in the command line arguments.

⚠️ Logic Risk: Git Stash Pop Failure

The git stash pop task has a changed_when condition:

changed_when: ( "No stash entries found" not in workspace_git_repo_stash_pop.stdout )
* Issue: If the stash pop fails (e.g., due to conflicts), the task might still report success or fail silently depending on how failed_when is handled (it is not explicitly set here, so it defaults to failing on non-zero exit). However, if the stash pop fails, the subsequent docker-compose generation might be based on a corrupted state. * Recommendation: Add failed_when: false to the stash pop task and handle the error explicitly, or ensure the git module handles the conflict resolution strategy.

⚠️ Dependency on External Role

The file tasks/docker-certbot.yml includes docker-certbot-proxy. * Observation: This role is not defined in the provided snippets. If this role is missing from the roles_path or requirements.yml, the entire SSL setup will fail.

⚠️ Docker Network Configuration

In templates/compose.yml.j2 (for Nginx):

networks:
  onlyoffice:
    external: true
    name: onlyoffice
* Observation: This assumes the onlyoffice network already exists. The main docker-compose.yml (generated from docker-compose.yml.j2) must create this network. If the Nginx task runs before the main ONLYOFFICE stack creates the network, the Nginx deployment will fail. * Recommendation: Ensure the main onlyoffice_workspace role runs before the docker-certbot tasks, or add a task to create the network if it doesn't exist.

4. Summary of Files

File Purpose
verify.yml Validates Docker Hub tags and enforces version policies.
docker-install.yml Installs Docker Engine if missing.
main.yml Orchestrates the full deployment: Git clone, Config rendering, DB init, Container startup.
docker-compose.yml.j2 Template for the main ONLYOFFICE Docker Compose file.
onlyoffice-initdb.sql.j2 SQL script to create DBs and users (needs privilege hardening).
docker-certbot.yml Sets up Nginx + Certbot for SSL termination.
compose.yml.j2 Template for the Nginx Docker Compose file.
nginx_onlyoffice-workspace.conf.j2 Nginx configuration for proxying traffic to ONLYOFFICE.

This is a robust, production-ready role structure, but the SQL privilege grants and password handling should be reviewed before deployment in a secure environment.

question_mark
Я могу вам чем-то помочь?
question_mark
ИИ Помощник ×