Skip to content
Snippets Groups Projects
Commit 22fccd30 authored by David Diederich's avatar David Diederich
Browse files

Initial Import

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 1166 additions and 0 deletions
##
# Needed to run the service
##
export LOG_PREFIX=
export legal_service_region=
export entitlements_service_endpoint=
export entitlements_service_api_key=
export LEGAL_HOSTNAME=
export CRON_JOB_IP=
export azure.activedirectory.session-stateless=
export aad_client_id=
export azure.activedirectory.AppIdUri=
export cosmosdb_database=
export storage_account=
export azure.storage.container-name=
export azure.storage.enable-https=
export servicebus_topic_name=
export servicebus_namespace_name=
export KEYVAULT_URI=
export AZURE_CLIENT_ID=
export AZURE_TENANT_ID=
export AZURE_CLIENT_SECRET=
export appinsights_key=
##
# Needed to run the integration tests
##
export INTEGRATION_TESTER=
export MY_TENANT=
export AZURE_TESTER_SERVICEPRINCIPAL_SECRET=
export AZURE_AD_TENANT_ID=
export AZURE_AD_APP_RESOURCE_ID=
export AZURE_LEGAL_STORAGE_ACCOUNT=
export AZURE_LEGAL_STORAGE_KEY=
export AZURE_LEGAL_SERVICEBUS=
export AZURE_LEGAL_TOPICNAME=
export HOST_URL=
export ENTITLEMENT_URL=
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
### macOS ###
*.DS_Store
# os-legal
## os-legal-azure
The steps for running `os-legal-azure` can be found in the [Azure Implementation README.md file](./provider/legal-azure/README.md).
## os-legal-aws
<!-- This should be replaced with a link to AWS specific docs -->
## os-legal-gcp
<!-- This should be replaced with a link to GCP specific docs -->
## License
Copyright 2017-2019, Schlumberger
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](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.
\ No newline at end of file
# Maven
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/java
trigger:
branches:
include:
- kuber-migration
paths:
exclude:
- README.md
- .gitignore
pool:
name: Hosted Ubuntu 1604
demands: maven
variables:
buildMavenModules: legal-core,provider/legal-gcp
dockerDir: provider/legal-gcp/docker
imageName: os-legal-app
deploymentDir: provider/legal-gcp/kubernetes/deployments
deploymentFile: deployment-os-legal-service.yml
mavenSettings: ./maven/settings.xml
integrationTestCorePom: testing/legal-test-core/pom.xml
integrationTestGcpPom: testing/legal-test-gcp/pom.xml
steps:
- task: DownloadSecureFile@1
name: gcrKey
inputs:
secureFile: cicd-push-image-to-cr-keyfile.json
- task: DownloadSecureFile@1
name: kuberConfig
inputs:
secureFile: kubeconfig
- task: DownloadSecureFile@1
name: gac
inputs:
secureFile: opendes-datafier-126f1340d475.json
- task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P legal-core install'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
- task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P legal-gcp -DskipTests package'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
- bash: |
#!/bin/bash
set -e
pushd $(dockerDir)
docker-compose build $(imageName)
echo 'Image done.'
cat $(gcrKey.secureFilePath) | docker login -u _json_key --password-stdin https://gcr.io
echo 'Login done.'
docker push gcr.io/opendes/$(imageName)
echo 'Push done.'
popd
pushd $(deploymentDir)
kubectl --kubeconfig $(kuberConfig.secureFilePath) delete -f $(deploymentFile)
kubectl --kubeconfig $(kuberConfig.secureFilePath) apply -f $(deploymentFile)
popd
attempt_counter=0
max_attempts=60
max_attempts=120
until [[ $(curl --head --write-out %{http_code} $(LEGAL_READINESS_URL) --silent -o /dev/null --fail) -eq 401 ]]; do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Service is not available, integraton tests are skipped"
exit 1
fi
printf '.'
attempt_counter=$(($attempt_counter+1))
sleep 2
done
- task: Maven@3
inputs:
mavenPomFile: '$(integrationTestCorePom)'
options: '--settings $(mavenSettings) -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) install'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
- task: Maven@3
inputs:
mavenPomFile: '$(integrationTestGcpPom)'
options: '--settings $(mavenSettings) -DargLine="-DGOOGLE_APPLICATION_CREDENTIALS=$(gac.secureFilePath)" -DSKIP_HTTP_TESTS=SKIP_HTTP_TESTS -Dsurefire.useFile=false -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -DINTEGRATION_TESTER=$(INTEGRATION_TESTER) -DNO_DATA_ACCESS_TESTER=$(NO_DATA_ACCESS_TESTER) -DHOST_URL=$(HOST_URL) -DINTEGRATION_TEST_AUDIENCE=$(INTEGRATION_TEST_AUDIENCE) -DMY_TENANT=$(MY_TENANT) -DMY_TENANT_PROJECT=$(MY_TENANT_PROJECT) compile'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
#####################
# README: Defines a template to be used as a starting point for defining a service pipeline
#####################
trigger:
batch: true
branches:
include:
- master
paths:
exclude:
- /**/*.md
- .gitignore
- images/
pr:
autoCancel: true
branches:
include:
- '*'
paths:
exclude:
- /**/*.md
- .gitignore
- images/
resources:
repositories:
- repository: infrastructure-templates
type: git
name: open-data-ecosystem/infrastructure-templates
variables:
- group: 'Azure Common Secrets'
- group: 'Azure - Common'
- name: serviceName
value: 'legal'
stages:
- template: devops/service-pipelines/build-stage.yml@infrastructure-templates
parameters:
mavenGoal: 'package'
mavenPublishJUnitResults: true
serviceCoreMavenOptions: '--settings ./maven/settings.xml -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P legal-core'
mavenOptions: '--settings ./maven/settings.xml -DVSTS_FEED_TOKEN=$(VSTS_FEED_TOKEN) -P legal-azure'
copyFileContents: |
pom.xml
provider/legal-azure/maven/settings.xml
provider/legal-azure/pom.xml
provider/legal-azure/target/*-spring-boot.jar
copyFileContentsToFlatten: ''
mavenSettingsFile: './maven/settings.xml'
serviceBase: ${{ variables.serviceName }}
testingRootFolder: 'testing'
- template: devops/service-pipelines/deploy-stages.yml@infrastructure-templates
parameters:
serviceName: ${{ variables.serviceName }}
testCoreMavenPomFile: 'testing/legal-test-core/pom.xml'
testCoreMavenOptions: '--settings $(System.DefaultWorkingDirectory)/drop/deploy/testing/maven/settings.xml'
providers:
- name: Azure
# Merges into Master
${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
environments: ['devint', 'qa', 'prod']
# PR updates / creations
${{ if ne(variables['Build.SourceBranchName'], 'master') }}:
environments: ['devint']
<?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>
<groupId>org.opengroup.osdu.legal</groupId>
<artifactId>legal-core</artifactId>
<version>0.0.5-SNAPSHOT</version>
<packaging>jar</packaging>
<name>legal-core</name>
<description>Legal service core</description>
<properties>
<springfox-version>2.7.0</springfox-version>
</properties>
<dependencies>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-common</artifactId>
<version>0.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.api.grpc/proto-google-common-protos -->
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<version>1.16.0</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>
<!-- 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>
<!-- swagger dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<threadCount>1</threadCount>
</configuration>
</plugin>
</plugins>
</build>
</project>
package org.opengroup.osdu.legal;
import org.opengroup.osdu.legal.tags.validation.rules.DefaultRule;
import org.opengroup.osdu.legal.tags.validation.rules.ExpiredContractRule;
import org.opengroup.osdu.core.common.model.legal.validation.rules.Rule;
import org.opengroup.osdu.legal.tags.validation.rules.UnownedDataRule;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.stereotype.Component;
import javax.inject.Provider;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@Component
public class RulesetProvider implements Provider<List<Rule>> {
@Inject
private ExpiredContractRule expiredContractRule;
@Inject
private UnownedDataRule unownedDataRule;
@Inject
private DefaultRule defaultRule;
@Inject
private RequestInfo requestInfo;
@Override
public List<Rule> get() {
List<Rule> ruleset = new ArrayList<>();
TenantInfo tenantInfo = requestInfo.getTenantInfo();
if (tenantInfo==null)
throw new RuntimeException("No tenant info");
switch(requestInfo.getTenantInfo().getComplianceRuleSet()) {
case TenantInfo.ComplianceRuleSets.SHARED:
ruleset.add(expiredContractRule);
ruleset.add(unownedDataRule);
ruleset.add(defaultRule);
return ruleset;
default:
throw new AppException(500, "Server error", "Unexpected ruleset requested " + requestInfo.getTenantInfo().getComplianceRuleSet());
}
}
}
package org.opengroup.osdu.legal;
public final class SwaggerDoc {
/*
* Shared
*/
public static final String NAME_EXAMPLE = "Private-USA-EHC";
/*
* LegalTag
*/
public static final String LEGALTAGS_TAGS = "LegalTag";
public static final String LEGALTAGS_DESC = "";
// create
public static final String LEGALTAGS_CREATE_TITLE = "Create a LegalTag";
public static final String LEGALTAGS_CREATE_NOTES = "This allows for the creation of your LegalTag. A LegalTag is uniquely identified by its name. A LegalTag must be created before you can start ingesting data and the correct LegaltTag should be assigned to that data. Allowed roles: service.legal.user, service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_CREATE_RESPONSE_OK = "Created LegalTag successfully.";
public static final String LEGALTAGS_CREATE_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
public static final String LEGALTAGS_CREATE_RESPONSE_CONFLICT = "A LegalTag with the given name already exists.";
// delete
public static final String LEGALTAGS_DELETE_TITLE = "Delete a LegalTag";
public static final String LEGALTAGS_DELETE_NOTES = "This allows for the deletion of your LegalTag with the given name. This makes the given legaltags data invalid. Allowed roles: service.legal.admin";
public static final String LEGALTAGS_DELETE_RESPONSE_OK = "Deleted the LegalTag successfully.";
public static final String LEGALTAGS_DELETE_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
// get
public static final String LEGALTAGS_GET_TITLE = "Gets a Legaltag";
public static final String LEGALTAGS_GET_NOTES = "This allows for the retrieval of your LegalTag using the 'name' associated with it. Allowed roles: service.legal.user, service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_GET_RESPONSE_OK = "Retrieved LegalTag successfully.";
public static final String LEGALTAGS_GET_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
public static final String LEGALTAGS_GET_RESPONSE_NOTFOUND = "Requested LegalTag was not found.";
// list
public static final String LEGALTAGS_LIST_TITLE = "List the Legaltags";
public static final String LEGALTAGS_LIST_NOTES = "This allows for the retrieval of all LegalTags. You can specify parameters to choose what subset of LegalTags you want to list. Allowed roles: service.legal.user, service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_LIST_RESPONSE_OK = "Retrieved LegalTags successfully.";
public static final String LEGALTAGS_LIST_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
public static final String LEGALTAGS_LIST_PARAMETER_VALID = "If true returns only valid LegalTags, if false returns only invalid LegalTags. Default value is true.";
// retrieve batch
public static final String LEGALTAGS_POST_BATCH_TITLE = "Get the Legaltags";
public static final String LEGALTAGS_POST_BATCH_NOTES = "This allows for the retrieval of your LegalTags using the 'name' associated with it. A maximum of 25 can be retrieved at once. Allowed roles: service.legal.user, service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_POST_BATCH_RESPONSE_OK = "Retrieved LegalTags successfully.";
public static final String LEGALTAGS_POST_BATCH_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
public static final String LEGALTAGS_POST_BATCH_RESPONSE_NOTFOUND = "One or more requested LegalTags were not found.";
// post validate
public static final String LEGALTAGS_POST_VALIDATE_TITLE = "Validate the Legaltags";
public static final String LEGALTAGS_POST_VALIDATE_NOTES = "This allows you to send the names of the LegalTags you want to validate. It will return any which are invalid and the reason they are. A maximum of 25 can be retrieved at once. Allowed roles: service.legal.user, service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_POST_VALIDATE_RESPONSE_OK = "Retrieved LegalTag names with reason successfully.";
public static final String LEGALTAGS_POST_VALIDATE_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
// update
public static final String LEGALTAGS_UPDATE_TITLE = "Update a Legaltag";
public static final String LEGALTAGS_UPDATE_NOTES = "This allows to update certain properties of your LegalTag using the 'name' associated with it. Allowed roles: service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_UPDATE_RESPONSE_OK = "Updated LegalTag successfully.";
public static final String LEGALTAGS_UPDATE_RESPONSE_BADREQUEST = "Invalid parameters were given on request.";
public static final String LEGALTAGS_UPDATE_RESPONSE_NOTFOUND = "Requested LegalTag to update was not found.";
// properties
public static final String LEGALTAGS_PROPERTIES_TITLE = "Get the allowed Legaltag values";
public static final String LEGALTAGS_PROPERTIES_NOTES = "This allows for the retrieval of the allowed values for the given tenant for LegalTag properties when creating a LegalTag. Allowed roles: service.legal.user, service.legal.editor, service.legal.admin";
public static final String LEGALTAGS_PROPERTIES_RESPONSE_OK = "Retrieved proeprties successfully.";
public static final String LEGALTAGS_API_UNAUTHORIZED = "You do not have permissions to access this API.";
/**
* DR
*/
public static final String DR_TAGS = "Disaster recovery";
// Backup tenant-specific
public static final String DR_BACKUP_TITLE = "Backup tenant-specific legal tags";
public static final String DR_BACKUP_NOTES = "Trigger the backup process for the tenant-related legal tags";
public static final String DR_BACKUP_SUCCESS = "The backup process was triggered successfully";
// Backup all tenants
public static final String DR_FULL_BACKUP_TITLE = "Backup all tenants legal tags";
public static final String DR_FULL_BACKUP_NOTES = "Trigger the backup process for all tenants legal tags";
public static final String DR_FULL_BACKUP_SUCCESS = "The backup process was triggered successfully";
// Restore
public static final String DR_RESTORE_TITLE = "Restore legal tags";
public static final String DR_RESTORE_NOTES = "Restore legal tags based on a specific backup date";
public static final String DR_RESTORE_SUCCESS = "The restore process was triggered successfully";
}
package org.opengroup.osdu.legal.api;
import javax.annotation.security.PermitAll;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/_ah")
public class HealthCheckApi {
@PermitAll
@GetMapping("/liveness_check")
public ResponseEntity<String> livenessCheck() {
return new ResponseEntity<String>("Legal service is alive", HttpStatus.OK);
}
@PermitAll
@GetMapping("/readiness_check")
public ResponseEntity<String> readinessCheck() {
return new ResponseEntity<String>("Legal service is ready", HttpStatus.OK);
}
}
package org.opengroup.osdu.legal.api;
import org.opengroup.osdu.legal.countries.LegalTagCountriesService;
import org.opengroup.osdu.legal.logging.AuditLogger;
import org.opengroup.osdu.legal.tags.LegalTagService;
import org.opengroup.osdu.legal.tags.dto.*;
import org.opengroup.osdu.core.common.model.legal.AllowedLegaltagPropertyValues;
import org.opengroup.osdu.core.common.model.legal.validation.ValidName;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.core.common.model.legal.ServiceConfig;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.inject.Inject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path= "/")
@Validated
public class LegalTagApi {
private RequestInfo requestInfo;
@Inject //injectMock only works on setter DI
public void setRequestInfo(RequestInfo requestInfo)
{
this.requestInfo = requestInfo;
}
private LegalTagService legalTagService;
@Inject //injectMock only works on setter DI
public void setLegalTagService(LegalTagService legalTagService)
{
this.legalTagService = legalTagService;
}
@Inject
private AllowedLegaltagPropertyValues allowedLegaltagPropertyValues;
@Inject
private AuditLogger auditLogger;
@Inject
private LegalTagCountriesService legalTagCountriesService;
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@PostMapping("/legaltags")
public ResponseEntity<LegalTagDto> createLegalTag(@NotNull @RequestBody LegalTagDto legalTag) {
LegalTagDto output = legalTagService.create(legalTag, requestInfo.getTenantInfo().getName());
return new ResponseEntity<LegalTagDto>(output, HttpStatus.CREATED);
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_ADMIN + "')")
@DeleteMapping("/legaltags/{name}")
public ResponseEntity<HttpStatus> deleteLegalTag(@PathVariable("name") @ValidName String name) {
if (legalTagService.delete(requestInfo.getTenantInfo().getProjectId(), name, requestInfo.getHeaders(), requestInfo.getTenantInfo().getName()))
return new ResponseEntity<HttpStatus>(HttpStatus.NO_CONTENT);
else
return new ResponseEntity<HttpStatus>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_USER + "', '" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@GetMapping("/legaltags/{name}")
public ResponseEntity<LegalTagDto> getLegalTag(@PathVariable("name") @ValidName String name) {
LegalTagDto output = legalTagService.get(name, requestInfo.getTenantInfo().getName());
if (output == null)
return new ResponseEntity<LegalTagDto>(output, HttpStatus.NOT_FOUND);
else {
return new ResponseEntity<LegalTagDto>(output, HttpStatus.OK);
}
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_USER + "', '" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@GetMapping("/legaltags")
public ResponseEntity<LegalTagDtos> listLegalTags(@RequestParam(name = "valid", required = false, defaultValue = "true") boolean valid) {
LegalTagDtos output = legalTagService.list(valid, requestInfo.getTenantInfo().getName());
return new ResponseEntity<LegalTagDtos>(output, HttpStatus.OK);
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@PutMapping("/legaltags")
public ResponseEntity<LegalTagDto> updateLegalTag(@Valid @NotNull @RequestBody UpdateLegalTag legalTag) {
LegalTagDto output = legalTagService.update(legalTag, requestInfo.getTenantInfo().getName());
return new ResponseEntity<LegalTagDto>(output, HttpStatus.OK);
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_USER + "', '" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@PostMapping("/legaltags:batchRetrieve")
public ResponseEntity<LegalTagDtos> getLegalTags(@Valid @NotNull @RequestBody RequestLegalTags requestedTags) {
String[] names = requestedTags.getNames().stream().toArray(String[]::new);
LegalTagDtos result = legalTagService.getBatch(names, requestInfo.getTenantInfo().getName());
if (result == null || result.getLegalTags().size() != requestedTags.getNames().size()) {
return new ResponseEntity<LegalTagDtos>(result, HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<LegalTagDtos>(result, HttpStatus.OK);
}
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_USER + "', '" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@PostMapping("/legaltags:validate")
public ResponseEntity<InvalidTagsWithReason> validateLegalTags(@Valid @NotNull @RequestBody RequestLegalTags requestedTags) {
InvalidTagsWithReason result = legalTagService.validate(requestedTags.getNames().toArray(new String[0]),
requestInfo.getTenantInfo().getName());
return new ResponseEntity<InvalidTagsWithReason>(result, HttpStatus.OK);
}
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_USER + "', '" + ServiceConfig.LEGAL_EDITOR + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@GetMapping("/legaltags:properties")
public ResponseEntity<ReadablePropertyValues> getLegalTagProperties() {
ReadablePropertyValues output = new ReadablePropertyValues();
output.setCountriesOfOrigin(legalTagCountriesService.getValidCOOs());
output.setOtherRelevantDataCountries(legalTagCountriesService.getValidORDCs());
output.setExportClassificationControlNumbers(allowedLegaltagPropertyValues.getEccns());
output.setPersonalDataTypes(allowedLegaltagPropertyValues.getPersonalDataType());
output.setSecurityClassifications(allowedLegaltagPropertyValues.getSecurityClassifications());
output.setDataTypes(allowedLegaltagPropertyValues.getDataTypes());
auditLogger.readLegalPropertiesSuccess();
return new ResponseEntity<ReadablePropertyValues>(output, HttpStatus.OK);
}
}
package org.opengroup.osdu.legal.api;
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.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.legal.jobs.LegalTagStatusJob;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
import org.opengroup.osdu.legal.logging.AuditLogger;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.core.common.model.legal.ServiceConfig;
import java.util.Collection;
import javax.inject.Inject;
import static java.util.Collections.singletonList;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/jobs")
public class LegalTagStatusJobApi {
@Inject
private RequestInfo requestInfo;
@Inject
private LegalTagStatusJob legalTagStatusJob;
@Inject
private ITenantFactory tenantStorageFactory;
@Inject
private AuditLogger auditLogger;
@Inject
private JaxRsDpsLog log;
@PreAuthorize("@authorizationFilter.hasPermission('" + ServiceConfig.LEGAL_CRON + "', '" + ServiceConfig.LEGAL_ADMIN + "')")
@GetMapping("/updateLegalTagStatus")
public ResponseEntity<HttpStatus> checkLegalTagStatusChanges() {
tenantStorageFactory.flushCache();
DpsHeaders convertedHeaders = requestInfo.getHeaders();
Collection<TenantInfo> tenantsInfo = tenantStorageFactory.listTenantInfo();
boolean allPassed = true;
for (TenantInfo tenantInfo : tenantsInfo) {
convertedHeaders.put(DpsHeaders.ACCOUNT_ID, tenantInfo.getName());
boolean result = runJob(convertedHeaders, legalTagStatusJob);
if (allPassed)
allPassed = result;
}
HttpStatus status = allPassed ? HttpStatus.NO_CONTENT : HttpStatus.INTERNAL_SERVER_ERROR;
return new ResponseEntity<HttpStatus>(status);
}
private boolean runJob(DpsHeaders convertedHeaders, LegalTagStatusJob legalTagStatusJob) {
boolean success = true;
try {
String projectId = requestInfo.getTenantInfo().getProjectId();
StatusChangedTags result = legalTagStatusJob.run(projectId, convertedHeaders, requestInfo.getTenantInfo().getName());
auditLogger.legalTagJobRanSuccess(singletonList(result.toString()));
} catch (Exception e) {
success = false;
log.error( "Error running check LegalTag compliance job on tenant " + convertedHeaders.getPartitionIdWithFallbackToAccountId(), e);
}
return success;
}
}
package org.opengroup.osdu.legal.countries;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import org.opengroup.osdu.core.common.model.http.AppException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public abstract class CountriesReader {
public List<Country> convertJsonIntoCountries(String jsonString) {
List<Country> countries = new ArrayList<>();
if (jsonString.isEmpty()) return countries;
try {
Gson gson = new Gson();
JsonReader reader = generateJsonReader(jsonString);
reader.beginArray();
while (reader.hasNext()) {
Country data = gson.fromJson(reader, Country.class);
countries.add(data);
}
reader.endArray();
reader.close();
} catch (Exception e) {
throw AppException.countryCodeLoadingError();
}
return countries;
}
abstract JsonReader generateJsonReader(String inputString) throws IOException;
}
package org.opengroup.osdu.legal.countries;
import com.google.gson.stream.JsonReader;
import java.io.StringReader;
public class CountriesReaderImpl extends CountriesReader {
public JsonReader generateJsonReader(String inputString) {
return new JsonReader(new StringReader(inputString));
}
}
package org.opengroup.osdu.legal.countries;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
public class Country {
private final String name;
private final String alpha2;
private final int numeric;
private final String residencyRisk;
private final List<String> typesNotApplyDataResidency;
public List<String> getTypesNotApplyDataResidency() {
return this.typesNotApplyDataResidency == null ? new ArrayList<>() : this.typesNotApplyDataResidency;
}
public static class RESIDENCY_RISK {
public static final String NO_RESTRICTION = "No restriction";
public static final String NOT_ASSIGNED = "Not assigned";
public static final String EMBARGOED = "Embargoed";
public static final String DEFAULT = "Default";
public static final String CLIENT_CONSENT_REQUIRED = "Client consent required";
}
public boolean isMatchByAlpha2(Country other) {
return this.getAlpha2().equalsIgnoreCase(other.getAlpha2());
}
}
package org.opengroup.osdu.legal.countries;
import com.google.gson.stream.JsonReader;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class DefaultCountriesReader extends CountriesReader {
public JsonReader generateJsonReader(String inputString) throws IOException {
ClassPathResource cpr = new ClassPathResource(inputString);
InputStream inputStream = cpr.getInputStream();
Reader reader = new InputStreamReader(inputStream);
return new JsonReader(reader);
}
}
package org.opengroup.osdu.legal.countries;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class DefaultCountriesRepository implements LegalTagCountriesRepository {
private static final String DEFAULT_COUNTRY_CODE_PATH = "DefaultCountryCode.json";
@Override
public List<Country> read() {
CountriesReader countriesReader = new DefaultCountriesReader();
return countriesReader.convertJsonIntoCountries(DEFAULT_COUNTRY_CODE_PATH);
}
}
package org.opengroup.osdu.legal.countries;
import java.util.List;
public interface LegalTagCountriesRepository {
List<Country> read();
}
package org.opengroup.osdu.legal.countries;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
public class LegalTagCountriesRepositoryImpl implements LegalTagCountriesRepository {
private IStorageReader storageReader;
public LegalTagCountriesRepositoryImpl(IStorageReader storageReader) {
this.storageReader = storageReader;
}
@Override
public List<Country> read() {
byte[] content = storageReader.readAllBytes();
String jsonString = new String(content, UTF_8);
CountriesReader countriesReader = new CountriesReaderImpl();
return countriesReader.convertJsonIntoCountries(jsonString);
}
}
package org.opengroup.osdu.legal.countries;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.springframework.stereotype.Service;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.core.common.model.legal.ServiceConfig;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
@Service
public class LegalTagCountriesService {
@Inject
private LegalTagCountriesTenantRepositories repositories;
@Inject
private RequestInfo requestInfo;
@Inject
private ServiceConfig serviceConfig;
@Inject
private DefaultCountriesRepository defaultCountriesRepository;
public Map<String, String> getValidCOOs() {
Map<String,String> coos = new HashMap<>();
TenantInfo tenant = requestInfo.getTenantInfo();
LegalTagCountriesRepository legalTagCountriesRepository = repositories.get(tenant, serviceConfig.getRegion());
generateCOOsFromRepository(coos, new DefaultCountriesRepository(), legalTagCountriesRepository, "none");
return coos;
}
public Map<String, String> getValidCOOs(String dataType) {
Map<String,String> coos = new HashMap<>();
TenantInfo tenant = requestInfo.getTenantInfo();
LegalTagCountriesRepository legalTagCountriesRepository = repositories.get(tenant, serviceConfig.getRegion());
generateCOOsFromRepository(coos, defaultCountriesRepository, legalTagCountriesRepository, dataType);
return coos;
}
public Map<String, String> getValidORDCs() {
Map<String,String> ordcs = new HashMap<>();
TenantInfo tenant = requestInfo.getTenantInfo();
LegalTagCountriesRepository legalTagCountriesRepository = repositories.get(tenant, serviceConfig.getRegion());
generateORDCsFromRepository(ordcs, defaultCountriesRepository);
generateORDCsFromRepository(ordcs, legalTagCountriesRepository);
return ordcs;
}
private void generateCOOsFromRepository(Map<String, String> coos, LegalTagCountriesRepository defaultCountriesRepository, LegalTagCountriesRepository cloudCountriesRepository, String dataType) {
List<Country> cloudStorageCountries = this.mergeCountriesRepositories(defaultCountriesRepository, cloudCountriesRepository);
for (Country country : cloudStorageCountries) {
if (country.getResidencyRisk() != null &&
(country.getResidencyRisk().equals(Country.RESIDENCY_RISK.NO_RESTRICTION) ||
country.getResidencyRisk().equals(Country.RESIDENCY_RISK.NOT_ASSIGNED) ||
country.getResidencyRisk().equals(Country.RESIDENCY_RISK.CLIENT_CONSENT_REQUIRED))) {
coos.put(country.getAlpha2(), country.getName());
} else if (country.getTypesNotApplyDataResidency().contains(dataType)) {
coos.put(country.getAlpha2(), country.getName());
}
}
}
private List<Country> mergeCountriesRepositories(LegalTagCountriesRepository defaultRepository, LegalTagCountriesRepository cloudRepository) {
List<Country> sourceCountries = defaultRepository.read();
List<Country> tenantCountries = cloudRepository.read();
for (Country tenantCountry : tenantCountries) {
for (int i = 0; i < sourceCountries.size(); i++) {
if (sourceCountries.get(i).isMatchByAlpha2(tenantCountry)) {
sourceCountries.set(i, tenantCountry);
}
}
}
return sourceCountries;
}
private void generateORDCsFromRepository(Map<String, String> coos, LegalTagCountriesRepository legalTagCountriesRepository) {
List<Country> cloudStorageCountries = legalTagCountriesRepository.read();
for (Country country : cloudStorageCountries) {
if (country.getResidencyRisk() != null && !country.getResidencyRisk().equals(Country.RESIDENCY_RISK.EMBARGOED)) {
coos.put(country.getAlpha2(), country.getName());
} else {
coos.remove(country.getAlpha2());
}
}
}
}
\ No newline at end of file
package org.opengroup.osdu.legal.countries;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReaderFactory;
import org.springframework.stereotype.Repository;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
@Repository
@Log
public class LegalTagCountriesTenantRepositories {
@Inject
private IStorageReaderFactory storageReaderFactory;
private final Map<String, LegalTagCountriesRepository> countriesTenantRepositories = new HashMap<>();
LegalTagCountriesRepository get(TenantInfo tenant, String projectRegion){
String tenantName = tenant.getName();
if(StringUtils.isBlank(tenantName))
throw invalidTenantGivenException(tenantName);
if(!countriesTenantRepositories.containsKey(tenantName)){
addRepository(tenant, projectRegion);
}
return countriesTenantRepositories.get(tenantName);
}
private void addRepository(TenantInfo tenant, String projectRegion) {
IStorageReader storageReader = storageReaderFactory.getReader(tenant, projectRegion);
LegalTagCountriesRepository repo = new LegalTagCountriesRepositoryImpl(storageReader);
countriesTenantRepositories.put(tenant.getName(), repo);
}
private AppException invalidTenantGivenException(String tenantName){
log.warning(String.format("Requested tenantname does not exist in list of tenants %s", tenantName));
return new AppException(403, "Forbidden", String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
}
}
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