🚀 جرب Zilliz Cloud، الـ Milvus المدارة بالكامل، مجاناً — تجربة أداء أسرع بـ 10 أضعاف! جرب الآن>>

milvus-logo
LFAI
الصفحة الرئيسية
  • دليل الإدارة
  • Home
  • Docs
  • دليل الإدارة

  • الأمن

  • التشفير أثناء النقل

التشفير أثناء النقل

TLS (أمان طبقة النقل) هو بروتوكول تشفير لضمان أمان الاتصال. يستخدم وكيل Milvus بروكسي Milvus مصادقة TLS أحادية الاتجاه وثنائية الاتجاه.

يصف هذا الموضوع كيفية تمكين TLS في وكيل Milvus لكل من gRPC وRESTful عمليات النقل.

TLS ومصادقة المستخدم هما نهجان مختلفان للأمان. إذا قمت بتمكين كل من مصادقة المستخدم ومصادقة TLS في نظام Milvus الخاص بك، فستحتاج إلى توفير اسم مستخدم وكلمة مرور ومسارات ملفات الشهادات. للحصول على معلومات حول كيفية تمكين مصادقة المستخدم، راجع مصادقة وصول المستخدم.

إنشاء الشهادة الخاصة بك

المتطلبات الأساسية

تأكد من تثبيت OpenSSL. إذا لم تقم بتثبيته، قم بإنشاء OpenSSL وتثبيته أولاً.

openssl version

إذا لم يكن OpenSSL غير مثبت. يمكن تثبيته باستخدام الأمر التالي في Ubuntu.

sudo apt install openssl

إنشاء الملفات

  1. قم بإنشاء الملف gen.sh.
mkdir cert && cd cert
touch gen.sh
  1. انسخ البرنامج النصي التالي إلى gen.sh.

من الضروري تكوين CommonName في الملف gen.sh. يشير CommonName إلى اسم الخادم الذي يجب أن يحدده العميل أثناء الاتصال.

gen.sh

#!/usr/bin/env sh
# your variables
Country="US"
State="CA"
Location="Redwood City"
Organization="zilliz"
OrganizationUnit="devops"
CommonName="localhost"
ExpireDays=3650 # 10 years

# generate private key for ca, server and client
openssl genpkey -quiet -algorithm rsa:2048 -out ca.key
openssl genpkey -quiet -algorithm rsa:2048 -out server.key
openssl genpkey -quiet -algorithm rsa:2048 -out client.key

# create a new ca certificate
openssl req -x509 -new -nodes -key ca.key -sha256 -days 36500 -out ca.pem \
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"

# prepare extension config for signing certificates
echo '[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS = '$CommonName > openssl.cnf

# sign server certificate with ca
openssl req -new -key server.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out server.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

# sign client certificate with ca
openssl req -new -key client.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out client.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

تعتبر المتغيرات في الملف gen.sh ضرورية لعملية إنشاء ملف طلب توقيع الشهادة. المتغيرات الخمسة الأولى هي معلومات التوقيع الأساسية، بما في ذلك البلد والولاية والموقع والمنظمة ووحدة التنظيم. يجب توخي الحذر عند تكوين CommonName حيث سيتم التحقق منها أثناء الاتصال بين العميل والخادم.

تشغيل gen.sh لإنشاء شهادة

قم بتشغيل الملف gen.sh لإنشاء الشهادة.

chmod +x gen.sh
./gen.sh

سيتم إنشاء الملفات السبعة التالية: ca.key ، ca.pem ، ca.srl ، ، server.key ، server.pem ، client.key ، client.pem.

تأكد من الاحتفاظ بالملفات ca.key ، ca.pem ، ، ca.srl آمنة من أجل تجديد شهاداتك لاحقًا. يتم استخدام الملفين server.key و server.pem من قبل الخادم، ويتم استخدام الملفين client.key و client.pem من قبل العميل.

تجديد الشهادات (اختياري)

إذا كنت ترغب في تجديد الشهادات في بعض الحالات، على سبيل المثال إذا كانت ستنتهي صلاحيتها قريباً، يمكنك استخدام البرنامج النصي التالي.

تحتاج إلى ca.key و ca.pem و ca.srl في دليل العمل الخاص بك.

renew.sh

#!/usr/bin/env sh
# your variables
Country="US"
State="CA"
Location="Redwood City"
Organization="zilliz"
OrganizationUnit="devops"
CommonName="localhost"
ExpireDays=3650 # 10 years

# generate private key for ca, server and client
openssl genpkey -quiet -algorithm rsa:2048 -out server.key
openssl genpkey -quiet -algorithm rsa:2048 -out client.key

# prepare extension config for signing certificates
echo '[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS = '$CommonName > openssl.cnf

# sign server certificate with ca
openssl req -new -key server.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out server.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

# sign client certificate with ca
openssl req -new -key client.key\
  -subj "/C=$Country/ST=$State/L=$Location/O=$Organization/OU=$OrganizationUnit/CN=$CommonName"\
  | openssl x509 -req -days $ExpireDays -out client.pem -CA ca.pem -CAkey ca.key -CAcreateserial \
    -extfile ./openssl.cnf -extensions v3_req

قم بتشغيل الملف renew.sh لإنشاء الشهادة.

chmod +x renew.sh
./renew.sh

إعداد خادم Milvus مع TLS

يوضح هذا القسم خطوات تكوين خادم Milvus مع تشفير TLS.

الإعداد لـ Docker Compose

1. تعديل تكوين خادم Milvus

لتمكين TLS الخارجي، أضف التكوينات التالية في ملف milvus.yaml:

proxy:
  http:
    # for now milvus do not support config restful on same port with grpc
    # so we set to 8080, grpc will still use 19530
    port: 8080 
tls:
  serverPemPath: /milvus/tls/server.pem
  serverKeyPath: /milvus/tls/server.key
  caPemPath: /milvus/tls/ca.pem

common:
  security:
    tlsMode: 1

المعلمات:

  • serverPemPath: المسار إلى ملف شهادة الخادم.
  • serverKeyPath: المسار إلى ملف مفتاح الخادم.
  • caPemPath: المسار إلى ملف شهادة CA.
  • tlsMode: وضع TLS للخدمة الخارجية. قيم صالحة:
    • 1: مصادقة أحادية الاتجاه، حيث يتطلب الخادم فقط شهادة ويقوم العميل بالتحقق منها. يتطلب هذا الوضع server.pem و server.key من جانب الخادم، و server.pem من جانب العميل.
    • 2: المصادقة ثنائية الاتجاه، حيث يتطلب كل من الخادم والعميل شهادات لإنشاء اتصال آمن. يتطلب هذا الوضع server.pem و server.key و ca.pem من جانب الخادم، و client.pem و client.key و ca.pem من جانب العميل.

لتمكين TLS الداخلي، أضف التكوينات التالية في الملف milvus.yaml:

internaltls:
  serverPemPath: /milvus/tls/server.pem
  serverKeyPath: /milvus/tls/server.key
  caPemPath: /milvus/tls/ca.pem

common:
  security:
    internaltlsEnabled: true 

المعلمات:

  • serverPemPath: المسار إلى ملف شهادة الخادم.
  • serverKeyPath: المسار إلى ملف مفتاح الخادم.
  • caPemPath: المسار إلى ملف شهادة CA.
  • internaltlsEnabled: ما إذا كان سيتم تمكين TLS الداخلي. في الوقت الحالي يتم دعم TLS أحادي الاتجاه فقط.

2. تعيين ملفات الشهادات إلى الحاوية

إعداد ملفات الشهادات

قم بإنشاء مجلد جديد باسم tls في نفس دليل docker-compose.yaml. انسخ server.pem و server.key و ca.pem إلى المجلد tls. ضعهم في بنية دليل على النحو التالي:

├── docker-compose.yml
├── milvus.yaml
└── tls
     ├── server.pem
     ├── server.key
     └── ca.pem

قم بتحديث تكوين Docker Compose

قم بتحرير الملف docker-compose.yaml لتعيين مسارات ملفات الشهادات داخل الحاوية كما هو موضح أدناه:

  standalone:
    container_name: milvus-standalone
    image: milvusdb/milvus:latest
    command: ["milvus", "run", "standalone"]
    security_opt:
    - seccomp:unconfined
    environment:
      ETCD_ENDPOINTS: etcd:2379
      MINIO_ADDRESS: minio:9000
    volumes:
      - ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus
      - ${DOCKER_VOLUME_DIRECTORY:-.}/tls:/milvus/tls
      - ${DOCKER_VOLUME_DIRECTORY:-.}/milvus.yaml:/milvus/configs/milvus.yaml
نشر ميلفوس باستخدام Docker Compose

قم بتنفيذ الأمر التالي لنشر ميلفوس:

sudo docker compose up -d

إعداد مشغل Milvus

ضع ملفات الشهادة في دليل العمل الخاص بك. يجب أن تبدو بنية الدليل هكذا:

├── milvus.yaml (to be created later)
├── server.pem
├── server.key
└── ca.pem

قم بإنشاء سر مع ملفات الشهادة:

kubectl create secret generic certs --from-file=server.pem --from-file=server.key --from-file=ca.pem

لتمكين TLS الخارجي، أضف التكوينات التالية في الملف milvus.yaml:

apiVersion: milvus.io/v1beta1
kind: Milvus
metadata:
  name: my-release
spec:
  config:
    proxy:
      http:
        # for now not support config restful on same port with grpc
        # so we set to 8080, grpc will still use 19530
        port: 8080 
    common:
      security:
        tlsMode: 1 # tlsMode for external service 1 for one-way TLS, 2 for Mutual TLS, 0 for disable
    tls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
  components:
    # mount the certs secret to the milvus container
    volumes:
      - name: certs
        secret:
          secretName: certs
    volumeMounts:
      - name: certs
        mountPath: /certs
        readOnly: true

لتمكين TLS الداخلي، أضف التكوينات التالية في الملف milvus.yaml:

تذكر استبدال الحقل internaltls.sni بالاسم الشائع في شهاداتك.

apiVersion: milvus.io/v1beta1
kind: Milvus
metadata:
  name: my-release
spec:
  config:
    proxy:
      http:
        # for now not support config restful on same port with grpc
        # so we set to 8080, grpc will still use 19530
        port: 8080 
    common:
      security:
        internaltlsEnabled: true # whether to enable internal tls
    # Configure tls certificates path for internal service
    internaltls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
      sni: localhost # the CommonName in your certificates
  components:
    # mount the certs secret to the milvus container
    volumes:
      - name: certs
        secret:
          secretName: certs
    volumeMounts:
      - name: certs
        mountPath: /certs
        readOnly: true

إنشاء ملف Milvus CR:

kubectl create -f milvus.yaml

الإعداد لـ Milvus Helm

ضع ملفات الشهادة في دليل العمل الخاص بك. يجب أن تبدو بنية الدليل هكذا:

├── values.yaml (to be created later)
├── server.pem
├── server.key
└── ca.pem

إنشاء سر مع ملفات الشهادة:

kubectl create secret generic certs --from-file=server.pem --from-file=server.key --from-file=ca.pem

لتمكين TLS الخارجي، أضف التكوينات التالية في الملف values.yaml:

extraConfigFiles:
  user.yaml: |+
    proxy:
      http:
        # for now not support config restful on same port with grpc
        # so we set to 8080, grpc will still use 19530
        port: 8080 
    common:
      security:
        tlsMode: 1 # tlsMode for external service 1 means set to 2 to enable Mutual TLS
    # Configure tls certificates path for external service
    tls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
# mount the certs secret to the milvus container
volumes:
  - name: certs
    secret:
      secretName: certs
volumeMounts:
  - name: certs
    mountPath: /certs
    readOnly: true

لتمكين TLS الداخلي، أضف التكوينات التالية في الملف values.yaml:

تذكر استبدال الحقل internaltls.sni بالاسم الشائع في شهاداتك.

extraConfigFiles:
  user.yaml: |+
    common:
      security:
        internaltlsEnabled: true # whether to enable internal tls
    # Configure tls certificates path for internal service
    internaltls:
      serverPemPath: /certs/server.pem
      serverKeyPath: /certs/server.key
      caPemPath: /certs/ca.pem
      sni: localhost
# mount the certs secret to the milvus container
volumes:
  - name: certs
    secret:
      secretName: certs
volumeMounts:
  - name: certs
    mountPath: /certs
    readOnly: true

قم بإنشاء إصدار ميلفوس:

helm repo add milvus https://zilliztech.github.io/milvus-helm/
helm repo update milvus
helm install my-release milvus/milvus -f values.yaml

التحقق من تمكين TLS الداخلي

من الصعب التحقق من TLS الداخلي مباشرة. يمكنك التحقق من سجل Milvus لمعرفة ما إذا كان TLS الداخلي ممكّنًا.

في سجل Milvus، يجب أن ترى الرسالة التالية إذا تم تمكين TLS الداخلي:

[...date time...] [INFO] [utils/util.go:56] ["Internal TLS Enabled"] [value=true]

الاتصال بخادم Milvus باستخدام TLS

بالنسبة لتفاعلات SDK، استخدم الإعدادات التالية اعتماداً على وضع TLS.

اتصال TLS أحادي الاتجاه

قم بتوفير المسار إلى server.pem وتأكد من تطابق server_name مع CommonName الذي تم تكوينه في الشهادة.

from pymilvus import MilvusClient

client = MilvusClient(
    uri="https://localhost:19530",
    secure=True,
    server_pem_path="path_to/server.pem",
    server_name="localhost"
)

اتصال TLS ثنائي الاتجاه

قم بتوفير المسارات إلى client.pem و client.key و ca.pem وتأكد من أن server_name يطابق CommonName المكوّن في الشهادة.

from pymilvus import MilvusClient

client = MilvusClient(
    uri="https://localhost:19530",
    secure=True,
    client_pem_path="path_to/client.pem",
    client_key_path="path_to/client.key",
    ca_pem_path="path_to/ca.pem",
    server_name="localhost"
)

انظر example_tls1.py و example_tls2.py لمزيد من المعلومات.

الاتصال بخادم Milvus RESTful مع TLS

بالنسبة لواجهات برمجة تطبيقات RESTful، يمكنك التحقق من TLS باستخدام الأمر curl.

اتصال TLS أحادي الاتجاه

curl --cacert path_to/ca.pem https://localhost:8080/v2/vectordb/collections/list

اتصال TLS ثنائي الاتجاه

curl --cert path_to/client.pem --key path_to/client.key --cacert path_to/ca.pem https://localhost:8080/v2/vectordb/collections/list

جرب Managed Milvus مجاناً

Zilliz Cloud خالي من المتاعب، ويعمل بواسطة Milvus ويعمل بسرعة 10 أضعاف.

ابدأ
التعليقات

هل كانت هذه الصفحة مفيدة؟