개발공부/Linux

[Linux] Elasticsearch SSL/TLS(https) 외부에서 접속하기

차나니 2024. 5. 20. 13:11

설치환경

- Linux : Ubuntu 22.04

- Eleasticsearch, logstash : 7.17.21

 

미리 준비하기

① 도메인 : 서버 IP를 DNS 정보로 등록하기

② SSL인증서 : 위 도메인 정보가 포함되어있는 인증서 발급하기

 

도메인의 경우는 유로로 구매할 수도 있고 무료로 사용할 수도 있습니다 ! 

저는 아래 포스팅과 같은 방법으로 진행하였으니 무료로 사용하실 분들은 참고해서 진행하셔도됩니다 !

 

[Linux] nginx에 certbot으로 SSL(https) 보안 인증서 적용하기

프로젝트를 배포하고 SSL 설정은 급한게 아니라 천천히 하려고 했지만 배포한 http에서 cookie에 저장이 안되는 이슈가 생겼다....이유는 https 적용할 경우 해결된다고 해서 부랴부랴 cerbot으로 SSL 적

chanhan.tistory.com

 

인증서 변환하기

elasticsearch에서는 .pem 파일이 아닌 cert와 개인키가 포함되어있는 .p12 파일을 통해 인증서 처리를 진행합니다.

그래서 발급받은 .pem 파일을 .p12의 형식으로 변환해줘야합니다 !

 

① 파일의 위치

$ cd /etc/letsencrypt/archive/{MyDomain}

 

정상적으로 인증서를 발급 받았다면 위의 경로에 .pem 파일로 아래와 같이 인증서에 대한 정보들이 각각 분리되어있습니다.

cert1.pem  chain1.pem  fullchain1.pem  privkey1.pem

 

② 파일 변환하기

$ openssl pkcs12 -export -inkey privkey1.pem -in cert1.pem -out cert_key.p12

 

인증서 패스워드 입력 : (keystore에 등록해줘야하기 때문에 잊어버리지 않도록 메모해놓기 !)

 

③ .p12 파일 복사하기($ES_PATH는 환경변수로 설정한 ES 경로)

$ cp cert_key.p12 $ES_PATH/config/cert_key.p12

 

④ cert_key.p12 파일에 권한 부여하기

chown elasticsearch:elasticsearch $ES_PATH/config/cert_key.p12
chmod 640 $ES_PATH/config/cert_key.p12

 

⑤ 인증서 비밀번호 변경

$ cd /usr/share/elasticsearch
bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.http.ssl.truststore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

위에서 설정해줬던 비밀번호 입력 :

 

⑥ elasticsearch.yml 파일 수정

$ vim /etc/elasticsearch/elasticsearch.yml
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: none
xpack.security.transport.ssl.keystore.path: /etc/elasticsearch/cert_key.p12
xpack.security.transport.ssl.truststore.path: /etc/elasticsearch/cert_key.p12

xpack.security.enabled: true
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.verification_mode: none
xpack.security.http.ssl.keystore.path: /etc/elasticsearch/cert_key.p12
xpack.security.http.ssl.truststore.path: /etc/elasticsearch/cert_key.p12
xpack.security.http.ssl.keystore.password: 'keypassword'
xpack.security.http.ssl.truststore.password: 'keypassword'

 

⑦ elasticsearch 실행

$ sudo systemctl start elasticsearch
// 인증서 없이 호출하기
$ curl -XGET https://xxx.xxx.xxx.xxx:9200/_nodes?pretty -k -u "elastic:비밀번호"

이제 브라우저에서 접속할 경우 접속이 가능합니다 !

왜냐하면 우리가 인증서를 발급 받는 순간 인증서는 브라우저의 인증 정보를 제공하기 때문입니다 !

하지만  터미널 curl이나, 다른 클라이언트로 연결할 경우 인증 못하는 경우가 발생할 수 있습니다.

 

 

참고

openssl을 사용해 .p12 파일을 만들 때 위 설명의 1번과 같은 파일 구조를 가지고 있지 않을 수 있습니다.

.p12 파일은 개인 키와 인증서를 하나의 파일로 통합하는 것이기에 아래의 옵션을 참고해서 진행하시길 바라겠습니다.

-export: .p12 파일 생성 옵션

-inkey privkey.pem: 개인 키 파일 지정

-in fullchain.pem: 인증서 파일 지정

-out keystore.p12: 출력 파일 이름 지정

-name "elasticsearch": .p12 파일의 엔트리 이름 지정 (원하는 이름으로 변경 가능)