github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/kms/single-key_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package kms
    19  
    20  import (
    21  	"bytes"
    22  	"context"
    23  	"encoding/base64"
    24  	"testing"
    25  )
    26  
    27  func TestSingleKeyRoundtrip(t *testing.T) {
    28  	KMS, err := Parse("my-key:eEm+JI9/q4JhH8QwKvf3LKo4DEBl6QbfvAl1CAbMIv8=")
    29  	if err != nil {
    30  		t.Fatalf("Failed to initialize KMS: %v", err)
    31  	}
    32  
    33  	key, err := KMS.GenerateKey(context.Background(), "my-key", Context{})
    34  	if err != nil {
    35  		t.Fatalf("Failed to generate key: %v", err)
    36  	}
    37  	plaintext, err := KMS.DecryptKey(key.KeyID, key.Ciphertext, Context{})
    38  	if err != nil {
    39  		t.Fatalf("Failed to decrypt key: %v", err)
    40  	}
    41  	if !bytes.Equal(key.Plaintext, plaintext) {
    42  		t.Fatalf("Decrypted key does not match generated one: got %x - want %x", key.Plaintext, plaintext)
    43  	}
    44  }
    45  
    46  func TestDecryptKey(t *testing.T) {
    47  	KMS, err := Parse("my-key:eEm+JI9/q4JhH8QwKvf3LKo4DEBl6QbfvAl1CAbMIv8=")
    48  	if err != nil {
    49  		t.Fatalf("Failed to initialize KMS: %v", err)
    50  	}
    51  
    52  	for i, test := range decryptKeyTests {
    53  		dataKey, err := base64.StdEncoding.DecodeString(test.Plaintext)
    54  		if err != nil {
    55  			t.Fatalf("Test %d: failed to decode plaintext key: %v", i, err)
    56  		}
    57  		ciphertext, err := base64.StdEncoding.DecodeString(test.Ciphertext)
    58  		if err != nil {
    59  			t.Fatalf("Test %d: failed to decode ciphertext key: %v", i, err)
    60  		}
    61  		plaintext, err := KMS.DecryptKey(test.KeyID, ciphertext, test.Context)
    62  		if err != nil {
    63  			t.Fatalf("Test %d: failed to decrypt key: %v", i, err)
    64  		}
    65  		if !bytes.Equal(plaintext, dataKey) {
    66  			t.Fatalf("Test %d: decrypted key does not generated one: got %x - want %x", i, plaintext, dataKey)
    67  		}
    68  	}
    69  }
    70  
    71  var decryptKeyTests = []struct {
    72  	KeyID      string
    73  	Plaintext  string
    74  	Ciphertext string
    75  	Context    Context
    76  }{
    77  	{
    78  		KeyID:      "my-key",
    79  		Plaintext:  "zmS7NrG765UZ0ZN85oPjybelxqVvpz01vxsSpOISy2M=",
    80  		Ciphertext: "eyJhZWFkIjoiQ2hhQ2hhMjBQb2x5MTMwNSIsIml2IjoiSmJJK3Z3dll3dzFsQ2I1VnBrQUZ1UT09Iiwibm9uY2UiOiJBUmpJakp4QlNENTQxR3o4IiwiYnl0ZXMiOiJLQ2JFYzJzQTBUTHZBN2FXVFdhMjNBZGNjVmZKTXBPeHdnRzhobSs0UGFOcnhZZnkxeEZXWmcyZ0VlblZyT2d2In0=",
    81  	},
    82  	{
    83  		KeyID:      "my-key",
    84  		Plaintext:  "UnPWsZgVI+T4L9WGNzFlP1PsP1Z6hn2Fx8ISeZfDGnA=",
    85  		Ciphertext: "eyJhZWFkIjoiQ2hhQ2hhMjBQb2x5MTMwNSIsIml2IjoicjQreWZpVmJWSVlSMFoySTlGcSs2Zz09Iiwibm9uY2UiOiIyWXB3R3dFNTlHY1ZyYUkzIiwiYnl0ZXMiOiJrL3N2TWdsT1U3L0tnd3Y3M2hlRzM4TldXNTc1WExjRnAzU2F4UUhETWpKR1l5UkkzRml5Z3UyT2V1dEdQWE5MIn0=",
    86  		Context:    Context{"key": "value"},
    87  	},
    88  }