티스토리 뷰

DevOps

Mairadb / postgresql SSL

김한성 2021. 7. 7. 19:43

Mariadb

클라이언트에서 mysql 서버에 접속할 때 기본적으로 평문 통신을 합니다. 이경우 데이터들을 그대로 네트워크상에 노출시키며, 이로인해 스니핑과 같은 공격으로 인해 중요 정보가 외부에 유출될 수 있습니다. 서버-클라이언트 사이에 전송되는 데이터를 SSL/TLS 프로토콜을 이용하여 암호화하여 DB 정보가 노출되지 않게 방지할수있습니다. 

 

MariaDB는 SSL이 지원되지만 기본적으로 아래처럼 disable상태입니다. 서버가 ssl connection을 지원한다면, 값은 YES 그렇지 않고 ssl지원이 compile안됐다면 값은 no가 됩니다.

MariaDB [(none)]> show variables like 'have_ssl';
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| have_ssl      | DISABLED |
+---------------+----------+
1 row in set (0.001 sec)

테스트한 버전

Mariadb 10.4.17

 

GnuTLS 사설 인증서 생성

 

□ GnuTLS 설치

sudo yum -y install gnutls-utils

rpm -qa | grep gnutls-utils

 

□ 인증서 저장 위치 생성

mkdir /etc/ssl/mysql

□ 사설 CA 인증서 생성

Common Name에 'admin만 입력' 나머지는 전부 엔터!

certtool --generate-privkey --outfile ca-key.pem
certtool --generate-self-signed --load-privkey ca-key.pem --outfile ca.pem

□ 서버 인증서 생성

Common Name에 'server 입력' 나머지는 전부 엔터!

certtool --generate-privkey --outfile server-key.pem
certtool --generate-certificate --load-privkey server-key.pem --outfile server-cert.pem --load-ca-certificate ca.pem --load-ca-privkey ca-key.pem

□ 클라이언트 인증서 생성

Common Name에 'client 입력' 나머지는 전부 엔터!

certtool --generate-privkey --outfile client-key.pem
certtool --generate-certificate --load-privkey client-key.pem --outfile client-cert.pem --load-ca-certificate ca.pem --load-ca-privkey ca-key.pem

그렇다면 아래와같이 인증서가 전부 생성 완료됩니다.

[admin@gurmwi-analysis-mariadb mysql]$ ll
합계 36
-rwxrwxrwx 1 admin admin 5826  6월 30 14:02 ca-key.pem
-rwxrwxrwx 1 admin admin 1115  6월 30 16:29 ca.pem
-rwxrwxrwx 1 admin admin 1159  6월 30 16:41 client-cert.pem
-rwxrwxrwx 1 admin admin 5823  6월 30 16:40 client-key.pem
-rwxrwxrwx 1 admin admin 1159  6월 30 16:31 server-cert.pem
-rwxrwxrwx 1 admin admin 5816  6월 30 16:30 server-key.pem

□ 해당 파일을 my.cnf에 추가해줍니다.

vi /etc/my.cnf

[mysqld]
### SSL/TLS
ssl_cert = /etc/ssl/mysql/server-cert.pem
ssl_key = /etc/ssl/mysql/server-key.pem
ssl_ca = /etc/ssl/mysql/ca.pem
ssl=on

mariadb 서비스 재실행

systemctl restart mariadb.service

만약 정상적으로 SSL이 적용되었다면 아래처럼 Value 값이 들어가있어야 합니다.

MariaDB [(none)]> show variables like '%ssl%';
+---------------------+----------------------------------+
| Variable_name       | Value                            |
+---------------------+----------------------------------+
| have_openssl        | YES                              |
| have_ssl            | YES                              |
| ssl_ca              | /etc/ssl/mysql/ca.pem            |
| ssl_capath          |                                  |
| ssl_cert            | /etc/ssl/mysql/server-cert.pem   |
| ssl_cipher          |                                  |
| ssl_crl             |                                  |
| ssl_crlpath         |                                  |
| ssl_key             | /etc/ssl/mysql/server-key.pem    |
| version_ssl_library | OpenSSL 1.0.2k-fips  26 Jan 2017 |
+---------------------+----------------------------------+
10 rows in set (0.001 sec)

이제 특정 계정에 ssl 옵션을 사용해 들어올수있도록 추가해줍니다. 

grant all privileges on *.* to 'hskim'@'%' identified by 'hskimpassword' require SSL;
flush privileges;

그렇다면 이제 hskim 계정으로 접속할때 아래처럼 접속하게되면 SSL: Cipher in use is DHE-RSA-AES256-GCM-SHA384 처럼 SSL이 정상적으로 적용된것으로 확인할 수 있습니다.

[admin@gurmwi-analysis-mariadb mysql]$ mysql -u hskim -p --ssl
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 117
Server version: 10.4.17-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> \s
--------------
mysql  Ver 15.1 Distrib 10.4.17-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:		117
Current database:
Current user:		hskim@localhost
SSL:			Cipher in use is DHE-RSA-AES256-GCM-SHA384
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server:			MariaDB
Server version:		10.4.17-MariaDB MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Uptime:			6 days 10 hours 36 sec

Threads: 7  Questions: 488  Slow queries: 0  Opens: 91  Flush tables: 1  Open tables: 85  Queries per second avg: 0.000
--------------

또한 위에처럼 접속할때 --ssl 붙이지 않으면 패스워드를 정확히 입력하여도 아래와 같이 접속이 안되는것을 볼 수 있습니다. 즉, 이제 hskim 계정으로 접속할때는 무조건 ssl 옵션을 붙여야 접속할 수 있게 됩니다.

[admin@gurmwi-analysis-mariadb mysql]$ mysql -u hskim -p
Enter password:
ERROR 1045 (28000): Access denied for user 'hskim'@'localhost' (using password: YES)

Postgresql

□ Generate SSL Certificates를 postgresql이 설치된 서버에 구성하여 줍니다. 먼저 postgresql 이 구성된 경로에 들어갑니다.

su - postgres
cd ~/pgsql/12/data

□ Generate private키를 생성합니다.

openssl genrsa -des3 -out server.key 1024

□ 해당 명령어로 암호를 제거합니다.

openssl rsa -in server.key -out server.key

□ 해당 명령어로 self-signed 인증서를 생성합니다. Common Name만 식별할수 있는 이름으로 지정하고 다른 옵션은 엔터를 칩니다.

openssl req -new -key server.key -days 3650 -out server.crt -x509

□ 위에서만든 self-signed를 복사하여 root certificate를 생성합니다.

cp server.crt root.crt

□ postgresql.conf 파일 수정해줍니다 아래처럼 위에서 생성한 crt파일의 경로를 지정해주고 ssl_prefer_server_ciphers를 on으로 해줍니다.

# - SSL -

ssl = on
ssl_ca_file = '~/pgsql/12/data/root.crt'
ssl_cert_file = '~/pgsql/12/data/server.crt'
ssl_key_file = '~/pgsql/12/data/server.key'
ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
ssl_prefer_server_ciphers = on

□ pg_hba.conf 파일에 아래처럼 설정을 추가해줍니다.

hostssl all             all              0.0.0.0/0              trust

□ 위의 설정까지 마쳤다면 postgresql를 재시작해줍니다.

systemctl status postgresql-12.service

□ 아래처럼 접속하게되면 SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off) 해당 항목이 보이게 되는데, SSL이 정상적으로 활성화 되었다는것을 확인할수 있습니다.

-bash-4.2$ psql "user=postgres dbname=postgres host=localhost"
Password for user postgres:
psql (12.7)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=#

 

참고

https://susoterran.github.io/mysql/mysql_ssl/

https://mariadb.com/kb/ko/ssl-ssl/

https://docs.microfocus.com/itom/Database_Middleware_Automation:10.61_Ultimate/SSLPostgreSQLClu

'DevOps' 카테고리의 다른 글

Pinoint server 구축 및 Kubernetes pod 연동  (0) 2021.12.21
Gitlab root password 변경  (0) 2021.07.07
Cloud Native Infra / MSA / Kubernetes  (0) 2021.03.25
Superset / Chart  (0) 2021.03.15
NextCloud docker로 구성  (0) 2021.02.02
댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31