Skip to content

Commit ffc45fa

Browse files
rickbijkerkRick Bijkerk
andauthored
expose an endpoint to debug trusted documents (#155)
adds a by default disabled endpoint to debug all the trusted documents that are currently in the cache ``` persisted_operations: # configures a '/internal/debug_trusted_documents' endpoint to print the persisted operations as json # Make sure you DONT expose this endpoint publicly if you enable this feature! enable_debug_endpoint: false ``` --------- Co-authored-by: Rick Bijkerk <[email protected]>
1 parent a1949ba commit ffc45fa

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

cmd/serve.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ldebruijn/graphql-protect/internal/business/rules/obfuscate_upstream_errors"
1212
"github.com/ldebruijn/graphql-protect/internal/business/schema"
1313
"github.com/ldebruijn/graphql-protect/internal/business/trusteddocuments"
14+
"github.com/ldebruijn/graphql-protect/internal/http/debug"
1415
"github.com/ldebruijn/graphql-protect/internal/http/middleware"
1516
"github.com/ldebruijn/graphql-protect/internal/http/proxy"
1617
"github.com/ldebruijn/graphql-protect/internal/http/readiness"
@@ -71,6 +72,7 @@ func httpServer(log *slog.Logger, cfg *config.Config, shutdown chan os.Signal) e
7172

7273
mux.Handle("/metrics", promhttp.Handler())
7374
mux.Handle("/internal/healthz/readiness", readiness.NewReadinessHandler())
75+
mux.Handle("/internal/debug_trusted_documents", debug.NewTrustedDocumentsDebugger(po, cfg.PersistedOperations.EnableDebugEndpoint))
7476
mux.Handle(cfg.Web.Path, mid(protectHandler))
7577

7678
api := http.Server{

docs/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ obfuscate_upstream_errors: true
6262
persisted_operations:
6363
# Enable or disable the feature, disabled by default
6464
enabled: false
65+
# configures a '/internal/debug_trusted_documents' endpoint to print the persisted operations as json
66+
# Make sure you DONT expose this endpoint publicly if you enable this feature!
67+
enable_debug_endpoint: false
6568
# Fail unknown operations, disable this feature to allow unknown operations to reach your GraphQL API
6669
reject_on_failure: true
6770
# Loader decides how persisted operations are loaded, see loader chapter for more details

docs/protections/trusted_documents.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ You can configure `graphql-protect` to enable Persisted Operations.
2929
persisted_operations:
3030
# Enable or disable the feature, disabled by default
3131
enabled: false
32+
# configures a '/internal/debug_trusted_documents' endpoint to print the persisted operations as json
33+
# Make sure you dont expose this ednpoint publicly if you enable this feature!
34+
enable_debug_endpoint: false
3235
# Fail unknown operations, disable this feature to allow unknown operations to reach your GraphQL API
3336
reject_on_failure: true
3437
# Loader decides how persisted operations are loaded, see loader chapter for more details

internal/app/config/config_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ obfuscate_upstream_errors: false
6767
6868
persisted_operations:
6969
enabled: true
70+
enable_debug_endpoint: true
7071
reject_on_failure: false
7172
loader:
7273
type: gcp
@@ -152,7 +153,8 @@ log:
152153
Host: "host",
153154
},
154155
PersistedOperations: trusteddocuments.Config{
155-
Enabled: true,
156+
Enabled: true,
157+
EnableDebugEndpoint: true,
156158
Loader: trusteddocuments.LoaderConfig{
157159
Type: "gcp",
158160
Location: "some-bucket",

internal/business/trusteddocuments/persisted_operations.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ type ErrorMessage struct {
5757
}
5858

5959
type Config struct {
60-
Enabled bool `yaml:"enabled"`
61-
RejectOnFailure bool `yaml:"reject_on_failure"`
62-
Loader LoaderConfig `yaml:"loader"`
60+
Enabled bool `yaml:"enabled"`
61+
EnableDebugEndpoint bool `yaml:"enable_debug_endpoint"`
62+
RejectOnFailure bool `yaml:"reject_on_failure"`
63+
Loader LoaderConfig `yaml:"loader"`
6364
}
6465

6566
func DefaultConfig() Config {
@@ -247,6 +248,10 @@ func (p *Handler) SwapHashForQuery(next http.Handler) http.Handler { // nolint:f
247248
return http.HandlerFunc(fn)
248249
}
249250

251+
func (p *Handler) GetTrustedDocuments() map[string]PersistedOperation {
252+
return p.cache
253+
}
254+
250255
func (p *Handler) Validate(validate func(operation string) gqlerror.List) []validation.Error {
251256
var errs []validation.Error
252257
for hash, operation := range p.cache {

internal/http/debug/debugging.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package debug
2+
3+
import (
4+
"encoding/json"
5+
"github.com/ldebruijn/graphql-protect/internal/business/trusteddocuments"
6+
"net/http"
7+
)
8+
9+
func NewTrustedDocumentsDebugger(po *trusteddocuments.Handler, enableDebugEndpoint bool) http.HandlerFunc {
10+
return func(w http.ResponseWriter, _ *http.Request) {
11+
if !enableDebugEndpoint {
12+
w.WriteHeader(http.StatusNotFound)
13+
} else {
14+
trustedDocuments := po.GetTrustedDocuments()
15+
16+
jsonData, err := json.MarshalIndent(trustedDocuments, "", " ")
17+
if err != nil {
18+
return
19+
}
20+
21+
w.Header().Set("Content-Type", "application/json")
22+
w.WriteHeader(http.StatusOK)
23+
24+
_, _ = w.Write(jsonData)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)