Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Dec 18, 2024
1 parent f97e83c commit 35fc4b0
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 326 deletions.
5 changes: 0 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,6 @@ declare module 'snowflake-sdk' {
* The option to pass passcode from DUO.
*/
passcode?: string;

/**
* The option to override the environment proxy value (HTTPS_PROXY) if the connection proxy is configured in GCS
*/
overrideGCSEnvProxy?: boolean;
}

export interface Connection {
Expand Down
13 changes: 0 additions & 13 deletions lib/connection/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const DEFAULT_PARAMS =
'credentialCacheDir',
'passcodeInPassword',
'passcode',
'overrideGCSEnvProxy'
];

function consolidateHostAndAccount(options) {
Expand Down Expand Up @@ -513,14 +512,6 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo) {

passcode = options.passcode;
}

let overrideGCSEnvProxy = true;
if (Util.exists(options.overrideGCSEnvProxy)) {
Errors.checkArgumentValid(Util.isBoolean(options.overrideGCSEnvProxy),
ErrorCodes.ERR_CONN_CREATE_INVALID_OVERRIDE_GCS_ENV_PROXY);

overrideGCSEnvProxy = options.overrideGCSEnvProxy;
}

if (validateDefaultParameters) {
for (const [key] of Object.entries(options)) {
Expand Down Expand Up @@ -844,10 +835,6 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo) {
return passcode;
};

this.getOverrideGCSEnvProxy = function () {
return overrideGCSEnvProxy;
};

/**
* Returns attributes of Connection Config object that can be used to identify
* the connection, when ID is not available in the scope. This is not sufficient set,
Expand Down
1 change: 0 additions & 1 deletion lib/constants/error_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ exports[404053] = 'A host must be specified.';
exports[404054] = 'Invalid host. The specified value must be a string.';
exports[404055] = 'Invalid passcodeInPassword. The specified value must be a boolean';
exports[404056] = 'Invalid passcode. The specified value must be a string';
exports[404057] = 'Invalid overrideGCSEnvProxy. The specified value must be a boolean';

// 405001
exports[405001] = 'Invalid callback. The specified value must be a function.';
Expand Down
1 change: 0 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ codes.ERR_CONN_CREATE_MISSING_HOST = 404053;
codes.ERR_CONN_CREATE_INVALID_HOST = 404054;
codes.ERR_CONN_CREATE_INVALID_PASSCODE_IN_PASSWORD = 404055;
codes.ERR_CONN_CREATE_INVALID_PASSCODE = 404056;
codes.ERR_CONN_CREATE_INVALID_OVERRIDE_GCS_ENV_PROXY = 404057;

// 405001
codes.ERR_CONN_CONNECT_INVALID_CALLBACK = 405001;
Expand Down
1 change: 0 additions & 1 deletion lib/file_transfer_agent/azure_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function AzureUtil(connectionConfig, azure, filestream) {
Logger.getInstance().trace(`Initializing the proxy information for the Azure Client: ${ProxyUtil.describeProxy(proxy)}`);

proxy = ProxyUtil.getAzureProxy(proxy);
Logger.getInstance().trace(connectionConfig.describe);
}
ProxyUtil.hideEnvironmentProxy();
const blobServiceClient = new AZURE.BlobServiceClient(
Expand Down
86 changes: 17 additions & 69 deletions lib/file_transfer_agent/gcs_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const FileHeader = require('./file_util').FileHeader;
const getProxyAgent = require('../http/node').getProxyAgent;
const ProxyUtil = require('../proxy_util');
const Util = require('../util');
const Logger = require('../logger');
const { shouldPerformGCPBucket } = require('../util');

const GCS_METADATA_PREFIX = 'x-goog-meta-';
Expand All @@ -24,7 +23,6 @@ const GCS_FILE_HEADER_ENCRYPTION_METADATA = 'gcs-file-header-encryption-metadata
const HTTP_HEADER_CONTENT_ENCODING = 'Content-Encoding';
const resultStatus = require('./file_util').resultStatus;
const { Storage } = require('@google-cloud/storage');
const { isBypassProxy } = require('../http/node');

const EXPIRED_TOKEN = 'ExpiredToken';

Expand All @@ -51,15 +49,8 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
let axios = httpClient;
const fs = typeof fileStream !== 'undefined' ? fileStream : require('fs');
const connectionProxy = connectionConfig.getProxy();
const proxyString = ProxyUtil.stringifyProxy(connectionProxy);
let isEnvProxyOverridden = false;
let envProxy = 'HTTP_PROXY';
let originalEnvProxy = process.env[envProxy];
if (!Util.exists(originalEnvProxy)) {
envProxy = 'HTTPS_PROXY';
originalEnvProxy = process.env[envProxy];
}

let isProxyEnabled = false;

/**
* Retrieve the GCS token from the stage info metadata.
*
Expand Down Expand Up @@ -94,31 +85,20 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
} else {
client = null;
}

const isByPassed = isBypassProxy(connectionProxy, endPoint || `storage.${stageInfo.region.toLowerCase()}.rep.googleapis.com`);
if (Util.isEmptyObject(connectionProxy) || isByPassed) {
isEnvProxyOverridden = false;
} else if (!Util.exists(originalEnvProxy)) {
isEnvProxyOverridden = true;
} else if (connectionConfig.getOverrideGCSEnvProxy()) {
if (proxyString !== originalEnvProxy) {
Logger.getInstance().debug(`The destination host is: ${ProxyUtil.getHostFromURL(endPoint || `https://storage.${stageInfo.region.toLowerCase()}.rep.googleapis.com`)} and the proxy host is: ${connectionProxy.host}`);
Logger.getInstance().warn(`The environment variable ${envProxy} value will be overwritten during the Storage HTTP calls`);
isEnvProxyOverridden = true;
}
}

if (isEnvProxyOverridden) {
Logger.getInstance().trace(`Initializing the proxy information for the GCS Client: ${ProxyUtil.describeProxy(connectionProxy)}`);
}

if (typeof httpClient === 'undefined') {
const proxyAgent = getProxyAgent(connectionProxy, new URL(connectionConfig.accessUrl), endPoint || `storage.${stageInfo.region.toLowerCase()}.rep.googleapis.com` );
axios = require('axios').create({
proxy: false,
httpAgent: proxyAgent,
httpsAgent: proxyAgent,
});
const proxy = ProxyUtil.getProxy(connectionConfig.getProxy(), 'S3 Util');
if (proxy) {
isProxyEnabled = true;
const proxyAgent = getProxyAgent(connectionProxy, new URL(connectionConfig.accessUrl), endPoint || `storage.${stageInfo.region.toLowerCase()}.rep.googleapis.com` );
axios = require('axios').create({
proxy: false,
httpAgent: proxyAgent,
httpsAgent: proxyAgent,
});
} else {
axios = require('axios');
}
}

return client;
Expand Down Expand Up @@ -183,14 +163,12 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
let matDescKey;

try {
if (shouldPerformGCPBucket(accessToken)) {
if (shouldPerformGCPBucket(accessToken) && !isProxyEnabled) {
const gcsLocation = this.extractBucketNameAndPath(meta['stageInfo']['location']);
overrideEnvProxy();
const metadata = await meta['client'].gcsClient
.bucket(gcsLocation.bucketName)
.file(gcsLocation.path + filename)
.getMetadata();
restoreEnvProxy();


digest = metadata[0].metadata[SFC_DIGEST];
Expand Down Expand Up @@ -325,10 +303,9 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
}

try {
if (shouldPerformGCPBucket(accessToken)) {
if (shouldPerformGCPBucket(accessToken) && !isProxyEnabled) {
const gcsLocation = this.extractBucketNameAndPath(meta['stageInfo']['location']);

overrideEnvProxy();
await meta['client'].gcsClient
.bucket(gcsLocation.bucketName)
.file(gcsLocation.path + meta['dstFileName'])
Expand All @@ -342,7 +319,6 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
}
}
});
restoreEnvProxy();
} else {
// Set maxBodyLength to allow large file uploading
await axios.put(uploadUrl, fileStream, { maxBodyLength: Infinity, headers: gcsHeaders });
Expand Down Expand Up @@ -400,9 +376,8 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
let size;

try {
if (shouldPerformGCPBucket(accessToken)) {
if (shouldPerformGCPBucket(accessToken) && !isProxyEnabled) {
const gcsLocation = this.extractBucketNameAndPath(meta['stageInfo']['location']);
overrideEnvProxy();
await meta['client'].gcsClient
.bucket(gcsLocation.bucketName)
.file(gcsLocation.path + meta['srcFileName'])
Expand All @@ -414,7 +389,6 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
.bucket(gcsLocation.bucketName)
.file(gcsLocation.path + meta['srcFileName'])
.getMetadata();
restoreEnvProxy();

encryptionDataprop = metadata[0].metadata[ENCRYPTIONDATAPROP];
matDescKey = metadata[0].metadata[MATDESC_KEY];
Expand Down Expand Up @@ -498,18 +472,6 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
return link;
};

//getIsEnvProxyOverridden and getEnvProxiesDuringProcess are for testing purpose
this.getIsEnvProxyOverridden = function () {
return isEnvProxyOverridden;
};

this.getEnvProxiesDuringProcess = function () {
if (isEnvProxyOverridden) {
return envProxy === 'HTTP_PROXY' ? [proxyString, process.env.HTTPS_PROXY] : [undefined, proxyString];
}
return [process.env.HTTP_PROXY, process.env.HTTPS_PROXY];
};

/**
* Left strip the specified character from a string.
*
Expand All @@ -524,20 +486,6 @@ function GCSUtil(connectionConfig, httpClient, fileStream) {
}
return str;
}

function overrideEnvProxy() {
if (!isEnvProxyOverridden) {
return;
}
process.env[envProxy] = proxyString;
}

function restoreEnvProxy() {
if (!isEnvProxyOverridden) {
return;
}
Util.restoreEnvVar(envProxy, originalEnvProxy);
}
}

module.exports = GCSUtil;
20 changes: 0 additions & 20 deletions test/unit/connection/connection_config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,17 +775,6 @@ describe('ConnectionConfig: basic', function () {
},
errorCode: ErrorCodes.ERR_CONN_CREATE_INVALID_PASSCODE
},
{
name: 'invalid overrideGCSEnvProxy',

options: {
account: 'account',
username: 'username',
password: 'password',
overrideGCSEnvProxy: 123456
},
errorCode: ErrorCodes.ERR_CONN_CREATE_INVALID_OVERRIDE_GCS_ENV_PROXY
},
];

const createNegativeITCallback = function (testCase) {
Expand Down Expand Up @@ -1677,15 +1666,6 @@ describe('ConnectionConfig: basic', function () {
result: '123456',
getter: 'getPasscode',
},
{
name: 'getOverrideGCSEnvProxy',
input: {
...mandatoryOption,
overrideGCSEnvProxy: false,
},
result: false,
getter: 'getOverrideGCSEnvProxy',
},
];

testCases.forEach(({ name, input, result, getter }) => {
Expand Down
Loading

0 comments on commit 35fc4b0

Please sign in to comment.