Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keeper: validate id against postgres repl slot name rules. #86

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type config struct {
var cfg config

func init() {
cmdKeeper.PersistentFlags().StringVar(&cfg.id, "id", "", "keeper id (must be unique in the cluster)")
cmdKeeper.PersistentFlags().StringVar(&cfg.id, "id", "", "keeper id (must be unique in the cluster and can contain only lower-case letters, numbers and the underscore character). If not provided a random id will be generated.")
cmdKeeper.PersistentFlags().StringVar(&cfg.etcdEndpoints, "etcd-endpoints", common.DefaultEtcdEndpoints, "a comma-delimited list of etcd endpoints")
cmdKeeper.PersistentFlags().StringVar(&cfg.dataDir, "data-dir", "", "data directory")
cmdKeeper.PersistentFlags().StringVar(&cfg.clusterName, "cluster-name", "", "cluster name")
Expand Down Expand Up @@ -701,6 +701,11 @@ func keeper(cmd *cobra.Command, args []string) {
log.Fatalf("cannot take exclusive lock on data dir %q: %v", cfg.dataDir, err)
}

if cfg.id != "" {
if !pg.IsValidReplSlotName(cfg.id) {
log.Fatalf("keeper id %q not valid. It can contain only lower-case letters, numbers and the underscore character", cfg.id)
}
}
id, err := getIDFromFile(cfg)
if err != nil {
log.Fatalf("error: %v", err)
Expand All @@ -716,11 +721,11 @@ func keeper(cmd *cobra.Command, args []string) {
u := uuid.NewV4()
id = fmt.Sprintf("%x", u[:4])
}
log.Infof("generated id: %s", id)
}
if err := saveIDToFile(cfg, id); err != nil {
log.Fatalf("error: %v", err)
}
log.Infof("generated id: %s", id)
}

log.Infof("id: %s", id)
Expand Down
8 changes: 8 additions & 0 deletions pkg/postgresql/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import (
"github.com/sorintlab/stolon/Godeps/_workspace/src/golang.org/x/net/context"
)

var (
ValidReplSlotName = regexp.MustCompile("^[a-z0-9_]+$")
)

func Exec(ctx context.Context, db *sql.DB, query string, args ...interface{}) (sql.Result, error) {
ch := make(chan struct {
res sql.Result
Expand Down Expand Up @@ -249,3 +253,7 @@ func GetTimelinesHistory(ctx context.Context, timeline uint64, replConnString st
}
return nil, fmt.Errorf("query returned 0 rows")
}

func IsValidReplSlotName(name string) bool {
return ValidReplSlotName.MatchString(name)
}
23 changes: 23 additions & 0 deletions pkg/postgresql/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,26 @@ func TestParseTimeLineHistory(t *testing.T) {
}

}

func TestValidReplSlotName(t *testing.T) {
tests := []struct {
name string
valid bool
}{
{"aaaaaaaa", true},
{"a12345aa", true},
{"_a1_2345aa_", true},
{"", false},
{"a-aaaaaaa", false},
{"_a1_-2345aa_", false},
{"ABC123", false},
{"$123", false},
}

for i, tt := range tests {
valid := IsValidReplSlotName(tt.name)
if valid != tt.valid {
t.Errorf("%d: replication slot name %q got valid: %t but wanted valid: %t", i, tt.name, valid, tt.valid)
}
}
}