Search in sources :

Example 1 with LanguageObject

use of org.teiid.query.sql.LanguageObject in project teiid by teiid.

the class TestAuthorizationValidationVisitor method helpTest.

private void helpTest(String sql, QueryMetadataInterface metadata, String[] expectedInaccesible, VDBMetaData vdb, DataPolicyMetadata... roles) throws QueryParserException, QueryResolverException, TeiidComponentException {
    QueryParser parser = QueryParser.getQueryParser();
    Command command = parser.parseCommand(sql);
    QueryResolver.resolveCommand(command, metadata);
    DataRolePolicyDecider dataRolePolicyDecider = createPolicyDecider(metadata, vdb, roles);
    // $NON-NLS-1$
    AuthorizationValidationVisitor visitor = new AuthorizationValidationVisitor(dataRolePolicyDecider, context);
    ValidatorReport report = Validator.validate(command, metadata, visitor);
    if (report.hasItems()) {
        ValidatorFailure firstFailure = report.getItems().iterator().next();
        // strings
        Set<String> expected = new HashSet<String>(Arrays.asList(expectedInaccesible));
        // elements
        Set<String> actual = new HashSet<String>();
        for (LanguageObject obj : firstFailure.getInvalidObjects()) {
            if (obj instanceof ElementSymbol) {
                actual.add(((ElementSymbol) obj).getName());
            } else {
                actual.add(obj.toString());
            }
        }
        assertEquals(expected, actual);
    } else if (expectedInaccesible.length > 0) {
        // $NON-NLS-1$
        fail("Expected inaccessible objects, but got none.");
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) QueryParser(org.teiid.query.parser.QueryParser) Command(org.teiid.query.sql.lang.Command) ValidatorFailure(org.teiid.query.validator.ValidatorFailure) ValidatorReport(org.teiid.query.validator.ValidatorReport) LanguageObject(org.teiid.query.sql.LanguageObject) HashSet(java.util.HashSet)

Example 2 with LanguageObject

use of org.teiid.query.sql.LanguageObject in project teiid by teiid.

the class TestOptimizer method helpGetCommand.

public static Command helpGetCommand(String sql, QueryMetadataInterface md, List<String> bindings) throws TeiidComponentException, TeiidProcessingException {
    // $NON-NLS-1$
    if (DEBUG)
        System.out.println("\n####################################\n" + sql);
    Command command = null;
    if (bindings != null && !bindings.isEmpty()) {
        command = TestResolver.helpResolveWithBindings(sql, md, bindings);
    } else {
        command = QueryParser.getQueryParser().parseCommand(sql);
        QueryResolver.resolveCommand(command, md);
    }
    ValidatorReport repo = Validator.validate(command, md);
    Collection<LanguageObject> failures = new ArrayList<LanguageObject>();
    repo.collectInvalidObjects(failures);
    if (failures.size() > 0) {
        // $NON-NLS-1$
        fail("Exception during validation (" + repo);
    }
    // rewrite
    command = QueryRewriter.rewrite(command, md, new CommandContext());
    return command;
}
Also used : CommandContext(org.teiid.query.util.CommandContext) Command(org.teiid.query.sql.lang.Command) ArrayList(java.util.ArrayList) ValidatorReport(org.teiid.query.validator.ValidatorReport) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 3 with LanguageObject

use of org.teiid.query.sql.LanguageObject in project teiid by teiid.

the class TestExpressionMappingVisitor method testSelectAlias1.

@Test
public void testSelectAlias1() {
    // $NON-NLS-1$
    ElementSymbol x = new ElementSymbol("y.x");
    // $NON-NLS-1$
    ElementSymbol y = new ElementSymbol("z.x");
    HashMap<ElementSymbol, ElementSymbol> map = new HashMap<ElementSymbol, ElementSymbol>();
    map.put(x, y);
    LanguageObject toMap = new Select(Arrays.asList(x));
    ExpressionMappingVisitor.mapExpressions(toMap, map);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Did not get expected mapped expression", " z.x", toMap.toString());
}
Also used : HashMap(java.util.HashMap) Select(org.teiid.query.sql.lang.Select) LanguageObject(org.teiid.query.sql.LanguageObject) Test(org.junit.Test)

Example 4 with LanguageObject

use of org.teiid.query.sql.LanguageObject in project teiid by teiid.

the class TestExpressionMappingVisitor method testSelectAlias.

@Test
public void testSelectAlias() {
    // $NON-NLS-1$
    ElementSymbol x = new ElementSymbol("y.x");
    // $NON-NLS-1$
    ElementSymbol y = new ElementSymbol("z.X");
    HashMap<Expression, Expression> map = new HashMap<Expression, Expression>();
    map.put(x, y);
    LanguageObject toMap = new Select(Arrays.asList(x));
    ExpressionMappingVisitor.mapExpressions(toMap, map);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Did not get expected mapped expression", " z.X AS x", toMap.toString());
}
Also used : HashMap(java.util.HashMap) Select(org.teiid.query.sql.lang.Select) LanguageObject(org.teiid.query.sql.LanguageObject) Test(org.junit.Test)

Example 5 with LanguageObject

use of org.teiid.query.sql.LanguageObject in project teiid by teiid.

the class DefaultAuthorizationValidator method validate.

@Override
public boolean validate(String[] originalSql, Command command, QueryMetadataInterface metadata, CommandContext commandContext, CommandType commandType) throws QueryValidatorException, TeiidComponentException {
    boolean modified = false;
    if (policyDecider != null && policyDecider.validateCommand(commandContext)) {
        if (ignoreUnathorizedInAsterisk(command, commandContext)) {
            Query query = (Query) command;
            HashMap<String, LanguageObject> map = null;
            for (Expression ex : query.getSelect().getSymbols()) {
                if (ex instanceof MultipleElementSymbol) {
                    MultipleElementSymbol mes = (MultipleElementSymbol) ex;
                    if (map == null) {
                        map = new HashMap<String, LanguageObject>();
                    }
                    for (Iterator<ElementSymbol> iter = mes.getElementSymbols().iterator(); iter.hasNext(); ) {
                        ElementSymbol es = iter.next();
                        Object metadataObject = es.getMetadataID();
                        if (metadataObject instanceof MultiSourceElement || metadataObject instanceof TempMetadataID) {
                            continue;
                        }
                        map.clear();
                        AuthorizationValidationVisitor.addToNameMap(metadataObject, es, map, commandContext.getMetadata());
                        Set<String> results = this.policyDecider.getInaccessibleResources(PermissionType.READ, map.keySet(), Context.QUERY, commandContext);
                        if (!results.isEmpty()) {
                            // remove from the select
                            iter.remove();
                            modified = true;
                        }
                    }
                }
            }
            if (query.getProjectedSymbols().isEmpty()) {
                throw new QueryValidatorException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31151));
            }
        }
        AuthorizationValidationVisitor visitor = new AuthorizationValidationVisitor(this.policyDecider, commandContext);
        Request.validateWithVisitor(visitor, metadata, command);
    }
    return modified;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Query(org.teiid.query.sql.lang.Query) TempMetadataID(org.teiid.query.metadata.TempMetadataID) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Expression(org.teiid.query.sql.symbol.Expression) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) LanguageObject(org.teiid.query.sql.LanguageObject) LanguageObject(org.teiid.query.sql.LanguageObject) MultiSourceElement(org.teiid.dqp.internal.process.multisource.MultiSourceElement)

Aggregations

LanguageObject (org.teiid.query.sql.LanguageObject)28 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)8 TeiidComponentException (org.teiid.core.TeiidComponentException)7 ArrayList (java.util.ArrayList)5 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)5 HashSet (java.util.HashSet)3 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)3 Command (org.teiid.query.sql.lang.Command)3 Criteria (org.teiid.query.sql.lang.Criteria)3 Select (org.teiid.query.sql.lang.Select)3 Expression (org.teiid.query.sql.symbol.Expression)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)2 MultiSourceElement (org.teiid.dqp.internal.process.multisource.MultiSourceElement)2 TempMetadataID (org.teiid.query.metadata.TempMetadataID)2 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)2 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)2