Commit 78cdeeea authored by Aman Verma's avatar Aman Verma Committed by Hema Vishnu Pola [Microsoft]
Browse files

Adding logic to generate Bearer token for azure, to be used in Integration Tests

parent 1bc8586d
......@@ -40,7 +40,7 @@
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<version>0.0.10</version>
<version>0.0.17</version>
</dependency>
<dependency>
......@@ -52,6 +52,12 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
......
// 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.schema.azure.definitions;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TenantInfoDoc {
private String id;
private String dataPartitionId;
private String complianceRuleSet;
}
\ No newline at end of file
......@@ -41,9 +41,6 @@ public class CosmosContainerConfig {
@Value("${source.container.name}")
private String sourceContainerName;
@Value("${tenant-info.container.name}")
private String tenantInfoContainerName;
@Bean
public String authorityContainer() {
return authorityContainerName;
......@@ -63,9 +60,4 @@ public class CosmosContainerConfig {
public String sourceContainer() {
return sourceContainerName;
}
@Bean
public String tenantInfoContainer() {
return tenantInfoContainerName;
}
}
\ No newline at end of file
// 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.schema.azure.di;
import org.opengroup.osdu.azure.CosmosStore;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.schema.azure.definitions.TenantInfoDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Component
public class TenantFactoryImpl implements ITenantFactory {
@Autowired
private String tenantInfoContainer;
@Autowired
private CosmosStore cosmosStore;
@Autowired
private String cosmosDBName;
@Autowired
private DpsHeaders headers;
private Map<String, TenantInfo> tenants;
public boolean exists(String tenantName)
{
if (this.tenants == null)
initTenants();
return this.tenants.containsKey(tenantName);
}
public TenantInfo getTenantInfo(String tenantName) {
if (this.tenants == null)
initTenants();
return this.tenants.get(tenantName);
}
public Collection<TenantInfo> listTenantInfo() {
if (this.tenants == null)
initTenants();
return this.tenants.values();
}
public <V> ICache<String, V> createCache(String tenantName, String host, int port, int expireTimeSeconds, Class<V> classOfV)
{
return null;
}
public void flushCache() {}
private void initTenants() {
this.tenants = new HashMap<>();
cosmosStore.findAllItems( headers.getPartitionId(), cosmosDBName, tenantInfoContainer, TenantInfoDoc.class)
.forEach(doc -> {
TenantInfo ti = new TenantInfo();
String tenantName = doc.getId();
ti.setName(tenantName);
String complianceRuleSet = doc.getComplianceRuleSet();
ti.setComplianceRuleSet(complianceRuleSet);
this.tenants.put(tenantName, ti) ;
});
}
}
......@@ -174,7 +174,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
@Override
public String getLatestMinorVerSchema(SchemaInfo schemaInfo) throws ApplicationException {
SqlQuerySpec query = new SqlQuerySpec("SELECT * FROM c WHERE STARTSWITH(c.id, @partitionId)" +
SqlQuerySpec query = new SqlQuerySpec("SELECT * FROM c WHERE c.dataPartitionId = @partitionId" +
" AND c.flattenedSchemaInfo.authority = @authority" +
" AND c.flattenedSchemaInfo.source = @source" +
" AND c.flattenedSchemaInfo.entityType = @entityType" +
......@@ -272,7 +272,7 @@ public class AzureSchemaInfoStore implements ISchemaInfoStore {
@Override
public List<SchemaInfo> getSchemaInfoList(QueryParams queryParams, String tenantId) throws ApplicationException {
String queryText = "SELECT * FROM c WHERE STARTSWITH(c.id, @partitionId)";
String queryText = "SELECT * FROM c WHERE c.dataPartitionId = @partitionId";
HashMap<String, Object> parameterMap = new HashMap<>();
// Populate the implicit partitionId parameter
parameterMap.put("@partitionId", tenantId);
......
......@@ -13,6 +13,7 @@
# limitations under the License.
LOG_PREFIX=schema
server.servlet.context-path=/api/schema-service/v1
AUTHORIZE_API=${entitlements_service_endpoint}
AUTHORIZE_API_KEY=${entitlements_service_api_key}
......@@ -41,5 +42,5 @@ authority.container.name=Authority
entity-type.container.name=EntityType
schema-info.container.name=SchemaInfo
source.container.name=Source
tenant-info.container.name=TenantInfo
tenantInfo.container.name=TenantInfo
server.port=8080
......@@ -20,6 +20,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
......@@ -48,6 +49,8 @@ import java.io.IOException;
import java.util.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
......@@ -124,7 +127,6 @@ public class AzureSchemaInfoStoreTest {
Mockito.when(schemaStore.getSchema(dataPartitionId, schemaId)).thenReturn(CONTENT);
assertEquals(CONTENT, schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo()));
}
@Test
......
......@@ -15,6 +15,18 @@
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-schema-core</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
<version>0.0.1</version>
</dependency>
......@@ -59,6 +71,11 @@
<version>1.76.0</version>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<version>0.0.17</version>
</dependency>
</dependencies>
<build>
......
package org.opengroup.osdu.schema.util;
import com.google.common.base.Strings;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
public class AuthUtil {
public synchronized String getToken() throws Exception {
public synchronized String getToken() throws Exception {
String token = null;
String vendor = System.getProperty("VENDOR", System.getenv("VENDOR"));
if (Strings.isNullOrEmpty(token) && vendor.equals("gcp")) {
String serviceAccountFile = System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
String serviceAccountFile = System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
String audience = System.getProperty("INTEGRATION_TEST_AUDIENCE",
System.getenv("INTEGRATION_TEST_AUDIENCE"));
token = new GoogleServiceAccount(serviceAccountFile).getAuthToken(audience);
}else if (Strings.isNullOrEmpty(token) && vendor.equals("aws")) {
System.out.println("Token generation code for aws comes here");
} else if (Strings.isNullOrEmpty(token) && vendor.equals("azure")) {
System.out.println("Token generation code for azure comes here");
} else if (Strings.isNullOrEmpty(token) && vendor.equals("ibm")) {
System.out.println("Token generation code for ibm comes here");
}
System.out.println("Token generation code for aws comes here");
} else if (Strings.isNullOrEmpty(token) && vendor.equals("azure")) {
String sp_id = System.getProperty("INTEGRATION_TESTER", System.getenv("INTEGRATION_TESTER"));
String sp_secret = System.getProperty("TESTER_SERVICEPRINCIPAL_SECRET", System.getenv("TESTER_SERVICEPRINCIPAL_SECRET"));
String tenant_id = System.getProperty("AZURE_AD_TENANT_ID", System.getenv("AZURE_AD_TENANT_ID"));
String app_resource_id = System.getProperty("AZURE_AD_APP_RESOURCE_ID", System.getenv("AZURE_AD_APP_RESOURCE_ID"));
token = new AzureServicePrincipal().getIdToken(sp_id, sp_secret, tenant_id, app_resource_id);
} else if (Strings.isNullOrEmpty(token) && vendor.equals("ibm")) {
System.out.println("Token generation code for ibm comes here");
}
System.out.println("Bearer " + token);
return "Bearer " + token;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment