Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1781419: when proxy is set in Connection, driver does send traffic through the proxy to S3, but not to Azure blob / GCS bucket (only Snowflake). Works with proxy envvar (Azure) #964

Merged
merged 18 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/connection/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const os = require('os');
const url = require('url');
const Util = require('../util');
const ProxyUtil = require('../proxy_util');
const Errors = require('../errors');
const ConnectionConstants = require('../constants/connection_constants');
const path = require('path');
Expand Down Expand Up @@ -223,7 +224,7 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo) {
protocol: proxyProtocol,
noProxy: noProxy
};
Util.validateProxy(proxy);
ProxyUtil.validateProxy(proxy);
}

const serviceName = options.serviceName;
Expand Down
28 changes: 20 additions & 8 deletions lib/file_transfer_agent/azure_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
const FileHeader = require('./file_util').FileHeader;
const expandTilde = require('expand-tilde');
const resultStatus = require('./file_util').resultStatus;
const ProxyUtil = require('../proxy_util');
const { isBypassProxy } = require('../http/node');
const Logger = require('../logger');

const EXPIRED_TOKEN = 'ExpiredToken';

Expand All @@ -26,7 +29,7 @@
* @returns {Object}
* @constructor
*/
function AzureUtil(azure, filestream) {
function AzureUtil(connectionConfig, azure, filestream) {
sfc-gh-dprzybysz marked this conversation as resolved.
Show resolved Hide resolved
const AZURE = typeof azure !== 'undefined' ? azure : require('@azure/storage-blob');
const fs = typeof filestream !== 'undefined' ? filestream : require('fs');

Expand All @@ -42,11 +45,23 @@
const sasToken = stageCredentials['AZURE_SAS_TOKEN'];

const account = stageInfo['storageAccount'];

const connectionString = `https://${account}.blob.core.windows.net${sasToken}`;
let proxy = ProxyUtil.getProxy(connectionConfig.getProxy(), 'Azure Util');
if (proxy && !isBypassProxy(proxy, connectionString)) {
Logger.getInstance().debug(`The destination host is: ${ProxyUtil.getHostFromURL(connectionString)} and the proxy host is: ${proxy.host}`);
sfc-gh-pmotacki marked this conversation as resolved.
Show resolved Hide resolved
Logger.getInstance().trace(`Initializing the proxy information for the Azure Client: ${ProxyUtil.describeProxy(proxy)}`);

Check warning on line 52 in lib/file_transfer_agent/azure_util.js

View check run for this annotation

Codecov / codecov/patch

lib/file_transfer_agent/azure_util.js#L51-L52

Added lines #L51 - L52 were not covered by tests

proxy = ProxyUtil.getAzureProxy(proxy);
Logger.getInstance().trace(connectionConfig.describe);

Check warning on line 55 in lib/file_transfer_agent/azure_util.js

View check run for this annotation

Codecov / codecov/patch

lib/file_transfer_agent/azure_util.js#L54-L55

Added lines #L54 - L55 were not covered by tests
}
ProxyUtil.hideEnvironmentProxy();
const blobServiceClient = new AZURE.BlobServiceClient(
`https://${account}.blob.core.windows.net${sasToken}`
connectionString, null,
{
proxyOptions: proxy,
}
);

ProxyUtil.restoreEnvironmentProxy();
return blobServiceClient;
};

Expand Down Expand Up @@ -203,7 +218,7 @@
blobContentEncoding: 'UTF-8',
blobContentType: 'application/octet-stream'
}
});
});
} catch (err) {
if (err['statusCode'] === 403 && detectAzureTokenExpireError(err)) {
meta['lastError'] = err;
Expand All @@ -215,7 +230,6 @@
}
return;
}

meta['dstFileSize'] = meta['uploadSize'];
meta['resultStatus'] = resultStatus.UPLOADED;
};
Expand Down Expand Up @@ -262,7 +276,6 @@
}
return;
}

meta['resultStatus'] = resultStatus.DOWNLOADED;
};

Expand All @@ -282,5 +295,4 @@
errstr.includes('Server failed to authenticate the request.');
}
}

module.exports = AzureUtil;
2 changes: 1 addition & 1 deletion lib/file_transfer_agent/remote_storage_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function RemoteStorageUtil(connectionConfig) {
if (type === 'S3') {
return new SnowflakeS3Util(connectionConfig);
} else if (type === 'AZURE') {
return new SnowflakeAzureUtil();
return new SnowflakeAzureUtil(connectionConfig);
} else if (type === 'GCS') {
return new SnowflakeGCSUtil();
} else {
Expand Down
12 changes: 2 additions & 10 deletions lib/file_transfer_agent/s3_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const EncryptionMetadata = require('./encrypt_util').EncryptionMetadata;
const FileHeader = require('./file_util').FileHeader;
const expandTilde = require('expand-tilde');
const getProxyAgent = require('../http/node').getProxyAgent;
const Util = require('../util');
const Logger = require('../logger');
const GlobalConfig = require('../global_config');
const ProxyUtil = require('../proxy_util');

const AMZ_IV = 'x-amz-iv';
const AMZ_KEY = 'x-amz-key';
Expand Down Expand Up @@ -79,13 +77,7 @@ function S3Util(connectionConfig, s3, filestream) {
useAccelerateEndpoint: useAccelerateEndpoint
};

let proxy = connectionConfig.getProxy();
if (!proxy && GlobalConfig.isEnvProxyActive()) {
proxy = Util.getProxyFromEnv();
if (proxy) {
Logger.getInstance().debug(`S3 Util loads the proxy info from the environment variable host: ${proxy.host}`);
}
}
const proxy = ProxyUtil.getProxy(connectionConfig.getProxy(), 'S3 Util');
if (proxy) {
const proxyAgent = getProxyAgent(proxy, new URL(connectionConfig.accessUrl), endPoint || SNOWFLAKE_S3_DESTINATION);
config.requestHandler = new NodeHttpHandler({
Expand Down
9 changes: 5 additions & 4 deletions lib/http/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

const Util = require('../util');
const ProxyUtil = require('../proxy_util');
const Base = require('./base');
const HttpsAgent = require('../agent/https_ocsp_agent');
const HttpsProxyAgent = require('../agent/https_proxy_agent');
Expand Down Expand Up @@ -56,7 +57,7 @@ function getFromCacheOrCreate(agentClass, options, agentId) {
Logger.getInstance().trace('Agent[id: %s] - new instance stored in cache.', agentId);

// detect and log PROXY envvar + agent proxy settings
const compareAndLogEnvAndAgentProxies = Util.getCompareAndLogEnvAndAgentProxies(agentOptions);
const compareAndLogEnvAndAgentProxies = ProxyUtil.getCompareAndLogEnvAndAgentProxies(agentOptions);
Logger.getInstance().debug('Agent[id: %s] - proxy settings used in requests: %s', agentId, compareAndLogEnvAndAgentProxies.messages);
// if there's anything to warn on (e.g. both envvar + agent proxy used, and they are different)
// log warnings on them
Expand Down Expand Up @@ -109,7 +110,7 @@ NodeHttpClient.prototype.getAgent = function (parsedUrl, proxy, mock) {
Logger.getInstance().trace('Agent[url: %s] - getting an agent instance.', parsedUrl.href);
if (!proxy && GlobalConfig.isEnvProxyActive()) {
const isHttps = parsedUrl.protocol === 'https:';
proxy = Util.getProxyFromEnv(isHttps);
proxy = ProxyUtil.getProxyFromEnv(isHttps);
if (proxy) {
Logger.getInstance().debug('Agent[url: %s] - proxy info loaded from the environment variable. Proxy host: %s', parsedUrl.href, proxy.host);
}
Expand All @@ -133,7 +134,7 @@ function getProxyAgent(proxyOptions, parsedUrl, destination, mock) {
}
}

const destHost = Util.getHostFromURL(destination);
const destHost = ProxyUtil.getHostFromURL(destination);
const agentId = createAgentId(agentOptions.protocol, agentOptions.hostname, destHost, agentOptions.keepAlive);
Logger.getInstance().debug('Agent[id: %s] - the destination host is: %s.', agentId, destHost);

Expand Down Expand Up @@ -170,4 +171,4 @@ function getAgentCacheSize() {
return httpsAgentCache.size;
}

module.exports = { NodeHttpClient, getProxyAgent, getAgentCacheSize };
module.exports = { NodeHttpClient, getProxyAgent, getAgentCacheSize, isBypassProxy };
Loading
Loading