Skip to content

Commit

Permalink
feat(traefik): add a local plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao authored and mergify[bot] committed May 12, 2022
1 parent 7d122b6 commit 1920f9b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions traefik/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
traefik:
traefik --configfile=traefik.yaml
19 changes: 19 additions & 0 deletions traefik/dynamic_conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
http:
routers:
graphql-server-entrypoint:
service: graphql-server-service
entrypoints:
- graphql-server-entrypoint
rule: Host(`localhost`)
middlewares:
- my-traefik-plugin-disable-graphql-introspection
services:
graphql-server-service:
loadBalancer:
servers:
- url: http://localhost:16020/
middlewares:
my-traefik-plugin-disable-graphql-introspection:
plugin:
traefik-plugin-disable-graphql-introspection:
GraphQLPath: /v1/graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
displayName: Disable GraphQL Introspection Plugin
type: middleware
import: github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection
summary: 'Disable GraphQL Introspection'
testData:
GraphQLPath: /graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection

go 1.17
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package traefik_plugin_disable_graphql_introspection

import (
"bytes"
"context"
"io/ioutil"
"log"
"net/http"
"strings"
)

type Config struct {
GraphQLPath string
}

func CreateConfig() *Config {
return &Config{
GraphQLPath: "/graphql",
}
}

type DisableGraphQLIntrospection struct {
next http.Handler
name string
graphQLPath string
}

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &DisableGraphQLIntrospection{
next: next,
name: name,
graphQLPath: config.GraphQLPath,
}, nil
}

func (d *DisableGraphQLIntrospection) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading body: %v", err)
rw.WriteHeader(http.StatusBadRequest)
rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(`{
"error": {
"code": 400,
"message": "Failed to read request body."
}
}`))
return
}
if r.Method == "POST" && r.URL.Path == d.graphQLPath {
if strings.Contains(string(body), "__schema") || strings.Contains(string(body), "__type") {
rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(`{
"errors": [
{
"message": "GraphQL introspection is not allowed."
}
]
}`))
return
}
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
d.next.ServeHTTP(rw, r)
}
15 changes: 15 additions & 0 deletions traefik/traefik.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
entryPoints:
graphql-server-entrypoint:
address: :16022
api:
insecure: true
dashboard: true
providers:
file:
filename: dynamic_conf.yaml
log:
level: DEBUG
experimental:
localPlugins:
traefik-plugin-disable-graphql-introspection:
modulename: github.com/Hongbo-Miao/traefik-plugin-disable-graphql-introspection

0 comments on commit 1920f9b

Please sign in to comment.