DevOps/System&Tools

x509: certificate signed by unknown authority

Jacob_baek 2022. 3. 25. 13:14

gitlab에서 사용중인 인증서 문제인지 gitlab에 접근하는

✗ failed to get Git repository "https://gitlab.jacobbaek.com/dubaek/flux-test": Get "https://gitlab.jacobbaek.com/api/v4/projects/dubaek%2Fflux-test": x509: certificate signed by unknown authority

PC에서 사용중인 루트 인증서 리스트에 사용하려는 인증서의 발급자가 없어서 발생된 이슈이다.

jacob@dubaek:/tmp$ echo | openssl s_client -showcerts -connect gitlab.jacobbaek.com:443
CONNECTED(00000003)
depth=0 CN = *.jacobbaek.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = *.jacobbaek.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:CN = *.jacobbaek.com
   i:C = GB, ST = Greater Manchester, L = Salford, O = Sectigo Limited, CN = Sectigo RSA Domain Validation Secure Server CA
-----BEGIN CERTIFICATE-----
MXX ... XXx
-----END CERTIFICATE-----
---
Server certificate
subject=CN = *.jacobbaek.com

issuer=C = GB, ST = Greater Manchester, L = Salford, O = Sectigo Limited, CN = Sectigo RSA Domain Validation Secure Server CA

---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA-PSS
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 2159 bytes and written 393 bytes
Verification error: unable to verify the first certificate
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 2048 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 21 (unable to verify the first certificate)
---
DONE

위 결과상에서 볼수 있듯이 sectigo_RSA CA 를 통해 발급받은 인증서이며 해당 CA 정보를 현재
x509 에러가 발생되는 PC의 ca-certifcates에 등록해야 한다.

Ubuntu

앞서 확인된 BEGIN CERTIFICATE ~ END CERTIFICATE 정보를 임의의 파일로 생성한다.

jacob@dubaek:/tmp$ cat /usr/local/share/ca-certificates/sectigo_RSA_CA.crt
-----BEGIN CERTIFICATE-----
MXX ... XXx
-----END CERTIFICATE-----

이후 적용을 위해 ca-certificate 명령을 수행하여 적용한다.

jacob@dubaek:/tmp$ sudo update-ca-certificates
Updating certificates in /etc/ssl/certs...
rehash: warning: skipping cacert.pem,it does not contain exactly one certificate or CRL
rehash: warning: skipping duplicate certificate in Comodo_AAA_Services_root.pem
1 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...

Adding debian:sectigo_RSA_CA.pem
done.
done.

이후 인증서 에러는 없이 gitlab에서 source code를 가져오거나 연동이 이루어졌다.

확인을 해보고자 하면 아래 명령과 같이 추가된 인증서의 마지막 라인을 복사하여 grep 해보면 결과가 나오게 된다.

jacob@dubaek:/tmp$ grep "V8rOiH5qB9c0uwUx8Zx" /etc/ssl/certs/ca-certificates.crt
3V8rOiH5qB9c0uwUx8Zx

참고로 update-ca-certificates 의 man page중에 관련 파일들에 대한 설명이다.

  • /etc/ca-certificates.conf : A configuration file.
  • /etc/ssl/certs/ca-certificates.crt : A single-file version of CA certificates. This holds all CA certificates that you activated in /etc/ca-certificates.conf.
  • /usr/share/ca-certificates : Directory of CA certificates.
  • /usr/local/share/ca-certificates : Directory of local CA certificates (with .crt extension).

CentOS or Rocky Linux

CentOS 나 Rocky Linux의 경우 기본적으로 ca-certificates 패키지가 설치되어 있지 않아 아래와 같은 명령으로 설치해야 한다.

[root@dubaek centos]# yum install ca-certificates

-----BEGIN CERTIFICATE-----
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.kakao.com
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
base                                                                                                                                                                                      | 3.6 kB  00:00:00
extras                                                                                                                                                                                    | 2.9 kB  00:00:00
updates                                                                                                                                                                                   | 2.9 kB  00:00:00
updates/7/x86_64/primary_db                                                                                                                                                               |  14 MB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package ca-certificates.noarch 0:2020.2.41-70.0.el7_8 will be updated
---> Package ca-certificates.noarch 0:2021.2.50-72.el7_9 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================
 Package                                               Arch                                         Version                                                  Repository                                     Size
=================================================================================================================================================================================================================
Updating:
 ca-certificates                                       noarch                                       2021.2.50-72.el7_9                                       updates                                       379 k

Transaction Summary
=================================================================================================================================================================================================================
Upgrade  1 Package

Total download size: 379 k
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
ca-certificates-2021.2.50-72.el7_9.noarch.rpm                                                                                                                                             | 379 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : ca-certificates-2021.2.50-72.el7_9.noarch                                                                                                                                                     1/2
  Cleanup    : ca-certificates-2020.2.41-70.0.el7_8.noarch                                                                                                                                                   2/2
  Verifying  : ca-certificates-2021.2.50-72.el7_9.noarch                                                                                                                                                     1/2
  Verifying  : ca-certificates-2020.2.41-70.0.el7_8.noarch                                                                                                                                                   2/2

Updated:
  ca-certificates.noarch 0:2021.2.50-72.el7_9

Complete!

이후 update-ca-trust 명령을 사용해 추가 작업을 한다.

먼저 알아두어야하는것은 update-ca-trust man page에 나온 내용이다.

SOURCE CONFIGURATION
       ...

       QUICK HELP 1: To add a certificate in the simple PEM or DER file formats to
       the list of CAs trusted on the system:

       ·   add it as a new file to directory /etc/pki/ca-trust/source/anchors/

       ·   run update-ca-trust extract

       QUICK HELP 2: If your certificate is in the extended BEGIN TRUSTED file
       format (which may contain distrust/blacklist trust flags, or trust flags for
       usages other than TLS) then:

       ·   add it as a new file to directory /etc/pki/ca-trust/source/

       ·   run update-ca-trust extract

간단히 요약해보자면 2가지의 certificate를 추가하는 방식이 있는데 첫번째는 PEM이나 DER 파일 포맷의 경우
/etc/pki/ca-trust/source/anchors/ 디렉토리에 인증서 파일을 복사해놓고 update-ca-trust extract 명령을 수행하면 된다.
두번째는 BEGIN TRUSTED 파일 포맷을 사용하는 경우 /etc/pki/ca-trust/source 경로에 인증서 파일을 복사해놓고 update-ca-trust extract 명령을 실행하면 된다는것이다.

참고로 위 update-ca-trust에 사용되는 extract는 다음과 같은 의미를 지닌다.

       extract
           Instruct update-ca-trust to scan the SOURCE CONFIGURATION and produce updated versions of the consolidated configuration
           files stored below the /etc/pki/ca-trust/extracted directory hierarchy.

앞선 Ubuntu 상에서 추가했던 방식과 동일하게 /etc/pki/ca-trust/source/anchors 디렉토리내에 인증서 파일을 만들고

[root@dubaek anchors]# pwd
/etc/pki/ca-trust/source/anchors
[root@dubaek anchors]# cat ca_issuer.crt
-----BEGIN CERTIFICATE-----
MIIE ... Ybg==
-----END CERTIFICATE-----

아래와 같이 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem 내에 방금 추가한 인증서 정보가
상단에 업데이트됨을 확인할 수 있다.

[root@dubaek anchors]# head -n50 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

kubernetes 상의 pod에서 x509: certificate signed by unknown authority 이슈 발생시

다음과 같은 방법을 사용해볼수 있다.

  • 추가할 인증서가 존재하는 경로를 volume mount하여 업데이트
  • Container 이미지 자체에 포함(build 과정에서 직접 추가)

실제 과정은 다음 링크를 참고해서 수행해본다.

참고사이트