Skip to content

Commit

Permalink
Allow list customer location (#2534)
Browse files Browse the repository at this point in the history
* add allow feature

* add classpath allow list

* format code

* fix check
  • Loading branch information
tigershi authored Aug 21, 2023
1 parent a2ac375 commit bf82ccc
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.messages.data.dao.impl;
Expand Down Expand Up @@ -158,8 +158,7 @@ public boolean accept(File dir, String name) {
* Get the content of the Allow Product List by bundle.json file name
*/
@Override
public String getAllowProductListContent() throws DataException {
String contentFilePath = bundleConfig.getBasePathWithSeparator() + ConstantsFile.L10N_BUNDLES_PATH +ConstantsFile.ALLOW_LIST_FILE;
public String getAllowProductListContent(String contentFilePath) throws DataException {
if (new File(contentFilePath).exists()) {
return new LocalJSONReader().readLocalJSONFile(contentFilePath);
}else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.messages.data.dao.pgimpl;
Expand Down Expand Up @@ -101,7 +101,7 @@ public List<String> getVersionList(String productName) throws DataException {
* get the allow list content from pg db
*/
@Override
public String getAllowProductListContent() throws DataException {
public String getAllowProductListContent(String path) throws DataException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.messages.data.conf;
Expand Down Expand Up @@ -71,6 +71,9 @@ public class S3Config {

@Value("${s3.roleArn}")
private String roleArn;

@Value("${allow.list.path.bucketName:}")
private String allowListBucketName;



Expand Down Expand Up @@ -136,4 +139,12 @@ public String getPublicKey() {
public String getRoleArn() {
return roleArn;
}

public String getAllowListBucketName() {
if (this.allowListBucketName != null && (!this.allowListBucketName.isBlank())){
return this.allowListBucketName;
}else {
return this.bucketName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ public List<String> getVersionList(String productName) throws DataException {
* Get the content of the Allow List by s3 object
*/
@Override
public String getAllowProductListContent() throws DataException {
String s3Path = S3Utils.S3_L10N_BUNDLES_PATH+ConstantsFile.ALLOW_LIST_FILE;
if (s3Client.getS3Client().doesObjectExist(config.getBucketName(), s3Path)) {
S3Object o = s3Client.getS3Client().getObject(config.getBucketName(), s3Path);
public String getAllowProductListContent(String s3Path) throws DataException {
if (s3Client.getS3Client().doesObjectExist(config.getAllowListBucketName(), s3Path)) {
S3Object o = s3Client.getS3Client().getObject(config.getAllowListBucketName(), s3Path);
if (o != null) {
try {
return S3Utils.convertS3Obj2Str(o);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.messages.data.dao.api;
Expand All @@ -24,5 +24,5 @@ public interface IProductDao {

public List<String> getVersionList(String productName) throws DataException;

public String getAllowProductListContent() throws DataException;
public String getAllowProductListContent(String path) throws DataException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.core.messages.service.product;
Expand Down Expand Up @@ -71,5 +71,6 @@ public boolean updateTranslation(ComponentMessagesDTO componentMessagesDTO)
/**
* get the allow product List
*/
public Map<String, Object> getAllowPrductList();
public Map<String, Object> getAllowProductList(String path);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -313,10 +318,17 @@ public List<String> getSupportVersionList(String productName) throws L3APIExcept
* get the allow product list
*/
@Override
public Map<String, Object> getAllowPrductList(){
String content;
public Map<String, Object> getAllowProductList(String path){


String content = null;
try {
content = productdao.getAllowProductListContent();
if (path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)){
content = getClasspathAllowList(path);
}else{
content = productdao.getAllowProductListContent(path);
}

} catch (DataException e1) {
logger.warn(e1.getMessage());
content =null;
Expand All @@ -339,4 +351,19 @@ public Map<String, Object> getAllowPrductList(){
return null;
}
}
private String getClasspathAllowList(String path){
Resource allowResource = new PathMatchingResourcePatternResolver().getResource(path);
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(allowResource.getInputStream()))) {
StringBuilder sb = new StringBuilder();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp + "\n");
}
return sb.toString();
} catch (IOException e) {
logger.error(e.getMessage(), e);
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public class WebConfiguration implements WebMvcConfigurer {
private String requestIdsStr;

@Value("${source.request.max-size}")
private Integer sourceReqBodySize;
private Integer sourceReqBodySize;

@Value("${allow.list.path:}")
private String allowListPath;

@Autowired
private CSPTokenService cspTokenService;
Expand Down Expand Up @@ -130,7 +133,7 @@ public void addInterceptors(InterceptorRegistry registry) {
*/

// Request Validation
InterceptorRegistration apival = registry.addInterceptor(new APIValidationInterceptor(productService.getAllowPrductList(), this.requestIdsStr)).addPathPatterns("/**")
InterceptorRegistration apival = registry.addInterceptor(new APIValidationInterceptor(productService.getAllowProductList(this.allowListPath), this.requestIdsStr)).addPathPatterns("/**")
.excludePathPatterns(API.I18N_API_ROOT+"doc/**", "/swagger-ui/**");

// authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ config.gzip.enable = on
config.gzip.minsize = 2048
#request IDs print in log that defined by customer
config.client.requestIds=csp-request-id

#Allow list configuration
allow.list.path=bundle.json

Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ config.gzip.minsize = 2048
#request IDs print in log that defined by customer
config.client.requestIds=csp-request-id

#Allow list configuration
allow.list.path=l10n/bundles/bundle.json
#allow list.path.bucketName=###


Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.i18n.l3;

import com.vmware.vip.BootApplication;
import com.vmware.vip.core.messages.service.product.IProductService;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = BootApplication.class)
public class ProductServiceTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private IProductService iProductService;
private static String testList1 = "classpath:bundle_unittest.json";
private static String testList2 = "bundle_unittest.json";
private static String testList2_content = "{\n" +
" \"unitTest\":[\"1.0.0\",\"2.0.0\"]\n" +
"}";
private static Logger logger = LoggerFactory.getLogger(ProductServiceTest.class);

@Test
public void testProductAllowList1(){
Map<String, Object> result = iProductService.getAllowProductList(testList1);
logger.info(String.valueOf(result.size()));
Assert.assertTrue(result.size()>0);
}

@Test
public void testProductAllowList2(){
File file = new File(testList2);
file.deleteOnExit();
try {
file.createNewFile();
FileUtils.write(file, testList2_content, "UTF-8");
Map<String, Object> result = iProductService.getAllowProductList(file.getAbsolutePath());
logger.info(String.valueOf(result.size()));
Assert.assertTrue(result.size()>0);
file.deleteOnExit();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}



}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"unitTest":["1.0.0","2.0.0"]
}

0 comments on commit bf82ccc

Please sign in to comment.