Skip to content

Commit

Permalink
Fixed CTE issue with auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydrocharged committed Nov 19, 2024
1 parent a20a5ab commit 6a19832
Show file tree
Hide file tree
Showing 4 changed files with 1,743 additions and 1,655 deletions.
2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -5010,7 +5010,7 @@ func (node *AliasedTableExpr) RemoveHints() *AliasedTableExpr {
}

type With struct {
Ctes []TableExpr
Ctes []*CommonTableExpr
Recursive bool
}

Expand Down
38 changes: 38 additions & 0 deletions go/vt/sqlparser/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,41 @@ func OverwriteAuthTargetNames(node SQLNode, targetNames []string, walkChildren b
}
return node
}

// WalkAuthNodes walks the node tree to find all nodes that implement AuthNode. This allows for fine-grained control of
// modifying AuthInformation within a tree.
func WalkAuthNodes(node SQLNode, f func(node AuthNode, authInfo AuthInformation)) {
if authNode, ok := node.(AuthNode); ok {
authInfo := authNode.GetAuthInformation()
f(authNode, authInfo)
}
if walkableNode, ok := node.(WalkableSQLNode); ok {
_ = walkableNode.walkSubtree(func(node SQLNode) (bool, error) {
if authNode, ok := node.(AuthNode); ok {
authInfo := authNode.GetAuthInformation()
f(authNode, authInfo)
}
return true, nil
})
}
}

// handleCTEAuth handles auth with CTEs, since we want to ignore any mentions of CTEs as far as auth is concerned. The
// CTE declarations will contain their own auth checks.
func handleCTEAuth(node SQLNode, with *With) {
if with == nil || node == nil {
return
}
aliases := make(map[string]struct{})
for _, cte := range with.Ctes {
aliases[cte.As.String()] = struct{}{}
}
WalkAuthNodes(node, func(node AuthNode, authInfo AuthInformation) {
for _, targetName := range authInfo.TargetNames {
if _, ok := aliases[targetName]; ok {
node.SetAuthType(AuthType_IGNORE)
break
}
}
})
}
Loading

0 comments on commit 6a19832

Please sign in to comment.