2020-02-24 19:14:46 +03:00
|
|
|
package allowlist
|
|
|
|
|
|
|
|
import (
|
2022-03-10 11:12:55 +03:00
|
|
|
"bytes"
|
2020-02-24 19:14:46 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-10-21 19:56:14 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2021-07-23 12:49:44 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-03-10 11:12:55 +03:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-02-24 19:14:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type AllowListConfig struct {
|
|
|
|
MetadataDir string
|
|
|
|
|
|
|
|
logger *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ec *cli.ExecutionContext, baseDir string) *AllowListConfig {
|
|
|
|
return &AllowListConfig{
|
|
|
|
MetadataDir: baseDir,
|
|
|
|
logger: ec.Logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AllowListConfig) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AllowListConfig) CreateFiles() error {
|
2022-10-21 19:56:14 +03:00
|
|
|
var op errors.Op = "allowlist.AllowListConfig.CreateFiles"
|
2020-02-24 19:14:46 +03:00
|
|
|
v := make([]interface{}, 0)
|
|
|
|
data, err := yaml.Marshal(v)
|
|
|
|
if err != nil {
|
2022-10-21 19:56:14 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-07-23 12:49:44 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(a.MetadataDir, a.Filename()), data, 0644)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
2022-10-21 19:56:14 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-19 15:51:29 +03:00
|
|
|
func (a *AllowListConfig) Build() (map[string]interface{}, error) {
|
2022-10-21 19:56:14 +03:00
|
|
|
var op errors.Op = "allowlist.AllowListConfig.Build"
|
2022-06-15 18:44:36 +03:00
|
|
|
data, err := metadataobject.ReadMetadataFile(filepath.Join(a.MetadataDir, a.Filename()))
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
2022-10-21 19:56:14 +03:00
|
|
|
return nil, errors.E(op, a.error(err))
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
var obj []yaml.Node
|
|
|
|
err = yaml.NewDecoder(bytes.NewReader(data)).Decode(&obj)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
2022-10-21 19:56:14 +03:00
|
|
|
return nil, errors.E(op, errors.KindBadInput, a.error(err))
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
return map[string]interface{}{a.Key(): obj}, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2022-10-19 15:51:29 +03:00
|
|
|
func (a *AllowListConfig) Export(metadata map[string]yaml.Node) (map[string][]byte, error) {
|
2022-10-21 19:56:14 +03:00
|
|
|
var op errors.Op = "allowlist.AllowListConfig.Export"
|
|
|
|
b, err := metadataobject.DefaultExport(a, metadata, a.error, metadataobject.DefaultObjectTypeSequence)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.E(op, a.error(err))
|
|
|
|
}
|
|
|
|
return b, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (a *AllowListConfig) Key() string {
|
2022-03-10 11:12:55 +03:00
|
|
|
return metadataobject.AllowListKey
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (a *AllowListConfig) Filename() string {
|
|
|
|
return "allow_list.yaml"
|
|
|
|
}
|
|
|
|
|
2022-10-20 07:34:44 +03:00
|
|
|
func (a *AllowListConfig) GetFiles() ([]string, error) {
|
2022-10-21 19:56:14 +03:00
|
|
|
var op errors.Op = "allowlist.AllowListConfig.GetFiles"
|
2021-12-09 20:25:54 +03:00
|
|
|
rootFile := filepath.Join(a.BaseDirectory(), a.Filename())
|
|
|
|
files, err := metadataobject.DefaultGetFiles(rootFile)
|
|
|
|
if err != nil {
|
2022-10-21 19:56:14 +03:00
|
|
|
return nil, errors.E(op, a.error(err))
|
2021-12-09 20:25:54 +03:00
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 09:57:39 +03:00
|
|
|
func (a *AllowListConfig) WriteDiff(opts metadataobject.WriteDiffOpts) error {
|
2022-10-21 19:56:14 +03:00
|
|
|
var op errors.Op = "allowlist.AllowListConfig.WriteDiff"
|
2021-12-09 20:25:54 +03:00
|
|
|
err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: a, WriteDiffOpts: opts})
|
|
|
|
if err != nil {
|
2022-10-21 19:56:14 +03:00
|
|
|
return errors.E(op, a.error(err))
|
2021-12-09 20:25:54 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AllowListConfig) BaseDirectory() string {
|
|
|
|
return a.MetadataDir
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (a *AllowListConfig) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
|
|
|
|
return metadataobject.NewErrParsingMetadataObject(a, err, additionalContext...)
|
2021-06-18 20:38:29 +03:00
|
|
|
}
|