Skip to content

Commit

Permalink
Keep an up-to-date copy of the KMS master key
Browse files Browse the repository at this point in the history
  • Loading branch information
allanrogerr committed Apr 12, 2024
1 parent c66df80 commit 6a91b19
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion internal/kms/kes.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (c *kesClient) keepKeyInCache() {
case <-ctx.Done():
return
}
_ = c.Verify(ctx)
_ = c.ValidateKey(ctx)
}
}

Expand Down Expand Up @@ -472,3 +472,45 @@ func (c *kesClient) Verify(ctx context.Context) []VerifyResult {
}
return results
}

// ValidateKey checks the validity of the KMS Master Key
func (c *kesClient) ValidateKey(ctx context.Context) []VerifyResult {
c.lock.RLock()
defer c.lock.RUnlock()

results := []VerifyResult{}
kmsContext := Context{"MinIO admin API": "ServerInfoHandler"} // Context for a test key operation
for _, endpoint := range c.client.Endpoints {
client := kes.Client{
Endpoints: []string{endpoint},
HTTPClient: c.client.HTTPClient,
}

// 1. Generate a new key using the KMS.
kmsCtx, err := kmsContext.MarshalText()
if err != nil {
results = append(results, VerifyResult{Status: "offline", Endpoint: endpoint})
continue
}
result := VerifyResult{Status: "online", Endpoint: endpoint}
key, err := client.GenerateKey(ctx, env.Get(EnvKESKeyName, ""), kmsCtx)
if err != nil {
result.Encrypt = fmt.Sprintf("Encryption failed: %v", err)
} else {
result.Encrypt = "success"
}
// 2. Verify that we can indeed decrypt the (encrypted) key
decryptedKey, err := client.Decrypt(ctx, env.Get(EnvKESKeyName, ""), key.Ciphertext, kmsCtx)
switch {
case err != nil:
result.Decrypt = fmt.Sprintf("Decryption failed: %v", err)
case subtle.ConstantTimeCompare(key.Plaintext, decryptedKey) != 1:
result.Decrypt = "Decryption failed: decrypted key does not match generated key"
default:
result.Decrypt = "success"
}
fmt.Println(result.Endpoint, result.Status, result.Encrypt, result.Decrypt)
results = append(results, result)
}
return results
}

0 comments on commit 6a91b19

Please sign in to comment.