1919
2020import com .google .cloud .kms .v1 .CryptoKey ;
2121import com .google .cloud .kms .v1 .CryptoKeyName ;
22- import com .google .cloud .kms .v1 .CryptoKeyVersion ;
2322import com .google .cloud .kms .v1 .KeyManagementServiceClient ;
2423import com .google .cloud .kms .v1 .KeyRingName ;
25- import com .google .cloud .kms .v1 .LocationName ;
2624import com .google .cloud .secretmanager .v1 .ProjectName ;
2725import com .google .cloud .secretmanager .v1 .SecretManagerServiceClient ;
2826import com .google .cloud .secretmanager .v1 .SecretName ;
@@ -60,8 +58,8 @@ public class GroupByKeyIT {
6058 private static String gcpSecretVersionName ;
6159 private static String gcpHsmSecretOption ;
6260 private static String secretId ;
63- private static String keyRingId ;
64- private static String keyId ;
61+ private static final String KEY_RING_ID = "gbek-it-key-ring" ;
62+ private static final String KEY_ID = "gbek-it-key" ;
6563
6664 @ BeforeClass
6765 public static void setup () throws IOException {
@@ -102,20 +100,28 @@ public static void setup() throws IOException {
102100 try {
103101 KeyManagementServiceClient kmsClient = KeyManagementServiceClient .create ();
104102 String locationId = "global" ;
105- keyRingId = "gbekit-key-ring-" + new SecureRandom ().nextInt (10000 );
106- KeyRingName keyRingName = KeyRingName .of (PROJECT_ID , locationId , keyRingId );
107- LocationName locationName = LocationName .of (PROJECT_ID , locationId );
108- kmsClient .createKeyRing (
109- locationName , keyRingId , com .google .cloud .kms .v1 .KeyRing .newBuilder ().build ());
110-
111- keyId = "gbekit-key-" + new SecureRandom ().nextInt (10000 );
112- CryptoKey key =
113- CryptoKey .newBuilder ().setPurpose (CryptoKey .CryptoKeyPurpose .ENCRYPT_DECRYPT ).build ();
114- kmsClient .createCryptoKey (keyRingName , keyId , key );
103+ KeyRingName keyRingName = KeyRingName .of (PROJECT_ID , locationId , KEY_RING_ID );
104+ com .google .cloud .kms .v1 .LocationName locationName =
105+ com .google .cloud .kms .v1 .LocationName .of (PROJECT_ID , locationId );
106+ try {
107+ kmsClient .getKeyRing (keyRingName );
108+ } catch (Exception e ) {
109+ kmsClient .createKeyRing (
110+ locationName , KEY_RING_ID , com .google .cloud .kms .v1 .KeyRing .newBuilder ().build ());
111+ }
112+
113+ CryptoKeyName keyName = CryptoKeyName .of (PROJECT_ID , locationId , KEY_RING_ID , KEY_ID );
114+ try {
115+ kmsClient .getCryptoKey (keyName );
116+ } catch (Exception e ) {
117+ CryptoKey key =
118+ CryptoKey .newBuilder ().setPurpose (CryptoKey .CryptoKeyPurpose .ENCRYPT_DECRYPT ).build ();
119+ kmsClient .createCryptoKey (keyRingName , KEY_ID , key );
120+ }
115121 gcpHsmSecretOption =
116122 String .format (
117123 "type:gcphsmgeneratedsecret;project_id:%s;location_id:%s;key_ring_id:%s;key_id:%s;job_name:%s" ,
118- PROJECT_ID , locationId , keyRingId , keyId , secretId );
124+ PROJECT_ID , locationId , KEY_RING_ID , KEY_ID , secretId );
119125 } catch (Exception e ) {
120126 gcpHsmSecretOption = null ;
121127 }
@@ -128,16 +134,6 @@ public static void tearDown() throws IOException {
128134 SecretName secretName = SecretName .of (PROJECT_ID , secretId );
129135 client .deleteSecret (secretName );
130136 }
131- if (gcpHsmSecretOption != null ) {
132- KeyManagementServiceClient kmsClient = KeyManagementServiceClient .create ();
133- CryptoKeyName keyName = CryptoKeyName .of (PROJECT_ID , "global" , keyRingId , keyId );
134- for (CryptoKeyVersion version : kmsClient .listCryptoKeyVersions (keyName ).iterateAll ()) {
135- if (version .getState () == CryptoKeyVersion .CryptoKeyVersionState .ENABLED
136- || version .getState () == CryptoKeyVersion .CryptoKeyVersionState .DISABLED ) {
137- kmsClient .destroyCryptoKeyVersion (version .getName ());
138- }
139- }
140- }
141137 }
142138
143139 @ Test
@@ -219,7 +215,7 @@ public void testGroupByKeyWithExistingGcpHsmGeneratedSecretOption() throws Excep
219215 return ;
220216 }
221217 // Create the secret beforehand
222- new GcpHsmGeneratedSecret (PROJECT_ID , "global" , keyRingId , keyId , secretId ).getSecretBytes ();
218+ new GcpHsmGeneratedSecret (PROJECT_ID , "global" , KEY_RING_ID , KEY_ID , secretId ).getSecretBytes ();
223219
224220 PipelineOptions options = TestPipeline .testingPipelineOptions ();
225221 options .setGbek (gcpHsmSecretOption );
0 commit comments