Validators { public static void validateCryptoKeyUri(String kmsKeyUri) throws GeneralSecurityException { if (!GCP_KMS_CRYPTO_KEY_PATTERN.matcher(kmsKeyUri).matches()) { if (GCP_KMS_CRYPTO_KEY_VERSION_PATTERN.matcher(kmsKeyUri).matches()) { throw new GeneralSecurityException( "Invalid Google Cloud KMS Key URI. " + "The URI must point to a CryptoKey, not a CryptoKeyVersion"); } throw new GeneralSecurityException( "Invalid Google Cloud KMS Key URI. " + "The URI must point to a CryptoKey in the format " + "projectskeyRings/*/cryptoKeys/*. " + "See https: + "/projects.locations.keyRings.cryptoKeys#CryptoKey"); } } private  Validators(); static void validateTypeUrl(String typeUrl); static void validateAesKeySize(int sizeInBytes); static void validateVersion(int candidate, int maxExpected); static void validateSignatureHash(HashType hash); static void validateRsaModulusSize(int modulusSize); static void validateRsaPublicExponent(
      BigInteger publicExponent); static void validateNotExists(File f); static void validateExists(File f); static String validateKmsKeyUriAndRemovePrefix(String expectedPrefix, String kmsKeyUri); static void validateCryptoKeyUri(String kmsKeyUri);  }
@Test public void testValidateCryptoKeyUri() throws Exception { try { Validators.validateCryptoKeyUri("a"); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { TestUtil.assertExceptionContains(e, "Invalid Google Cloud KMS Key URI"); } String cryptoKey = TestUtil.createGcpKmsKeyUri("projectId", "locationId", "ringId", "cryptoKeyId"); try { Validators.validateCryptoKeyUri(cryptoKey); } catch (GeneralSecurityException e) { fail("Valid CryptoKey URI should work: " + cryptoKey); } cryptoKey = TestUtil.createGcpKmsKeyUri("projectId.", "locationId-", "ringId_", "cryptoKeyId~"); try { Validators.validateCryptoKeyUri(cryptoKey); } catch (GeneralSecurityException e) { fail("Valid CryptoKey URI should work: " + cryptoKey); } cryptoKey = TestUtil.createGcpKmsKeyUri("projectId%", "locationId", "ringId", "cryptoKeyId"); try { Validators.validateCryptoKeyUri(cryptoKey); fail("CryptoKey URI cannot contain %"); } catch (GeneralSecurityException e) { } cryptoKey = TestUtil.createGcpKmsKeyUri("projectId/", "locationId", "ringId", "cryptoKeyId"); try { Validators.validateCryptoKeyUri(cryptoKey); fail("CryptoKey URI cannot contain /"); } catch (GeneralSecurityException e) { } String cryptoVersion = TestUtil.createGcpKmsKeyUri("projectId", "locationId", "ringId", "cryptoKeyId") + "/cryptoKeyVersions/versionId"; try { Validators.validateCryptoKeyUri(cryptoVersion); fail("CryptoKeyVersion is not a valid CryptoKey"); } catch (GeneralSecurityException e) { TestUtil.assertExceptionContains( e, "The URI must point to a CryptoKey, not a CryptoKeyVersion"); } }