11import { SecurityException } from "./tink/exception/security_exception" ;
22
3+ /**
4+ * Key management service (KMS) driver.
5+ */
36export interface KmsDriver {
47 getKeyUrlPrefix ( ) : string
58 newKmsClient ( config : Map < string , string > , keyUrl : string ) : KmsClient
69}
710
11+ /**
12+ * Key management service (KMS) client.
13+ */
814export interface KmsClient {
915 supported ( keyUri : string ) : boolean
1016 encrypt ( plaintext : Buffer ) : Promise < Buffer >
@@ -16,10 +22,18 @@ const kmsDrivers: KmsDriver[] = []
1622const kmsClients : KmsClient [ ] = [ ]
1723
1824
25+ /**
26+ * Register a KMS driver.
27+ * @param kmsDriver - the KMS driver to register
28+ */
1929export function registerKmsDriver ( kmsDriver : KmsDriver ) : void {
2030 kmsDrivers . push ( kmsDriver )
2131}
2232
33+ /**
34+ * Get the KMS driver for the given key URL.
35+ * @param keyUrl - the key URL
36+ */
2337export function getKmsDriver ( keyUrl : string ) : KmsDriver {
2438 for ( let driver of kmsDrivers ) {
2539 if ( keyUrl . startsWith ( driver . getKeyUrlPrefix ( ) ) ) {
@@ -29,10 +43,18 @@ export function getKmsDriver(keyUrl: string): KmsDriver {
2943 throw new SecurityException ( 'no KMS driver found for key URL: ' + keyUrl )
3044}
3145
46+ /**
47+ * Register a KMS client.
48+ * @param kmsClient - the KMS client to register
49+ */
3250export function registerKmsClient ( kmsClient : KmsClient ) : void {
3351 kmsClients . push ( kmsClient )
3452}
3553
54+ /**
55+ * Get the KMS client for the given key URL.
56+ * @param keyUrl - the key URL
57+ */
3658export function getKmsClient ( keyUrl : string ) : KmsClient | null {
3759 for ( let client of kmsClients ) {
3860 if ( client . supported ( keyUrl ) ) {
@@ -42,6 +64,9 @@ export function getKmsClient(keyUrl: string): KmsClient | null {
4264 return null
4365}
4466
67+ /**
68+ * Clear the KMS clients.
69+ */
4570export function clearKmsClients ( ) : void {
4671 kmsClients . length = 0
4772}
0 commit comments