无法使用Amazon SDK和Minio验证第一个证书

时间:2019-07-11 23:55:15

标签: ssl-certificate aws-sdk minio

尝试使用以下代码连接到minio服务器:

var AWS = require('aws-sdk');

var s3  = new AWS.S3({
          accessKeyId: 'minio' ,
          secretAccessKey: 'minio123' ,
          endpoint: 'https://minio.dev' ,
          s3ForcePathStyle: true, // needed with minio?
          signatureVersion: 'v4',
          sslEnabled: false,
          rejectUnauthorized: false
});

// putObject operation.

var params = {Bucket: 'documents', Key: 'testobject', Body: 'Hello from MinIO!!'};

s3.putObject(params, function(err, data) {
      if (err)
       console.log(err)
      else   
       console.log("Successfully uploaded data to documents/testobject");
});

// getObject operation.

var params = {Bucket: 'documents', Key: 'testobject'};

var file = require('fs').createWriteStream('/tmp/mykey');

s3.getObject(params).
on('httpData', function(chunk) { file.write(chunk); }).
on('httpDone', function() { file.end(); }).
send();

我收到以下错误:

{ Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1051:34)
    at TLSSocket.emit (events.js:189:13)
    at TLSSocket.EventEmitter.emit (domain.js:441:20)
    at TLSSocket._finishInit (_tls_wrap.js:633:8)
  message: 'unable to verify the first certificate',
  code: 'NetworkingError',
  region: 'us-east-1',
  hostname: 'minio.dev',
  retryable: true,
  time: 2019-07-11T23:38:45.382Z }

我已经传递了“ sslEnabled:false”选项,但这并没有改变任何东西。我还尝试在节点侧禁用SSL,它也无法更改行为。

有人对如何忽略自签名证书错误有任何想法吗? (如果这是问题,我认为是)

1 个答案:

答案 0 :(得分:0)

const AWS = require('aws-sdk');
const https = require('https');

// Allow use with Minio
AWS.NodeHttpClient.sslAgent = new https.Agent({ rejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0' });

// the rest of the code snippet remains unchanged

rejectUnauthorized: false是关键。在此示例中,我将其与常用环境变量的存在联系起来,该环境变量可切换request模块中的行为。 AWS开发工具包未将其用于其API,但由于它执行相同的功能,因此重新使用它似乎是适当的。

现在,如果设置了NODE_TLS_REJECT_UNAUTHORIZED=0,则包括AWS开发工具包在内的整个Node流程将与模拟的HTTPS终端一起使用。

警告:仅在开发环境中使用此功能,例如在本地工作站上模拟公共服务。它可以让您容易受到中间人攻击!