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.secret.macos.keychain;
018
019import org.apache.juneau.commons.secret.*;
020
021/**
022 * Provider that contributes a {@link KeychainSecretStore} via {@code ServiceLoader}, discoverable through
023 * {@link SecretStores#fromServiceLoader()} when this module is on the classpath and the host is macOS.
024 *
025 * <p>
026 * The service name defaults to {@value #DEFAULT_SERVICE} but can be overridden via the
027 * {@value #SERVICE_PROPERTY} system property; the fail mode defaults to {@link FailMode#FAIL_CLOSED}.  On a
028 * non-macOS host (or when the keychain CLI is absent) {@link #create()} returns <jk>null</jk> so discovery skips it.
029 *
030 * @since 10.0.0
031 */
032public class KeychainSecretStoreProvider implements SecretStoreProvider {
033
034   /** System property overriding the keychain service name used by the discovered store. */
035   public static final String SERVICE_PROPERTY = "juneau.secret.macos.keychain.service";
036
037   /** Default keychain service name when {@link #SERVICE_PROPERTY} is unset. */
038   public static final String DEFAULT_SERVICE = "org.apache.juneau";
039
040   @Override /* SecretStoreProvider */
041   public SecretStore create() {
042      if (! System.getProperty("os.name", "").toLowerCase().contains("mac"))
043         return null;
044      return new KeychainSecretStore(System.getProperty(SERVICE_PROPERTY, DEFAULT_SERVICE));
045   }
046
047   @Override /* SecretStoreProvider */
048   public int order() {
049      return 10;
050   }
051}