graphql-engine/cli/internal/metadataobject/network/network.go
Kali Vara Purushotham Santhati 4cf13762e2 cli: add support for network metadata object
Issue: https://github.com/hasura/graphql-engine/issues/7520

Problem: add support for network metadata object

Solution: add new network.yaml which consists of network metadata and in code add new network metadata object

Metadata.json
```
{
  "resource_version": 6,
  "metadata": {
    "version": 3,
    "sources": [
      {
        "name": "default",
        "kind": "postgres",
        "tables": [],
        "configuration": {
          "connection_info": {
            "use_prepared_statements": true,
            "database_url": {
              "from_env": "HASURA_GRAPHQL_DATABASE_URL"
            },
            "isolation_level": "read-committed",
            "pool_settings": {
              "connection_lifetime": 600,
              "retries": 1,
              "idle_timeout": 180,
              "max_connections": 50
            }
          }
        }
      }
    ],
    "network": {
      "tls_allowlist": [
        {
          "suffix": null,
          "permissions": [
            "self-signed"
          ],
          "host": "www.google.com"
        },
        {
          "suffix": null,
          "permissions": [
            "self-signed"
          ],
          "host": "play.golang.com"
        },
        {
          "suffix": null,
          "permissions": [
            "self-signed"
          ],
          "host": "hasura.io"
        }
      ]
    }
  }
}
```
network.yaml
```
tls_allowlist:
- suffix: null
  permissions:
  - self-signed
  host: www.google.com
- suffix: null
  permissions:
  - self-signed
  host: play.golang.com
- suffix: null
  permissions:
  - self-signed
  host: hasura.io

```

https://github.com/hasura/graphql-engine-mono/pull/2343

GitOrigin-RevId: ef508ae946dc5c7c5bcb4a9a3ccb982394ae8639
2021-09-22 08:12:46 +00:00

100 lines
2.1 KiB
Go

package network
import (
"io/ioutil"
"path/filepath"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
"github.com/sirupsen/logrus"
"github.com/hasura/graphql-engine/cli/v2"
"gopkg.in/yaml.v2"
)
type NetworkObject struct {
MetadataDir string
logger *logrus.Logger
}
func New(ec *cli.ExecutionContext, baseDir string) *NetworkObject {
return &NetworkObject{
MetadataDir: baseDir,
logger: ec.Logger,
}
}
func (o *NetworkObject) Validate() error {
return nil
}
func (o *NetworkObject) CreateFiles() error {
v := make([]interface{}, 0)
data, err := yaml.Marshal(v)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(o.MetadataDir, o.Filename()), data, 0644)
if err != nil {
return err
}
return nil
}
func (o *NetworkObject) Build(metadata *yaml.MapSlice) metadataobject.ErrParsingMetadataObject {
data, err := ioutil.ReadFile(filepath.Join(o.MetadataDir, o.Filename()))
if err != nil {
return o.error(err)
}
item := yaml.MapItem{
Key: o.Key(),
}
var obj yaml.MapSlice
err = yaml.Unmarshal(data, &obj)
if err != nil {
return o.error(err)
}
if len(obj) > 0 {
item.Value = obj
*metadata = append(*metadata, item)
}
return nil
}
func (o *NetworkObject) Export(metadata yaml.MapSlice) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
var network interface{}
for _, item := range metadata {
k, ok := item.Key.(string)
if !ok || k != o.Key() {
continue
}
network = item.Value
}
if network == nil {
o.logger.WithFields(logrus.Fields{
"object": o.Key(),
"reason": "not found in metadata",
}).Debugf("skipped building %s", o.Key())
return nil, nil
}
data, err := yaml.Marshal(network)
if err != nil {
return nil, o.error(err)
}
return map[string][]byte{
filepath.ToSlash(filepath.Join(o.MetadataDir, o.Filename())): data,
}, nil
}
func (o *NetworkObject) Key() string {
return "network"
}
func (o *NetworkObject) Filename() string {
return "network.yaml"
}
func (o *NetworkObject) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
return metadataobject.NewErrParsingMetadataObject(o, err, additionalContext...)
}