Skip to content

Commit 47235fb

Browse files
[SNOW-1313929] Add https to endpoints without it (#914)
### Description - Add https to endpoints without it - Add unit test for SetCommonClientConfig ### Checklist - [x] Code compiles correctly - [x] Code is formatted according to [Coding Conventions](../blob/master/CodingConventions.md) - [x] Created tests which fail without the change (if possible) - [x] All tests passing (`dotnet test`) - [x] Extended the README / documentation, if necessary - [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
1 parent 46e496a commit 47235fb

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

Snowflake.Data.Tests/UnitTests/SFS3ClientTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
using System;
6+
using Amazon.S3.Encryption;
67

78
namespace Snowflake.Data.Tests.UnitTests
89
{
@@ -219,6 +220,36 @@ public void TestUploadFile(string requestKey, ResultStatus expectedResultStatus)
219220
AssertForUploadFileTests(expectedResultStatus);
220221
}
221222

223+
[Test]
224+
public void TestAppendHttpsToEndpoint()
225+
{
226+
// Arrange
227+
var amazonS3Client = new AmazonS3Config();
228+
var endpoint = "endpointWithNoHttps.com";
229+
var expectedEndpoint = "https://endpointWithNoHttps.com";
230+
231+
// ACT
232+
SFS3Client.SetCommonClientConfig(amazonS3Client, string.Empty, endpoint, 1, 0);
233+
234+
// Assert
235+
Assert.That(amazonS3Client.ServiceURL, Is.EqualTo(expectedEndpoint));
236+
}
237+
238+
[Test]
239+
public void TestAppendHttpsToEndpointWithBrackets()
240+
{
241+
// Arrange
242+
var amazonS3Client = new AmazonS3Config();
243+
var endpoint = "[endpointWithNoHttps.com]";
244+
var expectedEndpoint = "https://endpointWithNoHttps.com";
245+
246+
// ACT
247+
SFS3Client.SetCommonClientConfig(amazonS3Client, string.Empty, endpoint, 1, 0);
248+
249+
// Assert
250+
Assert.That(amazonS3Client.ServiceURL, Is.EqualTo(expectedEndpoint));
251+
}
252+
222253
[Test]
223254
[TestCase(MockS3Client.AwsStatusOk, ResultStatus.UPLOADED)]
224255
[TestCase(SFS3Client.EXPIRED_TOKEN, ResultStatus.RENEW_TOKEN)]

Snowflake.Data/Core/FileTransfer/StorageClient/SFS3Client.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public SFS3Client(
116116
stageInfo.endPoint,
117117
maxRetry,
118118
parallel);
119-
119+
120120
// Get the AWS token value and create the S3 client
121121
if (stageInfo.stageCredentials.TryGetValue(AWS_TOKEN, out string awsSessionToken))
122122
{
@@ -164,7 +164,7 @@ public RemoteLocation ExtractBucketNameAndPath(string stageLocation)
164164
{
165165
bucketName = stageLocation.Substring(0, stageLocation.IndexOf('/'));
166166

167-
s3path = stageLocation.Substring(stageLocation.IndexOf('/') + 1,
167+
s3path = stageLocation.Substring(stageLocation.IndexOf('/') + 1,
168168
stageLocation.Length - stageLocation.IndexOf('/') - 1);
169169
if (s3path != null && !s3path.EndsWith("/"))
170170
{
@@ -287,13 +287,13 @@ private FileHeader HandleFileHeaderResponse(ref SFFileMetadata fileMetadata, Get
287287
}
288288

289289
/// <summary>
290-
/// Set the client configuration common to both client with and without client-side
290+
/// Set the client configuration common to both client with and without client-side
291291
/// encryption.
292292
/// </summary>
293293
/// <param name="clientConfig">The client config to update.</param>
294294
/// <param name="region">The region if any.</param>
295295
/// <param name="endpoint">The endpoint if any.</param>
296-
private static void SetCommonClientConfig(
296+
internal static void SetCommonClientConfig(
297297
AmazonS3Config clientConfig,
298298
string region,
299299
string endpoint,
@@ -309,23 +309,25 @@ private static void SetCommonClientConfig(
309309
}
310310

311311
// If a specific endpoint is specified use this
312-
if ((null != endpoint) && (0 != endpoint.Length))
312+
if (!string.IsNullOrEmpty(endpoint))
313313
{
314314
var start = endpoint.IndexOf('[');
315315
var end = endpoint.IndexOf(']');
316-
if(start > -1 && end > -1 && end > start)
316+
if (start > -1 && end > -1 && end > start)
317317
{
318318
endpoint = endpoint.Substring(start + 1, end - start - 1);
319-
if(!endpoint.Contains("https"))
320-
{
321-
endpoint = "https://" + endpoint;
322-
}
323319
}
320+
321+
if (!endpoint.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
322+
{
323+
endpoint = "https://" + endpoint;
324+
}
325+
324326
clientConfig.ServiceURL = endpoint;
325327
}
326328

327329
// The region information used to determine the endpoint for the service.
328-
// RegionEndpoint and ServiceURL are mutually exclusive properties.
330+
// RegionEndpoint and ServiceURL are mutually exclusive properties.
329331
// If both stageInfo.endPoint and stageInfo.region have a value, stageInfo.region takes
330332
// precedence and ServiceUrl will be reset to null.
331333
if ((null != region) && (0 != region.Length))
@@ -337,7 +339,6 @@ private static void SetCommonClientConfig(
337339
// Unavailable for .net framework 4.6
338340
//clientConfig.MaxConnectionsPerServer = parallel;
339341
clientConfig.MaxErrorRetry = maxRetry;
340-
341342
}
342343

343344
/// <summary>
@@ -410,7 +411,7 @@ private PutObjectRequest GetPutObjectRequest(ref AmazonS3Client client, SFFileMe
410411
{
411412
PutGetStageInfo stageInfo = fileMetadata.stageInfo;
412413
RemoteLocation location = ExtractBucketNameAndPath(stageInfo.location);
413-
414+
414415
// Create S3 PUT request
415416
fileBytesStream.Position = 0;
416417
PutObjectRequest putObjectRequest = new PutObjectRequest
@@ -585,4 +586,4 @@ private SFFileMetadata HandleDownloadFileErr(Exception ex, SFFileMetadata fileMe
585586
return fileMetadata;
586587
}
587588
}
588-
}
589+
}

0 commit comments

Comments
 (0)