如何在Google Apps脚本中使用SSL连接到数据库? [英] How to connect to database with SSL in google apps script?

查看:108
本文介绍了如何在Google Apps脚本中使用SSL连接到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Google Apps脚本中的SSL连接到数据库,引用这些文档.错误是:

I'm trying to connect to a database via SSL within Google Apps Script, referencing these docs. The error is:

Execution failed: Failed to establish a database connection. Check connection string, username and password.

我可以从另一个数据库客户端(Sequel Pro)使用这些完全相同的参数,并且工作正常.我的数据库接受来自任何IP地址(0.0.0.0/0)的连接.

I can use these exact same parameters from another db client (Sequel Pro) and it works fine. I've got the db accepting connections from any IP address (0.0.0.0/0).

我相信我已经消除了所有其他变量(用户名,密码等),仅当我尝试在Apps脚本中使用SSL失败时才使用它.

I believe I've eliminated all the other variables (user name, password, etc. etc.), its only when I attempt to use SSL within Apps Script that it fails.

任何人都可以提供在Google Apps脚本中使用SSL连接到MySQL数据库的有效示例吗?

我的应用脚本:

function connectDb() {
  var address = 'x.x.x.x';  // no, I'm not literally using x's in my ip address

  var instanceUrl = 'jdbc:mysql://' + address + ':3306/';

  var clientSslKey = '-----BEGIN RSA PRIVATE KEY-----\n' +
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n' +
    /* snip */
    '-----END RSA PRIVATE KEY-----';

  var clientSslCertificate = '-----BEGIN CERTIFICATE-----\n' +
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n' +
    /* snip */
    '-----END CERTIFICATE-----';

  var serverSslCertificate = '-----BEGIN CERTIFICATE-----\n' +
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n' +
    /* snip */
    '-----END CERTIFICATE-----';

  // https://developers.google.com/apps-script/reference/jdbc/jdbc#getconnectionurl-info
  var connParams = {
    user: 'temp',
    // no password set
    _serverSslCertificate: serverSslCertificate,
    _clientSslCertificate: clientSslCertificate,
    _clientSslKey: clientSslKey,
  };
  var conn = Jdbc.getConnection(instanceUrl, connParams);
}

编辑

我在此处提交了一个错误,该错误被标记为这另一个,它是"P2"优先级.也许这意味着它将很快得到解决?

I filed a bug here which got marked as a duplicate of this other one which as a "P2" priority. Maybe that means it will be fixed soonish?

推荐答案

我可以确认可以在Google Apps脚本中使用SSL连接到MySQL数据库.

I can confirm that I can connect to a MySQL database with SSL within Google Apps Script.

重要的是要注意useSSL = true确实是必需的

我可以按照 https://issuetracker.google上的示例进行操作.com/issues/36761592#comment18 (相关代码段在下面重复):

I was able to get it working by following the example at https://issuetracker.google.com/issues/36761592#comment18 (relevant snippet repeated below):

var conn = Jdbc.getConnection('jdbc:mysql://<ip address>/<db name>?useSSL=true', { 
  user: '<user>', 
  password: '<pass>', 
  _serverSslCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----', 
  _clientSslCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----', 
  _clientSslKey: '-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----' 
});

如果您使用的是Google Cloud SQL实例,则可能要考虑使用getCloudSqlConnection().大概也是加密的(基于当相应实例启用仅允许安全连接连接到该实例."时,我确实可以获得连接的事实),并且重要的是,不需要您进行管理(即不要失去对密钥的控制权并自行进行认证.

If you're using a Google Cloud SQL instance, you may want to consider using getCloudSqlConnection(). Presumably it's encrypted as well (based on the fact that I can indeed get a connection when the corresponding instance has "Only secured connections are allowed to connect to this instance." enabled), and, importantly, doesn't require you to manage (i.e. not lose control of) the key and certs yourself.

这篇关于如何在Google Apps脚本中使用SSL连接到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆