Skip to content
Snippets Groups Projects
Commit 54785a7d authored by Anuj Gupta's avatar Anuj Gupta Committed by Alan Braz
Browse files

Ibm Impl for register

parent 81b7af6b
No related branches found
No related tags found
No related merge requests found
Showing
with 1202 additions and 7 deletions
......@@ -106,10 +106,8 @@ The following software have components provided under the terms of this license:
- Lucene Spatial Extras (from )
- Lucene Suggest (from )
- MapStruct Core (from )
- Mockito (from http://mockito.org)
- Mockito (from http://www.mockito.org)
- MongoDB Java Driver (from http://www.mongodb.org)
- MongoDB Java Driver (from http://www.mongodb.org)
- Mockito (from http://mockito.org)
- Netty/Buffer (from http://netty.io/)
- Netty/Codec (from )
- Netty/Common (from )
......@@ -289,8 +287,6 @@ CC-BY-2.5
The following software have components provided under the terms of this license:
- Checker Qual (from https://checkerframework.org)
- MongoDB Java Driver (from http://www.mongodb.org)
- MongoDB Java Driver (from http://www.mongodb.org)
========================================================================
CC-BY-3.0
......@@ -507,9 +503,8 @@ The following software have components provided under the terms of this license:
- Java JWT (from http://www.jwt.io)
- Java JWT (from http://www.jwt.io)
- Lucene Core (from )
- Mockito (from http://mockito.org)
- Mockito (from http://www.mockito.org)
- MongoDB Java Driver (from http://www.mongodb.org)
- Mockito (from http://mockito.org)
- Netty/Common (from )
- Project Lombok (from https://projectlombok.org)
- SLF4J API Module (from http://www.slf4j.org)
......
......@@ -90,6 +90,7 @@
<modules>
<module>register-core</module>
<module>provider/register-gcp</module>
<module>provider/register-ibm</module>
<module>provider/register-azure</module>
</modules>
......
# os-register-ibm
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2020 IBM Corp. All Rights Reserved.
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.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.opengroup.osdu</groupId>
<artifactId>register-ibm</artifactId>
<version>1.0.0</version>
<description>Register service</description>
<packaging>jar</packaging>
<parent>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-register</artifactId>
<version>1.0.0</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-lib-ibm</artifactId>
<version>0.3.6-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-common</artifactId>
</dependency>
<dependency>
<groupId>org.opengroup.osdu</groupId>
<artifactId>register-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>${gitlab-server}</id>
<url>https://community.opengroup.org/api/v4/groups/17/-/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>${gitlab-server}</id>
<url>https://community.opengroup.org/api/v4/projects/157/packages/maven</url>
</repository>
<snapshotRepository>
<id>${gitlab-server}</id>
<url>https://community.opengroup.org/api/v4/projects/157/packages/maven</url>
</snapshotRepository>
</distributionManagement>
<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.register.provider.ibm.RegisterApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import javax.annotation.PostConstruct;
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
@ComponentScan({"org.opengroup.osdu"})
public class RegisterApplication {
@PostConstruct
void f() {
}
public static void main(String[] args) {
SpringApplication.run(RegisterApplication.class, args);
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.action.datastore;
import org.opengroup.osdu.register.action.model.Action;
public class ActionDoc extends Action{
private String _id;
private String _rev;
public ActionDoc(Action action) {
this._id=action.getId();
super.setName(action.getName());
super.setDescription(action.getDescription());
super.setUrl(action.getUrl());
super.setImg(action.getImg());
super.setContactEmail(action.getContactEmail());
super.setFilter(action.getFilter());
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String get_rev() {
return _rev;
}
public void set_rev(String _rev) {
this._rev = _rev;
}
public Action getAction() {
Action action=new Action();
action.setId(this.get_id());
action.setName(this.getName());
action.setDescription(this.getDescription());
action.setUrl(this.getUrl());
action.setImg(this.getImg());
action.setContactEmail(this.getContactEmail());
action.setFilter(this.getFilter());
return action;
}
@Override
public String toString() {
return "ActionDoc [_id=" + _id + ", _rev=" + _rev +", "+ getAction()+ "]";
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.action.datastore;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.util.Crc32c;
import org.opengroup.osdu.register.action.model.Action;
import org.opengroup.osdu.register.provider.ibm.ddms.datastore.DatastoreMultiTenantAccess;
import org.opengroup.osdu.register.provider.interfaces.action.IActionRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.cloudant.client.api.Database;
import com.cloudant.client.org.lightcouch.DocumentConflictException;
import com.google.gson.Gson;
import lombok.Getter;
@Repository
public class ActionRepository implements IActionRepo {
private static final String ACTION_DB_NAME = "action";
@Autowired
private JaxRsDpsLog log;
@Autowired
private DatastoreMultiTenantAccess dataStoreTenants;
@Autowired
private TenantInfo tenantInfo;
@Override
public Action createAction(Action a) {
return createActionInDatastore(a);
}
@Override
public Action get(String id) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
if(!(db.contains(id))) {
log.error(String.format("Action with id %s does not exist.", id));
throw new AppException(404, "Not found", String.format("Action with id %s does not exist.", id));
}
ActionDoc actionDoc = db.find(ActionDoc.class, id);
return actionDoc.getAction();
}
@Override
public boolean delete(String id) {
return deleteFromDatastore(id);
}
private Action createActionInDatastore(Action action) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
ActionDoc actionDoc = new ActionDoc(action);
try {
db.save(actionDoc);
} catch (DocumentConflictException e) {
throwConflict();
}
return action;
}
private Action throwConflict() {
throw new AppException(409, "Conflict", "An action already exists with the same message and endpoint combination");
}
private boolean deleteFromDatastore(String id) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
boolean output = false;
if(db.contains(id)) {
ActionDoc actionDoc = db.find(ActionDoc.class, id);
db.remove(actionDoc);
output = true;
}
else {
log.error(String.format("deletion failed. Could not find action id : %s", id));
output=false;
}
return output;
}
@Override
public List<Action> getAllActions() {
Database db = dataStoreTenants.get(getDbNameWithTenant());
List<Action> actionsList=new ArrayList<>();
try {
List<ActionDoc> actionDocsList = db.getAllDocsRequestBuilder().includeDocs(true).build().getResponse().getDocsAs(ActionDoc.class);
actionsList = actionDocsList.stream().map(i->i.getAction()).collect(Collectors.toList());
} catch (IOException e) {
log.error("IOException while fetching all actons",e);
e.printStackTrace();
}
return actionsList;
}
/**
* @return
*/
private String getDbNameWithTenant() {
return tenantInfo.getName()+"-"+ACTION_DB_NAME;
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.ddms.datastore;
import static com.cloudant.client.api.query.Expression.eq;
import static com.cloudant.client.api.query.Operation.and;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.register.ddms.model.Ddms;
import org.opengroup.osdu.register.provider.interfaces.ddms.IDdmsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.query.PredicateExpression;
import com.cloudant.client.api.query.PredicatedOperation.*;
import com.cloudant.client.api.query.QueryBuilder;
import com.cloudant.client.api.query.QueryResult;
import com.cloudant.client.org.lightcouch.DocumentConflictException;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Repository
public class DatastoreDdmsRepository implements IDdmsRepository {
private static final String DDMS_DATABASE = "DDMS";
@Autowired
private DatastoreMultiTenantAccess dataStoreTenants;
@Autowired
private TenantInfo tenantInfo;
@Override
public Ddms create(Ddms ddms) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
DdmsDoc ddmsDoc = new DdmsDoc(ddms);
try {
/*
* if(db.contains(ddms.getId())) { log.warn("DDMS already registered"); //throw
* new DocumentConflictException("Document already available"); }
*/
db.save(ddmsDoc);
} catch (DocumentConflictException e) {
log.error("DDMS already registered. A DDMS already exists with the same id : " + ddmsDoc.get_id());
throw new AppException(409, "Conflict", "DDMS already registered");
} catch (Exception e) {
log.error("Error while creating document {} in database {}", ddms.getId(), db.info().getDbName());
e.printStackTrace();
}
return ddms;
}
@Override
public Ddms get(String id) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
if (db.contains(id)) {
DdmsDoc ddmsDoc = db.find(DdmsDoc.class, id);
return ddmsDoc.getDdms();
} else {
log.error("DDMS with id {} does not exist", id);
throw new AppException(404, "Not found", String.format("DDMS with id %s does not exist.", id));
}
}
@Override
public List<Ddms> query(String type) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
String selectorString = "{\"selector\":{\"interfaces\":{\"$elemMatch\":{\"entityType\":\"%s\"}}}}";
String selector = String.format(selectorString, type);
QueryResult<DdmsDoc> results = db.query(selector, DdmsDoc.class);
if (results.getDocs().isEmpty()) {
throw new AppException(404, "Not found", String.format("DDMS with entity type %s does not exist.", type));
}
List<DdmsDoc> ddmsDocList = results.getDocs();
List<Ddms> ddmsList = ddmsDocList.stream().map(i -> i.getDdms()).collect(Collectors.toList());
return ddmsList;
}
@Override
public boolean delete(String id) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
boolean output = false;
try {
if (db.contains(id)) {
DdmsDoc ddmsDoc = db.find(DdmsDoc.class, id);
db.remove(ddmsDoc);
return true;
} else {
log.error("could not found DDMS id : " + id);
return false;
}
} catch (Exception e) {
log.error("failed to delete DDMS : " + id);
e.printStackTrace();
}
return output;
}
/**
* @return
*/
private String getDbNameWithTenant() {
return tenantInfo.getName() + "-" + DDMS_DATABASE;
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.ddms.datastore;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
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.core.ibm.auth.ServiceCredentials;
import org.opengroup.osdu.core.ibm.cloudant.IBMCloudantClientFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.cloudant.client.api.Database;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class DatastoreMultiTenantAccess {
@Value("${ibm.db.url}")
private String dbUrl;
@Value("${ibm.env.prefix}")
private String dBEnvPrefix;
@Value("${ibm.db.user:#{null}}")
private String dbUser;
@Value("${ibm.db.password:#{null}}")
private String dbPassword;
//private final IDatastoreFactory factory;
private IBMCloudantClientFactory cloudantFactory;
private final Map<String, Database> tenantRepositories = new HashMap<>();
/* public DatastoreMultiTenantAccess() {
this(new IBMCloudantClientFactory(new ServiceCredentials(dbUrl, dbUser, dbPassword)));
}
DatastoreMultiTenantAccess(IBMCloudantClientFactory factory) {
this.cloudantFactory = factory;
}*/
@PostConstruct
public void init() {
cloudantFactory = new IBMCloudantClientFactory(new ServiceCredentials(dbUrl, dbUser, dbPassword));
log.info("IBM Cloudant factory created ");
}
public Database 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) {
try {
Database db = cloudantFactory.getDatabase(dBEnvPrefix, tenantName);
if (db == null)
throw invalidTenantGivenException(tenantName);
tenantRepositories.put(tenantName, db);
log.info("Database created and added to tenant repository : "+db.info().getDbName());
} catch (MalformedURLException e) {
log.error("MalformedURLException while creating Database ");
e.printStackTrace();
}
}
private AppException invalidTenantGivenException(String tenantName) {
return new AppException(403, "Forbidden", String.format("You do not have access to the %s value given %s",
DpsHeaders.DATA_PARTITION_ID, tenantName));
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.ddms.datastore;
import org.opengroup.osdu.register.ddms.model.Ddms;
public class DdmsDoc extends Ddms {
private String _id;
private String _rev;
public DdmsDoc(Ddms ddms) {
super();
this.set_id(ddms.getId());
super.setName(ddms.getName());
super.setDescription(ddms.getDescription());
super.setContactEmail(ddms.getContactEmail());
super.setInterfaces(ddms.getInterfaces());
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String get_rev() {
return _rev;
}
public void set_rev(String _rev) {
this._rev = _rev;
}
public Ddms getDdms() {
Ddms ddms = new Ddms();
ddms.setId(this.get_id());
ddms.setName(this.getName());
ddms.setDescription(this.getDescription());
ddms.setContactEmail(this.getContactEmail());
ddms.getCreatedDateTimeEpoch();
ddms.setInterfaces(this.getInterfaces());
return ddms;
}
@Override
public String toString() {
return "DdmsDoc [_id=" + _id + ", _rev=" + _rev + ", getDdms()=" + getDdms() + "]";
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.security;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class IBMSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.httpBasic().disable()
.csrf().disable(); //disable default authN. AuthN handled by endpoints proxy
}
}
\ No newline at end of file
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.subscriber;
import static com.cloudant.client.api.query.Expression.eq;
import static java.lang.String.format;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.register.provider.ibm.ddms.datastore.DatastoreMultiTenantAccess;
import org.opengroup.osdu.register.subscriber.model.GsaSecret;
import org.opengroup.osdu.register.subscriber.model.GsaSecretValue;
import org.opengroup.osdu.register.subscriber.model.HmacSecret;
import org.opengroup.osdu.register.subscriber.model.Secret;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import org.opengroup.osdu.register.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.query.QueryBuilder;
import com.cloudant.client.api.query.QueryResult;
import com.cloudant.client.org.lightcouch.DocumentConflictException;
import com.cloudant.client.org.lightcouch.NoDocumentException;
import com.google.cloud.Timestamp;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Repository
public class DatastoreAccess implements IDatastoreAccess {
private static final String SUBSCRIBER_DB_NAME = "subscription";
@Autowired
private PushSubscription pushSubscription;
@Autowired
private DatastoreMultiTenantAccess dataStoreTenants;
@Autowired
private TenantInfo tenantInfo;
@Override
public Subscription create(Subscription s) {
s = createSubscriberInDatastore(s);
try {
// pushSubscription.create(s, tenantInfo, this.config.getGoogleCloudProject(),
// config.getServiceAccountIdentity());
} catch (Exception e) {
deleteFromDatastore(s.getId(), false);
throw new AppException(500, "Server Error", "Unexpected error creating subscription", e);
}
return s;
}
@Override
public Subscription get(String id) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
Subscription subscription = null;
try {
SubscriptionDoc subscriptionDoc = db.find(SubscriptionDoc.class, id);
subscription = convertToSubscription(subscriptionDoc);
} catch (NoDocumentException e) {
throw new AppException(e.getStatusCode(), e.getReason(),
String.format("Subscriber with id %s does not exist.", id));
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return subscription;
}
// TODO: Remove this api when switch notification to use cache
@Override
public List<Subscription> query(String notificationId) {
List<Subscription> output = new ArrayList<>();
Database db = dataStoreTenants.get(getDbNameWithTenant());
QueryResult<SubscriptionDoc> results = null;
try {
results = db.query(new QueryBuilder(eq("notificationId", notificationId)).build(), SubscriptionDoc.class);
if (results == null || results.getDocs().isEmpty()) {
throw new AppException(404, "Not Found",
format("Sunscriptio not forund for notification id %s ", notificationId));
}
} catch (Exception e) {
log.info("Unexpected error querying subscription");
e.printStackTrace();
}
output = results.getDocs().stream().map(i -> convertToSubscription(i)).collect(Collectors.toList());
return output;
}
@Override
public boolean delete(String id) {
return deleteFromDatastore(id, true);
}
@Override
public boolean patch(String id, Secret secret) {
return updateInDataStore(id, secret);
}
@Override
public List<Subscription> getAll() throws Exception {
Database db = dataStoreTenants.get(getDbNameWithTenant());
List<SubscriptionDoc> subscriptionDocList = db.getAllDocsRequestBuilder().includeDocs(true).build()
.getResponse().getDocsAs(SubscriptionDoc.class);
List<Subscription> subscriptionsList = subscriptionDocList.stream().map(i -> convertToSubscription(i))
.collect(Collectors.toList());
return subscriptionsList;
}
private Subscription createSubscriberInDatastore(Subscription s) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
SubscriptionDoc subscriptionDoc = new SubscriptionDoc(s);
try {
db.save(subscriptionDoc);
} catch (DocumentConflictException e) {
throw new AppException(409, "Conflict",
"A subscriber already exists with the same topic and endpoint combination");
} catch (Exception e) {
}
return convertToSubscription(subscriptionDoc);
}
private boolean deleteFromDatastore(String id, boolean deleteSubscription) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
boolean output = false;
if (!db.contains(id)) {
return output;
}
try {
SubscriptionDoc subscriptionDoc = db.find(SubscriptionDoc.class, id);
/*
* if(deleteSubscription) { // TODO Add flow for deleting the subscription to
* the topic //identify first parameter serviceProjectId
* //pushSubscription.delete(servicesProjectId,
* subscriptionDoc.getNotificationId()); }
*/
db.remove(subscriptionDoc);
output = true;
} catch (NoDocumentException e) {
throw new AppException(e.getStatusCode(), e.getReason(), e.getMessage());
} catch (DocumentConflictException e) {
throw new AppException(e.getStatusCode(), e.getReason(), e.getMessage());
}
return output;
}
private boolean updateInDataStore(String id, Secret secret) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
boolean output = false;
try {
SubscriptionDoc subscriptionDoc = db.find(SubscriptionDoc.class, id);
subscriptionDoc.setSecretType(secret.getSecretType());
subscriptionDoc.setSecretValue(secret.toString());
db.update(subscriptionDoc);
output = true;
} catch (NoDocumentException | DocumentConflictException e) {
throw new AppException(e.getStatusCode(), e.getReason(), e.getMessage());
} catch (Exception e) {
log.error("Unexpected Error updating subscription " + e);
}
return output;
}
/**
* @return
*/
private String getDbNameWithTenant() {
return tenantInfo.getName() + "-" + SUBSCRIBER_DB_NAME;
}
private Subscription convertToSubscription(SubscriptionDoc doc) {
String secretValue = doc.getSecretValue();
Secret secret;
if (doc.getSecretType().equals(Constants.GSASecret)) {
GsaSecret gsaSecret = new GsaSecret();
String[] splitSecret = secretValue.split("`");
gsaSecret.setValue(new GsaSecretValue(splitSecret[0], splitSecret[1]));
secret = gsaSecret;
} else {
HmacSecret hmacSecret = new HmacSecret();
hmacSecret.setValue(secretValue);
secret = hmacSecret;
}
return new Subscription(doc.get_id(), doc.getName(), doc.getDescription(), doc.getTopic(),
doc.getPushEndpoint(), doc.getCreatedBy(), Timestamp.of(doc.getCreatedOnEpoch()),
doc.getNotificationId(), secret);
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.subscriber;
import org.opengroup.osdu.register.subscriber.model.Secret;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import java.util.List;
public interface IDatastoreAccess {
Subscription create(Subscription s);
Subscription get(String id);
List<Subscription> query(String notificationId);
boolean delete(String id);
boolean patch(String id, Secret secret);
List<Subscription> getAll() throws Exception;
}
/*
* Copyright 2017-2020, 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
*
* 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.register.provider.ibm.subscriber;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.protobuf.Duration;
import com.google.pubsub.v1.ExpirationPolicy;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.PushConfig;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.register.utils.AppServiceConfig;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class PushSubscription {
private static final int ackDeadlineSeconds = 60;
private static final int messageRetentionSeconds = 432000;
private static final int subscriptionExpirationSeconds = 31540000;
@Autowired
private AppServiceConfig serviceConfig;
public Subscription create(Subscription subscription, TenantInfo tenant,
String masterGcpId, String serviceIdentityEmail)
throws IOException {
createPushSubscription(
tenant.getProjectId(),
masterGcpId,
subscription.getTopic(),
subscription.getNotificationId(),
serviceConfig.getRecordsChangePubsubEndpoint(),
serviceIdentityEmail);
return subscription;
}
public void delete(String servicesProjectId, String subscriptionId) throws IOException {
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
try {
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(servicesProjectId, subscriptionId);
subscriptionAdminClient.deleteSubscription(subscriptionName);
} catch (NotFoundException e) {
//silently catch
}
}
}
private com.google.pubsub.v1.Subscription createPushSubscription(String tenantProjectId, String servicesProjectId, String topicId,
String subscriptionId, String url, String serviceIdentityEmail) throws IOException {
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
ProjectTopicName topicName = ProjectTopicName.of(tenantProjectId, topicId);
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(servicesProjectId, subscriptionId);
PushConfig config = PushConfig.newBuilder()
.setPushEndpoint(url)
.setOidcToken(PushConfig.OidcToken.newBuilder().setServiceAccountEmail(serviceIdentityEmail).build())
.build();
return subscriptionAdminClient.createSubscription(com.google.pubsub.v1.Subscription.newBuilder()
.setName(subscriptionName.toString())
.setTopic(topicName.toString())
.setPushConfig(config)
.setExpirationPolicy(ExpirationPolicy.newBuilder().setTtl(
Duration.newBuilder().setSeconds(subscriptionExpirationSeconds).build()).build())
.setMessageRetentionDuration(Duration.newBuilder().setSeconds(messageRetentionSeconds).build())
.setAckDeadlineSeconds(ackDeadlineSeconds)
.build());
}
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.subscriber;
import java.sql.Timestamp;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SubscriptionDoc {
private String _id;
private String _rev;
private String name;
private String description;
private String topic;
private String pushEndpoint;
private String createdBy;
private Timestamp createdOnEpoch;
private String notificationId;
private String secretType;
private String secretValue;
public SubscriptionDoc(Subscription subscription) {
this._id = subscription.getId();
this.name = subscription.getName();
this.description = subscription.getDescription();
this.topic = subscription.getTopic();
this.pushEndpoint = subscription.getPushEndpoint();
this.createdBy = subscription.getCreatedBy();
this.createdOnEpoch = new Timestamp(System.currentTimeMillis());
this.notificationId = subscription.getNotificationId();
this.secretType = subscription.getSecret().getSecretType();
this.secretValue = subscription.getSecret().toString();
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.subscriber;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.subscriber.model.Secret;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ISubscriptionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class SubscriptionRepository implements ISubscriptionRepository {
@Autowired
private IDatastoreAccess datastoreAccess;
@Override
public Subscription create(Subscription input) throws Exception {
int existingSubscriptionsCount = datastoreAccess.getAll().size();
// Due to the limit of google subscription per project, we are restricting the limit per tenant to 50
// This already looks a difficult limit to reach, but need to keep a watch on this number.
if (existingSubscriptionsCount == 50)
throw new AppException(429, "Subscription quota reached",
"Number of subscription allowed have already reached to maximum");
input = datastoreAccess.create(input);
return input;
}
@Override
public Subscription get(String id) {
return datastoreAccess.get(id);
}
@Override
public List<Subscription> query(String notificationId) {
return datastoreAccess.query(notificationId);
}
@Override
public boolean delete(String id) {
return datastoreAccess.delete(id);
}
@Override
public boolean patch(Subscription subscription, Secret secret) {
return datastoreAccess.patch(subscription.getId(), secret);
}
@Override
public List<Subscription> getAll() throws Exception {
return datastoreAccess.getAll();
}
}
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* 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.register.provider.ibm.util;
import lombok.SneakyThrows;
import org.opengroup.osdu.register.utils.IGoogleServiceAccount;
import org.springframework.stereotype.Component;
@Component
public class GoogleServiceAccountImpl implements IGoogleServiceAccount {
@SneakyThrows
@Override
public String getIdToken(String keyString, String audience) {
//TODO Add implementation for generating GSA Tokens
return "token";
}
@SneakyThrows
@Override
public String getPrivateKeyId(String keyString) {
//TODO Add implementation for generating GSA Tokens
return "private-key";
}
}
\ No newline at end of file
server.servlet.contextPath=/api/register/v1
logging.level.org.springframework.web=DEBUG
server.port=8080
LOG_PREFIX=register
KEY_RING=csqp
KMS_KEY=registerService
SERVICE_IDENTITY=de-notification-service
\ No newline at end of file
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