|
|
|
@ -18,12 +18,24 @@
|
|
|
|
|
|
|
|
|
|
package io.entgra.device.mgt.core.apimgt.webapp.publisher;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.permission.DefaultPermission;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.permission.DefaultPermissions;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.exception.APIManagerPublisherException;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
|
|
|
|
import org.wso2.carbon.core.ServerStartupObserver;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
|
|
public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
@ -34,6 +46,7 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
|
private static final int MAX_RETRY_COUNT = 5;
|
|
|
|
|
private static Stack<APIConfig> failedAPIsStack = new Stack<>();
|
|
|
|
|
private static Stack<APIConfig> currentAPIsStack;
|
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
|
|
|
|
|
|
private APIPublisherService publisher;
|
|
|
|
|
|
|
|
|
@ -91,6 +104,8 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
|
log.error("failed to update scope role mapping.", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateScopeMetadataEntryWithDefaultScopes();
|
|
|
|
|
|
|
|
|
|
// execute after api publishing
|
|
|
|
|
for (PostApiPublishingObsever observer : APIPublisherDataHolder.getInstance().getPostApiPublishingObseverList()) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
@ -116,4 +131,39 @@ public class APIPublisherStartupHandler implements ServerStartupObserver {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update permission scope mapping entry with default scopes if perm-scope-mapping entry exists, otherwise this function
|
|
|
|
|
* will create that entry and update the value with default permissions.
|
|
|
|
|
*/
|
|
|
|
|
private void updateScopeMetadataEntryWithDefaultScopes() {
|
|
|
|
|
MetadataManagementService metadataManagementService = APIPublisherDataHolder.getInstance().getMetadataManagementService();
|
|
|
|
|
try {
|
|
|
|
|
DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
|
|
|
|
|
DefaultPermissions defaultPermissions = deviceManagementConfig.getDefaultPermissions();
|
|
|
|
|
Metadata permScopeMapping = metadataManagementService.retrieveMetadata(Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
|
Map<String, String> permScopeMap = (permScopeMapping != null) ? gson.fromJson(permScopeMapping.getMetaValue(), HashMap.class) :
|
|
|
|
|
new HashMap<>();
|
|
|
|
|
for (DefaultPermission defaultPermission : defaultPermissions.getDefaultPermissions()) {
|
|
|
|
|
permScopeMap.putIfAbsent(defaultPermission.getName(),
|
|
|
|
|
defaultPermission.getScopeMapping().getKey());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
APIPublisherDataHolder.getInstance().setPermScopeMapping(permScopeMap);
|
|
|
|
|
if (permScopeMapping != null) {
|
|
|
|
|
permScopeMapping.setMetaValue(gson.toJson(permScopeMap));
|
|
|
|
|
metadataManagementService.updateMetadata(permScopeMapping);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
permScopeMapping = new Metadata();
|
|
|
|
|
permScopeMapping.setMetaKey(Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
|
permScopeMapping.setMetaValue(gson.toJson(permScopeMap));
|
|
|
|
|
metadataManagementService.createMetadata(permScopeMapping);
|
|
|
|
|
} catch (MetadataManagementException e) {
|
|
|
|
|
log.error("Error encountered while updating permission scope mapping metadata with default scopes");
|
|
|
|
|
} catch (MetadataKeyAlreadyExistsException e) {
|
|
|
|
|
log.error("Metadata entry already exists for " + Constants.PERM_SCOPE_MAPPING_META_KEY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|