ProductKey.java

  1. /**
  2.  * ProductKey
  3.  */
  4. package gov.usgs.earthquake.distribution;

  5. import gov.usgs.earthquake.product.ProductId;
  6. import gov.usgs.util.Config;
  7. import gov.usgs.util.CryptoUtils;
  8. import gov.usgs.util.DefaultConfigurable;
  9. import gov.usgs.util.StringUtils;

  10. import java.security.PublicKey;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.logging.Logger;

  14. /**
  15.  * This represents a public key used to verify product signatures.
  16.  *
  17.  * A key should have at least one source and/or one type.
  18.  */
  19. public class ProductKey extends DefaultConfigurable {

  20.     /** Logger for use in file */
  21.     public final Logger LOGGER = Logger.getLogger(ProductKey.class.getName());

  22.     /** Property name for sources. */
  23.     public final String SOURCES_PROPERTY_NAME = "sources";

  24.     /** Property name for types. */
  25.     public final String TYPES_PROPERTY_NAME = "types";

  26.     /** Property name for key. */
  27.     public final String KEY_PROPERTY_NAME = "key";

  28.     /** The source(s) used for this key. */
  29.     private List<String> sources = new LinkedList<String>();

  30.     /** The type(s) used for this key. */
  31.     private List<String> types = new LinkedList<String>();

  32.     /** The key itself. */
  33.     private PublicKey key;

  34.     /**
  35.      * Empty constructor for the Configurable interface.
  36.      */
  37.     public ProductKey() {
  38.     }

  39.     /**
  40.      * Construct a new ProductPublicKey.
  41.      *
  42.      * Sources
  43.      *
  44.      * @param key
  45.      *            the public key.
  46.      * @param sources
  47.      *            the sources to use with this key.
  48.      * @param types
  49.      *            the types to use with this key.
  50.      */
  51.     public ProductKey(final PublicKey key, final List<String> sources,
  52.             final List<String> types) {
  53.         setKey(key);
  54.         if (sources != null) {
  55.             getSources().addAll(sources);
  56.         }
  57.         if (types != null) {
  58.             getTypes().addAll(types);
  59.         }
  60.     }

  61.     /**
  62.      * Check whether this key is a candidate for verifying a signature.
  63.      *
  64.      * If any sources, product source must be in list. If any types, product
  65.      * type must be in list.
  66.      *
  67.      * @param id
  68.      *            which product to check.
  69.      * @return true if this key might verify the signature for given product.
  70.      */
  71.     public boolean isForProduct(final ProductId id) {
  72.         if ((sources.size() == 0 || sources.contains(id.getSource()))
  73.                 && (types.size() == 0 || types.contains(id.getType()))) {
  74.             return true;
  75.         }
  76.         return false;
  77.     }

  78.     public void configure(final Config config) throws Exception {
  79.         String key = config.getProperty(KEY_PROPERTY_NAME);
  80.         if (key == null) {
  81.             throw new ConfigurationException(
  82.                     "'key' is a required configuration property");
  83.         }
  84.         LOGGER.config("key is '" + key + "'");
  85.         setKey(CryptoUtils.readOpenSSHPublicKey(key.getBytes()));

  86.         String sources = config.getProperty(SOURCES_PROPERTY_NAME);
  87.         if (sources != null) {
  88.             LOGGER.config("key sources '" + sources + "'");
  89.             getSources().addAll(StringUtils.split(sources, ","));
  90.         }

  91.         String types = config.getProperty(TYPES_PROPERTY_NAME);
  92.         if (types != null) {
  93.             LOGGER.config("key types '" + types + "'");
  94.             getTypes().addAll(StringUtils.split(types, ","));
  95.         }
  96.     }

  97.     public void shutdown() throws Exception {
  98.         // Nothing to do
  99.     }

  100.     public void startup() throws Exception {
  101.         // Nothing to do
  102.     }

  103.     /**
  104.      * @return the key
  105.      */
  106.     public PublicKey getKey() {
  107.         return key;
  108.     }

  109.     /**
  110.      * @param key
  111.      *            the key to set
  112.      */
  113.     public void setKey(PublicKey key) {
  114.         this.key = key;
  115.     }

  116.     /**
  117.      * @return the sources
  118.      */
  119.     public List<String> getSources() {
  120.         return sources;
  121.     }

  122.     /**
  123.      * @return the types
  124.      */
  125.     public List<String> getTypes() {
  126.         return types;
  127.     }

  128. }