paras200 commented on code in PR #986: URL: https://github.com/apache/ranger/pull/986#discussion_r3356840985
########## audit-server/audit-dispatcher/dispatcher-common/src/main/java/org/apache/ranger/audit/dispatcher/AuditEventDocMapper.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ranger.audit.dispatcher; + +import org.apache.ranger.audit.model.AuthzAuditEvent; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +/** + * Maps {@link AuthzAuditEvent} to an OpenSearch document. + */ +public final class AuditEventDocMapper { + /** Thread-safe ISO-8601 UTC date formatter. */ + private static final ThreadLocal<DateFormat> DATE_FORMAT = + ThreadLocal.withInitial(() -> { + SimpleDateFormat format = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format; + }); + + private AuditEventDocMapper() { + } + + /** + * Converts an audit event to a document map. + * + * @param auditEvent the audit event to convert + * @return map of field names to values + */ + public static Map<String, Object> toDoc( Review Comment: Done — removed all multi-line string concatenation splits and unnecessary line breaks across `OpenSearchDispatcherManager`, `AuditOpenSearchDispatcher`, and `AuditEventDocMapper` (now renamed). Also stripped verbose Javadoc comments that didn't add value beyond what the identifiers already communicate. All statements now fit naturally on a single line. ########## audit-server/audit-dispatcher/dispatcher-common/src/main/java/org/apache/ranger/audit/dispatcher/AuditEventDocMapper.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ranger.audit.dispatcher; + +import org.apache.ranger.audit.model.AuthzAuditEvent; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +/** + * Maps {@link AuthzAuditEvent} to an OpenSearch document. + */ +public final class AuditEventDocMapper { Review Comment: Done — moved from `dispatcher-common` to `dispatcher-opensearch`. Also renamed to `AuditEventOpenSearchDocMapper` to make its scope explicit now that it lives in the OpenSearch module. Test moved and renamed accordingly. Package unchanged. ########## audit-server/audit-dispatcher/dispatcher-opensearch/src/main/java/org/apache/ranger/audit/dispatcher/kafka/AuditOpenSearchDispatcher.java: ########## @@ -0,0 +1,416 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.ranger.audit.dispatcher.kafka; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AuthSchemeProvider; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.AuthSchemes; +import org.apache.http.config.Lookup; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.impl.auth.SPNegoSchemeFactory; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.clients.consumer.OffsetAndMetadata; +import org.apache.kafka.common.TopicPartition; +import org.apache.ranger.audit.dispatcher.AuditEventDocMapper; +import org.apache.ranger.audit.model.AuthzAuditEvent; +import org.apache.ranger.audit.provider.MiscUtil; +import org.apache.ranger.audit.server.AuditServerConstants; +import org.apache.ranger.audit.utils.AuditServerLogFormatter; +import org.apache.ranger.authorization.credutils.CredentialsProviderUtil; +import org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; + +public class AuditOpenSearchDispatcher + extends AuditDispatcherBase { + /** Class logger. */ + private static final Logger LOG = + LoggerFactory.getLogger( + AuditOpenSearchDispatcher.class); + + /** Kafka consumer group for this dispatcher. */ + private static final String DEFAULT_GROUP = + "ranger_audit_opensearch_dispatcher_group"; + /** Property prefix for OpenSearch destination config. */ + private static final String ES_DEST_PREFIX = + "xasecure.audit.destination.elasticsearch"; + /** Default OpenSearch index name. */ + private static final String DEFAULT_INDEX = + "ranger_audits"; + /** Sleep duration between retries on batch failure. */ + private static final long RETRY_SLEEP_MS = 5000L; + /** Default OpenSearch HTTP port. */ + private static final int DEFAULT_PORT = 9200; + /** Shared JSON serializer. */ + private static final ObjectMapper OBJECT_MAPPER = + new ObjectMapper(); + /** Type reference for bulk response parsing. */ + private static final TypeReference<Map<String, Object>> + MAP_TYPE = new TypeReference<Map<String, Object>>() { + }; + + /** OpenSearch REST client. */ + private RestClient openSearchClient; + /** Target index for audit documents. */ + private String openSearchIndex; + + /** + * Creates and initializes the OpenSearch dispatcher. + * + * @param props configuration properties + * @param propPrefix property key prefix + * @throws Exception if initialization fails + */ + public AuditOpenSearchDispatcher( + final Properties props, + final String propPrefix) throws Exception { + super(props, propPrefix, DEFAULT_GROUP); + + init(props, propPrefix); + } + + @Override + protected final String getDispatcherName() { + return "OPENSEARCH"; + } + + @Override + protected final DispatcherWorker createDispatcherWorker( + final String workerId, + final List<Integer> assignedPartitions) { + return new OpenSearchDispatcherWorker( + workerId, assignedPartitions); + } + + @Override + protected final void shutdownDestination() { + if (openSearchClient != null) { + try { + openSearchClient.close(); + } catch (Exception e) { + LOG.error( + "Error shutting down OpenSearch REST client", + e); + } + } + } + + private void init( + final Properties props, + final String propPrefix) throws Exception { + LOG.info("==> AuditOpenSearchDispatcher.init()"); + + String pfx = propPrefix + "."; + + this.openSearchIndex = MiscUtil.getStringProperty( + props, ES_DEST_PREFIX + ".index", DEFAULT_INDEX); Review Comment: Done — removed `ES_DEST_PREFIX` constant. `init()` and `createOpenSearchClient()` now use the `propPrefix` parameter for all OpenSearch connection property lookups (`.urls`, `.port`, `.protocol`, `.index`, `.user`, `.password`). Updated both site XML configs to use the `ranger.audit.dispatcher.*` namespace for these properties. The boolean enable/disable check (`xasecure.audit.destination.elasticsearch`) remains in `OpenSearchDispatcherManager` since it's a manager-level concern separate from the dispatcher's connection config. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
