001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.file.CmsObject;
032import org.opencms.main.OpenCms;
033
034/**
035 * Utility methods for fetching secrets from the secret provider.
036 */
037public final class CmsSecretUtil {
038
039    /**
040     * Gets the secret for the exact given key.
041     *
042     *  <p>If no secret is found, null will be returned.
043     *
044     * @param key the key for a secret
045     * @return the secret
046     */
047    public static String getSecret(String key) {
048
049        return OpenCms.getSecretStore().getSecret(key);
050    }
051
052    /**
053     * Looks up a path dependent secret in the secret store for the current URI.
054     *
055     *  <p>This is done as follows:
056     *  <p>For the root path of each subsitemap we are in, in ascending order, the key (prefix + &quot;.&quot; + path) is looked up.
057     *  The first match will be returned. As a special case, the key for the site root is always looked up, even if it's not configured as
058     *  a sitemap.
059     *
060     * @param prefix the key prefix
061     * @param cms the OpenCms user context to use when resolving the secret
062     * @return the secret (or null, if nothing was found)
063     */
064    public static String getSecretForUri(String prefix, CmsObject cms) {
065
066        return getSecretForUri(prefix, cms, cms.getRequestContext().getRootUri());
067    }
068
069    /**
070     * Looks up a path dependent secret in the secret store for the given root path.
071     *
072     *  <p>This is done as follows:
073     *  <p>For the root path of each subsitemap we are in, in ascending order, the key (prefix + &quot;.&quot; + path) is looked up.
074     *  The first match will be returned. As a special case, the key for the site root is always looked up, even if it's not configured as
075     *  a sitemap.
076     *
077     * @param prefix the key prefix
078     * @param cms the OpenCms user context to use when resolving the secret
079     * @param rootPath the root path for which the secret should be looked up
080     * @return the secret (or null, if nothing was found)
081     */
082    public static String getSecretForUri(String prefix, CmsObject cms, String rootPath) {
083
084        CmsADEConfigData config = OpenCms.getADEManager().lookupConfigurationWithCache(cms, rootPath);
085        if (config != null) {
086            return config.getSecretForUri(prefix);
087        }
088        return null;
089    }
090
091}