Special case for search_path in user options.

- search_path accepts a list of values that cannot be quoted, as
  quoting would make PostgreSQL interpret the result as a single
  value. Since we require quoting of values with commas in the
  operator's configMap in order to avoid confusing them with the
  separate map entities, we need to strip those quotes before
  passing the value to PostgreSQL.
- make ftm run
This commit is contained in:
Oleksii Kliukin 2017-11-08 15:38:43 +01:00
parent 2079d811b4
commit 6b2f5071f7
3 changed files with 31 additions and 17 deletions

View File

@ -1,9 +1,9 @@
package config
import (
"testing"
"reflect"
"fmt"
"reflect"
"testing"
)
var getMapPairsFromStringTest = []struct {
@ -29,4 +29,3 @@ func TestGetMapPairsFromString(t *testing.T) {
}
}
}

View File

@ -169,8 +169,8 @@ func processField(value string, field reflect.Value) error {
return nil
}
type parserState int
const (
Plain parserState = iota
DoubleQuoted
@ -185,7 +185,7 @@ func getMapPairsFromString(value string) (pairs []string , err error) {
state := Plain
var start, quote int
for i, ch := range(strings.Split(value, "")) {
for i, ch := range strings.Split(value, "") {
if ch == `"` {
if state == Plain {
state = DoubleQuoted

View File

@ -173,8 +173,8 @@ func produceAlterStmt(user spec.PgUser) string {
func produceAlterRoleSetStmts(user spec.PgUser) []string {
result := make([]string, 0)
result = append(result, fmt.Sprintf(alterRoleResetAllSQL, user.Name))
for key, value := range user.Parameters {
result = append(result, fmt.Sprintf(alterRoleSetSQL, user.Name, key, quoteValue(value)))
for name, value := range user.Parameters {
result = append(result, fmt.Sprintf(alterRoleSetSQL, user.Name, name, quoteParameterValue(name, value)))
}
return result
}
@ -193,10 +193,25 @@ func quoteMemberList(user spec.PgUser) string {
}
// quoteVal quotes values to be used at ALTER ROLE SET param = value if necessary
func quoteValue(val string) string {
if (strings.HasPrefix(val, `"`) && strings.HasSuffix(val, `"`)) ||
(strings.HasPrefix(val, `'`) && strings.HasSuffix(val, `'`)) {
func quoteParameterValue(name, val string) string {
start := val[0]
end := val[len(val)-1]
if name == "search_path" {
// strip single quotes from the search_path. Those are required in the YAML configuration
// to quote values containing commas, as otherwise NewFromMap would treat each comma-separated
// part of such string as a separate map entry. However, a search_path is interpreted as a list
// only if it is not quoted, otherwise it is treated as a single value. Therefore, we strip
// single quotes here. Note that you can still use double quotes in order to escape schemas
// containing spaces (but something more complex, like double quotes inside double quotes or spaces
// in the schema name would break the parsing code in the operator.)
if start == '\'' && end == '\'' {
return fmt.Sprintf("%s", val[1:len(val)-1])
} else {
return val
}
return fmt.Sprintf(`"%s"`, strings.Trim(val, " "))
}
if (start == '"' && end == '"') || (start == '\'' && end == '\'') {
return val
}
return fmt.Sprintf(`'%s'`, strings.Trim(val, " "))
}