github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/storage/bucket/s3/config_test.go (about) 1 package s3 2 3 import ( 4 "encoding/base64" 5 "net/http" 6 "testing" 7 8 "github.com/grafana/dskit/flagext" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestSSEConfig_Validate(t *testing.T) { 14 tests := map[string]struct { 15 setup func() *SSEConfig 16 expected error 17 }{ 18 "should pass with default config": { 19 setup: func() *SSEConfig { 20 cfg := &SSEConfig{} 21 flagext.DefaultValues(cfg) 22 23 return cfg 24 }, 25 }, 26 "should fail on invalid SSE type": { 27 setup: func() *SSEConfig { 28 return &SSEConfig{ 29 Type: "unknown", 30 } 31 }, 32 expected: errUnsupportedSSEType, 33 }, 34 "should fail on invalid SSE KMS encryption context": { 35 setup: func() *SSEConfig { 36 return &SSEConfig{ 37 Type: SSEKMS, 38 KMSEncryptionContext: "!{}!", 39 } 40 }, 41 expected: errInvalidSSEContext, 42 }, 43 "should pass on valid SSE KMS encryption context": { 44 setup: func() *SSEConfig { 45 return &SSEConfig{ 46 Type: SSEKMS, 47 KMSEncryptionContext: `{"department": "10103.0"}`, 48 } 49 }, 50 }, 51 } 52 53 for testName, testData := range tests { 54 t.Run(testName, func(t *testing.T) { 55 assert.Equal(t, testData.expected, testData.setup().Validate()) 56 }) 57 } 58 } 59 60 func TestSSEConfig_BuildMinioConfig(t *testing.T) { 61 tests := map[string]struct { 62 cfg *SSEConfig 63 expectedType string 64 expectedKeyID string 65 expectedContext string 66 }{ 67 "SSE KMS without encryption context": { 68 cfg: &SSEConfig{ 69 Type: SSEKMS, 70 KMSKeyID: "test-key", 71 }, 72 expectedType: "aws:kms", 73 expectedKeyID: "test-key", 74 expectedContext: "", 75 }, 76 "SSE KMS with encryption context": { 77 cfg: &SSEConfig{ 78 Type: SSEKMS, 79 KMSKeyID: "test-key", 80 KMSEncryptionContext: "{\"department\":\"10103.0\"}", 81 }, 82 expectedType: "aws:kms", 83 expectedKeyID: "test-key", 84 expectedContext: "{\"department\":\"10103.0\"}", 85 }, 86 } 87 88 for testName, testData := range tests { 89 t.Run(testName, func(t *testing.T) { 90 sse, err := testData.cfg.BuildMinioConfig() 91 require.NoError(t, err) 92 93 headers := http.Header{} 94 sse.Marshal(headers) 95 96 assert.Equal(t, testData.expectedType, headers.Get("x-amz-server-side-encryption")) 97 assert.Equal(t, testData.expectedKeyID, headers.Get("x-amz-server-side-encryption-aws-kms-key-id")) 98 assert.Equal(t, base64.StdEncoding.EncodeToString([]byte(testData.expectedContext)), headers.Get("x-amz-server-side-encryption-context")) 99 }) 100 } 101 } 102 103 func TestParseKMSEncryptionContext(t *testing.T) { 104 actual, err := parseKMSEncryptionContext("") 105 assert.NoError(t, err) 106 assert.Equal(t, map[string]string(nil), actual) 107 108 expected := map[string]string{ 109 "department": "10103.0", 110 } 111 actual, err = parseKMSEncryptionContext(`{"department": "10103.0"}`) 112 assert.NoError(t, err) 113 assert.Equal(t, expected, actual) 114 }