All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.feature.auditlog.v2.AuditLogV2Configuration Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/**************************************************************************
 * (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.feature.auditlog.v2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.sap.cds.services.ServiceCatalog;
import com.sap.cds.services.mt.DeploymentService;
import com.sap.cds.services.mt.TenantProviderService;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.cds.services.runtime.CdsRuntimeConfiguration;
import com.sap.cds.services.runtime.CdsRuntimeConfigurer;
import com.sap.cds.services.utils.CdsErrorStatuses;
import com.sap.cds.services.utils.ErrorStatusException;
import com.sap.cds.services.utils.environment.ServiceBindingUtils;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cds.repackaged.audit.client.impl.Communicator;
import com.sap.cds.repackaged.audit.client.impl.v2.AuditLogMessageFactoryImpl;

/**
 * CDS runtime configuration for the {@link AuditLogV2Handler}.
 */
public class AuditLogV2Configuration implements CdsRuntimeConfiguration {
	private static final Logger LOGGER = LoggerFactory.getLogger(AuditLogV2Configuration.class);
	static final String AUDITLOG = "auditlog";

	@Override
	public void eventHandlers(CdsRuntimeConfigurer configurer) {
		CdsRuntime runtime = configurer.getCdsRuntime();

		if (runtime.getEnvironment().getCdsProperties().getAuditLog().getV2().isEnabled()) {
			ServiceBinding binding = runtime.getEnvironment().getServiceBindings()
					.filter(b -> ServiceBindingUtils.matches(b, AUDITLOG)).findFirst().orElse(null);

			if (binding != null) {
				LOGGER.info(
						"Using Auditlog v2 service binding with name '{}' and plan '{}' to register Auditlog v2 event handler.",
						binding.getName().get(), binding.getServicePlan().get());

				AuditLogV2Handler handler = createHandler(binding, configurer);
				configurer.eventHandler(handler);
			} else {
				LOGGER.info("No Auditlog v2 service binding found, v2 handler not registered.");
			}
		} else {
			LOGGER.info("AuditLog v2 is disabled, v2 handler not registered.");
		}
	}

	@VisibleForTesting
	AuditLogV2Handler createHandler(ServiceBinding binding, CdsRuntimeConfigurer configurer) {
		boolean oAuth2;
		if (AuditLogV2Utils.isOAuth2BasedServicePlan(binding)) {
			oAuth2 = true;
			if (isMtEnabled(configurer)) {
				LOGGER.debug("Registering event handler to provide auditlog OAuth2 service binding '{}' as multitenancy dependency.", binding.getName().orElse(null));
				configurer.eventHandler(new AuditLogV2MtHandler(binding));
			}
		} else if (AuditLogV2Utils.isStandardPlan(binding)) {
			oAuth2 = false;
		} else {
			throw new ErrorStatusException(CdsErrorStatuses.AUDITLOG_SERVICE_INVALID_PLAN, binding.getServicePlan().orElse(null));
		}

		Communicator communicator = new CloudSdkCommunicator(binding, configurer.getCdsRuntime());
		AuditLogMessageFactoryImpl factory = new AuditLogMessageFactoryImpl(communicator);

		TenantProviderService tenantService = configurer.getCdsRuntime().getServiceCatalog()
				.getService(TenantProviderService.class, TenantProviderService.DEFAULT_NAME);
		return new AuditLogV2Handler(factory, oAuth2, tenantService, communicator.getClientId());
	}

	// static helpers
	private static boolean isMtEnabled(CdsRuntimeConfigurer configurer) {
		ServiceCatalog catalog = configurer.getCdsRuntime().getServiceCatalog();
		return (catalog.getServices(DeploymentService.class).count() > 0);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy