|
| 1 | +package io.confluent.oauth.azure.managedidentity; |
| 2 | + |
| 3 | +import io.confluent.kafka.schemaregistry.client.SchemaRegistryClientConfig; |
| 4 | +import io.confluent.kafka.schemaregistry.client.security.bearerauth.BearerAuthCredentialProvider; |
| 5 | +import org.apache.kafka.common.KafkaException; |
| 6 | +import org.apache.kafka.common.config.ConfigException; |
| 7 | +import org.apache.kafka.common.config.SaslConfigs; |
| 8 | +import org.apache.kafka.common.config.types.Password; |
| 9 | +import org.apache.kafka.common.security.JaasContext; |
| 10 | +import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken; |
| 11 | +import org.apache.kafka.common.security.oauthbearer.internals.secured.*; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import javax.security.auth.login.AppConfigurationEntry; |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.URL; |
| 18 | +import java.util.*; |
| 19 | + |
| 20 | +import static io.confluent.oauth.azure.managedidentity.OAuthBearerLoginCallbackHandler.createAccessTokenRetriever; |
| 21 | + |
| 22 | +public class RegistryBearerAuthCredentialProvider implements BearerAuthCredentialProvider { |
| 23 | + |
| 24 | + private static final Logger log = LoggerFactory.getLogger(RegistryBearerAuthCredentialProvider.class); |
| 25 | + public static final String SASL_IDENTITY_POOL_CONFIG = "extension_identityPoolId"; |
| 26 | + |
| 27 | + private String targetSchemaRegistry; |
| 28 | + private String targetIdentityPoolId; |
| 29 | + private Map<String, Object> moduleOptions; |
| 30 | + |
| 31 | + private AccessTokenRetriever accessTokenRetriever; |
| 32 | + private AccessTokenValidator accessTokenValidator; |
| 33 | + private boolean isInitialized; |
| 34 | + |
| 35 | + |
| 36 | + @Override |
| 37 | + public void configure(Map<String, ?> configs) { |
| 38 | + // from SaslOauthCredentialProvider |
| 39 | + Map<String, Object> updatedConfigs = getConfigsForJaasUtil(configs); |
| 40 | + JaasContext jaasContext = JaasContext.loadClientContext(updatedConfigs); |
| 41 | + List<AppConfigurationEntry> appConfigurationEntries = jaasContext.configurationEntries(); |
| 42 | + Map<String, ?> jaasconfig; |
| 43 | + if (Objects.requireNonNull(appConfigurationEntries).size() == 1 |
| 44 | + && appConfigurationEntries.get(0) != null) { |
| 45 | + jaasconfig = Collections.unmodifiableMap( |
| 46 | + ((AppConfigurationEntry) appConfigurationEntries.get(0)).getOptions()); |
| 47 | + } else { |
| 48 | + throw new ConfigException( |
| 49 | + String.format("Must supply exactly 1 non-null JAAS mechanism configuration (size was %d)", |
| 50 | + appConfigurationEntries.size())); |
| 51 | + } |
| 52 | + |
| 53 | + // make sure we have scope and sub set |
| 54 | + Map<String, Object> myConfigs = new HashMap<>(configs); |
| 55 | + myConfigs.put(SaslConfigs.SASL_OAUTHBEARER_SCOPE_CLAIM_NAME, "scope"); |
| 56 | + myConfigs.put(SaslConfigs.SASL_OAUTHBEARER_SUB_CLAIM_NAME, "sub"); |
| 57 | + |
| 58 | + |
| 59 | + ConfigurationUtils cu = new ConfigurationUtils(myConfigs); |
| 60 | + JaasOptionsUtils jou = new JaasOptionsUtils((Map<String, Object>) jaasconfig); |
| 61 | + |
| 62 | + targetSchemaRegistry = cu.validateString( |
| 63 | + SchemaRegistryClientConfig.BEARER_AUTH_LOGICAL_CLUSTER, false); |
| 64 | + |
| 65 | + // if the schema registry oauth configs are set it is given higher preference |
| 66 | + targetIdentityPoolId = cu.get(SchemaRegistryClientConfig.BEARER_AUTH_IDENTITY_POOL_ID) != null |
| 67 | + ? cu.validateString(SchemaRegistryClientConfig.BEARER_AUTH_IDENTITY_POOL_ID) |
| 68 | + : jou.validateString(SASL_IDENTITY_POOL_CONFIG, false); |
| 69 | + |
| 70 | + String saslMechanism = cu.validateString(SaslConfigs.SASL_MECHANISM); |
| 71 | + moduleOptions = JaasOptionsUtils.getOptions(saslMechanism, appConfigurationEntries); |
| 72 | + AccessTokenRetriever accessTokenRetriever = createAccessTokenRetriever(myConfigs, saslMechanism, moduleOptions); |
| 73 | + |
| 74 | + AccessTokenValidator accessTokenValidator = AccessTokenValidatorFactory.create(myConfigs, saslMechanism); |
| 75 | + init(accessTokenRetriever, accessTokenValidator); |
| 76 | + } |
| 77 | + |
| 78 | + /* |
| 79 | + * Package-visible for testing. |
| 80 | + */ |
| 81 | + |
| 82 | + void init(AccessTokenRetriever accessTokenRetriever, AccessTokenValidator accessTokenValidator) { |
| 83 | + this.accessTokenRetriever = accessTokenRetriever; |
| 84 | + this.accessTokenValidator = accessTokenValidator; |
| 85 | + |
| 86 | + try { |
| 87 | + this.accessTokenRetriever.init(); |
| 88 | + } catch (IOException e) { |
| 89 | + throw new KafkaException("The OAuth login configuration encountered an error when initializing the AccessTokenRetriever", e); |
| 90 | + } |
| 91 | + |
| 92 | + isInitialized = true; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @Override |
| 97 | + public String getBearerToken(URL url) { |
| 98 | + try { |
| 99 | + String accessToken = accessTokenRetriever.retrieve(); |
| 100 | + OAuthBearerToken token = accessTokenValidator.validate(accessToken); |
| 101 | + return accessToken; |
| 102 | + } catch (ValidateException | IOException e) { |
| 103 | + log.warn(e.getMessage(), e); |
| 104 | + return ""; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String getTargetIdentityPoolId() { |
| 110 | + return targetIdentityPoolId; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String getTargetSchemaRegistry() { |
| 115 | + return targetSchemaRegistry; |
| 116 | + } |
| 117 | + |
| 118 | + // from SaslOauthCredentialProvider |
| 119 | + Map<String, Object> getConfigsForJaasUtil(Map<String, ?> configs) { |
| 120 | + Map<String, Object> updatedConfigs = new HashMap<>(configs); |
| 121 | + if (updatedConfigs.containsKey(SaslConfigs.SASL_JAAS_CONFIG)) { |
| 122 | + Object saslJaasConfig = updatedConfigs.get(SaslConfigs.SASL_JAAS_CONFIG); |
| 123 | + if (saslJaasConfig instanceof String) { |
| 124 | + updatedConfigs.put(SaslConfigs.SASL_JAAS_CONFIG, new Password((String) saslJaasConfig)); |
| 125 | + } |
| 126 | + } |
| 127 | + return updatedConfigs; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments