Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing CC runtime artifacts retrieval issue and GW timeout issue during API subscription retrieval #12514

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15946,7 +15946,6 @@ public List<APIProductResource> getProductMappingsForAPI(API api) throws APIMana
APIProductResource productMapping = new APIProductResource();
productMapping.setProductIdentifier(apiProductIdentifier);
productMapping.setUriTemplate(uriTemplate);

productMappings.add(productMapping);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> ap
// adding env_properties.json
Map<String, Map<String, Environment>> environmentSpecificAPIProperties =
getEnvironmentSpecificAPIProperties(apiRuntimeArtifactDtoList);
String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(),
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME,
environmentSpecificAPIProperties);

if (environmentSpecificAPIProperties != null) {
String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(),
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE,
APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME,
environmentSpecificAPIProperties);
}
CommonUtil.archiveDirectory(tempDirectory.getAbsolutePath());
FileUtils.deleteQuietly(tempDirectory);
RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
Expand Down Expand Up @@ -150,7 +152,10 @@ private Map<String, Map<String, Environment>> getEnvironmentSpecificAPIPropertie
List<String> apiIds = apiRuntimeArtifactDtoList.stream()
.map(APIRuntimeArtifactDto::getApiId)
.collect(Collectors.toList());
return environmentSpecificAPIPropertyDao.getEnvironmentSpecificAPIPropertiesOfAPIs(apiIds);
if (!apiIds.isEmpty()) {
return environmentSpecificAPIPropertyDao.getEnvironmentSpecificAPIPropertiesOfAPIs(apiIds);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


public class RegistryPersistenceUtil {
private static final Log log = LogFactory.getLog(RegistryPersistenceUtil.class);

//Stores <tenantId, isTenantRXTLoaded> to load RXTs only once
private static final ConcurrentHashMap<Integer, Boolean> tenantRxtLoaded = new ConcurrentHashMap<>();

/**
* When an input is having '-AT-',replace it with @ [This is required to persist API data between registry and database]
*
Expand Down Expand Up @@ -483,76 +487,84 @@ public static void loadTenantRegistry(int tenantId) throws RegistryException {
}

public static void loadloadTenantAPIRXT(String tenant, int tenantID) throws RegistryException, PersistenceException {

RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
UserRegistry registry = null;
try {

registry = registryService.getGovernanceSystemRegistry(tenantID);
} catch (RegistryException e) {
throw new PersistenceException("Error when create registry instance ", e);
}

String rxtDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources"
+ File.separator + "rxts";
File file = new File(rxtDir);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override public boolean accept(File dir, String name) {
// if the file extension is .rxt return true, else false
return name.endsWith(".rxt");
// Synchronize on the tenant specific lock
synchronized (tenantID + "_loadTenantAPIRXT") {
// Check if RXTs have already been loaded for this tenant
if (Boolean.TRUE.equals(tenantRxtLoaded.get(tenantID))) {
return; // RXTs already loaded, exit method
}
};
String[] rxtFilePaths = file.list(filenameFilter);

if (rxtFilePaths == null) {
throw new PersistenceException("rxt files not found in directory " + rxtDir);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
UserRegistry registry = null;
try {

for (String rxtPath : rxtFilePaths) {
String resourcePath = GovernanceConstants.RXT_CONFIGS_PATH + RegistryConstants.PATH_SEPARATOR + rxtPath;
registry = registryService.getGovernanceSystemRegistry(tenantID);
} catch (RegistryException e) {
throw new PersistenceException("Error when create registry instance ", e);
}

//This is "registry" is a governance registry instance, therefore calculate the relative path to governance.
String govRelativePath = RegistryUtils.getRelativePathToOriginal(resourcePath,
RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(),
RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH));
try {
// calculate resource path
UserRealm tenantUserRealm = (UserRealm) ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID);
RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(tenantUserRealm);
resourcePath = authorizationManager.computePathOnMount(resourcePath);
String rxtDir = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator + "resources"
+ File.separator + "rxts";
File file = new File(rxtDir);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// if the file extension is .rxt return true, else false
return name.endsWith(".rxt");
}
};
String[] rxtFilePaths = file.list(filenameFilter);

org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance()
.getRealmService().
getTenantUserRealm(tenantID)
.getAuthorizationManager();
if (rxtFilePaths == null) {
throw new PersistenceException("rxt files not found in directory " + rxtDir);
}

if (registry.resourceExists(govRelativePath)) {
// set anonymous user permission to RXTs
if (!authManager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET)) {
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
for (String rxtPath : rxtFilePaths) {
String resourcePath = GovernanceConstants.RXT_CONFIGS_PATH + RegistryConstants.PATH_SEPARATOR + rxtPath;

//This is "registry" is a governance registry instance, therefore calculate the relative path to governance.
String govRelativePath = RegistryUtils.getRelativePathToOriginal(resourcePath,
RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(),
RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH));
try {
// calculate resource path
UserRealm tenantUserRealm = (UserRealm) ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID);
RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(tenantUserRealm);
resourcePath = authorizationManager.computePathOnMount(resourcePath);

org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance()
.getRealmService().
getTenantUserRealm(tenantID)
.getAuthorizationManager();

if (registry.resourceExists(govRelativePath)) {
// set anonymous user permission to RXTs
if (!authManager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET)) {
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
}
continue;
}
continue;
}

String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
Resource resource = registry.newResource();
resource.setContent(rxt.getBytes(Charset.defaultCharset()));
resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
registry.put(govRelativePath, resource);
String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
Resource resource = registry.newResource();
resource.setContent(rxt.getBytes(Charset.defaultCharset()));
resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
registry.put(govRelativePath, resource);

authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);
authManager.authorizeRole(APIConstants.ANONYMOUS_ROLE, resourcePath, ActionConstants.GET);

} catch (UserStoreException e) {
throw new PersistenceException("Error while adding role permissions to API", e);
} catch (IOException e) {
String msg = "Failed to read rxt files";
throw new PersistenceException(msg, e);
} catch (RegistryException e) {
String msg = "Failed to add rxt to registry ";
throw new PersistenceException(msg, e);
} catch (UserStoreException e) {
throw new PersistenceException("Error while adding role permissions to API", e);
} catch (IOException e) {
String msg = "Failed to read rxt files";
throw new PersistenceException(msg, e);
} catch (RegistryException e) {
String msg = "Failed to add rxt to registry ";
throw new PersistenceException(msg, e);
}
}
// Mark RXTs as loaded for this tenant
tenantRxtLoaded.put(tenantID, Boolean.TRUE);
}

}

/**
Expand Down
Loading