github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/chunk/client/aws/sse_config_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	s3 "github.com/grafana/loki/pkg/storage/bucket/s3"
    10  )
    11  
    12  func TestNewSSEParsedConfig(t *testing.T) {
    13  	kmsKeyID := "test"
    14  	kmsEncryptionContext := `{"a": "bc", "b": "cd"}`
    15  	// compact form of kmsEncryptionContext
    16  	parsedKMSEncryptionContext := "eyJhIjoiYmMiLCJiIjoiY2QifQ=="
    17  
    18  	tests := []struct {
    19  		name        string
    20  		params      s3.SSEConfig
    21  		expected    *SSEParsedConfig
    22  		expectedErr error
    23  	}{
    24  		{
    25  			name: "Test SSE encryption with SSES3 type",
    26  			params: s3.SSEConfig{
    27  				Type: s3.SSES3,
    28  			},
    29  			expected: &SSEParsedConfig{
    30  				ServerSideEncryption: sseS3Type,
    31  			},
    32  		},
    33  		{
    34  			name: "Test SSE encryption with SSEKMS type without context",
    35  			params: s3.SSEConfig{
    36  				Type:     s3.SSEKMS,
    37  				KMSKeyID: kmsKeyID,
    38  			},
    39  			expected: &SSEParsedConfig{
    40  				ServerSideEncryption: sseKMSType,
    41  				KMSKeyID:             &kmsKeyID,
    42  			},
    43  		},
    44  		{
    45  			name: "Test SSE encryption with SSEKMS type with context",
    46  			params: s3.SSEConfig{
    47  				Type:                 s3.SSEKMS,
    48  				KMSKeyID:             kmsKeyID,
    49  				KMSEncryptionContext: kmsEncryptionContext,
    50  			},
    51  			expected: &SSEParsedConfig{
    52  				ServerSideEncryption: sseKMSType,
    53  				KMSKeyID:             &kmsKeyID,
    54  				KMSEncryptionContext: &parsedKMSEncryptionContext,
    55  			},
    56  		},
    57  		{
    58  			name: "Test invalid SSE type",
    59  			params: s3.SSEConfig{
    60  				Type: "invalid",
    61  			},
    62  			expectedErr: errors.New("SSE type is empty or invalid"),
    63  		},
    64  		{
    65  			name: "Test SSE encryption with SSEKMS type without KMS Key ID",
    66  			params: s3.SSEConfig{
    67  				Type:     s3.SSEKMS,
    68  				KMSKeyID: "",
    69  			},
    70  			expectedErr: errors.New("KMS key id must be passed when SSE-KMS encryption is selected"),
    71  		},
    72  		{
    73  			name: "Test SSE with invalid KMS encryption context JSON",
    74  			params: s3.SSEConfig{
    75  				Type:                 s3.SSEKMS,
    76  				KMSKeyID:             kmsKeyID,
    77  				KMSEncryptionContext: `INVALID_JSON`,
    78  			},
    79  			expectedErr: errors.New("failed to parse KMS encryption context: failed to marshal KMS encryption context: json: error calling MarshalJSON for type json.RawMessage: invalid character 'I' looking for beginning of value"),
    80  		},
    81  	}
    82  
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			result, err := NewSSEParsedConfig(tt.params)
    86  			if tt.expectedErr != nil {
    87  				assert.Equal(t, tt.expectedErr.Error(), err.Error())
    88  			}
    89  			assert.Equal(t, tt.expected, result)
    90  		})
    91  	}
    92  }