public static String getJceProvider(Configuration conf) {
    final String provider = conf.getTrimmed(
        CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_KEY, "");
    final boolean autoAdd = conf.getBoolean(
        CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_AUTO_ADD_KEY,
        CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_AUTO_ADD_DEFAULT);

    // For backward compatible, auto-add BOUNCY_CASTLE_PROVIDER_CLASS when the provider is "BC".
    if (autoAdd && BOUNCY_CASTLE_PROVIDER_NAME.equals(provider)) {
      try {
        // Use reflection in order to avoid statically loading the class.
        final Class<?> clazz = Class.forName(BOUNCY_CASTLE_PROVIDER_CLASS);
        Security.addProvider((Provider) clazz.getConstructor().newInstance());
        LOG.debug("Successfully added security provider {}", provider);
        if (LOG.isTraceEnabled()) {
          LOG.trace("Trace", new Throwable());
        }
      } catch (ClassNotFoundException e) {
        LOG_FAILED_TO_LOAD_CLASS.warn("Failed to load " + BOUNCY_CASTLE_PROVIDER_CLASS, e);
      } catch (Exception e) {
        LOG_FAILED_TO_ADD_PROVIDER.warn("Failed to add security provider for {}", provider, e);
      }
    }
    return provider;
  }
@Test(timeout = 5_000)
  public void testAutoAddEnabled() {
    assertRemoveProvider();

    final Configuration conf = new Configuration();
    Assertions.assertThat(conf.get(HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_AUTO_ADD_KEY))
        .describedAs("conf: " + HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_AUTO_ADD_KEY)
        .isEqualToIgnoringCase("true");
    Assert.assertTrue(HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_AUTO_ADD_DEFAULT);

    conf.set(HADOOP_SECURITY_CRYPTO_JCE_PROVIDER_KEY, CryptoUtils.BOUNCY_CASTLE_PROVIDER_NAME);
    final String providerFromConf = CryptoUtils.getJceProvider(conf);
    Assert.assertEquals(CryptoUtils.BOUNCY_CASTLE_PROVIDER_NAME, providerFromConf);

    final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
    Assertions.assertThat(provider)
        .isInstanceOf(BouncyCastleProvider.class);

    assertRemoveProvider();
  }