use of org.bf2.test.Environment in project kafka-admin-api by bf2fc6cc711aee1a0c2a.
the class AdminClientFactory method createAdminClient.
/**
* Route handler common to all Kafka resource routes. Responsible for creating
* the map of properties used to configure the Kafka Admin Client. When OAuth
* has been enabled via the environment, the access token will be retrieved from
* the authenticated user principal present in the context (created by Vert.x
* handler when a valid JWT was presented by the client). The configuration property
* map will be placed in the context under the key identified by the
* {@link #ADMIN_CLIENT_CONFIG} constant.
*/
public AdminClient createAdminClient() {
Map<String, Object> acConfig = config.getAcConfig();
if (config.isOauthEnabled()) {
if (token.isResolvable()) {
final String accessToken = token.get().getRawToken();
if (accessToken == null) {
throw new NotAuthorizedException(Response.status(Status.UNAUTHORIZED));
}
acConfig.put(SaslConfigs.SASL_JAAS_CONFIG, String.format(SASL_OAUTH_CONFIG_TEMPLATE, accessToken));
} else {
log.warn("OAuth is enabled, but there is no JWT principal");
}
} else if (config.isBasicEnabled()) {
extractCredentials(Optional.ofNullable(headers.get().getHeaderString(HttpHeaders.AUTHORIZATION))).ifPresentOrElse(credentials -> acConfig.put(SaslConfigs.SASL_JAAS_CONFIG, credentials), () -> {
throw new NotAuthorizedException("Invalid or missing credentials", Response.status(Status.UNAUTHORIZED).build());
});
} else {
log.debug("OAuth is disabled - no attempt to set access token in Admin Client config");
}
return AdminClient.create(acConfig);
}
Aggregations