cli: refactor ec to allow setting hge api headers (#4347)

This commit is contained in:
Aravind 2020-04-09 15:00:47 +05:30 committed by GitHub
parent e1b0562fd8
commit 14b5d10dfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 19 deletions

View File

@ -45,6 +45,11 @@ const (
LastUpdateCheckFileName = "last_update_check_at"
)
const (
XHasuraAdminSecret = "X-Hasura-Admin-Secret"
XHasuraAccessKey = "X-Hasura-Access-Key"
)
// String constants
const (
StrTelemetryNotice = `Help us improve Hasura! The cli collects anonymized usage stats which
@ -204,6 +209,8 @@ type ExecutionContext struct {
MetadataDir string
// ConfigFile is the file where endpoint etc. are stored.
ConfigFile string
// HGE Headers, are the custom headers which can be passed to HGE API
HGEHeaders map[string]string
// Config is the configuration object storing the endpoint and admin secret
// information after reading from config file or env var.
@ -364,6 +371,10 @@ func (ec *ExecutionContext) setupInitTemplatesRepo() error {
return nil
}
func (ec *ExecutionContext) SetHGEHeaders(headers map[string]string) {
ec.HGEHeaders = headers
}
// Validate prepares the ExecutionContext ec and then validates the
// ExecutionDirectory to see if all the required files and directories are in
// place.
@ -435,6 +446,13 @@ func (ec *ExecutionContext) Validate() error {
ec.Telemetry.ServerUUID = ec.ServerUUID
ec.Logger.Debugf("server: uuid: %s", ec.ServerUUID)
// Set headers required for communicating with HGE
if ec.Config.AdminSecret != "" {
headers := map[string]string{
GetAdminSecretHeaderName(ec.Version): ec.Config.AdminSecret,
}
ec.SetHGEHeaders(headers)
}
return nil
}
@ -587,3 +605,10 @@ func (ec *ExecutionContext) setVersion() {
ec.Version = version.New()
}
}
func GetAdminSecretHeaderName(v *version.Version) string {
if v.ServerFeatureFlags.HasAccessKey {
return XHasuraAccessKey
}
return XHasuraAdminSecret
}

View File

@ -4,7 +4,6 @@ import (
"os"
"sync"
"github.com/hasura/graphql-engine/cli/migrate"
"github.com/hasura/graphql-engine/cli/util"
"github.com/gin-gonic/gin"
@ -110,7 +109,7 @@ func (o *ConsoleOptions) Run() error {
consoleAssetsVersion := templateProvider.GetAssetsVersion(o.EC.Version)
o.EC.Logger.Debugf("rendering console template [%s] with assets [%s]", consoleTemplateVersion, consoleAssetsVersion)
adminSecretHeader := migrate.GetAdminSecretHeaderName(o.EC.Version)
adminSecretHeader := cli.GetAdminSecretHeaderName(o.EC.Version)
consoleRouter, err := console.BuildConsoleRouter(templateProvider, consoleTemplateVersion, o.StaticDir, gin.H{
"apiHost": "http://" + o.Address,
"apiPort": o.APIPort,
@ -118,7 +117,7 @@ func (o *ConsoleOptions) Run() error {
"serverVersion": o.EC.Version.GetServerVersion(),
"dataApiUrl": o.EC.Config.ServerConfig.ParsedEndpoint.String(),
"dataApiVersion": "",
"hasAccessKey": adminSecretHeader == migrate.XHasuraAccessKey,
"hasAccessKey": adminSecretHeader == cli.XHasuraAccessKey,
"adminSecret": o.EC.Config.ServerConfig.AdminSecret,
"assetsVersion": consoleAssetsVersion,
"enableTelemetry": o.EC.GlobalConfig.EnableTelemetry,

View File

@ -9,8 +9,6 @@ import (
"strings"
"time"
version2 "github.com/hasura/graphql-engine/cli/version"
"github.com/hasura/graphql-engine/cli/metadata"
"github.com/hasura/graphql-engine/cli/metadata/actions"
"github.com/hasura/graphql-engine/cli/metadata/allowlist"
@ -148,8 +146,8 @@ func GetDataPath(ec *cli.ExecutionContext) *nurl.URL {
default:
q.Set("sslmode", "disable")
}
if GetAdminSecretHeaderName(ec.Version) != "" {
q.Add("headers", fmt.Sprintf("%s:%s", GetAdminSecretHeaderName(ec.Version), ec.Config.ServerConfig.AdminSecret))
for k, v := range ec.HGEHeaders {
q.Add("headers", fmt.Sprintf("%s:%s", k, v))
}
host.RawQuery = q.Encode()
return host
@ -177,18 +175,6 @@ func SetMetadataPluginsWithDir(ec *cli.ExecutionContext, drv *Migrate, dir ...st
drv.SetMetadataPlugins(plugins)
}
func GetAdminSecretHeaderName(v *version2.Version) string {
if v.ServerFeatureFlags.HasAccessKey {
return XHasuraAccessKey
}
return XHasuraAdminSecret
}
const (
XHasuraAdminSecret = "X-Hasura-Admin-Secret"
XHasuraAccessKey = "X-Hasura-Access-Key"
)
func GetFilePath(dir string) *nurl.URL {
host := &nurl.URL{
Scheme: "file",