使用fabric-ca生成kafka服務(wù)端和客戶端證書(shū)

前面我們提到了如何使用openssl工具命令行生成測(cè)試用kafka服務(wù)端和客戶端證書(shū)楚里;在這里我們使用另一種方式,即fabric-ca來(lái)搭建一個(gè)CA服務(wù)器颈渊,為kafka集群提供證書(shū)服務(wù)淋昭。

  1. 定義fabric-ca-server-config.yaml文件

為了簡(jiǎn)化俐填,因?yàn)槲覀冎皇切枰蒚LS證書(shū),而不需要fabric內(nèi)部使用的特性翔忽,所以忽略了fabric ca關(guān)于fabric網(wǎng)絡(luò)屬性的配置信息英融。

# Server's listening port (default: 7054)
port: 7054

# Enables debug logging (default: false)
debug: false

#############################################################################
#  TLS section for the server's listening port
#
#  The following types are supported for client authentication: NoClientCert,
#  RequestClientCert, RequireAnyClientCert, VerifyClientCertIfGiven,
#  and RequireAndVerifyClientCert.
#
#  Certfiles is a list of root certificate authorities that the server uses
#  when verifying client certificates.
#############################################################################
tls:
  # Enable TLS (default: false)
  enabled: false
  # TLS for the server's listening port
  certfile: tls-ca-cert.pem
  keyfile: tls-ca-key.pem
  clientauth:
    type: noclientcert
    certfiles:

#############################################################################
#  The CA section contains information related to the Certificate Authority
#  including the name of the CA, which should be unique for all members
#  of a blockchain network.  It also includes the key and certificate files
#  used when issuing enrollment certificates (ECerts) and transaction
#  certificates (TCerts).
#  The chainfile (if it exists) contains the certificate chain which
#  should be trusted for this CA, where the 1st in the chain is always the
#  root CA certificate.
#############################################################################
ca:
  # Name of this CA
  name: ca
  # Key file (default: ca-key.pem)
  keyfile: ca-key.pem
  # Certificate file (default: ca-cert.pem)
  certfile: ca-cert.pem
  # Chain file (default: chain-cert.pem)
  chainfile: ca-chain.pem

#############################################################################
#  The registry section controls how the fabric-ca-server does two things:
#  1) authenticates enrollment requests which contain a username and password
#     (also known as an enrollment ID and secret).
#  2) once authenticated, retrieves the identity's attribute names and
#     values which the fabric-ca-server optionally puts into TCerts
#     which it issues for transacting on the Hyperledger Fabric blockchain.
#     These attributes are useful for making access control decisions in
#     chaincode.
#  There are two main configuration options:
#  1) The fabric-ca-server is the registry.
#     This is true if "ldap.enabled" in the ldap section below is false.
#  2) An LDAP server is the registry, in which case the fabric-ca-server
#     calls the LDAP server to perform these tasks.
#     This is true if "ldap.enabled" in the ldap section below is true,
#     which means this "registry" section is ignored.
#############################################################################
registry:
  # Maximum number of times a password/secret can be reused for enrollment
  # (default: -1, which means there is no limit)
  maxenrollments: -1

  # Contains identity information which is used when LDAP is disabled
  identities:
     - name: admin
       pass: 123456
       type: client
       affiliation: ""
       maxenrollments: -1
       attrs:
          hf.Registrar.Roles: "client"
          hf.Registrar.DelegateRoles: "client"
          hf.Registrar.Attributes: "*"
          hf.Revoker: true
          hf.IntermediateCA: true

#############################################################################
#  Database section
#  Supported types are: "sqlite3", "postgres", and "mysql".
#  The datasource value depends on the type.
#  If the type is "sqlite3", the datasource value is a file name to use
#  as the database store.  Since "sqlite3" is an embedded database, it
#  may not be used if you want to run the fabric-ca-server in a cluster.
#  To run the fabric-ca-server in a cluster, you must choose "postgres"
#  or "mysql".
#############################################################################
db:
  type: sqlite3
  datasource: fabric-ca-server.db
  tls:
      enabled: false
      certfiles:
        - db-server-cert.pem
      client:
        certfile: db-client-cert.pem
        keyfile: db-client-key.pem

#############################################################################
#  LDAP section
#  If LDAP is enabled, the fabric-ca-server calls LDAP to:
#  1) authenticate enrollment ID and secret (i.e. username and password)
#     for enrollment requests;
#  2) To retrieve identity attributes
#############################################################################
ldap:
   # Enables or disables the LDAP client (default: false)
   # If this is set to true, the "registry" section is ignored.
   enabled: false
   # The URL of the LDAP server
   url: ldap://<adminDN>:<adminPassword>@<host>:<port>/<base>
   tls:
      certfiles:
        - ldap-server-cert.pem
      client:
         certfile: ldap-client-cert.pem
         keyfile: ldap-client-key.pem

#############################################################################
#  Affiliation section
#############################################################################
affiliations:
   org1:
      - department1
      - department2
   org2:
      - department1

#############################################################################
#  Signing section
#
#  The "default" subsection is used to sign enrollment certificates;
#  the default expiration ("expiry" field) is "8760h", which is 1 year in hours.
#
#  The "ca" profile subsection is used to sign intermediate CA certificates;
#  the default expiration ("expiry" field) is "43800h" which is 5 years in hours.
#  Note that "isca" is true, meaning that it issues a CA certificate.
#  A maxpathlen of 0 means that the intermediate CA cannot issue other
#  intermediate CA certificates, though it can still issue end entity certificates.
#  (See RFC 5280, section 4.2.1.9)
#############################################################################
signing:
    default:
      usage:
        - digital signature
      expiry: 8760h
    profiles:
      ca:
         usage:
           - cert sign
         expiry: 43800h
         caconstraint:
           isca: true
           maxpathlen: 0

###########################################################################
#  Certificate Signing Request (CSR) section.
#  This controls the creation of the root CA certificate.
#  The expiration for the root CA certificate is configured with the
#  "ca.expiry" field below, whose default value is "131400h" which is
#  15 years in hours.
#  The pathlength field is used to limit CA certificate hierarchy as described
#  in section 4.2.1.9 of RFC 5280.
#  Examples:
#  1) No pathlength value means no limit is requested.
#  2) pathlength == 1 means a limit of 1 is requested which is the default for
#     a root CA.  This means the root CA can issue intermediate CA certificates,
#     but these intermediate CAs may not in turn issue other CA certificates
#     though they can still issue end entity certificates.
#  3) pathlength == 0 means a limit of 0 is requested;
#     this is the default for an intermediate CA, which means it can not issue
#     CA certificates though it can still issue end entity certificates.
###########################################################################
csr:
   cn: kafkaca
   names:
      - C: US
        O: example
        OU: oneorg
   ca:
      expiry: 131400h
      pathlength: 1

#############################################################################
# BCCSP (BlockChain Crypto Service Provider) section is used to select which
# crypto library implementation to use
#############################################################################

bccsp:
    default: SW
    sw:
        hash: SHA2
        security: 256
        filekeystore:
            # The directory used for the software file-based keystore
            keystore: msp/keystore

#############################################################################
# Multi CA section
#
# Each Fabric CA server contains one CA by default.  This section is used
# to configure multiple CAs in a single server.
#
# 1) --cacount <number-of-CAs>
# Automatically generate <number-of-CAs> non-default CAs.  The names of these
# additional CAs are "ca1", "ca2", ... "caN", where "N" is <number-of-CAs>
# This is particularly useful in a development environment to quickly set up
# multiple CAs.
#
# 2) --cafiles <CA-config-files>
# For each CA config file in the list, generate a separate signing CA.  Each CA
# config file in this list MAY contain all of the same elements as are found in
# the server config file except port, debug, and tls sections.
#
# Examples:
# fabric-ca-server start -b admin:adminpw --cacount 2
#
# fabric-ca-server start -b admin:adminpw --cafiles ca/ca1/fabric-ca-server-config.yaml
# --cafiles ca/ca2/fabric-ca-server-config.yaml
#
#############################################################################

cacount:

cafiles:

#############################################################################
# Intermediate CA section
#
# The relationship between servers and CAs is as follows:
#   1) A single server process may contain or function as one or more CAs.
#      This is configured by the "Multi CA section" above.
#   2) Each CA is either a root CA or an intermediate CA.
#   3) Each intermediate CA has a parent CA which is either a root CA or another intermediate CA.
#
# This section pertains to configuration of #2 and #3.
# If the "intermediate.parentserver.url" property is set,
# then this is an intermediate CA with the specified parent
# CA.
#
# parentserver section
#    url - The URL of the parent server
#    caname - Name of the CA to enroll within the server
#
# enrollment section used to enroll intermediate CA with parent CA
#    profile - Name of the signing profile to use in issuing the certificate
#    label - Label to use in HSM operations
#
# tls section for secure socket connection
#   certfiles - PEM-encoded list of trusted root certificate files
#   client:
#     certfile - PEM-encoded certificate file for when client authentication
#     is enabled on server
#     keyfile - PEM-encoded key file for when client authentication
#     is enabled on server
#############################################################################
intermediate:
  parentserver:
    url:
    caname:

  enrollment:
    hosts:
    profile:
    label:

  tls:
    certfiles:
    client:
      certfile:
      keyfile:

在上述yaml文件中:

  • 定義bootstrap用戶admin/123456
  • 定義hf.Registrar.Roles只有一個(gè)值"client"。
  1. 定義docker-compose.yaml文件
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2'

networks:
  byfn:

services:

  kafkaca.oneorg.example.com:
    container_name: kafkaca.oneorg.example.com
    image: hyperledger/fabric-ca
    working_dir: /work
    environment:
      - FABRIC_CA_SERVER_HOME=/work
    ports:
      - "7054"
    command: sh -c 'fabric-ca-server start -c /work/fabric-ca-server-config.yaml'
    volumes:
      - ./kafkaca:/work
    networks:
      - byfn
  1. 啟動(dòng)fabric CA
$ docker-compose -f docker-compose-ca.yaml up
  1. Enrolling the bootstrap identity
fabric-ca-client enroll \
    -M /work/msp \
    -u http://admin:123456@kafkaca.oneorg.example.com:7054 \
    --home /work

admin是內(nèi)置的bootstrap賬號(hào)歇式,必須先enroll得到msp證書(shū)驶悟,才能執(zhí)行后面的新identity注冊(cè)register操作。運(yùn)行結(jié)果是:

  1. 會(huì)在/work目錄下產(chǎn)生fabric-ca-client.yaml文件贬丛。
  2. 會(huì)在/work/msp目錄下面生成一份admin的證書(shū)撩银。
  1. Registering a new identity
fabric-ca-client register \
    --id.name kafka \
    --id.secret "123456" \
    -u http://admin:123456@kafkaca.oneorg.example.com:7054 \
    --home /work

注冊(cè)一個(gè)kafka的identity。

  1. Enrolling a kafka identity
fabric-ca-client enroll \
    --enrollment.profile tls \
    --csr.names "C=US,O=example,OU=oneorg" \
    --csr.hosts "*.oneorg.example.com" \
    -M /work/kafka \
    -u http://kafka:123456@kafkaca.oneorg.example.com:7054 \
    --home /work

簽出前面注冊(cè)的kafka的TLS證書(shū)豺憔,放在目錄/work/kafka下面额获;我們驗(yàn)證簽出的kafka的證書(shū)信息:

$ openssl x509 -text -noout -in kafka/signcerts/cert.pem 
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            28:66:dc:e0:2a:8e:1a:75:9d:4f:90:36:f4:51:5c:3c:9b:d6:8a:5f
    Signature Algorithm: ecdsa-with-SHA256
        Issuer: C=US, O=example, OU=oneorg, CN=kafkaca
        ...
        Subject: C=US, O=example, OU=client, CN=kafka
        ...
        X509v3 extensions:
             ...
            X509v3 Subject Alternative Name: 
                DNS:*.oneorg.example.com
            1.2.3.4.5.6.7.8.1: 
                {"attrs":{"hf.Affiliation":"","hf.EnrollmentID":"kafka","hf.Type":"client"}}
            ...
  1. 接下來(lái)就可為多個(gè)kafka實(shí)例簽出多份TLS證書(shū)
  1. 也可以為kafka的客戶端注冊(cè)并且簽出TLS證書(shū)

類(lèi)似如下:

# register kafka client identity
fabric-ca-client register \
    --id.name kafkaclient \
    --id.secret "123456" \
    -u http://admin:123456@kafkaca.oneorg.example.com:7054 \
    --home /work

# enroll kafka client identity
fabric-ca-client enroll \
    --enrollment.profile tls \
    --csr.names "C=US,O=example,OU=oneorg" \
    --csr.hosts "*.oneorg.example.com" \
    -M /work/kafkaclient \
    -u http://kafkaclient:123456@kafkaca.oneorg.example.com:7054 \
    --home /work

你可以調(diào)整其中的參數(shù)够庙。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市抄邀,隨后出現(xiàn)的幾起案子耘眨,更是在濱河造成了極大的恐慌,老刑警劉巖境肾,帶你破解...
    沈念sama閱讀 212,686評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剔难,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡奥喻,警方通過(guò)查閱死者的電腦和手機(jī)偶宫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,668評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)环鲤,“玉大人纯趋,你說(shuō)我怎么就攤上這事±淅耄” “怎么了吵冒?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,160評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)西剥。 經(jīng)常有香客問(wèn)我痹栖,道長(zhǎng),這世上最難降的妖魔是什么瞭空? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,736評(píng)論 1 284
  • 正文 為了忘掉前任揪阿,我火速辦了婚禮,結(jié)果婚禮上匙铡,老公的妹妹穿的比我還像新娘图甜。我一直安慰自己碍粥,他們只是感情好鳖眼,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,847評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著嚼摩,像睡著了一般钦讳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上枕面,一...
    開(kāi)封第一講書(shū)人閱讀 50,043評(píng)論 1 291
  • 那天愿卒,我揣著相機(jī)與錄音,去河邊找鬼潮秘。 笑死琼开,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的枕荞。 我是一名探鬼主播柜候,決...
    沈念sama閱讀 39,129評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼搞动,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了渣刷?” 一聲冷哼從身側(cè)響起鹦肿,我...
    開(kāi)封第一講書(shū)人閱讀 37,872評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎辅柴,沒(méi)想到半個(gè)月后箩溃,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,318評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡碌嘀,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,645評(píng)論 2 327
  • 正文 我和宋清朗相戀三年涣旨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片股冗。...
    茶點(diǎn)故事閱讀 38,777評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡开泽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出魁瞪,到底是詐尸還是另有隱情穆律,我是刑警寧澤,帶...
    沈念sama閱讀 34,470評(píng)論 4 333
  • 正文 年R本政府宣布导俘,位于F島的核電站峦耘,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏旅薄。R本人自食惡果不足惜辅髓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,126評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望少梁。 院中可真熱鬧洛口,春花似錦、人聲如沸凯沪。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,861評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)妨马。三九已至挺举,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間烘跺,已是汗流浹背湘纵。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,095評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滤淳,地道東北人梧喷。 一個(gè)月前我還...
    沈念sama閱讀 46,589評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親铺敌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绊困,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,687評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • 說(shuō)明 啟動(dòng)fabric-ca 生成fabric-ca admin的憑證 創(chuàng)建聯(lián)盟 為每個(gè)組織準(zhǔn)備msp 注冊(cè)exa...
    萬(wàn)物一馬閱讀 3,114評(píng)論 0 5
  • 簡(jiǎn)介 此項(xiàng)目是一個(gè)模擬公民身份信息鏈的區(qū)塊鏈項(xiàng)目 github 地址: https://github.com/ak...
    CrazyWolf_46a9閱讀 8,743評(píng)論 0 0
  • 時(shí)常回想起那一年的夏天适刀,四方的小院兒里秤朗,大泡桐樹(shù)遮天蔽日,陽(yáng)光不時(shí)撥開(kāi)葉子罅隙笔喉,窸窸窣窣的四下游移取视。 那一年似乎發(fā)...
    稀音閱讀 918評(píng)論 39 16
  • 我和我徒弟去山里采草藥,為了一味藥翻過(guò)了山頭也沒(méi)找到常挚。無(wú)奈之下只好先回去作谭,打算著改天再來(lái)看看。 我們走了一會(huì)兒感覺(jué)...
    拾夢(mèng)齋齋主閱讀 129評(píng)論 0 0
  • 我把相遇寫(xiě)成詩(shī) 人生路上約定相伴 一場(chǎng)突如其來(lái)的大雨琳散了誓言 我怨這聚散無(wú)常世事滄桑 經(jīng)歷后才明白的這樣的無(wú)奈 ...
    伊心心心心閱讀 671評(píng)論 0 3