001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.secret;
018
019import java.util.*;
020
021import org.apache.juneau.commons.settings.*;
022
023/**
024 * Opt-in bridge that exposes a chosen {@link SecretStore} as a {@link PropertySource}, so a secret can be resolved
025 * through the property/{@code $P{...}}/{@code @Value} machinery <b>only where a consumer deliberately wires it in</b>.
026 *
027 * <p>
028 * By default a {@link SecretStore} is intentionally <b>not</b> part of the general config/SVL namespace &mdash; that is
029 * what keeps a secret one careless {@code Settings} dump or debug log away from disclosure.  This adapter is the
030 * conscious, greppable exception: attach it as a caller-scoped source (for example a session-scoped
031 * {@code PropertySource[]} bean on a {@code VarResolverSession}) and its bridged keys become resolvable there, and
032 * nowhere else.
033 *
034 * <p>
035 * It implements {@link SensitivePropertySource} so any dump/log path that honors that marker redacts its values, and
036 * its own {@link #toString()} never reveals the wrapped store or any secret.
037 *
038 * <p>
039 * <b>Materialization trade-off.</b> The {@link PropertySource} contract returns values as {@link String}, so this
040 * bridge necessarily converts the retrieved {@code char[]} into a {@link String} that cannot be explicitly zeroed and
041 * may linger in the string pool.  That reintroduced exposure is precisely why bridging is opt-in and marked
042 * sensitive rather than being the default way to reach a secret.
043 *
044 * @since 10.0.0
045 */
046public class SecretStorePropertySource implements SensitivePropertySource {
047
048   private final SecretStore store;
049
050   /**
051    * Constructor.
052    *
053    * @param store The secret store to expose.  Must not be <jk>null</jk>.
054    */
055   public SecretStorePropertySource(SecretStore store) {
056      this.store = Objects.requireNonNull(store, "store");
057   }
058
059   @Override /* PropertySource */
060   public PropertyLookupResult get(String name) {
061      if (name == null)
062         return PropertyLookupResult.missing();
063      return store.find(name)
064         .map(v -> PropertyLookupResult.present(new String(v)))
065         .orElseGet(PropertyLookupResult::missing);
066   }
067
068   @Override /* Object */
069   public String toString() {
070      return "SecretStorePropertySource(<redacted>)";
071   }
072}