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

com.sap.cloud.sdk.cloudplatform.tenant.ScpCfTenantFacade Maven / Gradle / Ivy

/*
 * Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
 */

package com.sap.cloud.sdk.cloudplatform.tenant;

import java.util.Optional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.slf4j.Logger;

import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.cloudplatform.tenant.exception.TenantAccessException;

/**
 * Facade providing access to tenant information on SAP Cloud Platform Cloud Foundry.
 */
public class ScpCfTenantFacade extends AbstractTenantFacade
{
    private static final Logger logger = CloudLoggerFactory.getLogger(ScpCfTenantFacade.class);

    private static final String VARIABLE_ALLOW_MOCKED_AUTH_HEADER = "ALLOW_MOCKED_AUTH_HEADER";

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public Class getTenantClass()
    {
        return ScpCfTenant.class;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    protected Tenant newMockedTenant()
    {
        return ScpCfTenant.ofMockedTenant();
    }

    /**
     * Returns a mocked {@link Tenant} as fallback, if the environment variable "ALLOW_MOCKED_AUTH_HEADER" is set to
     * "true".
     *
     * @return The mocked {@link Tenant}.
     */
    @Nonnull
    protected Optional getMockedTenantAsFallback()
    {
        @Nullable
        final String env = System.getenv(VARIABLE_ALLOW_MOCKED_AUTH_HEADER);

        if( Boolean.valueOf(env) ) {
            logger.error(
                "Falling back to mocked tenant with blank tenant identifier "
                    + "since no tenant is available and environment variable \""
                    + VARIABLE_ALLOW_MOCKED_AUTH_HEADER
                    + "\" is set to \"true\". "
                    + "SECURITY WARNING: This variable must never be used in productive environments! "
                    + "For details on the security configuration, "
                    + "please refer to the SAP Cloud Platform documentation. "
                    + "Tutorials on the configuration are available at "
                    + "\"https://help.sap.com/viewer/p/SAP_S4HANA_CLOUD_SDK\".");

            return Optional.of(newMockedTenant());
        }

        return Optional.empty();
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public Optional resolveCurrentTenant()
        throws TenantAccessException
    {
        final Optional mockedTenant = getMockedTenant();

        if( mockedTenant.isPresent() ) {
            return mockedTenant;
        }

        @Nullable
        final ScpCfTenant currentTenant = ScpCfTenant.ofCurrentTenant();

        if( currentTenant != null ) {
            return Optional.of(currentTenant);
        }

        return getMockedTenantAsFallback();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy