Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v20.11: fix(GraphQL): Fix auth-token propagation for HTTP endpoints resolved … #7251

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ func resolveWithAdminServer(gqlReq *schema.Request, r *http.Request,
ctx := metadata.NewIncomingContext(context.Background(), md)
ctx = x.AttachAccessJwt(ctx, r)
ctx = x.AttachRemoteIP(ctx, r)
ctx = x.AttachAuthToken(ctx, r)

return adminServer.Resolve(ctx, gqlReq)
}
Expand Down
37 changes: 37 additions & 0 deletions graphql/e2e/admin_auth/poorman_auth/admin_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package admin_auth

import (
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/dgraph-io/dgraph/x"
Expand Down Expand Up @@ -51,6 +53,20 @@ func TestAdminOnlyPoorManAuth(t *testing.T) {
common.SafelyUpdateGQLSchema(t, common.Alpha1HTTP, schema, headers)
}

func TestPoorManAuthOnAdminSchemaHttpEndpoint(t *testing.T) {
// without X-Dgraph-AuthToken should give error
require.Contains(t, makeAdminSchemaRequest(t, ""), "Invalid X-Dgraph-AuthToken")

// setting a wrong value for the token should still give error
require.Contains(t, makeAdminSchemaRequest(t, wrongAuthToken), "Invalid X-Dgraph-AuthToken")

// setting correct value for the token should successfully update the schema
oldCounter := common.RetryProbeGraphQL(t, common.Alpha1HTTP).SchemaUpdateCounter
require.JSONEq(t, `{"data":{"code":"Success","message":"Done"}}`, makeAdminSchemaRequest(t,
authToken))
common.AssertSchemaUpdateCounterIncrement(t, common.Alpha1HTTP, oldCounter)
}

func assertAuthTokenError(t *testing.T, schema string, headers http.Header) {
resp := common.RetryUpdateGQLSchema(t, common.Alpha1HTTP, schema, headers)
require.Equal(t, x.GqlErrorList{{
Expand All @@ -59,3 +75,24 @@ func assertAuthTokenError(t *testing.T, schema string, headers http.Header) {
}}, resp.Errors)
require.Nil(t, resp.Data)
}

func makeAdminSchemaRequest(t *testing.T, authTokenValue string) string {
schema := `type Person {
id: ID!
name: String! @id
}`
req, err := http.NewRequest(http.MethodPost, common.GraphqlAdminURL+"/schema",
strings.NewReader(schema))
require.NoError(t, err)
if authTokenValue != "" {
req.Header.Set(authTokenHeader, authTokenValue)
}

resp, err := (&http.Client{}).Do(req)
require.NoError(t, err)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)

return string(b)
}