Skip to content

Commit 7e6a197

Browse files
juergwcopybara-github
authored andcommitted
Deprecate KMS Registration functions in Golang.
In almost all uses cases, it is easier and less error-prone to directly use the KMS AEAD, instead of registering the KMS client. PiperOrigin-RevId: 558052260 Change-Id: Icd21a905b7fb7adc5a03f9476becd7e20c202a0b
1 parent a1888a7 commit 7e6a197

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

aead/aead_key_templates.go

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ func XChaCha20Poly1305KeyTemplate() *tinkpb.KeyTemplate {
138138
// remote KEK.
139139
//
140140
// If either uri or dekTemplate contain invalid input, an error is returned.
141+
//
142+
// Deprecated: Instead, call kmsClient.GetAEAD to get a remote AEAD, create
143+
// an envelope AEAD using aead.NewKMSEnvelopeAEAD2.
144+
// There is no need to call registry.RegisterKMSClient anymore.
141145
func CreateKMSEnvelopeAEADKeyTemplate(uri string, dekTemplate *tinkpb.KeyTemplate) (*tinkpb.KeyTemplate, error) {
142146
if !isSupporedKMSEnvelopeDEK(dekTemplate.GetTypeUrl()) {
143147
return nil, fmt.Errorf("unsupported DEK key type %s. Only Tink AEAD key types are supported", dekTemplate.GetTypeUrl())

aead/aead_key_templates_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,52 @@ func TestKMSEnvelopeAEADKeyTemplateMultipleKeysSameKEK(t *testing.T) {
174174
}
175175
}
176176

177+
// This test shows how migrate away from CreateKMSEnvelopeAEADKeyTemplate.
178+
func TestMigrateFromCreateKMSEnvelopeAEADKeyTemplateToNewKMSEnvelopeAEAD2(t *testing.T) {
179+
kmsClient, err := fakekms.NewClient("fake-kms://")
180+
if err != nil {
181+
t.Fatalf("fakekms.NewClient('fake-kms://') failed: %v", err)
182+
}
183+
kekURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"
184+
185+
// This code:
186+
registry.RegisterKMSClient(kmsClient)
187+
kmsEnvelopeAEADTemplate, err := aead.CreateKMSEnvelopeAEADKeyTemplate(kekURI, aead.AES128GCMKeyTemplate())
188+
if err != nil {
189+
t.Fatalf("CreateKMSEnvelopeAEADKeyTemplate() failed: %v", err)
190+
}
191+
handle, err := keyset.NewHandle(kmsEnvelopeAEADTemplate)
192+
if err != nil {
193+
t.Fatalf("keyset.NewHandle(kmsEnvelopeAEADTemplate) failed: %v", err)
194+
}
195+
aead1, err := aead.New(handle)
196+
if err != nil {
197+
t.Fatalf("aead.New(handle) failed: %v", err)
198+
}
199+
// can be replace by this:
200+
kekAEAD, err := kmsClient.GetAEAD(kekURI)
201+
if err != nil {
202+
t.Fatalf("kmsClient.GetAEAD(kekURI) failed: %v", err)
203+
}
204+
aead2 := aead.NewKMSEnvelopeAEAD2(aead.AES128GCMKeyTemplate(), kekAEAD)
205+
206+
// Check that aead1 and aead2 are compatible.
207+
plaintext := []byte("plaintext")
208+
associatedData := []byte("associatedData")
209+
210+
ciphertext, err := aead1.Encrypt(plaintext, associatedData)
211+
if err != nil {
212+
t.Fatalf("aead1.Encrypt(plaintext, associatedData) failed: %v", err)
213+
}
214+
decrypted, err := aead2.Decrypt(ciphertext, associatedData)
215+
if err != nil {
216+
t.Fatalf("aead2.Decrypt(ciphertext, associatedData) failed: %v", err)
217+
}
218+
if !bytes.Equal(plaintext, decrypted) {
219+
t.Fatalf("decrypted data doesn't match plaintext, got: %q, want: %q", decrypted, plaintext)
220+
}
221+
}
222+
177223
// Testing deprecated function, ignoring GoDeprecated.
178224
func TestCreateKMSEnvelopeAEADKeyTemplateCompatibleWithKMSEnevelopeAEADKeyTemplate(t *testing.T) {
179225
fakeKmsClient, err := fakekms.NewClient("fake-kms://")

core/registry/registry.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,24 @@ func Primitive(typeURL string, serializedKey []byte) (interface{}, error) {
120120
return keyManager.Primitive(serializedKey)
121121
}
122122

123-
// RegisterKMSClient is used to register a new KMS client
123+
// RegisterKMSClient is used to register a new KMS client.
124+
//
125+
// This function adds an object to a global list. It should only be called on
126+
// startup.
127+
//
128+
// Deprecated: It is preferable to not register clients. Instead, call
129+
// kmsClient.GetAEAD to get a remote AEAD, and then use it to encrypt
130+
// a keyset with keyset.Write, or to create an envelope AEAD using
131+
// aead.NewKMSEnvelopeAEAD2.
124132
func RegisterKMSClient(kmsClient KMSClient) {
125133
kmsClientsMu.Lock()
126134
defer kmsClientsMu.Unlock()
127135
kmsClients = append(kmsClients, kmsClient)
128136
}
129137

130138
// GetKMSClient fetches a KMSClient by a given URI.
139+
//
140+
// Deprecated: It is preferable to not register clients.
131141
func GetKMSClient(keyURI string) (KMSClient, error) {
132142
kmsClientsMu.RLock()
133143
defer kmsClientsMu.RUnlock()
@@ -140,6 +150,10 @@ func GetKMSClient(keyURI string) (KMSClient, error) {
140150
}
141151

142152
// ClearKMSClients removes all registered KMS clients.
153+
//
154+
// Should only be used in tests.
155+
//
156+
// Deprecated: It is preferable to not register clients.
143157
func ClearKMSClients() {
144158
kmsClientsMu.Lock()
145159
defer kmsClientsMu.Unlock()

0 commit comments

Comments
 (0)