|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Net.Http; |
| 5 | +using Amazon.Runtime; |
| 6 | +using NUnit.Framework; |
| 7 | +using Snowflake.Data.Core.Authenticator.WorkflowIdentity; |
| 8 | + |
| 9 | +namespace Snowflake.Data.Tests.UnitTests.Authenticator.WorkflowIdentity |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class AwsSignature4SignerTest |
| 13 | + { |
| 14 | + private const string AwsStsHost = "sts.eu-west-1.amazonaws.com"; |
| 15 | + private const string AwsAccessKey = "ABCDEFGHIJ12345KLMNO"; // pragma: allowlist secret |
| 16 | + private const string AwsSecretKey = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStT"; // pragma: allowlist secret |
| 17 | + private const string AwsToken = "HIJKLMNOPQRSTUWXYZ"; // pragma: allowlist secret |
| 18 | + private const string SnowflakeAudience = "snowflakecomputing.com"; |
| 19 | + private static readonly DateTime s_time = new(2025, 6, 12, 15, 46, 13, 5, new GregorianCalendar(), DateTimeKind.Utc); |
| 20 | + private const string ExpectedAmazonDate = "20250612T154613Z"; |
| 21 | + private const string ExpectedSignature = "3fa477a5d4df0381fa0d303cc944723b20e6fff8e1917602a19f4dc67c18df17"; // pragma: allowlist secret |
| 22 | + private static readonly string s_expectedAuthorization = $"AWS4-HMAC-SHA256 Credential={AwsAccessKey}/20250612/eu-west-1/sts/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token;x-snowflake-audience, Signature={ExpectedSignature}"; |
| 23 | + |
| 24 | + [Test] |
| 25 | + public void TestRequestSigning() |
| 26 | + { |
| 27 | + // arrange |
| 28 | + var request = new AttestationRequest |
| 29 | + { |
| 30 | + HttpMethod = HttpMethod.Post, |
| 31 | + Uri = new Uri($"https://{AwsStsHost}/?Action=GetCallerIdentity&Version=2011-06-15"), |
| 32 | + Headers = new Dictionary<string, string> |
| 33 | + { |
| 34 | + { "host", AwsStsHost }, |
| 35 | + { "x-snowflake-audience", SnowflakeAudience } |
| 36 | + } |
| 37 | + }; |
| 38 | + var awsConfig = new AwsConfiguration() |
| 39 | + { |
| 40 | + Region = "eu-west-1", |
| 41 | + Service = "sts", |
| 42 | + Credentials = new ImmutableCredentials(AwsAccessKey, AwsSecretKey, AwsToken) |
| 43 | + }; |
| 44 | + |
| 45 | + // act |
| 46 | + AwsSignature4Signer.AddTokenAndSignatureHeaders(request, awsConfig, s_time); |
| 47 | + |
| 48 | + // assert |
| 49 | + Assert.AreEqual(5, request.Headers.Count); |
| 50 | + Assert.AreEqual(AwsStsHost, request.Headers["host"]); |
| 51 | + Assert.AreEqual(SnowflakeAudience, request.Headers["x-snowflake-audience"]); |
| 52 | + Assert.AreEqual(ExpectedAmazonDate, request.Headers["x-amz-date"]); |
| 53 | + Assert.AreEqual(AwsToken, request.Headers["x-amz-security-token"]); |
| 54 | + Assert.AreEqual(s_expectedAuthorization, request.Headers["authorization"]); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments