001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.ws.security;
015
016import org.gbif.utils.file.properties.PropertiesUtil;
017
018import java.io.IOException;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Properties;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.springframework.stereotype.Component;
027
028@Component
029public class FileSystemKeyStore implements KeyStore {
030
031  private static final Logger LOG = LoggerFactory.getLogger(FileSystemKeyStore.class);
032
033  private final Map<String, String> store;
034
035  public FileSystemKeyStore(AppkeysConfigurationProperties appkeysConfiguration) {
036    try {
037      Properties props = PropertiesUtil.loadProperties(appkeysConfiguration.getFile());
038      Map<String, String> map = new HashMap<>();
039      props.forEach((key, value) -> map.put(String.valueOf(key), String.valueOf(value)));
040      store = Collections.unmodifiableMap(map);
041    } catch (IOException e) {
042      throw new IllegalArgumentException(
043          "Property file path to application keys does not exist: "
044              + appkeysConfiguration.getFile(),
045          e);
046    }
047    LOG.info("Initialised appkey store with {} keys", store.size());
048  }
049
050  @Override
051  public String getPrivateKey(final String applicationKey) {
052    return store.get(applicationKey);
053  }
054}