Can't connect from Google Apps Script to Google Cloud SQL - javascript

I'm trying to connect from my Google Apps Script project to my Google Cloud SQL (second generation) instance. Here's the code I've got now.
I was able to connect to it successfully when connecting thru IP, but that's unencrypted. All of the Google Apps Script IPs are whitelisted also.
If my Instance ID is 12345 then my Instance connection name is 12345:us-west1:12345.
var conn = Jdbc.getCloudSqlConnection('jdbc:google:rdbms://instanceConnectionName/database',{user: 'username', password: 'password'});
I've tried:
Taking the username and password out of the curly braces, so it's just string, string, string
Using the instance name (keep in mind the difference between Instance ID and Instance connection name)
Removing the database
Removing the //
Switching authentication modes (AuthMode.LIMITED and AuthMode.NONE)
A whole bunch of other stuff
So basically, I just can't figure it out and some help would be appreciated it. Thank you!

Google's documentation is difficult to understand here. Basically, they tell you that the connection url must look like this:
var conn = Jdbc.getCloudSqlConnection('jdbc:google:rdbms:subname');
However, for a MySQL database, it must look like this:
var conn = Jdbc.getCloudSqlConnection('jdbc:google:mysql://subname');
Just replace rdbms with mysql. That's it.

Related

How to query a database from an Azure function in js

I have an IOT hub getting some data. I also have an Azure function app in js that is triggered when an IOT event occurs. In the function app, I want to query the incoming data against a azure sql database.
In the azure function->application settings->connection string, I created a connection string x with value of the azure db connection string. My index.js file is as below.
module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${IoTHubMessages}`);
IoTHubMessages.forEach(message => {
context.log(`Processed message ${message}`);
var sqlConnection = x;
});
context.done();
};
I get an error that x is not defined. How can I access x? Also how how to execute a select query from here.
Any help will be greatly appreciated.
Try accessing your app setting using process.env["x"] Here are the docs, and here's a related issue you may face if you're trying to run this locally.
I don't have experience writing Javascript functions to execute a select query, but this documentation seems like a good place to start: Use Node.js to query Azure SQL Database
There is no easy way to access connection strings in Node.js.
Instead, the suggestion is to use app settings for all your secrets and connection strings. You can them access them using process.env.YourAppSetting.
See similar questions one and two.

How to save Watson Conversation history in my mySQL database?

I just deployed the Watson Conversation plugin on Wordpress and its working very well - I can talk to Watson and he works just as he does in Bluemix.
However, I have no way to then see or manage the conversation history (aside from going into Bluemix).
The WP plugin has the api.php and app.js and I know one of these must at some point handle the text that the user inputs and Watson responds.
Does anyone know how I could save that conversation into my WP mySQL database, so I can analyze and work with it?
Thank you!
If this plugin is using Nodejs (because of app.js) you need to create one custom code for it. Probably, the app.js have the call for the Conversation Service.
If this is your case...
You need to access the Log from Conversation service and get the return, and after it, save inside one MySQL database.
Using the plugin mysql inside the app.js if the lib Watson-Developer-Cloud is inside the code. And access the Logs with getLogs function. Where the Conversationv1 is the call for this service.
For use the mysql lib, please follow the example code:
var mysql = require('mysql');
var connMySQL = function(){
return mysql.createConnection({
host: 'localhost',
user: 'yourUser',
password: 'yourPassword',
database: 'nameDatabase'
});
}
module.exports = function (){
return connMySQL;
}
I really recommend this project for you know the follow steps. In this case, the call for conversation service is the conversation variable, and for access the return, like entities, context varibles, etc, you will use the data return.
For example:
data.context.contextVariableInsideWatson
Official reference from IBM Developers: watson-developer-cloud/node-sdk
Repository with one simple project using Conversation from IBM Developers here.

Securely store a db password without setting up a service

Currently making an web application for our users to access and edit a database
We only have one connection to this database and can't make more, but we also can't give this one credential to any of our web app future users.
The idea is to create another security layer in the application using windows accounts
Pseudo code :
db = "dbconnection1"
dbuser = "admin1"
dbpassword = "123456"
enabledWinUsers = { "winUsr1", "winUsr2", "winUsr3" }
winusr = aplication.getWinUser()
if winusr in enabledWinUsers :
db.connect( db, dbuser, dbpassword )
The database logic and connection will all be done in Python, for now, and the app in Javascript/Jquery
Question is, is it possible to secure this code or the db credentials in any way without setting up a service like Node.js?
I do have full access to the server machine running the web app
Well you can encrypt the user credentials in your source code and decrypt them from your source code. Another option is to set the credentials as environment variables

Unable to proceed with Twilio Programmable Video Javascript

I am new to Twilio and facing few problems. I have downloaded twilio programmable video SDK Javascript version for my web app and it works pretty fine however I am unable to figure out how I can enable different users to create conversations and invite people. I want Many to Many relationship and don't know if twilio video supports it or not. Any help would be highly appreciated. Please share any tutorial as well.
Here is the code which i Got from Twilio website for JS/C#
public string generate_token()
{
string AccountSid = "SID";
string ApiKeySid = "APIKEY";
string ApiKeySecret = "APISECRET";
const string ConfigurationProfileSid = "klinik";
// Create an Access Token
var token = new AccessToken(AccountSid, ApiKeySid, ApiKeySecret);
// Set the Identity of this token
token.Identity = "example-user";
// Grant access to Conversations
var grant = new ConversationsGrant();
grant.ConfigurationProfileSid = ConfigurationProfileSid;
token.AddGrant(grant);
return token.ToJWT().ToString();
// Serialize the token as a JWT
// Console.WriteLine(token.ToJWT());
}
I am getting the following error on .ToJWT function in visual studio:
Error CS0012 The type 'JwtHashAlgorithm' is defined in an assembly that is not referenced. You must add a reference to assembly 'JWT, Version=1.3.3.0, Culture=neutral, PublicKeyToken=null'.
Twilio developer evangelist here.
It's difficult to help you any further as you haven't shared any code or anything you've tried.
The JavaScript quickstart is the best example of how to get started with the Video SDK. I recommend taking a look through the code to see how it is built.
To make many to many conversations, you just need to invite another user to an existing conversation or invite many users at one time. Make sure you invite them from all the existing participants as this is a peer to peer connection, so every user needs to create a connection with every other user. For that reason, it's not recommended to make many to many conversations of more than 4 people total (though you should test to see what kind of results you get).
Let me know if that helps at all.

Server-side Javascript in production fails to open connection to a named instance of SQL2008

I've got a production site that has been working for years with a SQL Server 2000 default instance on server named MDWDATA. TCP port 1433 and Named Pipes are enabled there. My goal is to get this web app working with a copy of the database upgraded to SQL Server 2008. I've installed SQL2008 with SP1 on a server called DEVMOJITO and tested the new database using various VB6 desktop programs that exercise various stored procs in a client-server fashion and parts of the website itself work fine against the upgraded database residing on this named instance of SQL2008. So, while I am happy that the database upgrade seems fine there is a part of this website that fails with this Named Pipes Provider: Could not open a connection to SQL Server [1231]. I think this error is misleading. I disabled Named Pipes on the SQL2000 instance used by the production site, restarted SQL and all the ASP code still continued to work fine (plus we have a firewall between both database servers and these web virtual directories on a public facing webserver.
URL to my production virtual directory which demos the working page:
URL to my development v-directory which demos the failing page:
All the code is the same on both prod and dev sites except that on dev I'm trying to connect to the upgraded database.
I know there are dozens of things to check which I've been searching for but here are a few things I can offer to help you help me:
The code that is failing is server-side Javascript adapted from Brent Ashley's "Javascript Remote Scripting (JSRS)" code package years ago. It operates in an AJAX-like manner by posting requests back to different ASP pages and then handling a callback. I think the key thing to point out here is how I changed the connection to the database: (I cannot get Javascript to format right here!)
function setDBConnect(datasource)
{
var strConnect; //ADO connection string
//strConnect = "DRIVER=SQL Server;SERVER=MDWDATA;UID=uname;PASSWORD=x; DATABASE=StagingMDS;";
strConnect = "Provider=SQLNCLI10;Server=DEVMOJITO\MSSQLSERVER2008;Uid=uname;Pwd=x;DATABASE=StagingMDS;";
return strConnect;
}
function serializeSql( sql , datasource)
{
var conn = new ActiveXObject("ADODB.Connection");
var ConnectString = setDBConnect(datasource);
conn.Open( ConnectString );
var rs = conn.Execute( sql );
Please note how the connection string differs. I think that could be the problem but I don't know what to do. I am surprised the error returned says "named pipes" was involved because I really wanted to use TCP. The connection string syntax here is the same as used successfully on a different part of the site which uses VBScript which I'll paste here to show:
if DataBaseConnectionsAreNeeded(strScriptName) then
dim strWebDB
Set objConn = Server.CreateObject("ADODB.Connection")
if IsProductionWeb() Then
strWebDB = "DATABASE=MDS;SERVER=MDWDATA;DRIVER=SQL Server;UID=uname;PASSWORD=x;"
end if
if IsDevelopmentWeb() Then
strWebDB = "Provider=SQLNCLI10;Server=DEVMOJITO\MSSQLSERVER2008;Database=StagingMDS;UID=uname;PASSWORD=x;"
end if
objConn.ConnectionString = strWebDB
objConn.ConnectionTimeout = 30
objConn.Open
set oCmd = Server.CreateObject("ADODB.Command")
oCmd.ActiveConnection = objConn
This code works in both prod and dev virtual directories and other code in other parts of the web which use ASP.NET work against both databases correctly. Named pipes and TCP are both enabled on each server. I don't understand the string used by the Pipes but I am using the defaults always.
I wonder why the Javascript call above results in use of named pipes instead of TCP. Any ideas would be greatly appreciated.
Summary of what I did to get this working:
Add an extra slash to the connection string since this is server-side Javascript:
Server=tcp:DEVMOJITO\MSSQLSERVER2008,1219;
Explicitly code tcp: as a protocol prefix and port 1219. I learned that by default a named instance of SQL uses dynamic porting. I ended up turning that off and chose, somewhat arbitrarily, the port 1219, which dynamic had chosen before I turned it off. There are probably other ways to get this part working.
Finally, I discovered that SET NOCOUNT ON needed to be added to the stored procedure being called. Otherwise, the symptom is the message: "Operation is not allowed when the object is closed".

Categories

Resources