Nelluk
Nelluk

Reputation: 1251

Using a Cloud SQL back end in a multi-user Apps Script

I (user1) have an Apps Script that connects to a Cloud SQL instance, like so:

var conn = Jdbc.getCloudSqlConnection("jdbc:google:rdbms://blah:instance1/foo")

And it works like a charm. I am trying to share this script as a Library and possibly monetize it in the future.

When I login as a different user (user2) and add it as a library, it fails when the library runs a function that tries to make the connection:

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

So it seems like the connection is coming from user2 even though the code executing the connection is coming from a library that is owned by user1. Is that true? Is there any way to make this work beyond adding user2 as an editor to my Cloud SQL instance?

Upvotes: 1

Views: 310

Answers (1)

Henrique G. Abreu
Henrique G. Abreu

Reputation: 17792

Yes, this is the expected behavior. The library being owned by a different user does not really matter. The cloud-sql jdbc connector only authorize connections if the effective user can edit the database.

To workaround this you have turn this "library" into a webapp, running as you and shared to anyone, even anonymous. That accepts queries and commands to your database via HTTP parameters (either GET or POST). This webapp will return the results using ContentService on any of text formats available. I guess the easiest one is JSON, since you can work with regular js objects on Apps Script and then just JSON.stringify and JSON.parse to make the communication.

To make things easier you could then make a library to access to this webapp, abstracting the UrlFetch calls and parameters you have on the 1st script, allowing easy "native" plugin by other scripts.

Upvotes: 1

Related Questions