gNOIサービスを設定する
リモートネットワーク管理システムを、ネットワークデバイス上でgNOI操作を実行できるgRPCクライアントとして設定します。
gRPCネットワーク操作インターフェイス(gNOI)は、gRPCリモートプロシージャコール(gRPC)フレームワークを使用して、ネットワークデバイス上で操作を実行します。ネットワーク管理システムには、gRPCスタックがインストールされている必要があります。
OpenConfig は、 gNOI サービスのプロト定義ファイルを定義します。プロト定義ファイルは、特定のサービスの操作(RPC)とデータ構造(メッセージ)を定義します。定義は言語に依存しません。gRPCは、サービス操作を実行するための多くの異なる言語の使用をサポートしています。選択した言語用にプロト定義ファイルをコンパイルする必要があります。次に、コンパイルされたファイル内のオブジェクト(クラス、関数、メソッドなど)を使用して、ネットワークデバイス上のgRPCサーバーに接続し、目的の操作を実行するアプリケーションを作成します。
サポートされているさまざまな言語でgRPCを使用する方法については、 gRPCのドキュメントを参照してください。次のセクションでは、gRPC クライアントを設定し、Python 用の gNOI プロト定義ファイルをダウンロードしてコンパイルするためのサンプル コマンドを提供します。オペレーティングシステム、環境、および選択したgRPC言語に適したコマンドを使用する必要があります。
始める前に:
- gRPCサービスの設定の説明に従ってgRPCサーバーを設定します。
gRPC クライアントを設定する
gNOIは、gRPCフレームワークを使用して、ネットワークデバイス上で操作を実行します。gRPCは、多くの異なる言語の使用をサポートしています。選択した言語を使用してgNOI操作を実行する前に、ネットワーク管理システムにgRPCスタックをインストールする必要があります。
たとえば、Ubuntu 20.04 LTS を実行しているネットワーク管理システムに Python 用 gRPC スタックをインストールするには (必要に応じて sudo を使用します)。
プロト定義ファイルをコンパイルする
gRPC は、多くの言語の使用をサポートしています。ネットワークデバイス上でgRPC操作を実行するには、選択した言語用のそれぞれのプロト定義ファイルと依存ファイルをコンパイルする必要があります。OpenConfig は、 OpenConfig GitHub リポジトリに必要なプロト定義ファイルを提供します。プロトコルバッファコンパイラ(protoc または同等のアプリケーション)を使用して、 .proto ファイルをコンパイルします。
この設定では、目的の依存 .proto ファイルをすべてディレクトリにコピーし、相対インポート ステートメントを使用するようにファイルを更新してから、ファイルをコンパイルするスクリプトを実行します。
Python 用の gNOI プロト定義ファイルをダウンロードしてコンパイルするには:
gNOIアプリケーションの作成
プロト定義ファイルをコンパイルした後、コンパイルされたファイル内のオブジェクトを使用するアプリケーションを作成します。アプリケーションは、ネットワークデバイス上のgRPCサーバーに接続し、目的の操作を実行します。このセクションでは、2 つのサンプル Python モジュールを提供し、それぞれのセクションで説明します。
grpc_channel.py
grpc_channel.py Python モジュールは、選択した 認証、サーバーのみ、または相互のメソッドに対して提供された引数を使用して gRPC チャネルを作成するサンプル関数を提供します。
import grpc
from os.path import isfile
def grpc_authenticate_channel_mutual(server, port, root_ca_cert="", client_key="", client_cert=""):
if not isfile(root_ca_cert):
raise Exception("Error: root_ca_cert file does not exist")
if (client_key == "") or (not isfile(client_key)):
raise Exception(
"Error: client_key option is missing or target file does not exist")
elif (client_cert == "") or (not isfile(client_cert)):
raise Exception(
"Error: client_cert option is empty or target file does not exist")
print("Creating channel")
creds = grpc.ssl_channel_credentials(open(root_ca_cert, 'rb').read(),
open(client_key, 'rb').read(),
open(client_cert, 'rb').read())
channel = grpc.secure_channel('%s:%s' % (server, port), creds)
return channel
def grpc_authenticate_channel_server_only(server, port, root_ca_cert=""):
if isfile(root_ca_cert):
print("Creating channel")
creds = grpc.ssl_channel_credentials(open(root_ca_cert, 'rb').read(),
None,
None)
channel = grpc.secure_channel('%s:%s' % (server, port), creds)
return channel
else:
raise Exception("root_ca_cert file does not exist")
gnoi_connect_cert_auth_mutual.py
gnoi_connect_cert_auth_mutual.py Python アプリケーションは、指定された gRPC サーバーで gRPC チャネルを確立し、単純な gNOI Systemサービス操作を実行します。ユーザーは、必要な接続情報と相互認証情報をアプリケーションへの入力として提供します。アプリケーションは、grpc_channel.pyモジュールで適切な機能を呼び出して、クライアントとサーバー間のgRPCチャネルを確立します。アプリケーションがgRPCチャネルを正常に確立すると、単純なシステムサービスRPCを実行して、ネットワークデバイスから時間を取得します。
"""gRPC gNOI Time request utility."""
from __future__ import print_function
import argparse
import logging
from getpass import getpass
import system_pb2
import system_pb2_grpc
from grpc_channel import grpc_authenticate_channel_mutual
def get_args(parser):
parser.add_argument('--server',
dest='server',
type=str,
default='localhost',
help='Server IP or name. Default is localhost')
parser.add_argument('--port',
dest='port',
nargs='?',
type=int,
default=32767,
help='The server port. Default is 32767')
parser.add_argument('--client_key',
dest='client_key',
type=str,
default='',
help='Full path of the client private key. Default ""')
parser.add_argument('--client_cert',
dest='client_cert',
type=str,
default='',
help='Full path of the client certificate. Default ""')
parser.add_argument('--root_ca_cert',
dest='root_ca_cert',
required=True,
type=str,
help='Full path of the Root CA certificate.')
parser.add_argument('--user_id',
dest='user_id',
required=True,
type=str,
help='User ID for RPC call credentials.')
args = parser.parse_args()
return args
def send_rpc(channel, metadata):
stub = system_pb2_grpc.SystemStub(channel)
print("Executing GNOI::System::Time RPC")
req = system_pb2.TimeRequest()
try:
response = stub.Time(request=req, metadata=metadata, timeout=60)
except Exception as e:
logging.error('Error executing RPC: %s', e)
print(e)
else:
logging.info('Received message: %s', response)
return response
def main():
parser = argparse.ArgumentParser()
args = get_args(parser)
grpc_server_password = getpass("gRPC server password for executing RPCs: ")
metadata = [('username', args.user_id),
('password', grpc_server_password)]
try:
# Establish grpc channel to network device
channel = grpc_authenticate_channel_mutual(
args.server, args.port, args.root_ca_cert, args.client_key, args.client_cert)
response = send_rpc(channel, metadata)
print("Response received: time since last epoch in nanoseconds is ", str(response))
except Exception as e:
logging.error('Received error: %s', e)
print(e)
if __name__ == '__main__':
logging.basicConfig(filename='gnoi-testing.log',
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
main()
アプリケーションの実行
gNOI サービス操作を実行するアプリケーションを作成したら、アプリケーションを実行し、必要な引数を指定します。次の例では、前のセクションで提供されたスクリプトを使用して、ネットワークデバイス上のgRPCサーバーに接続し、時間をリクエストします。gRPC サーバーは、クライアントの証明書を要求して検証するように構成されています。
-
相互認証のために、クライアントはサーバーのIPアドレス、gRPCポート、ルートCA証明書に加えて、独自のキーとPEM形式のX.509公開キー証明書を提供します。クライアントは RPC 呼び出しの資格情報も提供します。
user_id引数はユーザー名を提供し、アプリケーションはユーザーパスワードの入力を求めます。lab@gnoi-client:~/src/proto$ python3 gnoi_connect_cert_auth_mutual.py --server 10.53.52.169 --port 32767 --root_ca_cert /etc/pki/certs/serverRootCA.crt --client_key /home/lab/certs/client.key --client_cert /home/lab/certs/client.crt --user_id gnoi-user gRPC server password for executing RPCs: Creating channel Executing GNOI::System::Time RPC Response received: time since last epoch in nanoseconds is time: 1650061065769701762