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

Open postgres to listen on the network and enforce protection with a random password #15

Merged
merged 13 commits into from
Jun 21, 2024

Conversation

stephdl
Copy link
Collaborator

@stephdl stephdl commented Jun 14, 2024

These changes improve security by avoiding the use of hardcoded passwords in the service configuration files and allow to open the network tcp port inside the trusted zone

This pull request includes changes to the postgresql-app.service and pgadmin-app.service files to update them to use secrets for the database password. It also adds a new script, create-secrets, for module initialization. The create-secrets script generates a random password for the PostgreSQL user and stores it in a secrets file.

pgadmin connects to the databse on a unix socket

@stephdl stephdl changed the title Update postgresql and pgadmin-app services to use secrets for database password Open postgres to listen on the network and enforce protection with a random password Jun 17, 2024
The create-secrets script has been updated to restrict file permissions to 400, ensuring that the secrets file is only accessible by the owner. This improves the security of the secrets stored in the file.
@DavidePrincipi
Copy link
Member

Why this change is required? The hardcoded password seems valid only for connections coming from the pod's private network. What are the benefits of this change?

@stephdl
Copy link
Collaborator Author

stephdl commented Jun 19, 2024

In fact actually postgresql can be contacted only from the host so I have to install the webserver on the node where is running postgres. I would like to use postgres from any servers inside the cluster

  • I need to open the port inside the pod to allow connection not from 127.0.0.1
  • I need a more complex password for the postgres user which is the master
  • I need to make an upgrade path

@DavidePrincipi
Copy link
Member

DavidePrincipi commented Jun 19, 2024

Isn't user postgres already limited to connect from 127.0.0.1?

https://github.com/NethServer/ns8-postgresql/pull/15/files#diff-b7d23e7e7108b85544eb3542076bd22cf2daed2d8a246be5ea3d0b6ac956bdc7L33

It is the private loopback IP, not the host one.

As alternative to the random password approach, please evaluate the IP-based check.

From podman-run manpage:

• port_handler=rootlesskit: Use rootlesskit for port forwarding. Default. Note: Rootlesskit changes the source IP address of incoming packets to an IP address in the container network namespace, usually 10.0.2.100.
If the application requires the real source IP address, e.g. web server logs, use the slirp4netns port handler. The rootlesskit port handler is also used for rootless containers when connected to user-defined net‐
works.

• port_handler=slirp4netns: Use the slirp4netns port forwarding, it is slower than rootlesskit but preserves the correct source IP address. This port handler cannot be used for user-defined networks.

The default of private network should already work

This commit updates the PostgreSQL configuration to use the `/var/run/postgresql` directory as the location for the socket file. This change ensures that the socket file is accessible for communication with the PostgreSQL server.
Copy link
Collaborator Author

@stephdl stephdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we use a unix socket to connect to the databse for pgadmin, no need for a password

However the POSTGRES_PASSWORD is the only environment variable needed by the container, we cannot create it without, we cannot ask to the container to make it randomly itself like mariadb container does.


if [[ ! -f ~/.config/state/secrets/passwords.secret ]]; then
password_postgres=$(/usr/bin/openssl rand -hex 20)
/usr/bin/echo "POSTGRES_PASSWORD=$password_postgres" > ~/.config/state/secrets/passwords.secret
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an environment file syntax

Suggested change
/usr/bin/echo "POSTGRES_PASSWORD=$password_postgres" > ~/.config/state/secrets/passwords.secret
/usr/bin/echo "POSTGRES_PASSWORD=$password_postgres" > ~/.config/state/secrets/passwords.env

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is upgrade necessary? Shouldn't existing installations be already hardened?

Copy link
Collaborator Author

@stephdl stephdl Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrade is necessary to change the postgres password from Nethesis,1234 to a random one

Else you could connect easily you know it

README.md Outdated Show resolved Hide resolved
`${TCP_PORT_PGSQL} `is set inside the environment of the module

`IP_of_Node` is the IP running the container, it might be the internal wiregard IP or the external IP of the node
`IP_of_Node` is the IP running the container, it must be the internal wiregard IP for example 10.5.4.1, the port is not opened in the firewall
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the firewall command required to open that port? Please, make an example.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without tls encryption I am not sure to open a tcp port is smart. I can be wrong

@stephdl stephdl merged commit 39f60fc into main Jun 21, 2024
1 check passed
@stephdl stephdl deleted the openNetwork branch June 21, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants