(options string)
| 28 | } |
| 29 | |
| 30 | func sanitizeAlterRoleOptions(options string) string { |
| 31 | const AlterRolePrefix = `ALTER ROLE "any" WITH ` |
| 32 | |
| 33 | // Parse the options and discard them completely when incoherent. |
| 34 | parsed, err := pg_query.Parse(AlterRolePrefix + options) |
| 35 | if err != nil || len(parsed.GetStmts()) != 1 { |
| 36 | return "" |
| 37 | } |
| 38 | |
| 39 | // Rebuild the options list without invalid options. TODO(go1.21) TODO(slices) |
| 40 | orig := parsed.GetStmts()[0].GetStmt().GetAlterRoleStmt().GetOptions() |
| 41 | next := make([]*pg_query.Node, 0, len(orig)) |
| 42 | for i, option := range orig { |
| 43 | if strings.EqualFold(option.GetDefElem().GetDefname(), "password") { |
| 44 | continue |
| 45 | } |
| 46 | next = append(next, orig[i]) |
| 47 | } |
| 48 | if len(next) > 0 { |
| 49 | parsed.GetStmts()[0].GetStmt().GetAlterRoleStmt().Options = next |
| 50 | } else { |
| 51 | return "" |
| 52 | } |
| 53 | |
| 54 | // Turn the modified statement back into SQL and remove the ALTER ROLE portion. |
| 55 | sql, _ := pg_query.Deparse(parsed) |
| 56 | return strings.TrimPrefix(sql, AlterRolePrefix) |
| 57 | } |
| 58 | |
| 59 | // WriteUsersInPostgreSQL calls exec to create users that do not exist in |
| 60 | // PostgreSQL. Once they exist, it updates their options and passwords and |
no outgoing calls