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-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 (
|
|
|
|
AL_QUERY int = iota + 1
|
|
|
|
AL_VARS
|
|
|
|
)
|
|
|
|
|
|
|
|
type Item struct {
|
2020-02-04 06:20:25 +01:00
|
|
|
Name string
|
|
|
|
key string
|
|
|
|
Query string
|
|
|
|
Vars json.RawMessage
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var q string
|
|
|
|
|
|
|
|
for i := 0; i < len(query); i++ {
|
|
|
|
c := query[i]
|
|
|
|
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' {
|
|
|
|
q = query
|
|
|
|
break
|
|
|
|
|
|
|
|
} else if c == '{' {
|
|
|
|
q = "query " + query
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
al.saveChan <- Item{
|
2020-02-04 06:20:25 +01:00
|
|
|
Comment: comment,
|
|
|
|
Query: q,
|
|
|
|
Vars: vars,
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (al *List) Load() ([]Item, error) {
|
|
|
|
var list []Item
|
2020-05-22 06:12:17 +02:00
|
|
|
varString := "variables"
|
2020-02-01 16:54:19 +01:00
|
|
|
|
|
|
|
b, err := ioutil.ReadFile(al.filepath)
|
|
|
|
if err != nil {
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 06:20:25 +01:00
|
|
|
var comment bytes.Buffer
|
2020-02-01 16:54:19 +01:00
|
|
|
var varBytes []byte
|
|
|
|
|
|
|
|
itemMap := make(map[string]struct{})
|
|
|
|
|
|
|
|
s, e, c := 0, 0, 0
|
|
|
|
ty := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
fq := false
|
|
|
|
|
|
|
|
if c == 0 && b[e] == '#' {
|
|
|
|
s = e
|
|
|
|
for e < len(b) && b[e] != '\n' {
|
|
|
|
e++
|
|
|
|
}
|
|
|
|
if (e - s) > 2 {
|
2020-02-04 06:20:25 +01:00
|
|
|
comment.Write(b[(s + 1):(e + 1)])
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e >= len(b) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if matchPrefix(b, e, "query") || matchPrefix(b, e, "mutation") {
|
|
|
|
if c == 0 {
|
|
|
|
s = e
|
|
|
|
}
|
|
|
|
ty = AL_QUERY
|
2020-05-22 06:12:17 +02:00
|
|
|
} else if matchPrefix(b, e, varString) {
|
2020-02-01 16:54:19 +01:00
|
|
|
if c == 0 {
|
2020-05-22 06:12:17 +02:00
|
|
|
s = e + len(varString) + 1
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
ty = AL_VARS
|
|
|
|
} else if b[e] == '{' {
|
|
|
|
c++
|
|
|
|
|
|
|
|
} else if b[e] == '}' {
|
|
|
|
c--
|
|
|
|
|
|
|
|
if c == 0 {
|
|
|
|
if ty == AL_QUERY {
|
|
|
|
fq = true
|
|
|
|
} else if ty == AL_VARS {
|
|
|
|
varBytes = b[s:(e + 1)]
|
|
|
|
}
|
|
|
|
ty = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fq {
|
|
|
|
query := string(b[s:(e + 1)])
|
|
|
|
name := QueryName(query)
|
|
|
|
key := strings.ToLower(name)
|
|
|
|
|
|
|
|
if _, ok := itemMap[key]; !ok {
|
|
|
|
v := Item{
|
2020-02-04 06:20:25 +01:00
|
|
|
Name: name,
|
|
|
|
key: key,
|
|
|
|
Query: query,
|
|
|
|
Vars: varBytes,
|
|
|
|
Comment: comment.String(),
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
list = append(list, v)
|
2020-02-04 06:20:25 +01:00
|
|
|
comment.Reset()
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
varBytes = nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
e++
|
|
|
|
if e >= len(b) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, v := range list {
|
2020-02-04 06:20:25 +01:00
|
|
|
cmtLines := strings.Split(v.Comment, "\n")
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, c := range cmtLines {
|
2020-05-22 22:49:54 +02:00
|
|
|
if c = strings.TrimSpace(c); c == "" {
|
2020-02-04 06:20:25 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := f.WriteString(fmt.Sprintf("# %s\n", c))
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(v.Vars) != 0 && !bytes.Equal(v.Vars, []byte("{}")) {
|
2020-05-07 16:27:22 +02:00
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
if err := jsn.Clear(&buf, v.Vars); err != nil {
|
|
|
|
return fmt.Errorf("failed to clean vars: %w", err)
|
|
|
|
}
|
|
|
|
vj := json.RawMessage(buf.Bytes())
|
|
|
|
|
|
|
|
vj, err = json.MarshalIndent(vj, "", " ")
|
2020-02-01 16:54:19 +01:00
|
|
|
if err != nil {
|
2020-05-07 16:27:22 +02:00
|
|
|
return fmt.Errorf("failed to marshal vars: %w", err)
|
2020-02-01 16:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.WriteString(fmt.Sprintf("variables %s\n\n", vj))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Query[0] == '{' {
|
|
|
|
_, err = f.WriteString(fmt.Sprintf("query %s\n\n", v.Query))
|
|
|
|
} else {
|
|
|
|
_, err = f.WriteString(fmt.Sprintf("%s\n\n", v.Query))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchPrefix(b []byte, i int, s string) bool {
|
|
|
|
if (len(b) - i) < len(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for n := 0; n < len(s); n++ {
|
|
|
|
if b[(i+n)] != s[n] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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 == '_'
|
|
|
|
}
|