diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java index fc33e78a6275a8a4ef707b586021e8be9b86a40e..e0ea8092be175046e27d39eb4338927a4ec3596a 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java @@ -43,7 +43,8 @@ public class IndexCacheImpl implements IIndexCache<String, Boolean>, AutoCloseab public IndexCacheImpl() throws K8sParameterNotFoundException, JsonProcessingException { int expTimeSeconds = 60 * 60; K8sLocalParameterProvider provider = new K8sLocalParameterProvider(); - if (provider.getLocalMode()){ + local = provider.getLocalMode(); + if (local){ if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))){ cache = new DummyCache<>(); }else{ @@ -61,7 +62,6 @@ public class IndexCacheImpl implements IIndexCache<String, Boolean>, AutoCloseab } cache = new RedisCache<String, Boolean>(host, port, password, expTimeSeconds, String.class,Boolean.class); } - local = cache instanceof AutoCloseable; } @Override diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java index 27a1f3792a25a47a331117a85e52f55a62510efd..c615cc0c524a9001d81d053ced48e3885030a834 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java @@ -42,7 +42,8 @@ public class SchemaCacheImpl implements ISchemaCache<String, String>, AutoClosea public SchemaCacheImpl() throws K8sParameterNotFoundException, JsonProcessingException { int expTimeSeconds = 60 * 60; K8sLocalParameterProvider provider = new K8sLocalParameterProvider(); - if (provider.getLocalMode()){ + local = provider.getLocalMode(); + if (local){ if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))){ cache = new DummyCache<>(); }else{ @@ -60,13 +61,12 @@ public class SchemaCacheImpl implements ISchemaCache<String, String>, AutoClosea } cache = new RedisCache<>(host, port, password, expTimeSeconds, String.class, String.class); } - local = cache instanceof AutoCloseable; } @Override public void close() throws Exception { if (this.local){ - // do nothing, this is using local dummy cache + // do nothing, this is using local dummy cache or vm cache }else { // cast to redis cache so it can be closed ((AutoCloseable)this.cache).close(); diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5a354cfb802ec11fed8cdf70599d9650212330f7 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImplTest.java @@ -0,0 +1,114 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.cache; + +import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.opengroup.osdu.core.common.cache.RedisCache; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(MockitoJUnitRunner.class) +public class IndexCacheImplTest { + + + private final String s = "s"; + private final boolean o = true; + private final String password = "password"; + private final String endpoint = "CACHE_CLUSTER_ENDPOINT"; + private final String port = "6369"; + + @Test + public void local_VmCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(true); + })) { + IndexCacheImpl cacheImpl = new IndexCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + assertNull(cacheImpl.get(s)); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + + @Test + public void non_local_Null_Credential_RedisCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(null); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + IndexCacheImpl cacheImpl = new IndexCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + + @Test + public void non_Local_notNull_Credential_RedisCache_test() throws Exception{ + + Map<String, String> map = new HashMap<String, String>(); + map.put("token", password); + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(map); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + IndexCacheImpl cacheImpl = new IndexCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..61e3e73c7a02da8978db9663598ffd8ed3a6eed5 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImplTest.java @@ -0,0 +1,115 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.cache; + +import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.opengroup.osdu.core.common.cache.RedisCache; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(MockitoJUnitRunner.class) +public class SchemaCacheImplTest { + + + private final String s = "s"; + private final String o = "o"; + private final String password = "password"; + private final String endpoint = "CACHE_CLUSTER_ENDPOINT"; + private final String port = "6369"; + + + @Test + public void local_VmCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(true); + })) { + SchemaCacheImpl cacheImpl = new SchemaCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + assertNull(cacheImpl.get(s)); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + + @Test + public void non_local_Null_Credential_RedisCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(null); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + SchemaCacheImpl cacheImpl = new SchemaCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + + @Test + public void non_Local_notNull_Credential_RedisCache_test() throws Exception{ + + Map<String, String> map = new HashMap<String, String>(); + map.put("token", password); + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(map); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + SchemaCacheImpl cacheImpl = new SchemaCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/persistence/ElasticRepositoryImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/persistence/ElasticRepositoryImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c89cbb150983ce5bae6c5aec96344c766df24644 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/persistence/ElasticRepositoryImplTest.java @@ -0,0 +1,81 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.persistence; + + +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(SpringJUnit4ClassRunner.class) +public class ElasticRepositoryImplTest { + + private final String s = "s"; + private final String o = "o"; + private final String username = "username"; + private final String password = "password"; + private final String host = "host"; + private final String port = "6369"; + + + @Test + public void getElasticClusterSettings_Null_User_Test() { + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_HOST"), any())).thenReturn(host); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("ELASTICSEARCH_CREDENTIALS"))).thenReturn(null); + })) { + ElasticRepositoryImpl elasticImpl = new ElasticRepositoryImpl(); + + assertNotNull(elasticImpl.getElasticClusterSettings(new TenantInfo())); + + } + + } + @Test + public void getElasticClusterSettings_not_Null_User_Test() { + + Map<String, String> map = new HashMap<String, String>(); + map.put("username", username); + map.put("password", password); + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_HOST"), any())).thenReturn(host); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("ELASTICSEARCH_CREDENTIALS"))).thenReturn(map); + })) { + ElasticRepositoryImpl elasticImpl = new ElasticRepositoryImpl(); + elasticImpl.isHttps = true; + assertNotNull(elasticImpl.getElasticClusterSettings(new TenantInfo())); + + } + + } +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManagerTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManagerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..865fc17d062743a33449fb5e5d5b63015488b2b1 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManagerTest.java @@ -0,0 +1,194 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.service; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.math.BigInteger; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Principal; +import java.security.PublicKey; +import java.security.SignatureException; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.X509Certificate; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.boot.test.context.SpringBootTest; +import org.opengroup.osdu.indexer.aws.IndexerAwsApplication; +import java.net.Socket; + +@RunWith(MockitoJUnitRunner.class) +@SpringBootTest(classes = {IndexerAwsApplication.class}) +public class UnsafeX509ExtendedTrustManagerTest { + + + @Test + public void createClientBuilder() throws Exception { + + X509Certificate[] certificates = new X509Certificate[1]; + + certificates[0] = new Certificate(); + + UnsafeX509ExtendedTrustManager manager = UnsafeX509ExtendedTrustManager.INSTANCE; + + manager.checkClientTrusted(certificates, "auth"); + manager.checkClientTrusted(certificates, "auth", new Socket()); + manager.checkServerTrusted(certificates, "auth"); + manager.checkServerTrusted(certificates, "auth", new Socket()); + + } + +} + +class Certificate extends X509Certificate{ + + @Override + public boolean hasUnsupportedCriticalExtension() { + return true; + } + + @Override + public Set<String> getCriticalExtensionOIDs() { + return new HashSet<String>(); + } + + @Override + public Set<String> getNonCriticalExtensionOIDs() { + return new HashSet<String>(); + } + + @Override + public byte[] getExtensionValue(String oid) { + return new byte[0]; + } + + @Override + public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException { + } + + @Override + public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException { + } + + @Override + public int getVersion() { + return 0; + } + + @Override + public BigInteger getSerialNumber() { + return new BigInteger("0"); + } + + @Override + public Principal getIssuerDN() { + return null; + } + + @Override + public Principal getSubjectDN() { + return null; + } + + @Override + public Date getNotBefore() { + return null; + } + + @Override + public Date getNotAfter() { + return null; + } + + @Override + public byte[] getTBSCertificate() throws CertificateEncodingException { + return null; + } + + @Override + public byte[] getSignature() { + return null; + } + + @Override + public String getSigAlgName() { + return ""; + } + + @Override + public String getSigAlgOID() { + return ""; + } + + @Override + public byte[] getSigAlgParams() { + return null; + } + + @Override + public boolean[] getIssuerUniqueID() { + return null; + } + + @Override + public boolean[] getSubjectUniqueID() { + return null; + } + + @Override + public boolean[] getKeyUsage() { + return null; + } + + @Override + public int getBasicConstraints() { + return 0; + } + + @Override + public byte[] getEncoded() throws CertificateEncodingException { + return null; + } + + @Override + public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, + NoSuchProviderException, SignatureException { + } + + @Override + public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, + InvalidKeyException, NoSuchProviderException, SignatureException { + } + + @Override + public String toString() { + return ""; + } + + @Override + public PublicKey getPublicKey() { + return null; + } + +} \ No newline at end of file diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java index 3dcafab9dab54ddaeed30831108f69a38dd4cc56..cc60dfc77bc9b36feba4749765d22b1e8b7d9725 100644 --- a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java @@ -21,7 +21,6 @@ import org.mockito.Mockito; import org.opengroup.osdu.core.common.http.HttpClient; import org.opengroup.osdu.core.common.http.HttpRequest; import org.opengroup.osdu.core.common.http.HttpResponse; -import org.springframework.stereotype.Component; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull;