Skip to content
Snippets Groups Projects
Commit b347c9c2 authored by Rostislav Dublin (EPAM)'s avatar Rostislav Dublin (EPAM)
Browse files

Merge branch 'anthos-gcp' into 'master'

Cloud-agnostic module `legal-reference` for Anthos (GONRG-1423)

See merge request !92
parents 34ea47ef 965d9418
No related branches found
No related tags found
1 merge request!92Cloud-agnostic module `legal-reference` for Anthos (GONRG-1423)
Pipeline #21818 passed with warnings
Showing
with 1218 additions and 0 deletions
......@@ -43,6 +43,7 @@
<module>provider/legal-azure</module>
<module>provider/legal-aws</module>
<module>provider/legal-ibm</module>
<module>provider/legal-reference</module>
</modules>
<repositories>
......
# Use the official AdoptOpenJDK for a base image.
# https://hub.docker.com/_/openjdk
FROM openjdk:8-slim
WORKDIR /app
COPY target/legal-reference-0.0.1-SNAPSHOT-spring-boot.jar legal.jar
# Run the web service on container startup.
CMD java -Djava.security.egd=file:/dev/./urandom -Dserver.port=8080 -jar /app/legal.jar
apiVersion: v1
data:
AUTHORIZE_API: ${AUTHORIZE_API}
MONGO_DB_URL: ${MONGO_DB_URL}
MONGO_DB_USER: ${MONGO_DB_USER}
MONGO_DB_NAME: ${MONGO_DB_NAME}
REGION: ${REGION}
kind: ConfigMap
metadata:
labels:
app: legal
name: legal-config
namespace: default
---
apiVersion: apps/v1
kind: Deployment
metadata:
generateName: legal-anthos
labels:
app: legal
name: legal
namespace: default
spec:
selector:
matchLabels:
app: legal
replicas: 1
template:
metadata:
labels:
app: legal
spec:
containers:
- env:
- name: AUTHORIZE_API
valueFrom:
configMapKeyRef:
key: AUTHORIZE_API
name: legal-config
- name: MONGO_DB_URL
valueFrom:
configMapKeyRef:
key: MONGO_DB_URL
name: legal-config
- name: MONGO_DB_USER
valueFrom:
configMapKeyRef:
key: MONGO_DB_USER
name: legal-config
- name: MONGO_DB_PASSWORD
valueFrom:
secretKeyRef:
name: legal-secret
key: mongo.db.password
- name: MONGO_DB_NAME
valueFrom:
configMapKeyRef:
key: MONGO_DB_NAME
name: legal-config
- name: MB_RABBITMQ_URI
valueFrom:
secretKeyRef:
name: legal-secret
key: mb.rabbitmq.uri
- name: REGION
valueFrom:
configMapKeyRef:
key: REGION
name: legal-config
image: us.gcr.io/osdu-anthos-02/os-legal/anthos-legal:latest
name: os-legal-1-sha256-1
---
apiVersion: v1
kind: Service
metadata:
name: legal
namespace: default
spec:
ports:
- protocol: TCP
port: 80
targetPort: 8080
selector:
app: legal
type: LoadBalancer
---
apiVersion: v1
data:
mongo.db.password: ${MONGO_DB_PASSWORD}
mb.rabbitmq.uri: ${MB_RABBITMQ_URI}
kind: Secret
metadata:
name: legal-secret
namespace: default
type: Opaque
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.opengroup.osdu.legal</groupId>
<artifactId>legal-service</artifactId>
<version>0.0.5-SNAPSHOT</version>
</parent>
<artifactId>legal-reference</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>legal-reference</name>
<description>Legal service for Anthos</description>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.opengroup.osdu.legal</groupId>
<artifactId>legal-core</artifactId>
<version>0.0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-common</artifactId>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-gcp</artifactId>
<version>0.1.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.3</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-circuitbreaker -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>0.17.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-retry -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>0.17.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.stefanbirkner/system-rules -->
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
org.opengroup.osdu.legal.LegalApplication
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
apiVersion: skaffold/v2beta5
kind: Config
metadata:
name: legal-reference
build:
artifacts:
- image: us.gcr.io/osdu-anthos-02/os-legal/anthos-legal
deploy:
kubectl:
manifests:
- kubernetes/deployments/deployment-legal-service.yml
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal;
import org.opengroup.osdu.core.gcp.multitenancy.TenantFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@ComponentScan(value = {"org.opengroup.osdu"}, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = TenantFactory.class)
})
@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
public class LegalApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(LegalApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(LegalApplication.class, args);
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties
@Getter
@Setter
public class MongoDBConfigProperties {
private String mongoDbUrl;
private String mongoDbUser;
private String mongoDbPassword;
private String mongoDbName;
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties
@Getter
@Setter
public class RabbitMqConfigProperties {
private String mbRabbitMqUri;
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.countries;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.legal.config.MongoDBConfigProperties;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReaderFactory;
import org.opengroup.osdu.legal.tags.dataaccess.MongoClientProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class StorageReaderFactoryImpl implements IStorageReaderFactory {
private static final String LEGAL_COLLECTION_NAME = "countries";
private final MongoDBConfigProperties mongoDBConfigProperties;
private final MongoClientProvider clientProvider;
@Autowired
public StorageReaderFactoryImpl(MongoDBConfigProperties mongoDBConfigProperties, MongoClientProvider clientProvider) {
this.mongoDBConfigProperties = mongoDBConfigProperties;
this.clientProvider = clientProvider;
}
@Override
public IStorageReader getReader(TenantInfo tenant, String projectRegion) {
return new StorageReaderImpl(tenant, projectRegion, mongoDBConfigProperties.getMongoDbName(),
LEGAL_COLLECTION_NAME, clientProvider);
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.countries;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.List;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.opengroup.osdu.legal.tags.dataaccess.MongoClientProvider;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
public class StorageReaderImpl implements IStorageReader {
private final TenantInfo tenantInfo;
private final String cloudRegion;
private final String dbName;
private final String collectionName;
private final MongoClientProvider client;
public StorageReaderImpl(TenantInfo tenantInfo, String projectRegion, String dbName,
String collectionName, MongoClientProvider client) {
this.tenantInfo = tenantInfo;
this.cloudRegion = projectRegion;
this.collectionName = collectionName;
this.dbName = dbName;
this.client = client;
}
@Override
public byte[] readAllBytes() {
MongoOperations ops = new MongoTemplate(client.getMongoClient(), dbName);
Query query = new Query(Criteria
.where("tenant").is(tenantInfo.getName())
.and("region").is(cloudRegion));
query.fields().include("name");
query.fields().include("alpha2");
query.fields().include("numeric");
query.fields().include("residencyRisk");
query.fields().include("typesNotApplyDataResidency");
List<JsonObject> objects = ops.find(query, JsonObject.class, collectionName);
JsonArray array = new JsonArray();
for (JsonObject s : objects) {
array.add(s);
}
return new Gson().toJson(array).getBytes();
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.di;
import org.opengroup.osdu.core.gcp.multitenancy.IPublisherFactory;
import org.opengroup.osdu.core.gcp.multitenancy.PublisherFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class PublisherFactoryService extends AbstractFactoryBean<IPublisherFactory> {
@Override
protected IPublisherFactory createInstance() throws Exception {
return new PublisherFactory();
}
@Override
public Class<?> getObjectType() {
return IPublisherFactory.class;
}
}
\ No newline at end of file
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.di;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;
import javax.annotation.PostConstruct;
import org.opengroup.osdu.legal.config.RabbitMqConfigProperties;
import org.opengroup.osdu.legal.messagebus.IMessageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQFactoryImpl implements IMessageFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQFactoryImpl.class);
private final RabbitMqConfigProperties rabbitMqConfigProperties;
private Channel channel;
@Autowired
public RabbitMQFactoryImpl(RabbitMqConfigProperties rabbitMqConfigProperties) {
this.rabbitMqConfigProperties = rabbitMqConfigProperties;
}
@PostConstruct
private void init() {
ConnectionFactory factory = new ConnectionFactory();
try {
String mbRabbitMqUri = rabbitMqConfigProperties.getMbRabbitMqUri();
LOGGER.debug(String.format("RabbitMQ Uri = %s", mbRabbitMqUri));
factory.setUri(mbRabbitMqUri);
factory.setAutomaticRecoveryEnabled(true);
Connection conn = factory.newConnection();
this.channel = conn.createChannel();
LOGGER.debug("RabbitMQ Channel was created.");
for (String queue : Arrays
.asList(DEFAULT_QUEUE_NAME, INDEXER_QUEUE_NAME, LEGAL_QUEUE_NAME)) {
channel.queueDeclare("os-legal-" + queue, false, false, false, null);
LOGGER.debug(String.format("Queue [os-legal- %s ] was declared.", queue));
}
} catch (KeyManagementException | NoSuchAlgorithmException | URISyntaxException | IOException | TimeoutException e) {
LOGGER.error(e.getMessage(), e);
}
}
@Override
public void sendMessage(String msg) {
this.sendMessage("records", msg);
}
@Override
public void sendMessage(String queueName, String msg) {
String queueNameWithPrefix = "os-legal-" + queueName;
try {
channel.basicPublish("", queueNameWithPrefix, null, msg.getBytes());
LOGGER.info(String.format("[x] Sent '%s' to queue [ %s]", msg, queueNameWithPrefix));
} catch (IOException e) {
LOGGER.error(String.format("Unable to publish message to [ %s ]", queueNameWithPrefix));
LOGGER.error(e.getMessage(), e);
}
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.di;
import static java.util.Objects.isNull;
import com.google.gson.Gson;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.bson.Document;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.legal.tags.dataaccess.MongoClientProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
@Component
public class TenantFactoryImpl implements ITenantFactory {
private static final Logger LOG = LoggerFactory.getLogger(TenantFactoryImpl.class);
private static final String SCHEMA_DATABASE = "main";
private static final String TENANT_INFO = "tenantinfo";
private final MongoClientProvider clientProvider;
@Autowired
public TenantFactoryImpl(MongoClientProvider clientProvider) {
this.clientProvider = clientProvider;
}
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 (Objects.isNull(tenants) || this.tenants.isEmpty()) {
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<>();
MongoCollection<Document> mongoCollection = clientProvider
.getMongoClient()
.getDatabase(SCHEMA_DATABASE)
.getCollection(TENANT_INFO, Document.class);
FindIterable<Document> results = mongoCollection.find();
if (isNull(results) || isNull(results.first())) {
LOG.error(String.format("Collection \'%s\' is empty.", results));
}
for (Document document : results) {
TenantInfo tenantInfo = new Gson().fromJson(document.toJson(), TenantInfo.class);
this.tenants.put(tenantInfo.getName(), tenantInfo);
}
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.entity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.Data;
import org.opengroup.osdu.core.common.model.legal.LegalTag;
import org.opengroup.osdu.core.common.model.legal.Properties;
import org.springframework.data.annotation.Id;
@Data
public class LegalTagMongoEntity {
@Id
private Long id;
private String name;
private String description;
private PropertiesMongoEntity properties;
private Boolean isValid;
public LegalTagMongoEntity() {
}
public LegalTagMongoEntity(LegalTag legalTag) {
this.id = legalTag.getId();
this.name = legalTag.getName();
this.description = legalTag.getDescription();
this.properties = new PropertiesMongoEntity(legalTag.getProperties());
this.isValid = legalTag.getIsValid();
}
public static LegalTag convertTo(LegalTagMongoEntity entity) {
LegalTag legalTag = new LegalTag();
legalTag.setId(entity.getId());
legalTag.setProperties(PropertiesMongoEntity.convertTo(entity.getProperties()));
legalTag.setDescription(entity.getDescription());
legalTag.setName(entity.getName());
legalTag.setIsValid(entity.getIsValid());
return legalTag;
}
public static LegalTagMongoEntity convertFrom(LegalTag legalTag) {
LegalTagMongoEntity entity = new LegalTagMongoEntity();
entity.setId(legalTag.getId());
entity.setProperties(new PropertiesMongoEntity(legalTag.getProperties()));
entity.setDescription(legalTag.getDescription());
entity.setName(legalTag.getName());
entity.setIsValid(legalTag.getIsValid());
return entity;
}
@Data
private static class PropertiesMongoEntity {
private List<String> countryOfOrigin = new ArrayList();
private String contractId = "";
private Date expirationDate;
private String originator = "";
private String dataType = "";
private String securityClassification = "";
private String personalData = "";
private String exportClassification = "";
public PropertiesMongoEntity() {
}
public PropertiesMongoEntity(Properties properties) {
this.countryOfOrigin = properties.getCountryOfOrigin();
this.contractId = properties.getContractId();
this.expirationDate = new Date(properties.getExpirationDate().getTime());
this.originator = properties.getOriginator();
this.dataType = properties.getDataType();
this.securityClassification = properties.getSecurityClassification();
this.personalData = properties.getPersonalData();
this.exportClassification = properties.getExportClassification();
}
public static Properties convertTo(PropertiesMongoEntity entity) {
Properties properties = new Properties();
properties.setCountryOfOrigin(entity.getCountryOfOrigin());
properties.setContractId(entity.getContractId());
properties.setExpirationDate(new java.sql.Date(entity.getExpirationDate().getTime()));
properties.setOriginator(entity.getOriginator());
properties.setDataType(entity.getDataType());
properties.setSecurityClassification(entity.getSecurityClassification());
properties.setPersonalData(entity.getPersonalData());
properties.setExportClassification(entity.getExportClassification());
return properties;
}
public static PropertiesMongoEntity convertFrom(Properties properties) {
PropertiesMongoEntity entity = new PropertiesMongoEntity();
entity.countryOfOrigin = properties.getCountryOfOrigin();
entity.contractId = properties.getContractId();
entity.expirationDate = new Date(properties.getExpirationDate().getTime());
entity.originator = properties.getOriginator();
entity.dataType = properties.getDataType();
entity.securityClassification = properties.getSecurityClassification();
entity.personalData = properties.getPersonalData();
entity.exportClassification = properties.getExportClassification();
return entity;
}
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.jobs;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTag;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
import org.opengroup.osdu.legal.messagebus.IMessageFactory;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class LegalTagPublisherImpl implements ILegalTagPublisher {
private final IMessageFactory mq;
@Autowired
public LegalTagPublisherImpl(IMessageFactory mq) {
this.mq = mq;
}
@Override
public void publish(String projectId, DpsHeaders headers, StatusChangedTags tags) {
final int BATCH_SIZE = 50;
Gson gson = new Gson();
Map<String, String> message = new HashMap<>();
for (int i = 0; i < tags.getStatusChangedTags().size(); i += BATCH_SIZE) {
List<StatusChangedTag> batch = tags.getStatusChangedTags().subList(i,
Math.min(tags.getStatusChangedTags().size(), i + BATCH_SIZE));
String json = gson.toJson(batch);
message.put("data", json);
message.put(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
message.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
headers.addCorrelationIdIfMissing();
message.put(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
mq.sendMessage(IMessageFactory.LEGAL_QUEUE_NAME, gson.toJson(message));
}
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.messagebus;
public interface IMessageFactory {
String DEFAULT_QUEUE_NAME = "records";
String LEGAL_QUEUE_NAME = "legal";
String INDEXER_QUEUE_NAME = "indexer";
void sendMessage(String msg);
void sendMessage(String queueName, String msg);
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.security;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties
@Getter
@Setter
public class EntitlementsConfig {
private String authorizeApi;
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.security;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().disable()
.csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/api-docs")
.antMatchers("/index")
.antMatchers("/swagger");
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.tags;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagRepository;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagRepositoryFactory;
import org.opengroup.osdu.legal.tags.dataaccess.MongoLegalTagRepository;
import org.opengroup.osdu.legal.tags.dataaccess.ResilientLegalTagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
@Primary
public class LegalTagRepositoryFactoryMongoImpl implements ILegalTagRepositoryFactory {
private final Map<String, ILegalTagRepository> tenantRepositories = new HashMap<>();
@Autowired
private MongoLegalTagRepository mongoLegalTagRepository;
@Override
public ILegalTagRepository get(String tenantName) {
if (StringUtils.isBlank(tenantName)) {
throw invalidTenantGivenException(tenantName);
}
if (!tenantRepositories.containsKey(tenantName)) {
addRepository(tenantName);
}
return tenantRepositories.get(tenantName);
}
private void addRepository(String tenantName) {
ILegalTagRepository repo = new ResilientLegalTagRepository(mongoLegalTagRepository);
tenantRepositories.put(tenantName, repo);
}
private AppException invalidTenantGivenException(String tenantName) {
return new AppException(403, "Forbidden",
String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
}
}
/*
* Copyright 2021 Google LLC
* Copyright 2021 EPAM Systems, Inc
*
* 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
*
* https://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.legal.tags.dataaccess;
import static java.util.Objects.isNull;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.legal.config.MongoDBConfigProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
@Component
public class MongoClientProvider {
private static final Logger LOG = LoggerFactory.getLogger(MongoClientProvider.class);
private static final String MONGO_PREFIX = "mongodb://";
private static final String MONGO_OPTIONS = "retryWrites=true&w=majority&maxIdleTimeMS=10000";
private final MongoDBConfigProperties mongoDBConfigProperties;
private MongoClient mongoClient = null;
@Autowired
public MongoClientProvider(MongoDBConfigProperties mongoDBConfigProperties) {
this.mongoDBConfigProperties = mongoDBConfigProperties;
}
private MongoClient initMongoClient() throws RuntimeException {
final String connectionString = String.format("%s%s:%s@%s/?%s",
MONGO_PREFIX,
mongoDBConfigProperties.getMongoDbUser(),
mongoDBConfigProperties.getMongoDbPassword(),
mongoDBConfigProperties.getMongoDbUrl(),
MONGO_OPTIONS);
ConnectionString connString = new ConnectionString(connectionString);
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connString)
.retryWrites(true)
.build();
try {
mongoClient = MongoClients.create(settings);
} catch (Exception ex) {
LOG.error("Error connecting MongoDB", ex);
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error connecting MongoDB",
ex.getMessage(), ex);
}
return mongoClient;
}
public MongoClient getMongoClient() {
return isNull(mongoClient) ? initMongoClient() : mongoClient;
}
public MongoOperations getOps(String dbName) {
return new MongoTemplate(getMongoClient(), dbName);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment