shohu / estate-sample

Read-only mirror of https://gerrit.hyperledger.org/r/#/admin/projects/fabric-samples
https://hyperledger.org
Apache License 2.0
0 stars 0 forks source link

fabric-ca-client の中身を探る #14

Open shohu opened 5 years ago

shohu commented 5 years ago

offlineのtestコードがfabric-ca-client を使っていて、enroll をしているが、adminのpasswordを渡してチャンネルを作成している。 これってoffline署名ということになるのかどうかよくわからず、、、

/**
 * Enroll a registered user in order to receive a signed X509 certificate
 * @param {string} enrollmentID The registered ID to use for enrollment
 * @param {string} enrollmentSecret The secret associated with the enrollment ID
 * @param {string} csr PEM-encoded PKCS#10 certificate signing request
 * @param {string} profile The profile name.  Specify the 'tls' profile for a TLS certificate; otherwise, an enrollment certificate is issued.
 * @param {AttributeRequest[]} attr_reqs An array of {@link AttributeRequest}
 * @returns {Promise} {@link EnrollmentResponse}
 * @throws Will throw an error if all parameters are not provided
 * @throws Will throw an error if calling the enroll API fails for any reason
 */
enroll(enrollmentID, enrollmentSecret, csr, profile, attr_reqs) {

  const self = this;

  // check for required args
  if (arguments.length < 3) {
    return Promise.reject('Missing required parameters.  \'enrollmentID\', \'enrollmentSecret\' and \'csr\' are all required.');
  }

  const requestOptions = {
    hostname: self._hostname,
    port: self._port,
    path: self._baseAPI + 'enroll',
    method: 'POST',
    auth: enrollmentID + ':' + enrollmentSecret,
    ca: self._tlsOptions.trustedRoots,
    rejectUnauthorized: self._tlsOptions.verify
  };

  const enrollRequest = {
    caname: self._caName,
    certificate_request: csr
  };

  if (profile) {
    enrollRequest.profile = profile;
  }

  if (attr_reqs) {
    enrollRequest.attr_reqs = attr_reqs;
  }

  return new Promise(((resolve, reject) => {

    const request = self._httpClient.request(requestOptions, (response) => {

      const responseBody = [];
      response.on('data', (chunk) => {
        responseBody.push(chunk);
      });

      response.on('end', (data) => {

        const payload = responseBody.join('');

        if (!payload) {
          return reject(new Error(
            util.format('Enrollment failed with HTTP status code', response.statusCode)));
        }
        // response should be JSON
        try {
          const res = JSON.parse(payload);
          if (res.success) {
            // we want the result field which is Base64-encoded PEM
            const enrollResponse = new Object();
            // Cert field is Base64-encoded PEM
            enrollResponse.enrollmentCert = Buffer.from(res.result.Cert, 'base64').toString();
            enrollResponse.caCertChain = Buffer.from(res.result.ServerInfo.CAChain, 'base64').toString();
            return resolve(enrollResponse);
          } else {
            return reject(new Error(
              util.format('Enrollment failed with errors [%s]', JSON.stringify(res.errors))));
          }

        } catch (err) {
          return reject(new Error(
            util.format('Could not parse enrollment response [%s] as JSON due to error [%s]', payload, err)));
        }
      });

      response.on('error', (error) => {
        reject(new Error(
          util.format('Enrollment failed with error [%s]', error)));
      });
    });

    request.on('error', (err) => {
      reject(new Error(util.format('Calling enrollment endpoint failed with error [%s]', err)));
    });

    const body = JSON.stringify(enrollRequest);
    request.end(body);

  }));

}