Commit 8ace9465 authored by Auto GO3-NRG Ticket's avatar Auto GO3-NRG Ticket
Browse files

Auto commit when pipeline succeeds

parents 07ca836b 1a1b9356
variables:
OSDU_GCP_APPLICATION_NAME: os-notification
OSDU_GCP_VENDOR: gcp
OSDU_GCP_SERVICE: notification
OSDU_GCP_ENV_VARS: APP_PROJECT=${OSDU_GCP_PROJECT},APP_ENTITLEMENTS=${OSDU_GCP_ENTITLEMENTS_URL},APP_REGISTER=${OSDU_GCP_REGISTER_URL},APP_GOOGLEAUDIENCE=${GOOGLE_AUDIENCE}
include:
- project: 'osdu/platform/ci-cd-pipelines'
ref: 'master'
......@@ -14,3 +20,10 @@ include:
- project: 'osdu/platform/ci-cd-pipelines'
ref: 'master'
file: 'scanners/gitlab-ultimate.yml'
- project: 'osdu/platform/ci-cd-pipelines'
ref: 'master'
file: 'cloud-providers/osdu-gcp-cloudrun.yml'
osdu-gcp-test:
allow_failure: true
# The file makes Jacoco understand that the Lambok's @Data's creation, should not be accounted for Jacoco's analysis.
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
......@@ -19,11 +19,8 @@ import org.opengroup.osdu.core.common.model.search.IdToken;
import org.opengroup.osdu.core.common.provider.interfaces.IJwtCache;
import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.inject.Named;
@Component
public class JwtCache implements IJwtCache<String, IdToken> {
private VmCache<String, IdToken> cache;
......@@ -31,7 +28,9 @@ public class JwtCache implements IJwtCache<String, IdToken> {
// Azure service account id_token can be requested only for 1 hr
private final static int EXPIRED_AFTER = 59;
public JwtCache(@Named("MAX_CACHE_VALUE_SIZE") String cacheSize){
@Autowired
public JwtCache(AppProperties appProperties){
String cacheSize = appProperties.getMaxCacheSize();
cache = new VmCache<>(EXPIRED_AFTER * 60, Integer.parseInt(cacheSize));
}
......
......@@ -14,37 +14,17 @@
package org.opengroup.osdu.notification.provider.azure.security;
import com.microsoft.azure.spring.autoconfigure.aad.AADAppRoleStatelessAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AADAppRoleStatelessAuthenticationFilter appRoleAuthFilter;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.authorizeRequests()
.antMatchers("/",
"/swagger",
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html",
"/webjars/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(appRoleAuthFilter, UsernamePasswordAuthenticationFilter.class);
public void configure(HttpSecurity http) throws Exception {
http.httpBasic().disable().csrf().disable();
}
}
......@@ -17,16 +17,12 @@ package org.opengroup.osdu.notification.provider.azure.util;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.notification.provider.interfaces.IAppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Configuration;
import javax.inject.Named;
@Component
@Configuration
public class AppProperties implements IAppProperties {
@Value("${app.entitlements}")
......@@ -44,15 +40,9 @@ public class AppProperties implements IAppProperties {
@Value("${aad.oboApi}")
private String aadOboAPI;
@Value("${app.maxCacheSize")
private String CacheValueSize;
@Autowired
private SecretClient secretClient;
@Autowired
private JaxRsDpsLog logger;
private String authURL;
private String authClientID;
......@@ -97,8 +87,6 @@ public class AppProperties implements IAppProperties {
return this.authURL;
}
@Bean
@Named("MAX_CACHE_VALUE_SIZE")
public String getMaxCacheSize() {
return maxCacheSize;
}
......@@ -106,13 +94,11 @@ public class AppProperties implements IAppProperties {
private String getKeyVaultSecret(SecretClient kv, String secretName) {
KeyVaultSecret secret = kv.getSecret(secretName);
if (secret == null) {
logger.error(String.format("Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
throw new IllegalStateException(String.format("No secret found with name %s", secretName));
}
String secretValue = secret.getValue();
if (secretValue == null) {
logger.error(String.format("Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
throw new IllegalStateException(String.format(
"Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
}
......
......@@ -14,20 +14,17 @@
package org.opengroup.osdu.notification.provider.azure.util;
import lombok.SneakyThrows;
import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount;
import org.springframework.stereotype.Component;
import javax.naming.AuthenticationNotSupportedException;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
@Component
public class GoogleServiceAccountImpl implements IGoogleServiceAccount {
@SneakyThrows
@Override
public String getIdToken(String keyString, String audience) {
// TODO : Check if it is to be supported
throw new AuthenticationNotSupportedException();
throw new NotImplementedException();
}
}
// Copyright © Microsoft Corporation
//
// 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.notification.cache;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.search.IdToken;
import org.opengroup.osdu.notification.provider.azure.cache.JwtCache;
import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
public class JWTCacheTest {
private static String testTenantName = "testTenant";
@Mock
private AppProperties appProperties;
@Mock
private IdToken idToken;
private JwtCache sut;
@Before
public void setup() {
initMocks(this);
when(appProperties.getMaxCacheSize()).thenReturn("10");
sut = new JwtCache(appProperties);
}
@Test
public void should_throwWhenCacheSizeIsInvalid_JwtCache(){
// Set up
when(appProperties.getMaxCacheSize()).thenReturn(null);
try {
// Act
sut = new JwtCache(appProperties);
// Assert
fail("Should throw exception");
} catch (Exception exception){
Assertions.assertEquals(exception.getClass(), NumberFormatException.class);
}
// Set Up
when(appProperties.getMaxCacheSize()).thenReturn("");
try {
// Act
sut = new JwtCache(appProperties);
// Assert
fail("Should throw exception");
} catch (Exception exception){
Assertions.assertEquals(exception.getClass(), NumberFormatException.class);
}
// Set Up
when(appProperties.getMaxCacheSize()).thenReturn("-1");
try {
// Act
sut = new JwtCache(appProperties);
// Assert
fail("Should throw exception");
} catch (Exception exception){
Assertions.assertEquals(exception.getClass(), IllegalArgumentException.class);
}
}
@Test
public void should_return_getPut(){
// Act
IdToken observed = sut.get("test Tenant");
// Assert
Assert.assertNull(observed);
// Set Up
sut.put(testTenantName, idToken);
// Act
observed = sut.get(testTenantName);
// Asset
Assert.assertEquals(idToken, observed);
// Act
observed = sut.get("notInCache");
// Assert
Assert.assertNull(observed);
try {
// Act
sut.put(null, null);
} catch (Exception e){
Assert.assertEquals(NullPointerException.class, e.getClass());
}
}
@Test
public void should_returnAppropriate_delete() {
// Set Up
sut.put(testTenantName, idToken);
// Act
sut.delete(testTenantName);
// Assert
IdToken observed = sut.get(testTenantName);
Assert.assertNull(observed);
try {
// Act
sut.delete("notInCache");
// Assert
} catch (Exception e){
Assert.assertEquals(NullPointerException.class, e.getClass());
}
}
@Test
public void should_returnAppropriate_clearAll() {
// Set Up
sut.put(testTenantName, idToken);
// Act
sut.clearAll();
// Assert
IdToken observed = sut.get(testTenantName);
Assert.assertNull(observed);
}
}
// Copyright © Microsoft Corporation
//
// 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.notification.di;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient;
import org.opengroup.osdu.notification.provider.azure.di.ServiceAccountJwtClientFactory;
import org.opengroup.osdu.notification.provider.azure.util.ServiceAccountJwtAzureClientImpl;
import static org.junit.Assert.fail;
import static org.mockito.MockitoAnnotations.initMocks;
public class ServiceAccountJwtClientFactoryTest {
@InjectMocks
ServiceAccountJwtClientFactory sut;
@Before
public void init() {
initMocks(this);
}
@Test
public void testGettingInstance() {
try {
// Act
Class<?> objectType = sut.getObjectType();
// Assert
Assert.assertEquals(IServiceAccountJwtClient.class, objectType);
// Act
IServiceAccountJwtClient serviceAccountJwtClient = sut.createInstance();
// Assert
Assert.assertNotNull("Should not be null", serviceAccountJwtClient);
Assert.assertEquals(serviceAccountJwtClient.getClass(), ServiceAccountJwtAzureClientImpl.class);
} catch (Exception exception) {
fail("Should not throw this exception" + exception.getMessage());
}
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ package org.opengroup.osdu.notification.provider.azure;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
......@@ -31,6 +32,7 @@ import java.io.StringReader;
import java.util.Map;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
......@@ -113,13 +115,13 @@ public class EventGridRequestBodyExtractorTest {
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutAttributes));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -154,7 +156,7 @@ public class EventGridRequestBodyExtractorTest {
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutData));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -183,13 +185,13 @@ public class EventGridRequestBodyExtractorTest {
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n"+
" \"data\": \"dGVzdA==\",\n"+
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutDataPartitionId));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -219,16 +221,16 @@ public class EventGridRequestBodyExtractorTest {
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes \"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
String expectedData = "[{\"kind\":\"testkind\",\"id\":\"testid\",\"operationtype\":\"create\"},{\"kind\":\"testkind2\",\"id\":\"testid2\",\"operationtype\":\"update\"}]";
String expectedData = "test";
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -253,13 +255,13 @@ public class EventGridRequestBodyExtractorTest {
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -301,4 +303,110 @@ public class EventGridRequestBodyExtractorTest {
// Assert
Assert.assertEquals(observedResponse, expectedResponse);
}
@Test
public void should_throwWhenNotHandshakeRequest_getHandshakeResponse() throws IOException {
// Set up
String validHandshakeRequestRoot =
"[{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
String expectedResponse = null;
BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String observedResponse = this.sut.getValidationCodeForHandshake();
// Assert
Assert.assertNull(observedResponse);
}
@Test
public void should_throwWhenHandshakeRequest_extractDataFromRequestBody() throws IOException {
String inVaidRequestRoot = " [{\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
" \"subject\": \"\",\n" +
" \"data\": {\n" +
" \"validationCode\": \"testValidationCode\",\n" +
" \"validationUrl\": \"testURL\"\n" +
" },\n" +
" \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(inVaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody();
Assert.assertNull(observedAttributes);
}
@Test
public void should_returnNotificationId_extractNotificationIdFromRequestBody() throws IOException {
// Set Up
String vaidRequestRoot = "[{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +