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

查看:29
本文介绍了如何在谷歌应用程序脚本中使用 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 Script 中使用 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-----
' +
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' +
    /* snip */
    '-----END RSA PRIVATE KEY-----';

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

  var serverSslCertificate = '-----BEGIN CERTIFICATE-----
' +
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' +
    /* 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 Script 中使用 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-----
...
-----END CERTIFICATE-----', 
  _clientSslCertificate: '-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----', 
  _clientSslKey: '-----BEGIN RSA PRIVATE KEY-----
...
-----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.

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

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