Skip to content

Instantly share code, notes, and snippets.

@APTy
Created July 22, 2024 21:12
Show Gist options
  • Save APTy/9c0b859a5da3aa4003191cdd1681351c to your computer and use it in GitHub Desktop.
Save APTy/9c0b859a5da3aa4003191cdd1681351c to your computer and use it in GitHub Desktop.
Snowflake-sdk patch to fix #705 when using application_default_credentials.json
diff --git a/node_modules/snowflake-sdk/lib/file_transfer_agent/gcs_util.js b/node_modules/snowflake-sdk/lib/file_transfer_agent/gcs_util.js
index 2a9cf25..8611b31 100644
--- a/node_modules/snowflake-sdk/lib/file_transfer_agent/gcs_util.js
+++ b/node_modules/snowflake-sdk/lib/file_transfer_agent/gcs_util.js
@@ -138,7 +138,7 @@ function GCSUtil(httpclient, filestream) {
let matDescKey;
try {
- if (shouldPerformGCPBucket(accessToken)) {
+ if (await shouldPerformGCPBucket(accessToken)) {
const gcsLocation = this.extractBucketNameAndPath(meta['stageInfo']['location']);
const metadata = await meta['client'].gcsClient
@@ -278,7 +278,7 @@ function GCSUtil(httpclient, filestream) {
}
try {
- if (shouldPerformGCPBucket(accessToken)) {
+ if (await shouldPerformGCPBucket(accessToken)) {
const gcsLocation = this.extractBucketNameAndPath(meta['stageInfo']['location']);
await meta['client'].gcsClient
@@ -351,7 +351,7 @@ function GCSUtil(httpclient, filestream) {
let size;
try {
- if (shouldPerformGCPBucket(accessToken)) {
+ if (await shouldPerformGCPBucket(accessToken)) {
const gcsLocation = this.extractBucketNameAndPath(meta['stageInfo']['location']);
await meta['client'].gcsClient
diff --git a/node_modules/snowflake-sdk/lib/util.js b/node_modules/snowflake-sdk/lib/util.js
index e8bc492..13655d8 100644
--- a/node_modules/snowflake-sdk/lib/util.js
+++ b/node_modules/snowflake-sdk/lib/util.js
@@ -5,6 +5,10 @@
const util = require('util');
const Url = require('url');
const os = require('os');
+const path = require('path');
+const fs = require('fs/promises')
+
+const APPLICATION_DEFAULT_CREDENTIALS_FILE = '~/.config/gcloud/application_default_credentials.json'
/**
* Note: A simple wrapper around util.inherits() for now, but this might change
@@ -660,10 +664,33 @@ exports.removeScheme = function (input) {
return input.toString().replace(/(^\w+:|^)\/\//, '');
};
-exports.shouldPerformGCPBucket = function (accessToken) {
+exports.shouldPerformGCPBucket = async function (accessToken) {
+ if (await fileExists(resolveHome(APPLICATION_DEFAULT_CREDENTIALS_FILE))) {
+ return false
+ }
return !!accessToken && process.env.SNOWFLAKE_FORCE_GCP_USE_DOWNSCOPED_CREDENTIAL !== 'true';
};
+function resolveHome(filepath) {
+ if (filepath[0] === '~') {
+ return path.join(process.env.HOME, filepath.slice(1));
+ }
+ return filepath;
+}
+
+async function fileExists(filename) {
+ try {
+ await fs.access(filename)
+ return true
+ } catch (err) {
+ if (err.code === 'ENOENT') {
+ return false
+ }
+
+ throw err
+ }
+}
+
/**
* Checks if the provided file or directory permissions are correct.
* @param filePath
@APTy
Copy link
Author

APTy commented Jul 22, 2024

Put this file in patches/snowflake-sdk+1.11.0.patch and apply with npx patch-package.

Patches cleanly with snowflake-sdk@1.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment