View Javadoc

1   package com.atlassian.asap.core.keys.privatekey;
2   
3   import com.atlassian.asap.api.exception.CannotRetrieveKeyException;
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.atlassian.asap.core.validator.ValidatedKeyId;
8   import org.junit.Rule;
9   import org.junit.Test;
10  import org.junit.contrib.java.lang.system.ProvideSystemProperty;
11  import org.junit.runner.RunWith;
12  import org.mockito.Mock;
13  import org.mockito.runners.MockitoJUnitRunner;
14  
15  import java.net.URI;
16  import java.security.PrivateKey;
17  import java.util.Optional;
18  
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.instanceOf;
21  import static org.junit.Assert.assertThat;
22  import static org.mockito.Mockito.when;
23  
24  @RunWith(MockitoJUnitRunner.class)
25  public class PrivateKeyProviderFactoryTest {
26      private static final String PROPERTY_NAME = "base.name";
27      private static final String VALID_KEY_DATA = "data:application/pkcs8;kid=apikey;base64,MDoCAQAwDQYJKoZIhvcNAQEBBQAEJjAkAgEAAgMBGE4CAwEAAQICTGsCAwCMJwIBAgICTGsCAQACAkYU";
28  
29      @Rule
30      public ProvideSystemProperty provideSystemProperty = new ProvideSystemProperty(PROPERTY_NAME, VALID_KEY_DATA);
31  
32      @Mock
33      private PemReader pemReader;
34      @Mock
35      private Environment environment;
36  
37      @Test(expected = IllegalArgumentException.class)
38      public void shouldRejectUnknownSchemes() {
39          PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("unknown://baseurl"));
40      }
41  
42      @Test(expected = IllegalArgumentException.class)
43      public void shouldRejectInsecureHttp() {
44          PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("http://example.test/"));
45      }
46  
47      @Test
48      public void shouldCreateClasspathProvider() throws Exception {
49          KeyProvider<PrivateKey> provider = PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("classpath:///asap_private_keys/"));
50          assertThat(provider, instanceOf(ClasspathPrivateKeyProvider.class));
51          PrivateKey testkey = provider.getKey(ValidatedKeyId.validate("testkey/key.pem"));
52          assertThat(testkey.getFormat(), equalTo("PKCS#8"));
53      }
54  
55      @Test
56      public void shouldCreateFileProvider() {
57          KeyProvider<PrivateKey> provider = PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("file:///some/location/"));
58          assertThat(provider, instanceOf(FilePrivateKeyProvider.class));
59      }
60  
61      @Test
62      public void shouldCreateSystemPropertyProvider() throws Exception {
63          KeyProvider<PrivateKey> provider = PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("sysprop:///" + PROPERTY_NAME));
64          assertThat(provider, instanceOf(SystemPropertyKeyProvider.class));
65          PrivateKey apikey = provider.getKey(ValidatedKeyId.validate("apikey"));
66          assertThat(apikey.getFormat(), equalTo("PKCS#8"));
67      }
68  
69      @Test
70      public void shouldCreateEnvironmentVariableProvider() throws Exception {
71          when(environment.getVariable(PROPERTY_NAME)).thenReturn(Optional.of(VALID_KEY_DATA));
72          KeyProvider<PrivateKey> provider = PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("env:///" + PROPERTY_NAME), environment);
73          assertThat(provider, instanceOf(EnvironmentVariableKeyProvider.class));
74          PrivateKey apikey = provider.getKey(ValidatedKeyId.validate("apikey"));
75          assertThat(apikey.getFormat(), equalTo("PKCS#8"));
76      }
77  
78      @Test(expected = CannotRetrieveKeyException.class)
79      public void shouldCreateNullProvider() throws Exception {
80          KeyProvider<PrivateKey> provider = PrivateKeyProviderFactory.createPrivateKeyProvider(URI.create("null:/"));
81          assertThat(provider, instanceOf(NullKeyProvider.class));
82          provider.getKey(ValidatedKeyId.validate("apikey"));
83      }
84  
85      @Test
86      public void shouldCreateDataProvider() throws Exception {
87          KeyProvider<PrivateKey> provider = PrivateKeyProviderFactory.createPrivateKeyProvider(new URI(VALID_KEY_DATA));
88          assertThat(provider, instanceOf(DataUriKeyProvider.class));
89          PrivateKey apikey = provider.getKey(ValidatedKeyId.validate("apikey"));
90          assertThat(apikey.getFormat(), equalTo("PKCS#8"));
91      }
92  }