Skip to content

Commit

Permalink
SNOW-930831: Add randomization to the table name in put/get test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pmotacki committed Oct 4, 2023
1 parent c69ec03 commit 14f7a85
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 59 deletions.
8 changes: 5 additions & 3 deletions lib/authentication/auth_web.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const net = require('net');
const querystring = require('querystring');
const URLUtil = require('./../../lib/url_util');
const Util = require('./../../lib/util');
var SsoUrlProvider = require('../authentication/sso_url_provider');

/**
* Creates an external browser authenticator.
Expand All @@ -18,10 +19,11 @@ const Util = require('./../../lib/util');
* @returns {Object}
* @constructor
*/
function auth_web(connectionConfig, ssoUrlProvider, webbrowser) {
function auth_web(connectionConfig, httpClient, webbrowser) {

const host = connectionConfig.host;
const browserActionTimeout = connectionConfig.getBrowserActionTimeout();
const ssoUrlProvider = new SsoUrlProvider(connectionConfig, httpClient);

if (!Util.exists(host)) {
throw new Error(`Invalid value for host: ${host}`);
Expand All @@ -32,7 +34,6 @@ function auth_web(connectionConfig, ssoUrlProvider, webbrowser) {

const open = typeof webbrowser !== "undefined" ? webbrowser : require('open');

let ssoURL;
let proofKey;
let token;

Expand Down Expand Up @@ -81,10 +82,11 @@ function auth_web(connectionConfig, ssoUrlProvider, webbrowser) {
server.address().port,
username,
host);
ssoURL = ssoData['ssoUrl'];

proofKey = ssoData['proofKey'];

// Step 2: validate URL
let ssoURL = ssoData['ssoUrl'];
if (!URLUtil.isValidURL(ssoURL)) {
throw new Error(util.format("Invalid SSO URL found - %s ", ssoURL));
}
Expand Down
32 changes: 15 additions & 17 deletions lib/authentication/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Copyright (c) 2015-2021 Snowflake Computing Inc. All rights reserved.
*/

var auth_default = require('./auth_default');
var auth_web = require('./auth_web');
var auth_keypair = require('./auth_keypair');
var auth_oauth = require('./auth_oauth');
var auth_okta = require('./auth_okta');
const auth_default = require('./auth_default');
const auth_web = require('./auth_web');
const auth_keypair = require('./auth_keypair');
const auth_oauth = require('./auth_oauth');
const auth_okta = require('./auth_okta');

var authenticationTypes =
const authenticationTypes =
{
DEFAULT_AUTHENTICATOR: 'SNOWFLAKE', // default authenticator name
EXTERNAL_BROWSER_AUTHENTICATOR: 'EXTERNALBROWSER',
Expand Down Expand Up @@ -37,9 +37,8 @@ exports.formAuthJSON = function formAuthJSON(
clientType,
clientVersion,
clientEnv
)
{
var body =
) {
const body =
{
data:
{
Expand Down Expand Up @@ -67,20 +66,19 @@ exports.formAuthJSON = function formAuthJSON(
*
* @returns {Object} the authenticator.
*/
exports.getAuthenticator = function getAuthenticator(connectionConfig, ssoUrlProvider)
{
var auth = connectionConfig.getAuthenticator();
exports.getAuthenticator = function getAuthenticator(connectionConfig, httpClient) {
const auth = connectionConfig.getAuthenticator();

if (auth == authenticationTypes.DEFAULT_AUTHENTICATOR) {
if (auth === authenticationTypes.DEFAULT_AUTHENTICATOR) {
return new auth_default(connectionConfig.password);
} else if (auth == authenticationTypes.EXTERNAL_BROWSER_AUTHENTICATOR) {
return new auth_web(connectionConfig, ssoUrlProvider);
} else if (auth === authenticationTypes.EXTERNAL_BROWSER_AUTHENTICATOR) {
return new auth_web(connectionConfig, httpClient);
}
if (auth == authenticationTypes.KEY_PAIR_AUTHENTICATOR) {
if (auth === authenticationTypes.KEY_PAIR_AUTHENTICATOR) {
return new auth_keypair(connectionConfig.getPrivateKey(),
connectionConfig.getPrivateKeyPath(),
connectionConfig.getPrivateKeyPass());
} else if (auth == authenticationTypes.OAUTH_AUTHENTICATOR) {
} else if (auth === authenticationTypes.OAUTH_AUTHENTICATOR) {
return new auth_oauth(connectionConfig.getToken());
} else if (auth.startsWith('HTTPS://')) {
return new auth_okta(connectionConfig.password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

const Util = require('../util');
const Errors = require('../errors');
const HttpClient = require("../http/node");
const axios = require("axios");
const {rest} = require("../global_config");
const { rest } = require('../global_config');

/**
* Creates a new instance of an LargeResultSetService.
* Creates a new instance of an SsoUrlProvider.
*
* @param {Object} connectionConfig
* @param {Object} httpClient
* @constructor
*/
function SsoUrlProvider(connectionConfig, httpClient) {
// validate input

Errors.assertInternal(Util.isObject(connectionConfig));
Errors.assertInternal(Util.isObject(httpClient));

Expand All @@ -29,49 +27,45 @@ function SsoUrlProvider(connectionConfig, httpClient) {
* @param {String} authenticator
* @param {String} serviceName
* @param {String} account
* @param {Number} callback_port
* @param {Number} callbackPort
* @param {String} user
* @param {String} host
*
* @returns {String} the SSO URL.
*/
this.getSSOURL = function (authenticator, serviceName, account, callback_port, user, host) {
this.getSSOURL = function (authenticator, serviceName, account, callbackPort, user, host) {
// Create URL to send POST request to
const url = protocol + '://' + host + "/session/authenticator-request";
const url = protocol + '://' + host + '/session/authenticator-request';

let header;
if (serviceName) {
header = {
'HTTP_HEADER_SERVICE_NAME': serviceName
}
};
}
const body = {
"data": {
"ACCOUNT_NAME": account,
"LOGIN_NAME": user,
"PORT": port,
"PROTOCOL": protocol,
"AUTHENTICATOR": authenticator,
"BROWSER_MODE_REDIRECT_PORT": callback_port.toString()
'data': {
'ACCOUNT_NAME': account,
'LOGIN_NAME': user,
'PORT': port,
'PROTOCOL': protocol,
'AUTHENTICATOR': authenticator,
'BROWSER_MODE_REDIRECT_PORT': callbackPort.toString()
}
};

const httpsClient = new HttpClient(connectionConfig)
const agent = httpsClient.getAgent(url, connectionConfig.getProxy());
const agent = httpClient.getAgent(url, connectionConfig.getProxy());

const requestOptions =
{
method: 'post',
url: url,
headers: header,
data: body,
requestOCSP: false,
rejectUnauthorized: true,
httpsAgent: agent
};

// Post request to get the SSO URL
return axios.request(requestOptions)
return httpClient.post(url, body, requestOptions)
.then((response) => {
const data = response['data']['data'];
return data;
Expand Down
2 changes: 1 addition & 1 deletion lib/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function Connection(context)
var self = this;

// Get authenticator to use
var auth = Authenticator.getAuthenticator(connectionConfig, context.getServices().ssoUrlProvider);
var auth = Authenticator.getAuthenticator(connectionConfig, context.getHttpClient());

try
{
Expand Down
11 changes: 9 additions & 2 deletions lib/connection/connection_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var Util = require('../util');
var Errors = require('../errors');
var SfService = require('../services/sf');
var LargeResultSetService = require('../services/large_result_set');
var SsoUrlProvider = require('../services/sso_url_provider');

/**
* Creates a new ConnectionContext.
Expand Down Expand Up @@ -40,7 +39,6 @@ function ConnectionContext(connectionConfig, httpClient, config)
{
sf: new SfService(connectionConfig, httpClient, sfServiceConfig),
largeResultSet: new LargeResultSetService(connectionConfig, httpClient),
ssoUrlProvider: new SsoUrlProvider(connectionConfig, httpClient)
};

/**
Expand Down Expand Up @@ -79,6 +77,15 @@ function ConnectionContext(connectionConfig, httpClient, config)
}
};
};
/**
* Returns instance of httpClient
*
* @returns {NodeHttpClient}
*/
this.getHttpClient = function ()
{
return httpClient;
};
}

module.exports = ConnectionContext;
2 changes: 2 additions & 0 deletions lib/http/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ HttpClient.prototype.getAgent = function (url, proxy, mock) {
return null;
};

HttpClient.prototype.post = axios.post;

module.exports = HttpClient;

/**
Expand Down
51 changes: 37 additions & 14 deletions test/unit/authentication/authentication_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var auth_keypair = require('./../../../lib/authentication/auth_keypair');
var auth_oauth = require('./../../../lib/authentication/auth_oauth');
var auth_okta = require('./../../../lib/authentication/auth_okta');
var authenticationTypes = require('./../../../lib/authentication/authentication').authenticationTypes;
const HttpAgent = require('https').Agent;

var MockTestUtil = require('./../mock/mock_test_util');

Expand Down Expand Up @@ -74,6 +75,7 @@ describe('external browser authentication', function ()
const BROWSER_ACTION_TIMEOUT = 10000;
const connectionConfig= {
getBrowserActionTimeout: () => BROWSER_ACTION_TIMEOUT,
getProxy: () => {},
host: 'fakehost'
}

Expand All @@ -89,25 +91,35 @@ describe('external browser authentication', function ()
return;
}
});
mock('ssoUrlProvider', {
getSSOURL: async function (authenticator, serviceName, account, callback_port, user, host) {
mock('httpclient', {
getAgent: function (url, body, header)
{
return new HttpAgent();
},
post: async function (url, body, header)
{
const data =
{
ssoUrl: mockSsoURL,
proofKey: mockProofKey
data: {
data:
{
ssoUrl: mockSsoURL,
proofKey: mockProofKey
}
}
}
browserRedirectPort = callback_port.toString();
browserRedirectPort = body['data']['BROWSER_MODE_REDIRECT_PORT'];
return data;
}
});

webbrowser = require('webbrowser');
ssoUrlProvider = require('ssoUrlProvider');
httpclient = require('httpclient');
});

it('external browser - authenticate method is thenable', done =>
{
const auth = new auth_web(connectionConfig, ssoUrlProvider, webbrowser.open);
const auth = new auth_web(connectionConfig, httpclient, webbrowser.open);

auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username, credentials.host)
.then(done)
Expand All @@ -116,7 +128,7 @@ describe('external browser authentication', function ()

it('external browser - get success', async function ()
{
const auth = new auth_web(connectionConfig, ssoUrlProvider, webbrowser.open);
const auth = new auth_web(connectionConfig, httpclient, webbrowser.open);
await auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username, credentials.host);

var body = { data: {} };
Expand All @@ -138,21 +150,32 @@ describe('external browser authentication', function ()
return;
}
});
mock('ssoUrlProvider', {
getSSOURL: async function (authenticator, serviceName, account, callback_port, user, host) {

mock('httpclient', {
getAgent: function (url, body, header)
{
return new HttpAgent();
},
post: async function (url, body, header)
{
const data =
{
ssoUrl: mockSsoURL
data: {
data:
{
ssoUrl: mockSsoURL
}
}
}
browserRedirectPort = callback_port.toString();
browserRedirectPort = body['data']['BROWSER_MODE_REDIRECT_PORT'];
return data;
}
});

webbrowser = require('webbrowser');
ssoUrlProvider = require('ssoUrlProvider');
httpclient = require('httpclient');

const auth = new auth_web(connectionConfig, ssoUrlProvider, webbrowser.open);
const auth = new auth_web(connectionConfig, httpclient, webbrowser.open);
await auth.authenticate(credentials.authenticator, '', credentials.account, credentials.username, credentials.host);

var body = { data: {} };
Expand Down

0 comments on commit 14f7a85

Please sign in to comment.