Search in sources :

Example 31 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project carbon-identity-framework by wso2.

the class FilterTreeBuilder method validateAndBuildFilterExpression.

/**
 * Validate the simple filter and build a ExpressionNode
 *
 * @param filterString   the filter string.
 * @param expressionNode the expression node.
 */
private void validateAndBuildFilterExpression(String filterString, ExpressionNode expressionNode) throws IdentityException {
    if (StringUtils.isNotBlank(filterString) && !filterString.equals("-1")) {
        String trimmedFilter = filterString.trim();
        String[] filterParts;
        if (Pattern.compile(Pattern.quote(" eq "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" eq | EQ | eQ | Eq ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " eq ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" ne "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" ne | NE | nE | Ne ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " ne ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" co "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" co | CO | cO | Co ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " co ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" sw "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" sw | SW | sW | Sw ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " sw ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" ew "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" ew | EW | eW | Ew ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " ew ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" pr"), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            // with filter PR, there should not be whitespace after.
            filterParts = trimmedFilter.split(" pr| PR| pR| Pr");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " pr", null, expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" gt "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" gt | GT | gT | Gt ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " gt ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" ge "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" ge | GE | gE | Ge ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " ge ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" lt "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" lt | LT | lT | Lt ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " lt ", filterParts[1], expressionNode);
            }
        } else if (Pattern.compile(Pattern.quote(" le "), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
            filterParts = trimmedFilter.split(" le | LE | lE | Le ");
            if (filterParts.length >= 2) {
                setExpressionNodeValues(filterParts[0], " le ", filterParts[1], expressionNode);
            }
        } else {
            throw new IdentityException("Given filter operator is not supported. Filter attribute: " + filterString);
        }
    }
}
Also used : IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 32 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project kubernetes by ballerinax.

the class IstioGatewayAnnotationProcessor method processIstioGatewayServerAnnotation.

/**
 * Process server field of @istio:Gateway annotation.
 *
 * @param gatewayModel The gateway model.
 * @param serversField List of servers of the gateway.
 * @throws KubernetesPluginException Unable to process annotation
 */
private void processIstioGatewayServerAnnotation(IstioGatewayModel gatewayModel, BLangListConstructorExpr serversField) throws KubernetesPluginException {
    List<IstioServerModel> servers = new LinkedList<>();
    for (ExpressionNode serverRecord : serversField.getExpressions()) {
        if (serverRecord instanceof BLangRecordLiteral) {
            BLangRecordLiteral serverFieldRecord = (BLangRecordLiteral) serverRecord;
            IstioServerModel server = new IstioServerModel();
            for (BLangRecordLiteral.BLangRecordKeyValueField serverField : convertRecordFields(serverFieldRecord.getFields())) {
                switch(ServerConfig.valueOf(serverField.getKey().toString())) {
                    case port:
                        BLangRecordLiteral portRecord = (BLangRecordLiteral) serverField.getValue();
                        processIstioGatewayPortAnnotation(server, convertRecordFields(portRecord.getFields()));
                        break;
                    case hosts:
                        server.setHosts(getList(serverField.getValue()));
                        break;
                    case tls:
                        BLangRecordLiteral tlsRecord = (BLangRecordLiteral) serverField.getValue();
                        processIstioGatewayTLSAnnotation(server, convertRecordFields(tlsRecord.getFields()));
                        break;
                    default:
                        throw new KubernetesPluginException("unknown field found for istio gateway server.");
                }
            }
            servers.add(server);
        }
    }
    gatewayModel.setServers(servers);
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) IstioServerModel(org.ballerinax.kubernetes.models.istio.IstioServerModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) LinkedList(java.util.LinkedList)

Example 33 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project kubernetes by ballerinax.

the class KubernetesUtils method getList.

/**
 * Generate array of string using a {@link BLangListConstructorExpr}.
 *
 * @param expr Array literal.
 * @return Convert string.
 */
public static List<String> getList(BLangExpression expr) throws KubernetesPluginException {
    if (expr.getKind() != NodeKind.LIST_CONSTRUCTOR_EXPR) {
        throw new KubernetesPluginException("unable to parse value: " + expr.toString());
    } else {
        BLangListConstructorExpr array = (BLangListConstructorExpr) expr;
        List<String> scopeSet = new LinkedList<>();
        for (ExpressionNode bLangExpression : array.getExpressions()) {
            scopeSet.add(getStringValue((BLangExpression) bLangExpression));
        }
        return scopeSet;
    }
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) LinkedList(java.util.LinkedList)

Example 34 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMUserManagerTest method testListGroupsWithFilter.

@Test(dataProvider = "groupNameWithFilters")
public void testListGroupsWithFilter(String filter, String roleName, String userStoreDomain) throws Exception {
    ExpressionNode node = new ExpressionNode(filter);
    List<String> list = new ArrayList<>();
    list.add(roleName);
    List<org.wso2.carbon.user.core.common.User> users = new ArrayList<>();
    org.wso2.carbon.user.core.common.User user = new org.wso2.carbon.user.core.common.User();
    user.setUserID(UUID.randomUUID().toString());
    user.setUserStoreDomain(userStoreDomain);
    user.setUsername("testUser");
    users.add(user);
    Map<String, Boolean> requiredAttributes = null;
    Map<String, String> attributes = new HashMap<String, String>() {

        {
            put(SCIMConstants.CommonSchemaConstants.ID_URI, "1");
        }
    };
    whenNew(GroupDAO.class).withAnyArguments().thenReturn(mockedGroupDAO);
    when(mockedGroupDAO.getGroupNameList(anyString(), anyString(), anyInt(), anyString())).thenReturn(list.toArray(new String[0]));
    mockStatic(IdentityUtil.class);
    when(mockedGroupDAO.isExistingGroup("testRole", 0)).thenReturn(true);
    when(mockedGroupDAO.getSCIMGroupAttributes(0, "testRole")).thenReturn(attributes);
    when(IdentityUtil.extractDomainFromName(anyString())).thenReturn(userStoreDomain);
    mockedUserStoreManager = PowerMockito.mock(AbstractUserStoreManager.class);
    MemberModifier.field(AbstractUserStoreManager.class, "userStoreManagerHolder").set(mockedUserStoreManager, new HashMap<String, UserStoreManager>());
    when(mockedUserStoreManager.isExistingRole(anyString(), anyBoolean())).thenReturn(true);
    when(mockedUserStoreManager.getRealmConfiguration()).thenReturn(mockRealmConfig);
    when(mockedUserStoreManager.getSecondaryUserStoreManager(anyString())).thenReturn(mockedUserStoreManager);
    when(mockedUserStoreManager.isSCIMEnabled()).thenReturn(true);
    when(mockedUserStoreManager.getUserListWithID(anyString(), anyString(), anyString())).thenReturn(users);
    when(mockedUserStoreManager.getRoleListOfUserWithID(anyString())).thenReturn(list);
    org.wso2.carbon.user.core.common.Group[] groupsArray = { buildUserCoreGroupResponse(roleName, "1234", "dummyDomain") };
    when(mockedUserStoreManager.listGroups(any(Condition.class), anyString(), anyInt(), anyInt(), anyString(), anyString())).thenReturn(Arrays.asList(groupsArray.clone()));
    when(mockedUserStoreManager.getGroupByGroupName(roleName, null)).thenReturn(buildUserCoreGroupResponse(roleName, "123456789", null));
    whenNew(RealmConfiguration.class).withAnyArguments().thenReturn(mockRealmConfig);
    when(mockRealmConfig.getAdminRoleName()).thenReturn("admin");
    when(mockRealmConfig.isPrimary()).thenReturn(false);
    when(mockRealmConfig.getUserStoreProperty(anyString())).thenReturn("value");
    when(mockRealmConfig.getEveryOneRoleName()).thenReturn("admin");
    when(mockIdentityUtil.extractDomainFromName(anyString())).thenReturn("value");
    Map<String, String> scimToLocalClaimsMap = new HashMap<>();
    scimToLocalClaimsMap.put("urn:ietf:params:scim:schemas:core:2.0:User:userName", "http://wso2.org/claims/username");
    mockStatic(SCIMCommonUtils.class);
    when(SCIMCommonUtils.getSCIMtoLocalMappings()).thenReturn(scimToLocalClaimsMap);
    SCIMUserManager scimUserManager = new SCIMUserManager(mockedUserStoreManager, mockedClaimManager);
    List<Object> roleList = scimUserManager.listGroupsWithGET(node, 1, 1, null, null, null, requiredAttributes);
    assertEquals(roleList.size(), 2);
}
Also used : Group(org.wso2.charon3.core.objects.Group) User(org.wso2.charon3.core.objects.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Condition(org.wso2.carbon.user.core.model.Condition) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) ExpressionNode(org.wso2.charon3.core.utils.codeutils.ExpressionNode) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 35 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class SCIMUserManagerTest method testFilterApplicationRolesWithDomainParam.

@Test(dataProvider = "applicationDomainWithFilters")
public void testFilterApplicationRolesWithDomainParam(String filter, String[] roles, Map<String, String> attributes) throws Exception {
    ExpressionNode node = new ExpressionNode(filter);
    Map<String, Boolean> requiredAttributes = null;
    AbstractUserStoreManager abstractUserStoreManager = mock(AbstractUserStoreManager.class);
    when(abstractUserStoreManager.getRoleNames(anyString(), anyInt(), anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(roles);
    when(abstractUserStoreManager.isExistingRole(anyString(), anyBoolean())).thenReturn(true);
    for (String role : roles) {
        when(abstractUserStoreManager.getGroupByGroupName(role, null)).thenReturn(buildUserCoreGroupResponse(role, "123456", "dummyDomain"));
    }
    mockStatic(UserCoreUtil.class);
    when(UserCoreUtil.isEveryoneRole("role", mockedRealmConfig)).thenReturn(false);
    whenNew(GroupDAO.class).withAnyArguments().thenReturn(mockedGroupDAO);
    when(mockedGroupDAO.isExistingGroup(anyString(), anyInt())).thenReturn(true);
    when(mockedGroupDAO.getSCIMGroupAttributes(anyInt(), anyString())).thenReturn(attributes);
    mockStatic(SCIMCommonUtils.class);
    when(SCIMCommonUtils.getSCIMGroupURL()).thenReturn("https://localhost:9443/scim2/Groups");
    SCIMUserManager scimUserManager = new SCIMUserManager(abstractUserStoreManager, mockedClaimManager);
    List<Object> roleList = scimUserManager.listGroupsWithGET(node, 1, null, null, null, "Application", requiredAttributes);
    // The first entry is the count of roles.
    roleList.remove(0);
    assertEquals("Application/MyApp", ((Group) roleList.get(0)).getDisplayName());
    assertEquals(roleList.size(), 1);
}
Also used : ExpressionNode(org.wso2.charon3.core.utils.codeutils.ExpressionNode) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)21 ArrayList (java.util.ArrayList)16 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)16 ExpressionNode (org.wso2.carbon.identity.core.model.ExpressionNode)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)12 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 ExpressionNode (org.wso2.charon3.core.utils.codeutils.ExpressionNode)7 Map (java.util.Map)6 IdentityException (org.wso2.carbon.identity.base.IdentityException)6 Attribute (org.wso2.charon3.core.attributes.Attribute)6 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)6 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)6 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)6 Connection (java.sql.Connection)5 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)5 JSONObject (org.json.JSONObject)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 Node (org.wso2.carbon.identity.core.model.Node)5 IdpSearchResult (org.wso2.carbon.idp.mgt.model.IdpSearchResult)5