use of org.wso2.balana.xacml3.Attributes in project carbon-apimgt by wso2.
the class RegistryPersistenceUtil method getDevPortalAPIForSearch.
public static DevPortalAPI getDevPortalAPIForSearch(GenericArtifact apiArtifact) throws APIPersistenceException {
DevPortalAPI api = new DevPortalAPI();
try {
api.setContext(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
api.setDescription(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
api.setId(apiArtifact.getId());
api.setStatus(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
api.setApiName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
api.setProviderName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
;
api.setVersion(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
} catch (GovernanceException e) {
throw new APIPersistenceException("Error while extracting api attributes ", e);
}
return api;
}
use of org.wso2.balana.xacml3.Attributes in project carbon-apimgt by wso2.
the class ApiMgtDAO method getApplicationByUUID.
/**
* Retrieves the Application which is corresponding to the given UUID String
*
* @param uuid UUID of Application
* @return
* @throws APIManagementException
*/
public Application getApplicationByUUID(String uuid) throws APIManagementException {
Connection connection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
int applicationId = 0;
Application application = null;
try {
connection = APIMgtDBUtil.getConnection();
String query = SQLConstants.GET_APPLICATION_BY_UUID_SQL;
prepStmt = connection.prepareStatement(query);
prepStmt.setString(1, uuid);
rs = prepStmt.executeQuery();
if (rs.next()) {
String applicationName = rs.getString("NAME");
String subscriberId = rs.getString("SUBSCRIBER_ID");
String subscriberName = rs.getString("USER_ID");
Subscriber subscriber = new Subscriber(subscriberName);
subscriber.setId(Integer.parseInt(subscriberId));
application = new Application(applicationName, subscriber);
application.setDescription(rs.getString("DESCRIPTION"));
application.setStatus(rs.getString("APPLICATION_STATUS"));
application.setCallbackUrl(rs.getString("CALLBACK_URL"));
applicationId = rs.getInt("APPLICATION_ID");
application.setId(applicationId);
application.setGroupId(rs.getString("GROUP_ID"));
application.setUUID(rs.getString("UUID"));
application.setTier(rs.getString("APPLICATION_TIER"));
application.setTokenType(rs.getString("TOKEN_TYPE"));
application.setOwner(rs.getString("CREATED_BY"));
application.setOrganization(rs.getString("ORGANIZATION"));
subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
if (multiGroupAppSharingEnabled) {
if (application.getGroupId() == null || application.getGroupId().isEmpty()) {
application.setGroupId(getGroupId(connection, application.getId()));
}
}
Timestamp createdTime = rs.getTimestamp("CREATED_TIME");
application.setCreatedTime(createdTime == null ? null : String.valueOf(createdTime.getTime()));
try {
Timestamp updated_time = rs.getTimestamp("UPDATED_TIME");
application.setLastUpdatedTime(updated_time == null ? null : String.valueOf(updated_time.getTime()));
} catch (SQLException e) {
// fixing Timestamp issue with default value '0000-00-00 00:00:00'for existing applications created
application.setLastUpdatedTime(application.getCreatedTime());
}
}
// Get custom attributes of application
if (application != null) {
Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
application.setApplicationAttributes(applicationAttributes);
}
} catch (SQLException e) {
handleException("Error while obtaining details of the Application : " + uuid, e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
}
return application;
}
use of org.wso2.balana.xacml3.Attributes in project carbon-apimgt by wso2.
the class APIConsumerImpl method getAppAttributesFromConfig.
/**
* This method is used to get keys of custom attributes, configured by user
*
* @param userId user name of logged in user
* @return Array of JSONObject, contains keys of attributes
* @throws APIManagementException
*/
public JSONArray getAppAttributesFromConfig(String userId) throws APIManagementException {
String tenantDomain = MultitenantUtils.getTenantDomain(userId);
int tenantId = 0;
try {
tenantId = getTenantId(tenantDomain);
} catch (UserStoreException e) {
handleException("Error in getting tenantId of " + tenantDomain, e);
}
JSONArray applicationAttributes = null;
JSONObject applicationConfig = APIUtil.getAppAttributeKeysFromRegistry(tenantDomain);
if (applicationConfig != null) {
applicationAttributes = (JSONArray) applicationConfig.get(APIConstants.ApplicationAttributes.ATTRIBUTES);
} else {
APIManagerConfiguration configuration = getAPIManagerConfiguration();
applicationAttributes = configuration.getApplicationAttributes();
}
return applicationAttributes;
}
use of org.wso2.balana.xacml3.Attributes in project carbon-apimgt by wso2.
the class APIProviderImpl method validateAppliedPolicyWithSpecification.
private boolean validateAppliedPolicyWithSpecification(OperationPolicySpecification policySpecification, OperationPolicy appliedPolicy, API api) throws APIManagementException {
// Validate the policy applied direction
if (!policySpecification.getApplicableFlows().contains(appliedPolicy.getDirection())) {
if (log.isDebugEnabled()) {
log.debug("The policy " + policySpecification.getName() + " is not support in the " + appliedPolicy.getDirection() + " flow. Hence skipped.");
}
throw new APIManagementException(policySpecification.getName() + " cannot be used in the " + appliedPolicy.getDirection() + " flow.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
// Validate the API type
if (!policySpecification.getSupportedApiTypes().contains(api.getType())) {
if (log.isDebugEnabled()) {
log.debug("The policy " + policySpecification.getName() + " cannot be used for the " + api.getType() + " API type.");
}
throw new APIManagementException(policySpecification.getName() + " cannot be used for the " + api.getType() + " API type.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
// Validate policy Attributes
if (policySpecification.getPolicyAttributes() != null) {
for (OperationPolicySpecAttribute attribute : policySpecification.getPolicyAttributes()) {
if (attribute.isRequired()) {
Object appliedPolicyAttribute = appliedPolicy.getParameters().get(attribute.getName());
if (appliedPolicyAttribute != null) {
if (attribute.getValidationRegex() != null) {
Pattern pattern = Pattern.compile(attribute.getValidationRegex(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher((String) appliedPolicyAttribute);
if (!matcher.matches()) {
throw new APIManagementException("Policy attribute " + attribute.getName() + " regex validation error.", ExceptionCodes.INVALID_OPERATION_POLICY_PARAMETERS);
}
}
} else {
if (log.isDebugEnabled()) {
log.debug("Required policy attribute " + attribute.getName() + " is not found for the the policy " + policySpecification.getName());
}
throw new APIManagementException("Required policy attribute " + attribute.getName() + " is not found for the the policy " + policySpecification.getName() + appliedPolicy.getDirection() + " flow.", ExceptionCodes.MISSING_MANDATORY_POLICY_ATTRIBUTES);
}
}
}
}
return true;
}
use of org.wso2.balana.xacml3.Attributes in project carbon-apimgt by wso2.
the class APIManagerConfiguration method readChildElements.
private void readChildElements(OMElement serverConfig, Stack<String> nameStack) throws APIManagementException {
for (Iterator childElements = serverConfig.getChildElements(); childElements.hasNext(); ) {
OMElement element = (OMElement) childElements.next();
String localName = element.getLocalName();
nameStack.push(localName);
if ("APIKeyValidator".equals(localName)) {
OMElement keyManagerServiceUrl = element.getFirstChildWithName(new QName(APIConstants.AUTHSERVER_URL));
if (keyManagerServiceUrl != null) {
String serviceUrl = keyManagerServiceUrl.getText();
addKeyManagerConfigsAsSystemProperties(APIUtil.replaceSystemProperty(serviceUrl));
}
} else if (TOKEN_REVOCATION_NOTIFIERS.equals(localName)) {
tokenRevocationClassName = element.getAttributeValue(new QName("class"));
} else if (REALTIME_NOTIFIER.equals(localName)) {
Iterator revocationPropertiesIterator = element.getChildrenWithLocalName("Property");
Properties properties = new Properties();
while (revocationPropertiesIterator.hasNext()) {
OMElement propertyElem = (OMElement) revocationPropertiesIterator.next();
properties.setProperty(propertyElem.getAttributeValue(new QName("name")), propertyElem.getText());
}
realtimeNotifierProperties = properties;
} else if (PERSISTENT_NOTIFIER.equals(localName)) {
Iterator revocationPropertiesIterator = element.getChildrenWithLocalName("Property");
Properties properties = new Properties();
while (revocationPropertiesIterator.hasNext()) {
OMElement propertyElem = (OMElement) revocationPropertiesIterator.next();
if (propertyElem.getAttributeValue(new QName("name")).equalsIgnoreCase("password")) {
if (secretResolver.isInitialized() && secretResolver.isTokenProtected(TOKEN_REVOCATION_NOTIFIERS_PASSWORD)) {
properties.setProperty(propertyElem.getAttributeValue(new QName("name")), secretResolver.resolve(TOKEN_REVOCATION_NOTIFIERS_PASSWORD));
} else {
properties.setProperty(propertyElem.getAttributeValue(new QName("name")), propertyElem.getText());
}
} else {
properties.setProperty(propertyElem.getAttributeValue(new QName("name")), propertyElem.getText());
}
}
persistentNotifierProperties = properties;
} else if ("Analytics".equals(localName)) {
OMElement properties = element.getFirstChildWithName(new QName("Properties"));
Iterator analyticsPropertiesIterator = properties.getChildrenWithLocalName("Property");
Map<String, String> analyticsProps = new HashMap<>();
while (analyticsPropertiesIterator.hasNext()) {
OMElement propertyElem = (OMElement) analyticsPropertiesIterator.next();
String name = propertyElem.getAttributeValue(new QName("name"));
String value = propertyElem.getText();
analyticsProps.put(name, value);
}
OMElement authTokenElement = element.getFirstChildWithName(new QName("AuthToken"));
String resolvedAuthToken = MiscellaneousUtil.resolve(authTokenElement, secretResolver);
analyticsProps.put("auth.api.token", resolvedAuthToken);
analyticsProperties = analyticsProps;
} else if ("PersistenceConfigs".equals(localName)) {
OMElement properties = element.getFirstChildWithName(new QName("Properties"));
Iterator analyticsPropertiesIterator = properties.getChildrenWithLocalName("Property");
Map<String, String> persistenceProps = new HashMap<>();
while (analyticsPropertiesIterator.hasNext()) {
OMElement propertyElem = (OMElement) analyticsPropertiesIterator.next();
String name = propertyElem.getAttributeValue(new QName("name"));
String value = propertyElem.getText();
persistenceProps.put(name, value);
}
persistenceProperties = persistenceProps;
} else if (APIConstants.REDIS_CONFIG.equals(localName)) {
OMElement redisHost = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_HOST));
OMElement redisPort = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_PORT));
OMElement redisUser = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_USER));
OMElement redisPassword = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_PASSWORD));
OMElement redisDatabaseId = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_DATABASE_ID));
OMElement redisConnectionTimeout = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_CONNECTION_TIMEOUT));
OMElement redisIsSslEnabled = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_IS_SSL_ENABLED));
OMElement propertiesElement = element.getFirstChildWithName(new QName(APIConstants.CONFIG_REDIS_PROPERTIES));
redisConfig.setRedisEnabled(true);
redisConfig.setHost(redisHost.getText());
redisConfig.setPort(Integer.parseInt(redisPort.getText()));
if (redisUser != null && redisPassword != null && redisDatabaseId != null && redisConnectionTimeout != null && redisIsSslEnabled != null) {
redisConfig.setUser(redisUser.getText());
redisConfig.setPassword(MiscellaneousUtil.resolve(redisPassword, secretResolver).toCharArray());
redisConfig.setDatabaseId(Integer.parseInt(redisDatabaseId.getText()));
redisConfig.setConnectionTimeout(Integer.parseInt(redisConnectionTimeout.getText()));
redisConfig.setSslEnabled(Boolean.parseBoolean(redisIsSslEnabled.getText()));
}
if (propertiesElement != null) {
Iterator<OMElement> properties = propertiesElement.getChildElements();
if (properties != null) {
while (properties.hasNext()) {
OMElement propertyNode = properties.next();
if (APIConstants.CONFIG_REDIS_MAX_TOTAL.equals(propertyNode.getLocalName())) {
redisConfig.setMaxTotal(Integer.parseInt(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_MAX_IDLE.equals(propertyNode.getLocalName())) {
redisConfig.setMaxIdle(Integer.parseInt(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_MIN_IDLE.equals(propertyNode.getLocalName())) {
redisConfig.setMinIdle(Integer.parseInt(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_TEST_ON_BORROW.equals(propertyNode.getLocalName())) {
redisConfig.setTestOnBorrow(Boolean.parseBoolean(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_TEST_ON_RETURN.equals(propertyNode.getLocalName())) {
redisConfig.setTestOnReturn(Boolean.parseBoolean(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_TEST_WHILE_IDLE.equals(propertyNode.getLocalName())) {
redisConfig.setTestWhileIdle(Boolean.parseBoolean(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_BLOCK_WHEN_EXHAUSTED.equals(propertyNode.getLocalName())) {
redisConfig.setBlockWhenExhausted(Boolean.parseBoolean(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_MIN_EVICTABLE_IDLE_TIME_IN_MILLIS.equals(propertyNode.getLocalName())) {
redisConfig.setMinEvictableIdleTimeMillis(Long.parseLong(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_TIME_BETWEEN_EVICTION_RUNS_IN_MILLIS.equals(propertyNode.getLocalName())) {
redisConfig.setTimeBetweenEvictionRunsMillis(Long.parseLong(propertyNode.getText()));
} else if (APIConstants.CONFIG_REDIS_NUM_TESTS_PER_EVICTION_RUNS.equals(propertyNode.getLocalName())) {
redisConfig.setNumTestsPerEvictionRun(Integer.parseInt(propertyNode.getText()));
}
}
}
}
} else if (elementHasText(element)) {
String key = getKey(nameStack);
String value = MiscellaneousUtil.resolve(element, secretResolver);
addToConfiguration(key, APIUtil.replaceSystemProperty(value));
} else if ("Environments".equals(localName)) {
Iterator environmentIterator = element.getChildrenWithLocalName("Environment");
apiGatewayEnvironments = new LinkedHashMap<String, Environment>();
while (environmentIterator.hasNext()) {
OMElement environmentElem = (OMElement) environmentIterator.next();
setEnvironmentConfig(environmentElem);
}
} else if (APIConstants.EXTERNAL_API_STORES.equals(localName)) {
// Initialize 'externalAPIStores' config elements
Iterator apistoreIterator = element.getChildrenWithLocalName("ExternalAPIStore");
externalAPIStores = new HashSet<APIStore>();
while (apistoreIterator.hasNext()) {
APIStore store = new APIStore();
OMElement storeElem = (OMElement) apistoreIterator.next();
String type = storeElem.getAttributeValue(new QName(APIConstants.EXTERNAL_API_STORE_TYPE));
// Set Store type [eg:wso2]
store.setType(type);
String className = storeElem.getAttributeValue(new QName(APIConstants.EXTERNAL_API_STORE_CLASS_NAME));
try {
store.setPublisher((APIPublisher) APIUtil.getClassInstance(className));
} catch (InstantiationException e) {
String msg = "One or more classes defined in" + APIConstants.EXTERNAL_API_STORE_CLASS_NAME + "cannot be instantiated";
log.error(msg, e);
throw new APIManagementException(msg, e);
} catch (IllegalAccessException e) {
String msg = "One or more classes defined in" + APIConstants.EXTERNAL_API_STORE_CLASS_NAME + "cannot be access";
log.error(msg, e);
throw new APIManagementException(msg, e);
} catch (ClassNotFoundException e) {
String msg = "One or more classes defined in" + APIConstants.EXTERNAL_API_STORE_CLASS_NAME + "cannot be found";
log.error(msg, e);
throw new APIManagementException(msg, e);
}
String name = storeElem.getAttributeValue(new QName(APIConstants.EXTERNAL_API_STORE_ID));
if (name == null) {
log.error("The ExternalAPIStore name attribute is not defined in api-manager.xml.");
}
// Set store name
store.setName(name);
OMElement configDisplayName = storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_DISPLAY_NAME));
String displayName = (configDisplayName != null) ? APIUtil.replaceSystemProperty(configDisplayName.getText()) : name;
// Set store display name
store.setDisplayName(displayName);
store.setEndpoint(APIUtil.replaceSystemProperty(storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_ENDPOINT)).getText()));
store.setPublished(false);
if (APIConstants.WSO2_API_STORE_TYPE.equals(type)) {
OMElement password = storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_PASSWORD));
if (password != null) {
String value = MiscellaneousUtil.resolve(password, secretResolver);
store.setPassword(APIUtil.replaceSystemProperty(value));
store.setUsername(APIUtil.replaceSystemProperty(storeElem.getFirstChildWithName(new QName(APIConstants.EXTERNAL_API_STORE_USERNAME)).getText()));
} else {
log.error("The user-credentials of API Publisher is not defined in the <ExternalAPIStore> " + "config of api-manager.xml.");
}
}
externalAPIStores.add(store);
}
} else if (APIConstants.LOGIN_CONFIGS.equals(localName)) {
Iterator loginConfigIterator = element.getChildrenWithLocalName(APIConstants.LOGIN_CONFIGS);
while (loginConfigIterator.hasNext()) {
OMElement loginOMElement = (OMElement) loginConfigIterator.next();
parseLoginConfig(loginOMElement);
}
} else if (APIConstants.AdvancedThrottleConstants.THROTTLING_CONFIGURATIONS.equals(localName)) {
setThrottleProperties(serverConfig);
} else if (APIConstants.WorkflowConfigConstants.WORKFLOW.equals(localName)) {
setWorkflowProperties(serverConfig);
} else if (APIConstants.ApplicationAttributes.APPLICATION_ATTRIBUTES.equals(localName)) {
Iterator iterator = element.getChildrenWithLocalName(APIConstants.ApplicationAttributes.ATTRIBUTE);
while (iterator.hasNext()) {
OMElement omElement = (OMElement) iterator.next();
Iterator attributes = omElement.getChildElements();
JSONObject jsonObject = new JSONObject();
boolean isHidden = Boolean.parseBoolean(omElement.getAttributeValue(new QName(APIConstants.ApplicationAttributes.HIDDEN)));
boolean isRequired = Boolean.parseBoolean(omElement.getAttributeValue(new QName(APIConstants.ApplicationAttributes.REQUIRED)));
jsonObject.put(APIConstants.ApplicationAttributes.HIDDEN, isHidden);
while (attributes.hasNext()) {
OMElement attribute = (OMElement) attributes.next();
if (attribute.getLocalName().equals(APIConstants.ApplicationAttributes.NAME)) {
jsonObject.put(APIConstants.ApplicationAttributes.ATTRIBUTE, attribute.getText());
} else if (attribute.getLocalName().equals(APIConstants.ApplicationAttributes.DESCRIPTION)) {
jsonObject.put(APIConstants.ApplicationAttributes.DESCRIPTION, attribute.getText());
} else if (attribute.getLocalName().equals(APIConstants.ApplicationAttributes.TOOLTIP)) {
jsonObject.put(APIConstants.ApplicationAttributes.TOOLTIP, attribute.getText());
} else if (attribute.getLocalName().equals(APIConstants.ApplicationAttributes.TYPE)) {
jsonObject.put(APIConstants.ApplicationAttributes.TYPE, attribute.getText());
} else if (attribute.getLocalName().equals(APIConstants.ApplicationAttributes.DEFAULT) && isRequired) {
jsonObject.put(APIConstants.ApplicationAttributes.DEFAULT, attribute.getText());
}
}
if (isHidden && isRequired && !jsonObject.containsKey(APIConstants.ApplicationAttributes.DEFAULT)) {
log.error("A default value needs to be given for required, hidden application attributes.");
}
jsonObject.put(APIConstants.ApplicationAttributes.REQUIRED, isRequired);
applicationAttributes.add(jsonObject);
}
} else if (APIConstants.Monetization.MONETIZATION_CONFIG.equals(localName)) {
OMElement additionalAttributes = element.getFirstChildWithName(new QName(APIConstants.Monetization.ADDITIONAL_ATTRIBUTES));
if (additionalAttributes != null) {
setMonetizationAdditionalAttributes(additionalAttributes);
}
} else if (APIConstants.JWT_CONFIGS.equals(localName)) {
setJWTConfiguration(element);
} else if (APIConstants.TOKEN_ISSUERS.equals(localName)) {
setJWTTokenIssuers(element);
} else if (APIConstants.API_RECOMMENDATION.equals(localName)) {
setRecommendationConfigurations(element);
} else if (APIConstants.GlobalCacheInvalidation.GLOBAL_CACHE_INVALIDATION.equals(localName)) {
setGlobalCacheInvalidationConfiguration(element);
} else if (APIConstants.KeyManager.EVENT_HUB_CONFIGURATIONS.equals(localName)) {
setEventHubConfiguration(element);
} else if (APIConstants.GatewayArtifactSynchronizer.SYNC_RUNTIME_ARTIFACTS_PUBLISHER_CONFIG.equals(localName)) {
setRuntimeArtifactsSyncPublisherConfig(element);
} else if (APIConstants.GatewayArtifactSynchronizer.SYNC_RUNTIME_ARTIFACTS_GATEWAY_CONFIG.equals(localName)) {
setRuntimeArtifactsSyncGatewayConfig(element);
} else if (APIConstants.SkipListConstants.SKIP_LIST_CONFIG.equals(localName)) {
setSkipListConfigurations(element);
} else if (APIConstants.ExtensionListenerConstants.EXTENSION_LISTENERS.equals(localName)) {
setExtensionListenerConfigurations(element);
} else if (APIConstants.JWT_AUDIENCES.equals(localName)) {
setRestApiJWTAuthAudiences(element);
}
readChildElements(element, nameStack);
nameStack.pop();
}
}
Aggregations