From 21c0b8b1c761fb01690d51deb322f52cc3985ad0 Mon Sep 17 00:00:00 2001 From: Apoorv Vardhan Date: Mon, 8 Jun 2020 19:59:05 +0530 Subject: [PATCH 1/3] Added support for custom names in headers --- graphql/schema/rules.go | 8 ++++++-- graphql/schema/schemagen.go | 6 +++++- graphql/schema/wrappers.go | 16 ++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/graphql/schema/rules.go b/graphql/schema/rules.go index 8dbd41eac0b..8d257c31b15 100644 --- a/graphql/schema/rules.go +++ b/graphql/schema/rules.go @@ -1526,9 +1526,13 @@ func customDirectiveValidation(sch *ast.Schema, headers := http.Header{} if secretHeaders != nil { for _, h := range secretHeaders.Children { + key := strings.Split(h.Value.Raw, ":") + if len(key) != 2 { + continue + } // We try and fetch the value from the stored secrets. - val := secrets[h.Value.Raw] - headers.Add(h.Value.Raw, string(val)) + val := secrets[key[1]] + headers.Add(key[0], string(val)) } } if err := validateRemoteGraphql(&remoteGraphqlMetadata{ diff --git a/graphql/schema/schemagen.go b/graphql/schema/schemagen.go index 8def8805b89..3825496d912 100644 --- a/graphql/schema/schemagen.go +++ b/graphql/schema/schemagen.go @@ -252,7 +252,11 @@ func getAllowedHeaders(sch *ast.Schema, definitions []string) string { return } for _, h := range forwardHeaders.Children { - headers[h.Value.Raw] = struct{}{} + key := strings.Split(h.Value.Raw, ":") + if len(key) != 2 { + continue + } + headers[key[1]] = struct{}{} } } diff --git a/graphql/schema/wrappers.go b/graphql/schema/wrappers.go index 96366c4f542..12adb53cae0 100644 --- a/graphql/schema/wrappers.go +++ b/graphql/schema/wrappers.go @@ -847,8 +847,12 @@ func getCustomHTTPConfig(f *field, isQueryOrMutation bool) (FieldHTTPConfig, err if secretHeaders != nil { hc.RLock() for _, h := range secretHeaders.Children { - val := string(hc.secrets[h.Value.Raw]) - fconf.ForwardHeaders.Set(h.Value.Raw, val) + key := strings.Split(h.Value.Raw, ":") + if len(key) != 2 { + continue + } + val := string(hc.secrets[key[1]]) + fconf.ForwardHeaders.Set(key[0], val) } hc.RUnlock() } @@ -857,8 +861,12 @@ func getCustomHTTPConfig(f *field, isQueryOrMutation bool) (FieldHTTPConfig, err if forwardHeaders != nil { for _, h := range forwardHeaders.Children { // We would override the header if it was also specified as part of secretHeaders. - reqHeaderVal := f.op.header.Get(h.Value.Raw) - fconf.ForwardHeaders.Set(h.Value.Raw, reqHeaderVal) + key := strings.Split(h.Value.Raw, ":") + if len(key) != 2 { + continue + } + reqHeaderVal := f.op.header.Get(key[1]) + fconf.ForwardHeaders.Set(key[0], reqHeaderVal) } } From 6d92d4fadeb763c2fb6e042add7e7ac77eee7332 Mon Sep 17 00:00:00 2001 From: Apoorv Vardhan Date: Mon, 8 Jun 2020 20:34:58 +0530 Subject: [PATCH 2/3] Continue to support old header definition --- graphql/schema/rules.go | 6 +++--- graphql/schema/schemagen.go | 2 +- graphql/schema/wrappers.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/graphql/schema/rules.go b/graphql/schema/rules.go index 8d257c31b15..7110df47e1c 100644 --- a/graphql/schema/rules.go +++ b/graphql/schema/rules.go @@ -1528,11 +1528,11 @@ func customDirectiveValidation(sch *ast.Schema, for _, h := range secretHeaders.Children { key := strings.Split(h.Value.Raw, ":") if len(key) != 2 { - continue + key = []string{h.Value.Raw, h.Value.Raw} } // We try and fetch the value from the stored secrets. - val := secrets[key[1]] - headers.Add(key[0], string(val)) + val := secrets[key[0]] + headers.Add(key[1], string(val)) } } if err := validateRemoteGraphql(&remoteGraphqlMetadata{ diff --git a/graphql/schema/schemagen.go b/graphql/schema/schemagen.go index 3825496d912..72d203661dc 100644 --- a/graphql/schema/schemagen.go +++ b/graphql/schema/schemagen.go @@ -254,7 +254,7 @@ func getAllowedHeaders(sch *ast.Schema, definitions []string) string { for _, h := range forwardHeaders.Children { key := strings.Split(h.Value.Raw, ":") if len(key) != 2 { - continue + key = []string{h.Value.Raw, h.Value.Raw} } headers[key[1]] = struct{}{} } diff --git a/graphql/schema/wrappers.go b/graphql/schema/wrappers.go index 12adb53cae0..690c5616136 100644 --- a/graphql/schema/wrappers.go +++ b/graphql/schema/wrappers.go @@ -849,7 +849,7 @@ func getCustomHTTPConfig(f *field, isQueryOrMutation bool) (FieldHTTPConfig, err for _, h := range secretHeaders.Children { key := strings.Split(h.Value.Raw, ":") if len(key) != 2 { - continue + key = []string{h.Value.Raw, h.Value.Raw} } val := string(hc.secrets[key[1]]) fconf.ForwardHeaders.Set(key[0], val) @@ -863,7 +863,7 @@ func getCustomHTTPConfig(f *field, isQueryOrMutation bool) (FieldHTTPConfig, err // We would override the header if it was also specified as part of secretHeaders. key := strings.Split(h.Value.Raw, ":") if len(key) != 2 { - continue + key = []string{h.Value.Raw, h.Value.Raw} } reqHeaderVal := f.op.header.Get(key[1]) fconf.ForwardHeaders.Set(key[0], reqHeaderVal) From ab5dff3b3b0ec8f4e39a8e3f73f0f174c5b599c9 Mon Sep 17 00:00:00 2001 From: Apoorv Vardhan Date: Tue, 9 Jun 2020 16:16:05 +0530 Subject: [PATCH 3/3] Addressed comments --- graphql/schema/rules.go | 11 ++++++++--- graphql/schema/schemagen.go | 4 +++- graphql/schema/wrappers.go | 8 ++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/graphql/schema/rules.go b/graphql/schema/rules.go index 7110df47e1c..8c48397b192 100644 --- a/graphql/schema/rules.go +++ b/graphql/schema/rules.go @@ -1527,12 +1527,17 @@ func customDirectiveValidation(sch *ast.Schema, if secretHeaders != nil { for _, h := range secretHeaders.Children { key := strings.Split(h.Value.Raw, ":") - if len(key) != 2 { + if len(key) == 1 { key = []string{h.Value.Raw, h.Value.Raw} + } else if len(key) > 2 { + return gqlerror.ErrorPosf(graphql.Position, + "Type %s; Field %s; secretHeaders in @custom directive should be of the form 'remote_headername:local_headername' or just 'headername'"+ + ", found: `%s`.", + typ.Name, field.Name, h.Value.Raw) } // We try and fetch the value from the stored secrets. - val := secrets[key[0]] - headers.Add(key[1], string(val)) + val := secrets[key[1]] + headers.Add(key[0], string(val)) } } if err := validateRemoteGraphql(&remoteGraphqlMetadata{ diff --git a/graphql/schema/schemagen.go b/graphql/schema/schemagen.go index 72d203661dc..87b6e9f77dc 100644 --- a/graphql/schema/schemagen.go +++ b/graphql/schema/schemagen.go @@ -253,8 +253,10 @@ func getAllowedHeaders(sch *ast.Schema, definitions []string) string { } for _, h := range forwardHeaders.Children { key := strings.Split(h.Value.Raw, ":") - if len(key) != 2 { + if len(key) == 1 { key = []string{h.Value.Raw, h.Value.Raw} + } else if len(key) > 2 { + continue } headers[key[1]] = struct{}{} } diff --git a/graphql/schema/wrappers.go b/graphql/schema/wrappers.go index 690c5616136..b243ea417be 100644 --- a/graphql/schema/wrappers.go +++ b/graphql/schema/wrappers.go @@ -848,8 +848,10 @@ func getCustomHTTPConfig(f *field, isQueryOrMutation bool) (FieldHTTPConfig, err hc.RLock() for _, h := range secretHeaders.Children { key := strings.Split(h.Value.Raw, ":") - if len(key) != 2 { + if len(key) == 1 { key = []string{h.Value.Raw, h.Value.Raw} + } else if len(key) > 2 { + continue } val := string(hc.secrets[key[1]]) fconf.ForwardHeaders.Set(key[0], val) @@ -862,8 +864,10 @@ func getCustomHTTPConfig(f *field, isQueryOrMutation bool) (FieldHTTPConfig, err for _, h := range forwardHeaders.Children { // We would override the header if it was also specified as part of secretHeaders. key := strings.Split(h.Value.Raw, ":") - if len(key) != 2 { + if len(key) == 1 { key = []string{h.Value.Raw, h.Value.Raw} + } else if len(key) > 2 { + continue } reqHeaderVal := f.op.header.Get(key[1]) fconf.ForwardHeaders.Set(key[0], reqHeaderVal)