View Javadoc

1   package com.atlassian.asap.core.keys.privatekey;
2   
3   import com.atlassian.asap.core.keys.DataUriKeyReader;
4   import com.atlassian.asap.core.keys.KeyProvider;
5   import com.atlassian.asap.core.keys.PemReader;
6   import com.atlassian.asap.core.keys.privatekey.EnvironmentVariableKeyProvider.Environment;
7   import com.google.common.annotations.VisibleForTesting;
8   
9   import java.io.File;
10  import java.net.URI;
11  import java.security.PrivateKey;
12  
13  public class PrivateKeyProviderFactory {
14      /**
15       * Creates an instance of {@link com.atlassian.asap.core.keys.KeyProvider}.
16       *
17       * @param privateKeyPath the base url to use for retrieving private keys
18       * @return a private key provider
19       * @throws IllegalArgumentException if the privateKeyPath is syntactically incorrect
20       */
21      public static KeyProvider<PrivateKey> createPrivateKeyProvider(URI privateKeyPath) {
22          return createPrivateKeyProvider(privateKeyPath, new Environment());
23      }
24  
25      @VisibleForTesting
26      static KeyProvider<PrivateKey> createPrivateKeyProvider(URI privateKeyPath, Environment environment) {
27          switch (privateKeyPath.getScheme()) {
28              case "file":
29                  return new FilePrivateKeyProvider(new File(privateKeyPath), new PemReader());
30              case "classpath":
31                  return new ClasspathPrivateKeyProvider(privateKeyPath.getPath(), new PemReader());
32              case "data":
33                  return new DataUriKeyProvider(privateKeyPath, new DataUriKeyReader());
34              case SystemPropertyKeyProvider.URL_SCHEME:
35                  return SystemPropertyKeyProvider.fromUri(privateKeyPath, new DataUriKeyReader());
36              case EnvironmentVariableKeyProvider.URL_SCHEME:
37                  return EnvironmentVariableKeyProvider.fromUri(privateKeyPath, new DataUriKeyReader(), environment);
38              case NullKeyProvider.URL_SCHEME:
39                  return new NullKeyProvider();
40              default:
41                  throw new IllegalArgumentException("Unsupported private key base URL: " + privateKeyPath);
42          }
43      }
44  }