Skip to content

Commit

Permalink
Ensure different key ids use different client instances (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayokota authored Dec 4, 2024
1 parent 5e79eef commit d1e7c67
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion schemaregistry/rules/encryption/awskms/aws-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {AwsCredentialIdentity, AwsCredentialIdentityProvider} from "@smithy/type
export class AwsKmsClient implements KmsClient {

private kmsClient: KMSClient
private keyUri: string
private keyId: string

constructor(keyUri: string, creds?: AwsCredentialIdentity | AwsCredentialIdentityProvider) {
if (!keyUri.startsWith(AwsKmsDriver.PREFIX)) {
throw new Error(`key uri must start with ${AwsKmsDriver.PREFIX}`)
}
this.keyUri = keyUri
this.keyId = keyUri.substring(AwsKmsDriver.PREFIX.length)
const tokens = this.keyId.split(':')
if (tokens.length < 4) {
Expand All @@ -29,7 +31,7 @@ export class AwsKmsClient implements KmsClient {
}

supported(keyUri: string): boolean {
return keyUri.startsWith(AwsKmsDriver.PREFIX)
return this.keyUri === keyUri
}

async encrypt(plaintext: Buffer): Promise<Buffer> {
Expand Down
4 changes: 3 additions & 1 deletion schemaregistry/rules/encryption/azurekms/azure-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ export class AzureKmsClient implements KmsClient {
private static ALGORITHM: EncryptionAlgorithm = 'RSA-OAEP-256'

private kmsClient: CryptographyClient
private keyUri: string
private keyId: string

constructor(keyUri: string, creds: TokenCredential) {
if (!keyUri.startsWith(AzureKmsDriver.PREFIX)) {
throw new Error(`key uri must start with ${AzureKmsDriver.PREFIX}`)
}
this.keyUri = keyUri
this.keyId = keyUri.substring(AzureKmsDriver.PREFIX.length)
this.kmsClient = new CryptographyClient(this.keyId, creds)
}

supported(keyUri: string): boolean {
return keyUri.startsWith(AzureKmsDriver.PREFIX)
return this.keyUri === keyUri
}

async encrypt(plaintext: Buffer): Promise<Buffer> {
Expand Down
4 changes: 3 additions & 1 deletion schemaregistry/rules/encryption/gcpkms/gcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import {KeyManagementServiceClient} from "@google-cloud/kms";
export class GcpKmsClient implements KmsClient {

private kmsClient: KeyManagementServiceClient
private keyUri: string
private keyId: string

constructor(keyUri: string, creds?: GcpCredentials) {
if (!keyUri.startsWith(GcpKmsDriver.PREFIX)) {
throw new Error(`key uri must start with ${GcpKmsDriver.PREFIX}`)
}
this.keyUri = keyUri
this.keyId = keyUri.substring(GcpKmsDriver.PREFIX.length)
this.kmsClient = creds != null
? new KeyManagementServiceClient({credentials: creds})
: new KeyManagementServiceClient()
}

supported(keyUri: string): boolean {
return keyUri.startsWith(GcpKmsDriver.PREFIX)
return this.keyUri === keyUri
}

async encrypt(plaintext: Buffer): Promise<Buffer> {
Expand Down
4 changes: 3 additions & 1 deletion schemaregistry/rules/encryption/hcvault/hcvault-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import NodeVault from "node-vault";
export class HcVaultClient implements KmsClient {

private kmsClient: NodeVault.client
private keyUri: string
private keyId: string
private keyName: string

constructor(keyUri: string, namespace?: string, token?: string) {
if (!keyUri.startsWith(HcVaultDriver.PREFIX)) {
throw new Error(`key uri must start with ${HcVaultDriver.PREFIX}`)
}
this.keyUri = keyUri
this.keyId = keyUri.substring(HcVaultDriver.PREFIX.length)
let url = new URL(this.keyId)
let parts = url.pathname.split('/')
Expand All @@ -28,7 +30,7 @@ export class HcVaultClient implements KmsClient {
}

supported(keyUri: string): boolean {
return keyUri.startsWith(HcVaultDriver.PREFIX)
return this.keyUri === keyUri
}

async encrypt(plaintext: Buffer): Promise<Buffer> {
Expand Down

0 comments on commit d1e7c67

Please sign in to comment.