2020-02-01 16:54:19 +01:00
|
|
|
package allow
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-05-31 08:34:28 +02:00
|
|
|
"log"
|
2020-02-01 16:54:19 +01:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-06-07 19:02:57 +02:00
|
|
|
"text/scanner"
|
2020-05-07 16:27:22 +02:00
|
|
|
|
2020-05-22 06:12:17 +02:00
|
|
|
"github.com/chirino/graphql/schema"
|
2020-05-07 16:27:22 +02:00
|
|
|
"github.com/dosco/super-graph/jsn"
|
2020-02-01 16:54:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-06-07 19:02:57 +02:00
|
|
|
expComment = iota + 1
|
|
|
|
expVar
|
|
|
|
expQuery
|
2020-02-01 16:54:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Item struct {
|
2020-02-04 06:20:25 +01:00
|
|
|
Name string
|
|
|
|
key string
|
|
|
|
Query string
|
2020-06-07 19:02:57 +02:00
|
|
|
Vars string
|
2020-02-04 06:20:25 +01:00
|
|
|
Comment string
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type List struct {
|
|
|
|
filepath string
|
|
|
|
saveChan chan Item
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
CreateIfNotExists bool
|
|
|
|
Persist bool
|
2020-05-31 08:34:28 +02:00
|
|
|
Log *log.Logger
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
func New(filename string, conf Config) (*List, error) {
|
2020-02-01 16:54:19 +01:00
|
|
|
al := List{}
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if filename != "" {
|
2020-04-11 08:45:06 +02:00
|
|
|
fp := filename
|
2020-02-01 16:54:19 +01:00
|
|
|
|
|
|
|
if _, err := os.Stat(fp); err == nil {
|
|
|
|
al.filepath = fp
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if al.filepath == "" {
|
2020-02-01 16:54:19 +01:00
|
|
|
fp := "./allow.list"
|
|
|
|
|
|
|
|
if _, err := os.Stat(fp); err == nil {
|
|
|
|
al.filepath = fp
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if al.filepath == "" {
|
2020-02-01 16:54:19 +01:00
|
|
|
fp := "./config/allow.list"
|
|
|
|
|
|
|
|
if _, err := os.Stat(fp); err == nil {
|
|
|
|
al.filepath = fp
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if al.filepath == "" {
|
2020-02-01 16:54:19 +01:00
|
|
|
if !conf.CreateIfNotExists {
|
|
|
|
return nil, errors.New("allow.list not found")
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if filename == "" {
|
2020-02-01 16:54:19 +01:00
|
|
|
al.filepath = "./config/allow.list"
|
|
|
|
} else {
|
2020-04-11 08:45:06 +02:00
|
|
|
al.filepath = filename
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
2020-05-31 08:34:28 +02:00
|
|
|
|
|
|
|
if file, err := os.OpenFile(al.filepath, os.O_RDONLY|os.O_CREATE, 0644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
file.Close()
|
|
|
|
}
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if conf.Persist {
|
|
|
|
al.saveChan = make(chan Item)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for v := range al.saveChan {
|
2020-05-31 08:34:28 +02:00
|
|
|
err := al.save(v)
|
|
|
|
|
|
|
|
if err != nil && conf.Log != nil {
|
|
|
|
conf.Log.Println("WRN allow list save:", err)
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &al, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (al *List) IsPersist() bool {
|
|
|
|
return al.saveChan != nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 06:20:25 +01:00
|
|
|
func (al *List) Set(vars []byte, query, comment string) error {
|
2020-02-01 16:54:19 +01:00
|
|
|
if al.saveChan == nil {
|
|
|
|
return errors.New("allow.list is read-only")
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if query == "" {
|
2020-02-01 16:54:19 +01:00
|
|
|
return errors.New("empty query")
|
|
|
|
}
|
|
|
|
|
|
|
|
al.saveChan <- Item{
|
2020-02-04 06:20:25 +01:00
|
|
|
Comment: comment,
|
2020-06-07 19:02:57 +02:00
|
|
|
Query: query,
|
|
|
|
Vars: string(vars),
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (al *List) Load() ([]Item, error) {
|
|
|
|
b, err := ioutil.ReadFile(al.filepath)
|
|
|
|
if err != nil {
|
2020-06-07 19:02:57 +02:00
|
|
|
return nil, err
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
return parse(string(b), al.filepath)
|
|
|
|
}
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-15 16:16:47 +02:00
|
|
|
func parse(b, filename string) ([]Item, error) {
|
2020-06-07 19:02:57 +02:00
|
|
|
var items []Item
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
var s scanner.Scanner
|
|
|
|
s.Init(strings.NewReader(b))
|
|
|
|
s.Filename = filename
|
|
|
|
s.Mode ^= scanner.SkipComments
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
var op, sp scanner.Position
|
|
|
|
var item Item
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
newComment := false
|
|
|
|
st := expComment
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
|
|
|
|
txt := s.TokenText()
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(txt, "/*"):
|
|
|
|
if st == expQuery {
|
|
|
|
v := b[sp.Offset:s.Pos().Offset]
|
|
|
|
item.Query = strings.TrimSpace(v[:strings.LastIndexByte(v, '}')+1])
|
|
|
|
items = append(items, item)
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
item = Item{Comment: strings.TrimSpace(txt[2 : len(txt)-2])}
|
|
|
|
sp = s.Pos()
|
|
|
|
st = expComment
|
|
|
|
newComment = true
|
|
|
|
|
|
|
|
case !newComment && strings.HasPrefix(txt, "#"):
|
|
|
|
if st == expQuery {
|
|
|
|
v := b[sp.Offset:s.Pos().Offset]
|
|
|
|
item.Query = strings.TrimSpace(v[:strings.LastIndexByte(v, '}')+1])
|
|
|
|
items = append(items, item)
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
item = Item{}
|
|
|
|
sp = s.Pos()
|
|
|
|
st = expComment
|
|
|
|
|
|
|
|
case strings.HasPrefix(txt, "variables"):
|
|
|
|
if st == expComment {
|
|
|
|
v := b[sp.Offset:s.Pos().Offset]
|
|
|
|
item.Comment = strings.TrimSpace(v[:strings.IndexByte(v, '\n')])
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
sp = s.Pos()
|
|
|
|
st = expVar
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
case isGraphQL(txt):
|
|
|
|
if st == expVar {
|
|
|
|
v := b[sp.Offset:s.Pos().Offset]
|
|
|
|
item.Vars = strings.TrimSpace(v[:strings.LastIndexByte(v, '}')+1])
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
sp = op
|
|
|
|
st = expQuery
|
2020-02-01 16:54:19 +01:00
|
|
|
|
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
op = s.Pos()
|
|
|
|
}
|
2020-02-01 16:54:19 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
if st == expQuery {
|
|
|
|
v := b[sp.Offset:s.Pos().Offset]
|
|
|
|
item.Query = strings.TrimSpace(v[:strings.LastIndexByte(v, '}')+1])
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range items {
|
|
|
|
items[i].Name = QueryName(items[i].Query)
|
|
|
|
items[i].key = strings.ToLower(items[i].Name)
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGraphQL(s string) bool {
|
|
|
|
return strings.HasPrefix(s, "query") ||
|
|
|
|
strings.HasPrefix(s, "mutation") ||
|
|
|
|
strings.HasPrefix(s, "subscription")
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (al *List) save(item Item) error {
|
2020-05-07 16:27:22 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2020-05-22 06:12:17 +02:00
|
|
|
qd := &schema.QueryDocument{}
|
|
|
|
|
|
|
|
if err := qd.Parse(item.Query); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
qd.WriteTo(&buf)
|
|
|
|
query := buf.String()
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
item.Name = QueryName(query)
|
2020-02-01 16:54:19 +01:00
|
|
|
item.key = strings.ToLower(item.Name)
|
|
|
|
|
2020-05-22 22:49:54 +02:00
|
|
|
if item.Name == "" {
|
2020-02-01 16:54:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := al.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
index := -1
|
|
|
|
|
|
|
|
for i, v := range list {
|
|
|
|
if strings.EqualFold(v.Name, item.Name) {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if index != -1 {
|
2020-05-22 22:49:54 +02:00
|
|
|
if list[index].Comment != "" {
|
2020-02-04 06:20:25 +01:00
|
|
|
item.Comment = list[index].Comment
|
|
|
|
}
|
2020-02-01 16:54:19 +01:00
|
|
|
list[index] = item
|
|
|
|
} else {
|
|
|
|
list = append(list, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(al.filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
return strings.Compare(list[i].key, list[j].key) == -1
|
|
|
|
})
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
for i, v := range list {
|
|
|
|
var vars string
|
|
|
|
if v.Vars != "" {
|
|
|
|
buf.Reset()
|
|
|
|
if err := jsn.Clear(&buf, []byte(v.Vars)); err != nil {
|
2020-02-04 06:20:25 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
vj := json.RawMessage(buf.Bytes())
|
2020-02-04 06:20:25 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
if vj, err = json.MarshalIndent(vj, "", " "); err != nil {
|
|
|
|
continue
|
2020-02-04 06:20:25 +01:00
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
vars = string(vj)
|
2020-02-04 06:20:25 +01:00
|
|
|
}
|
2020-06-07 19:02:57 +02:00
|
|
|
list[i].Vars = vars
|
|
|
|
list[i].Comment = strings.TrimSpace(v.Comment)
|
|
|
|
}
|
2020-02-04 06:20:25 +01:00
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
for _, v := range list {
|
|
|
|
if v.Comment != "" {
|
2020-06-09 01:28:22 +02:00
|
|
|
_, err = f.WriteString(fmt.Sprintf("/* %s */\n\n", v.Comment))
|
2020-02-04 06:20:25 +01:00
|
|
|
} else {
|
2020-06-09 01:28:22 +02:00
|
|
|
_, err = f.WriteString(fmt.Sprintf("/* %s */\n\n", v.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
if v.Vars != "" {
|
|
|
|
_, err = f.WriteString(fmt.Sprintf("variables %s\n\n", v.Vars))
|
2020-02-01 16:54:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
_, err = f.WriteString(fmt.Sprintf("%s\n\n", v.Query))
|
2020-02-01 16:54:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func QueryName(b string) string {
|
|
|
|
state, s := 0, 0
|
|
|
|
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
switch {
|
2020-02-04 06:20:25 +01:00
|
|
|
case state == 2 && !isValidNameChar(b[i]):
|
2020-02-01 16:54:19 +01:00
|
|
|
return b[s:i]
|
|
|
|
case state == 1 && b[i] == '{':
|
|
|
|
return ""
|
2020-02-04 06:20:25 +01:00
|
|
|
case state == 1 && isValidNameChar(b[i]):
|
2020-02-01 16:54:19 +01:00
|
|
|
s = i
|
|
|
|
state = 2
|
|
|
|
case i != 0 && b[i] == ' ' && (b[i-1] == 'n' || b[i-1] == 'y'):
|
|
|
|
state = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
2020-02-04 06:20:25 +01:00
|
|
|
|
|
|
|
func isValidNameChar(c byte) bool {
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
|
|
|
|
}
|