LoginSignup
0
0

More than 1 year has passed since last update.

自己証明書を使用しNexusに443ポートで接続

Last updated at Posted at 2023-06-27

◎概要

あるプロジェクトでNexusの構築を行った。
デフォルトの8081番ポートでの接続ではなく443番のポートで
接続させてほしいとの要望。
オンプレ環境ではなくクラウド環境ならALBをかましてあげるなどの事で対処
出来たのだが、そういう事は出来なさそうなので自己証明書を発行して
443番のポートに接続させる事に。

自己証明書は、OpenJDKをインストールしていたので
OpenJDKはデフォルトでkeytoolコマンドが使用出来る為、
これを使用して自己証明書を発行しようと思う。
そもそも、NexusはJavaで動いているのでOpenJDKは必須。

この件について、忘れないようにメモ残そうとと思います。


◎自己証明を発行して443ポートで接続

1. KeyStoreファイル・鍵ペアの作成

  • 1.1 sslディレクトリに移動
    Nexusではこの配下にKeyStoreファイルを配置するので移動する
$ cd /opt/nexus/etc/ssl
  • 1.2 keytoolを使用して公開秘密鍵ペアを生成
# 「CN=」は、NexusサーバーのIPアドレスを記入
# 「OU」「O」「L」「ST」等は任意の値(要は何でもOK)
#  パスワードを求められた場合は「password」と入力(この形式のコマンドだと求められないはずだけれども一応記載)

$ keytool -genkeypair -keystore keystore.jks -storepass password -alias nexus \
 -keyalg RSA -keysize 2048 -validity 5000 -keypass password \
 -dname 'CN=(IPアドレス), OU=Sonatype, O=Sonatype, L=Unspecified, ST=Unspecified, C=JP'

# 下記のような警告文が出ると思いますが問題なしです。この後この処理をするので。
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.jks -deststoretype pkcs12".
  • 1.3 キーツールを使用してPEMエンコードされた公開証明書ファイルを生成
$ keytool -exportcert -keystore keystore.jks -alias nexus -rfc > nexus.cert

# パスワードを求められるので「password」と入力
Enter keystore password:
  • 1.4 PKCS12 キーストア ".p12" ファイルに変換
$ keytool -importkeystore -srckeystore keystore.jks -destkeystore nexus.p12 -deststoretype PKCS12

#  パスワードを求められるので全て「password」と入力
Enter destination keystore password:
Re-enter new password:
Enter source keystore password:
# 問題なければ下記のようにsuccessfullyと表示される
Entry for alias nexus successfully imported.
  • 1.5 内容を検証
$ keytool -list -keystore nexus.p12 -storetype PKCS12

#  パスワードを求められるので「password」と入力
Enter keystore password:
# 問題なければ下記のように表示される
Keystore type: PKCS12
Keystore provider: SunJSSE
Your keystore contains 1 entry
  • 1.6 各fileが作成されたかの確認
ssl $ ls
→ keystore.jks  nexus.cert  nexus.p12

  • 参考
    今迄入力したパスワードは、jetty-https.xmlの中に記載されています。
    基本何もいじっていなければデフォルトで「password」と設定されている。
cat /opt/nexus/etc/jetty/jetty-https.xml

jetty-https.xmlの下記の画像の部分記載されている
image.png


2. Nexusの設定ファイルを修正

  • 2.1 nexus-default.properties fileを編集
# viコマンドでnexus-default.properties を編集
$ sudo vi /opt/nexus/etc/nexus-default.properties

# 下記をファイルの中に追記する

application-port-ssl=443

# ${jetty.etc}/jetty-https.xmlをこの中に追記する
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-https.xml,${jetty.etc}/jetty-requestlog.xml

# 1.2で作成したkeystore.jksのパスを記入
ssl.etc=/opt/nexus/etc/ssl

下記の画像のように上記の3つを追記する。
image.png

  • 2.2 jetty-https.xmlの中身確認
$ cat /opt/nexus/etc/jetty/jetty-https.xml

# jetty-https.xmlの中のKeyStorePathが下記のようになっているか
# 2.1で作成したkeystore.jksのパスの変数がProperty nameに記載されているか
# 1.2で作成したkeystore.jksのファイル名が記載されているか

<Set name="KeyStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
<Set name="TrustStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>

image.png


3. 443ポートに接続させる

443ポートは、ウェルノウンポートなのでバインドしようとするエラーに
なりますので、カーネルのパラメータの設定を変更する必要があります。

また、再起動等しても設定が消えないように、/etc/sysctl.confに追記する

# 443のポートを使えるようにする
$ sysctl -w net.ipv4.ip_unprivileged_port_start=443 >> /etc/sysctl.conf

# 設定されたか確認
$ sysctl -a |grep 443
→ net.ipv4.ip_unprivileged_port_start = 443

# /etc/sysctl.confに記入されたか確認
cat /etc/sysctl.conf
→ # sysctl settings are defined through files in
  # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
  #
  # Vendors settings live in /usr/lib/sysctl.d/.
  # To override a whole file, create a new file with the same in
  # /etc/sysctl.d/ and put new settings there. To override
  # only specific settings, add a file with a lexically later
  # name in /etc/sysctl.d/ and put new settings there.
  #
  # For more information, see sysctl.conf(5) and sysctl.d(5).
  net.ipv4.ip_unprivileged_port_start = 443


4. 再起動

$ sudo systemctl restart nexus

5. ステータス確認

$ sudo systemctl status nexus.service

→ active (running) である事を確認
● nexus.service - nexus service
     Loaded: loaded (/etc/systemd/system/nexus.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2023-05-02 07:36:14 UTC; 11min ago
    Process: 689 ExecStart=/opt/nexus/bin/nexus start (code=exited, status=0/SUCCESS)
   Main PID: 1020 (java)
      Tasks: 77 (limit: 24517)
     Memory: 2.2G
        CPU: 1min 23.601s
     CGroup: /system.slice/nexus.service


後はブラウザ上で、「https://IPアドレス」
でアクセスできることを確認する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0