use of org.wso2.carbon.registry.core.config.Mount in project kubernetes by ballerinax.
the class KubernetesAnnotationProcessor method processSecureSocketAnnotation.
/**
* Extract key-store/trust-store file location from endpoint.
*
* @param endpointName Endpoint name
* @param secureSocketKeyValues secureSocket annotation struct
* @return List of @{@link SecretModel} objects
*/
Set<SecretModel> processSecureSocketAnnotation(String endpointName, List<BLangRecordLiteral.BLangRecordKeyValue> secureSocketKeyValues) throws KubernetesPluginException {
Set<SecretModel> secrets = new HashSet<>();
String keyStoreFile = null;
String trustStoreFile = null;
for (BLangRecordLiteral.BLangRecordKeyValue keyValue : secureSocketKeyValues) {
// extract file paths.
String key = keyValue.getKey().toString();
if ("keyStore".equals(key)) {
keyStoreFile = extractFilePath(keyValue);
} else if ("trustStore".equals(key)) {
trustStoreFile = extractFilePath(keyValue);
}
}
if (keyStoreFile != null && trustStoreFile != null) {
if (getMountPath(keyStoreFile).equals(getMountPath(trustStoreFile))) {
// trust-store and key-store mount to same path
String keyStoreContent = readSecretFile(keyStoreFile);
String trustStoreContent = readSecretFile(trustStoreFile);
SecretModel secretModel = new SecretModel();
secretModel.setName(getValidName(endpointName) + "-secure-socket");
secretModel.setMountPath(getMountPath(keyStoreFile));
Map<String, String> dataMap = new HashMap<>();
dataMap.put(String.valueOf(Paths.get(keyStoreFile).getFileName()), keyStoreContent);
dataMap.put(String.valueOf(Paths.get(trustStoreFile).getFileName()), trustStoreContent);
secretModel.setData(dataMap);
secrets.add(secretModel);
return secrets;
}
}
if (keyStoreFile != null) {
String keyStoreContent = readSecretFile(keyStoreFile);
SecretModel secretModel = new SecretModel();
secretModel.setName(getValidName(endpointName) + "-keystore");
secretModel.setMountPath(getMountPath(keyStoreFile));
Map<String, String> dataMap = new HashMap<>();
dataMap.put(String.valueOf(Paths.get(keyStoreFile).getFileName()), keyStoreContent);
secretModel.setData(dataMap);
secrets.add(secretModel);
}
if (trustStoreFile != null) {
String trustStoreContent = readSecretFile(trustStoreFile);
SecretModel secretModel = new SecretModel();
secretModel.setName(getValidName(endpointName) + "-truststore");
secretModel.setMountPath(getMountPath(trustStoreFile));
Map<String, String> dataMap = new HashMap<>();
dataMap.put(String.valueOf(Paths.get(trustStoreFile).getFileName()), trustStoreContent);
secretModel.setData(dataMap);
secrets.add(secretModel);
}
return secrets;
}
use of org.wso2.carbon.registry.core.config.Mount in project carbon-apimgt by wso2.
the class RegistryCacheInvalidationService method invalidateCache.
/**
* This method invalidates registry cache for given resource in given tenant domain
* @param path
* @param tenantDomain
* @throws APIManagementException
*/
public void invalidateCache(String path, String tenantDomain) throws APIManagementException {
Registry registry;
boolean isTenantFlowStarted = false;
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
isTenantFlowStarted = true;
registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
Cache<RegistryCacheKey, GhostResource> cache = RegistryUtils.getResourceCache(RegistryConstants.REGISTRY_CACHE_BACKED_ID);
RegistryCacheKey cacheKey = null;
// Is registry mounted
if (registry.getRegistryContext().getRemoteInstances().size() > 0) {
for (Mount mount : registry.getRegistryContext().getMounts()) {
for (RemoteConfiguration configuration : registry.getRegistryContext().getRemoteInstances()) {
if (path.startsWith(mount.getPath())) {
DataBaseConfiguration dataBaseConfiguration = registry.getRegistryContext().getDBConfig(configuration.getDbConfig());
String connectionId = (dataBaseConfiguration.getUserName() != null ? dataBaseConfiguration.getUserName().split("@")[0] : dataBaseConfiguration.getUserName()) + "@" + dataBaseConfiguration.getDbUrl();
cacheKey = RegistryUtils.buildRegistryCacheKey(connectionId, tenantId, path);
if (cacheKey != null && cache.containsKey(cacheKey)) {
cache.remove(cacheKey);
}
}
}
}
} else {
DataBaseConfiguration dataBaseConfiguration = registry.getRegistryContext().getDefaultDataBaseConfiguration();
String connectionId = (dataBaseConfiguration.getUserName() != null ? dataBaseConfiguration.getUserName().split("@")[0] : dataBaseConfiguration.getUserName()) + "@" + dataBaseConfiguration.getDbUrl();
cacheKey = RegistryUtils.buildRegistryCacheKey(connectionId, tenantId, path);
if (cacheKey != null && cache.containsKey(cacheKey)) {
cache.remove(cacheKey);
}
}
} catch (RegistryException e) {
APIUtil.handleException("Error in accessing governance registry while invalidating cache for " + path + "in tenant " + tenantDomain, e);
} catch (UserStoreException e) {
APIUtil.handleException("Error in retrieving Tenant Information while invalidating cache for " + path + "in tenant " + tenantDomain, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.registry.core.config.Mount in project carbon-business-process by wso2.
the class HumanTaskStore method deploy.
/**
* Handles the deployment steps for the master node and salve node in the cluster
* @param humanTaskFile
* @throws Exception
*/
public void deploy(File humanTaskFile) throws Exception {
// Currently using the registry read/write mount property to determine whether this node is a master node
// or a salve node.
// Handle this properly with hazelcast leader for cluster scenario TODO
boolean isMasterServer = !isServerReadOnly();
// Versions of this ht package is already deployed
boolean isExistingPackage = false;
// Exactly matching ht package already exists
boolean isPackageReload = false;
DeploymentUnitDAO currentlyActiveTaskPackage = null;
String md5sum = HumanTaskStoreUtils.getMD5Checksum(humanTaskFile);
String packageName = FilenameUtils.removeExtension(humanTaskFile.getName());
List<DeploymentUnitDAO> existingDeploymentUnitsForPackage = getExistingDeploymentUnitsForPackage(packageName.trim());
if (existingDeploymentUnitsForPackage != null && existingDeploymentUnitsForPackage.size() > 0) {
isExistingPackage = true;
for (DeploymentUnitDAO dao : existingDeploymentUnitsForPackage) {
if ((dao.getStatus() == (TaskPackageStatus.ACTIVE))) {
// extract the currently active task package
currentlyActiveTaskPackage = dao;
if (dao.getChecksum().equals(md5sum)) {
// Check whether the md5sum matches the active task package.
isPackageReload = true;
}
}
}
}
// We will only allow writes to db only for the master node to avoid duplicate version creation
if (isExistingPackage && isPackageReload) {
// Reload the existing versions of the human task package . No need of creating a new version of the package
// This could be due to server restart, deployment of the same package or master node has already deployed the
// new version of the package
// First check if the currently active task package is already loaded
String activePackageName = loadedPackages.get(currentlyActiveTaskPackage.getPackageName());
if (activePackageName != null && activePackageName.equals(currentlyActiveTaskPackage.getName())) {
if (log.isDebugEnabled()) {
log.debug("This task package and its previous versions are already loaded " + activePackageName);
}
// This task package and its previous versions are already loaded , hence return
return;
}
// Load the existing versions of the package
reloadExistingTaskVersions(existingDeploymentUnitsForPackage, humanTaskFile, md5sum, isMasterServer);
return;
}
// New version of the package is being deployed on top of the existing version
if (isExistingPackage && !isPackageReload) {
if (isMasterServer) {
// Retire the existing version of the package and deploy the new version
// This could be two scenarios. Server restart with new version and deploying on existing version.
String activePackageName = loadedPackages.get(currentlyActiveTaskPackage.getPackageName());
if (activePackageName == null) {
// This is a server restart, we need to load existing versions
reloadExistingTaskVersions(existingDeploymentUnitsForPackage, humanTaskFile, md5sum, isMasterServer);
}
long newVersion = getNextVersion();
HumanTaskDeploymentUnit newDeploymentUnit = createNewDeploymentUnit(humanTaskFile, tenantId, newVersion, md5sum);
validateTaskConfig(newDeploymentUnit);
retireTaskPackageConfigurations(currentlyActiveTaskPackage.getName());
currentlyActiveTaskPackage.setStatus(TaskPackageStatus.RETIRED);
updateDeploymentUnitDao(currentlyActiveTaskPackage);
// Retiring of currently active package is complete.
// Create and deploy new version
deployNewTaskVersion(newDeploymentUnit, newVersion);
// Add new version of human task package to registry
// Update the zip and package properties in the registry
repository.handleNewHumanTaskPackageAddition(newDeploymentUnit, humanTaskFile);
// Successfully deployed the packages.
return;
} else {
// Cannot allow creation of a new version from slave nodes, deploy the new version on the master node
// first to avoid duplicate version creation
// Write log, issue warning and return
log.warn("Cannot deploy new version of the task in slave node. Hence deploy the task archive in master" + "node fist");
return;
}
}
if (!isMasterServer) {
// Issue warning, write warn message and return as we cannot allow deployment of new versions on slave nodes
// before deployment of the ht package in the master node
log.warn("Cannot deploy a new version on the package on the salve node first, " + "Deploy the package on the master node first");
return;
}
// Create new version of deployment unit
// Process the human task configurations
// Store deployment unit information to the db
// Deploy axis2 services
// Adding HumanTask package the registry.
long newVersion = getNextVersion();
HumanTaskDeploymentUnit newDeploymentUnit = createNewDeploymentUnit(humanTaskFile, tenantId, newVersion, md5sum);
validateTaskConfig(newDeploymentUnit);
deployNewTaskVersion(newDeploymentUnit, newVersion);
repository.handleNewHumanTaskPackageAddition(newDeploymentUnit, humanTaskFile);
return;
}
use of org.wso2.carbon.registry.core.config.Mount in project carbon-apimgt by wso2.
the class RegistryCacheInvalidationServiceTestCase method testInvalidateCacheWhenRegistryMountedAndCached.
@Test
public void testInvalidateCacheWhenRegistryMountedAndCached() throws APIManagementException {
List<RemoteConfiguration> remoteConfigurationList = new ArrayList<RemoteConfiguration>();
RemoteConfiguration remoteConfiguration = new RemoteConfiguration();
remoteConfiguration.setDbConfig("");
remoteConfigurationList.add(remoteConfiguration);
List<Mount> mountList = new ArrayList<Mount>();
Mount mount = new Mount();
mount.setPath("/_system/config");
mountList.add(mount);
DataBaseConfiguration dataBaseConfiguration = Mockito.mock(DataBaseConfiguration.class);
Mockito.when(registryContext.getMounts()).thenReturn(mountList);
Mockito.when(registryContext.getDBConfig(remoteConfiguration.getDbConfig())).thenReturn(dataBaseConfiguration);
Mockito.when(registryContext.getRemoteInstances()).thenReturn(remoteConfigurationList);
Mockito.when(registryContext.getDefaultDataBaseConfiguration()).thenReturn(dataBaseConfiguration);
PowerMockito.when(RegistryUtils.getResourceCache(RegistryConstants.REGISTRY_CACHE_BACKED_ID)).thenReturn(cache);
Mockito.when(dataBaseConfiguration.getUserName()).thenReturn("john@foo.com");
Mockito.when(dataBaseConfiguration.getDbUrl()).thenReturn("xyz.com/db");
RegistryCacheKey registryCacheKey = Mockito.mock(RegistryCacheKey.class);
PowerMockito.when(RegistryUtils.buildRegistryCacheKey("john@xyz.com/db", 6543, path)).thenReturn(registryCacheKey);
Mockito.when(cache.containsKey(registryCacheKey)).thenReturn(true);
RegistryCacheInvalidationService registryCacheInvalidationService = new RegistryCacheInvalidationService();
registryCacheInvalidationService.invalidateCache(path, tenantDomain);
Mockito.verify(cache, Mockito.times(1)).remove(Matchers.any());
}
Aggregations