Add ability to add comments to the allow list

This commit is contained in:
Vikram Rangnekar 2020-02-04 00:20:25 -05:00
parent 62fd1eac55
commit c85d379fe2
3 changed files with 53 additions and 27 deletions

View File

@ -20,9 +20,9 @@ const (
type Item struct { type Item struct {
Name string Name string
key string key string
URI string
Query string Query string
Vars json.RawMessage Vars json.RawMessage
Comment string
} }
type List struct { type List struct {
@ -105,7 +105,7 @@ func (al *List) IsPersist() bool {
return al.saveChan != nil return al.saveChan != nil
} }
func (al *List) Add(vars []byte, query, uri string) error { func (al *List) Set(vars []byte, query, comment string) error {
if al.saveChan == nil { if al.saveChan == nil {
return errors.New("allow.list is read-only") return errors.New("allow.list is read-only")
} }
@ -129,7 +129,7 @@ func (al *List) Add(vars []byte, query, uri string) error {
} }
al.saveChan <- Item{ al.saveChan <- Item{
URI: uri, Comment: comment,
Query: q, Query: q,
Vars: vars, Vars: vars,
} }
@ -149,7 +149,7 @@ func (al *List) Load() ([]Item, error) {
return list, nil return list, nil
} }
var uri string var comment bytes.Buffer
var varBytes []byte var varBytes []byte
itemMap := make(map[string]struct{}) itemMap := make(map[string]struct{})
@ -166,7 +166,7 @@ func (al *List) Load() ([]Item, error) {
e++ e++
} }
if (e - s) > 2 { if (e - s) > 2 {
uri = strings.TrimSpace(string(b[(s + 1):e])) comment.Write(b[(s + 1):(e + 1)])
} }
} }
@ -209,11 +209,12 @@ func (al *List) Load() ([]Item, error) {
v := Item{ v := Item{
Name: name, Name: name,
key: key, key: key,
URI: uri,
Query: query, Query: query,
Vars: varBytes, Vars: varBytes,
Comment: comment.String(),
} }
list = append(list, v) list = append(list, v)
comment.Reset()
} }
varBytes = nil varBytes = nil
@ -252,6 +253,9 @@ func (al *List) save(item Item) error {
} }
if index != -1 { if index != -1 {
if len(list[index].Comment) != 0 {
item.Comment = list[index].Comment
}
list[index] = item list[index] = item
} else { } else {
list = append(list, item) list = append(list, item)
@ -269,10 +273,30 @@ func (al *List) save(item Item) error {
}) })
for _, v := range list { for _, v := range list {
_, err := f.WriteString(fmt.Sprintf("# %s\n\n", v.URI)) cmtLines := strings.Split(v.Comment, "\n")
i := 0
for _, c := range cmtLines {
if c = strings.TrimSpace(c); len(c) == 0 {
continue
}
_, err := f.WriteString(fmt.Sprintf("# %s\n", c))
if err != nil { if err != nil {
return err return err
} }
i++
}
if i != 0 {
if _, err := f.WriteString("\n"); err != nil {
return err
}
} else {
if _, err := f.WriteString(fmt.Sprintf("# Query named %s\n\n", v.Name)); err != nil {
return err
}
}
if len(v.Vars) != 0 && !bytes.Equal(v.Vars, []byte("{}")) { if len(v.Vars) != 0 && !bytes.Equal(v.Vars, []byte("{}")) {
vj, err := json.MarshalIndent(v.Vars, "", " ") vj, err := json.MarshalIndent(v.Vars, "", " ")
@ -317,17 +341,13 @@ func QueryName(b string) string {
for i := 0; i < len(b); i++ { for i := 0; i < len(b); i++ {
switch { switch {
case state == 2 && b[i] == '{': case state == 2 && !isValidNameChar(b[i]):
return b[s:i]
case state == 2 && b[i] == ' ':
return b[s:i] return b[s:i]
case state == 1 && b[i] == '{': case state == 1 && b[i] == '{':
return "" return ""
case state == 1 && b[i] != ' ': case state == 1 && isValidNameChar(b[i]):
s = i s = i
state = 2 state = 2
case state == 1 && b[i] == ' ':
continue
case i != 0 && b[i] == ' ' && (b[i-1] == 'n' || b[i-1] == 'y'): case i != 0 && b[i] == ' ' && (b[i-1] == 'n' || b[i-1] == 'y'):
state = 1 state = 1
} }
@ -335,3 +355,7 @@ func QueryName(b string) string {
return "" return ""
} }
func isValidNameChar(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
}

View File

@ -21,7 +21,9 @@ func TestGQLName1(t *testing.T) {
func TestGQLName2(t *testing.T) { func TestGQLName2(t *testing.T) {
var q = ` var q = `
query hakuna_matata { query hakuna_matata
{
products( products(
distinct: [price] distinct: [price]
where: { id: { and: { greater_or_equals: 20, lt: 28 } } } where: { id: { and: { greater_or_equals: 20, lt: 28 } } }

View File

@ -242,7 +242,7 @@ func (c *coreContext) resolveSQL() ([]byte, *stmt, error) {
} }
if allowList.IsPersist() { if allowList.IsPersist() {
if err := allowList.Add(c.req.Vars, c.req.Query, c.req.ref); err != nil { if err := allowList.Set(c.req.Vars, c.req.Query, c.req.ref); err != nil {
return nil, nil, err return nil, nil, err
} }
} }