ラベル OpenVPN の投稿を表示しています。 すべての投稿を表示
ラベル OpenVPN の投稿を表示しています。 すべての投稿を表示

2025年9月17日水曜日

nifcloud の Gitlab インスタンスにリモートアクセスVPNゲートウェイ経由でプライベートIPアドレスでアクセスする方法

nifcloud の Gitlab インスタンスにリモートアクセスVPNゲートウェイ経由でプライベートIPアドレスでアクセスする方法

概要

前回EasyRSAを使って nifcloud のリモートアクセスVPNゲートウェイを接続しました
今回は nifcloud 側に Gitlab インスタンスにプライベートIPを割り当て割り当てたプライベートIPを使ってリモートアクセスVPNゲートウェイ経由でアクセスしてみます

環境

  • macOS 15.6.1
    • OpenVPN Connect-3.7.1

Gitlab インスタンスにプライベートIPを割り振る

前回リモートアクセスVPNゲートウェイ用に作成したPVLANを使って Gitlab インスタンスにプライベートIPを割り振ります

Gitlab インスタンスのファイアウォールに macOS からのアクセスを許可する

macOS 側の IP 帯は 192.168.1.0/24 なのでこれを追加します
また VPN 経由の場合 192.168 から 172.16 に変換されてアクセスも来るので PVLAN の 172.16.0.0/16 も許可します

確認用で ICMP を追加していますがこれはなくても OK です

macOS 側の hosts ファイルを編集する

プライベートIPでアクセスできるようにします

  • sudo vim /private/etc/hosts
172.16.0.100    xxx.jp-east-1.gitlab.devops.nifcloud.com

OpenVPN Connect を使って VPN 接続する

前回登録したプロフィールを使って接続します

アクセスできるか確認する

hosts に登録した FQDN でアクセスできるか確認しましょう
問題なくアクセスできれば Gitlab ログインの画面が表示されます

あくせすできない場合は ping を打ってみたり Gitlab 側のファイアウォールの設定などを確認しましょう

FQDN でうまくアクセスできない場合は

macOS の場合 /private/etc/hosts に書いた情報を優先的に使ってくれるのですがキャッシュがあるとそれが更に優先されます

一度キャッシュを削除してから再度アクセスしてみてください

  • sudo dscacheutil -flushcache
  • sudo killall -HUP mDNSResponde

以下でプライベートIPになることを確認しましょう

  • dscacheutil -q host -a name xxx.jp-east-1.gitlab.devops.nifcloud.com
name: xxx.jp-east-1.gitlab.devops.nifcloud.com
ip_address: 172.16.0.100

最後に

nifcloud のリモートアクセスVPNゲートウェイを使って Gitlab インスタンスにプライベート IP でアクセスしてみました

hosts ファイルを編集する必要があるのが面倒ですそればっかりはどうしようもないかなと思います

2025年9月16日火曜日

nifcloud のリモートアクセスVPNゲートウェイを使って macOS とニフクラをVPN接続する

nifcloud のリモートアクセスVPNゲートウェイを使って macOS とニフクラをVPN接続する

概要

試してみたのでメモです
macOS 側は OpenVPN Connect を使います

環境

  • macOS 15.6.1
    • OpenVPN Connect-3.7.1

事前準備

こちらの記事を参考にサーバ証明書とクライアント証明書を作成しておきましょう
また作成したサーバ証明書は nifcloud 側にアップロードしておきます

PVLAN 作成

作成時の設定ポイントは以下です
この CIDR は接続元の macOS 側で払い出されているプライベート IP 帯と被らないようにしましょう
なお今回 macOS 側は 192.168.1.0/24 がプライベート IP 帯になります

  • CIDR -> 172.16.0.0/16

リモートアクセスVPNゲートウェイの作成

作成時の設定ポイントは以下です
IP アドレスは nifcloud 側に作成した PVLAN 上の CIDR 範囲から指定します
ネットワークプールのCIDR は macOS 側のプライベート IP 帯を指定します
サーバ証明書は Let’s Encrypt で取得した fullchain.pem を指定します
なので CA 証明書は fullchain.pem から分割した CA 証明書を nifcloud にアップロードして指定します

  • IPアドレス -> 172.16.0.1
  • ネットワークプールのCIDR -> 192.168.1.0/24
  • サーバー証明書 -> アップロードしたサーバ証明書
  • CA 証明書 -> アップロードしたCA証明書

ユーザの追加

リモートアクセスVPNゲートウェイにユーザを追加します
作成したリモートアクセスVPNゲートウェイユーザを選択しプルダウンから「ユーザ作成」で追加します

好きなユーザ名パスワードで作成しましょう
なおこのユーザ名とパスワードは macOS から接続時に使います

クライアント設定ファイルのダウンロード

作成したリモートアクセスVPNゲートウェイユーザを選択し「クライアント設定ファイル->ダウンロード」でダウンロードできます

これを使って OpenVPN Connect から接続します

ovpn の編集

nifcloud からダウンロードした ovpn ファイルはそのままでは使えないので編集します
事前に作成したクライアント証明書、クライアント証明書キー、CA証明書の情報をダウンロードした ovpn ファイルを開いて編集します

  • <cert></cert> タグの追加
  • <key></key> タグの追加
  • <ca></ca> タグの追加
  • 各種 [inline] のある行の削除

OpenVPN Connect インストール

  • brew install openvpn-connect

OpenVPN Connect を使って接続テスト

ダウンロードした ovpn ファイルを編集後にダブルクリックすると勝手に設定を取り込んでくれます
あとは接続し先ほど作成したユーザ名とパスワードで接続できるか確認しましょう

上記のようになれば接続完了です

最後に

nifcloud のリモートアクセスVPNゲートウェイを使って macOS から VPN 接続してみました
あとは nifcloud 上に作成した PVLAN に接続するインスタンスを作成しプライベート IP アドレスを付与すれば自宅の Mac からプライベート IP で nifcloud 上のインスタンスにアクセスできるようになります

参考サイト

2025年9月15日月曜日

macOS で EasyRSA を使ってサーバ証明書とクライアント証明書を作成する

macOS で EasyRSA を使ってサーバ証明書とクライアント証明書を作成する

概要

OpenVPN で接続するために使います
ちゃんとした認証局(CA)からサーバ証明書もクライアント証明書も発行できる場合はそちらを使いましょう

環境

  • macOS 15.6.1
  • EasyRSA 3.2.4

インストール

  • brew install easy-rsa

初期化

  • easyrsa init-pki
Notice
------
'init-pki' complete; you may now create a CA or requests.

Your newly created PKI dir is:
* /opt/homebrew/etc/easy-rsa/pki

Using Easy-RSA configuration:
* undefined

認証局の作成

  • easyrsa build-ca

パスワードと Common Name を入力します

Enter New CA Key Passphrase: 
Passphrase must be at least 4 characters!

Enter New CA Key Passphrase: 

Confirm New CA Key Passphrase: 

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:ytest

Notice
------
CA creation complete. Your new CA certificate is at:
* /opt/homebrew/etc/easy-rsa/pki/ca.crt

Build-ca completed successfully.

サーバ証明書

  • easyrsa gen-req server nopass

Common Name を入力します

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [server]:vpn.ytest

Notice
------
Private-Key and Public-Certificate-Request files created.
Your files are:
* req: /opt/homebrew/etc/easy-rsa/pki/reqs/server.req
* key: /opt/homebrew/etc/easy-rsa/pki/private/server.key

このままだと使えないのでCAで署名します

  • easyrsa sign-req server server

Confirm requested details は yes にします
あとは先程設定した認証局用のパスワードを入力すれば server.crt が作成されます

Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.
You are about to sign the following certificate:

  Requested CN:     'vpn.ytest'
  Requested type:   'server'
  Valid for:        '825' days


subject=
    commonName                = vpn.ytest

Type the word 'yes' to continue, or any other input to abort.
  Confirm requested details: yes

Using configuration from /opt/homebrew/etc/easy-rsa/pki/96bd4afc/temp.02
Enter pass phrase for /opt/homebrew/etc/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'vpn.ytest'
Certificate is to be certified until Dec 15 07:19:19 2027 GMT (825 days)

Write out database with 1 new entries
Database updated

WARNING
=======
INCOMPLETE Inline file created:
* /opt/homebrew/etc/easy-rsa/pki/inline/private/server.inline


Notice
------
Certificate created at:
* /opt/homebrew/etc/easy-rsa/pki/issued/server.crt

OpenVPN サーバに設定するのは以下の3つです

  • サーバ証明書 /opt/homebrew/etc/easy-rsa/pki/issued/server.crt
  • サーバ秘密鍵 /opt/homebrew/etc/easy-rsa/pki/private/server.key
  • CA証明書 /opt/homebrew/etc/easy-rsa/pki/ca.crt

クライアント証明書

  • easyrsa gen-req client1 nopass

Common Name を入力します

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [client1]:macos.ytest

Notice
------
Private-Key and Public-Certificate-Request files created.
Your files are:
* req: /opt/homebrew/etc/easy-rsa/pki/reqs/client1.req
* key: /opt/homebrew/etc/easy-rsa/pki/private/client1.key

あとはサーバ証明書同様 CA で署名します
yes と CA のパスワードを入力して完了です

  • easyrsa sign-req client client1
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.
You are about to sign the following certificate:

  Requested CN:     'macos.ytest'
  Requested type:   'client'
  Valid for:        '825' days


subject=
    commonName                = macos.ytest

Type the word 'yes' to continue, or any other input to abort.
  Confirm requested details: yes

Using configuration from /opt/homebrew/etc/easy-rsa/pki/25c39202/temp.02
Enter pass phrase for /opt/homebrew/etc/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'macos.ytest'
Certificate is to be certified until Dec 15 07:25:17 2027 GMT (825 days)

Write out database with 1 new entries
Database updated

WARNING
=======
INCOMPLETE Inline file created:
* /opt/homebrew/etc/easy-rsa/pki/inline/private/client1.inline


Notice
------
Certificate created at:
* /opt/homebrew/etc/easy-rsa/pki/issued/client1.crt

OpenVPN Connect のクライアント側には以下の3つのファイルを使います

  • クライアント証明書 /opt/homebrew/etc/easy-rsa/pki/issued/client1.crt
  • クライアントキー /opt/homebrew/etc/easy-rsa/pki/private/client1.key
  • CA証明書 /opt/homebrew/etc/easy-rsa/pki/ca.crt

最後に

OpenVPN は基本的にはサーバ証明書とクライアント証明書の2つが必要になります
Let’sEncrypt など無料の証明書は基本的に「サーバ証明書」しか発行できないのでクライアント認証などでクライアント証明書が必要になる場合はEasyRSAなどを使ってオレオレ証明書を作成する必要があります

参考サイト