iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🛡️

Security Improvement for Database Connections: Starting with the Principle of Least Privilege

に公開

Introduction

In the early stages of system development, it is common to use a database administrator account for application connections for convenience. However, continuing to operate in this manner poses significant security risks. This article explains steps to gradually reduce these risks, starting from an assessment of the current situation.

Target Audience

  • Engineers who are concerned about security configurations in development or staging environments
  • Those who are using administrator users for applications in development or staging environments
  • Individuals who feel uneasy about storing passwords in plain text in environment variable files

Risk Perspectives

We will examine the database connection status from the following perspectives and assess the risks.

Risk 1: Excessive Privileges

Administrator users can drop tables and create users. If an SQL injection attack occurs, it could lead to the loss of the entire database.

Risk 2: Exposure of Authentication Information

If the server is compromised, plain-text passwords in .env files allow immediate full control of the database.

Risk 3: Vulnerable Network Paths

Direct connections without a jump server are nearly equivalent to leaving the port open to all attackers on the internet.

Step 1: Assess the Current Situation

First, you must accurately understand how your application is configured.

1. Locate Authentication Configuration Files on the Server

Find the database connection information. It may be in .env or scattered across various files depending on the tech stack.

  • YAML Format
    docker-compose.yml, database.yml, etc.

  • JSON Format
    config.json, scripts inside package.json, etc.

  • PHP Format
    config.php, wp-config.php, etc.

# Recursively search files containing PASSWORD or DB_ in the directory
grep -rnE "DB_USERNAME|DB_PASSWORD|MYSQL_ROOT_PASSWORD" my-project/

2. Verify Permissions in the Database

Next, investigate what privileges the user has in the database. Execute the following SQL using a DB client application (DBeaver, TablePlus, etc.) or from the terminal.

-- Check the privileges of the current user
SHOW GRANTS FOR CURRENT_USER;

-- Or check the privileges of a specific user (e.g., admin)
SHOW GRANTS FOR 'admin'@'%';

# Example output
GRANT ALL PRIVILEGES ON *.* TO `admin`@`%` WITH GRANT OPTION

If the output shows ALL PRIVILEGES, WITH GRANT OPTION, or a long list like GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER..., the application has excessive privileges.

Step 2: Separate Users

Revoke the excessive privileges identified during the investigation and migrate to an application-specific user.

Create an Application-Specific User and Grant Privileges

-- 1. Create an application-specific user
CREATE USER 'webapp_user'@'%' IDENTIFIED BY 'complex_password';

-- 2. Grant only necessary DML privileges
GRANT SELECT, INSERT, UPDATE, DELETE ON target_db_name.* TO 'webapp_user'@'%';

-- 3. Apply changes
FLUSH PRIVILEGES;

This step prevents the destruction of the database structure or manipulation of other users even if an unauthorized operation is attempted through the application. This is a measure based on the principle of least privilege.

Step 3: Conceal Passwords

Storing passwords in environment variable files (.env) is convenient in early development, but it carries the risk of accidentally committing them to a repository or leaving plain-text files on the server. For further hardening, it is recommended to use managed services such as AWS Secrets Manager to externalize authentication information.

How to Integrate into the Application

There are two main patterns:

  • Retrieval via API: Use the AWS SDK at application startup to retrieve credentials from Secrets Manager.
  • Injection into environment variables: Use AWS ECS or Lambda features to expand Secrets Manager values as environment variables when containers or functions start.

Step 4: Enhance Connection Paths

If the database is in a location directly accessible from the internet (public subnet), even with firewall (Security Group) restrictions, there remains a risk of attack due to operational misconfiguration or zero-day vulnerabilities. Place the database in a private subnet isolated from the internet and secure the connection path using one of the following methods.

1. Use a Bastion Server

This is a common approach. Place a relay server called a "Bastion" in a public subnet and connect to the private database through it.

2. Use SSM Session Manager (Recommended)

A more modern and secure configuration is to use AWS Systems Manager (SSM) Session Manager.

This concludes the basic measures to improve database security by evaluating risks from three perspectives.


Conclusion

Even just switching from an administrator user to a dedicated user dramatically reduces the risk to the system you are protecting. Why not start by assessing the current risks? Reviewing the configuration one step at a time leads to a more robust database. I hope this article helps you in that process.


ONE WEDGE Co., Ltd.

【Making the world more fun with Serverless】
ONE WEDGE is a value-creating company that uses Serverless system development as its core technology, sincerely facing customers with energy and technical strength in web system development, business system and service development using AWS/GCP, mobile development with PWA, Alexa skill development, and more.
https://onewedge.co.jp/

GitHubで編集を提案

Discussion