Skip to content

Commit

Permalink
fix: use registry client for schema loading
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 31, 2023
1 parent ea91483 commit 8ece82f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
29 changes: 21 additions & 8 deletions schema/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package schema

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/ory/x/otelx"

Check failure on line 11 in schema/handler.go

View workflow job for this annotation

GitHub Actions / Run tests and lints

File is not `goimports`-ed with -local github.com/ory (goimports)
"io"
"net/http"
"os"
Expand All @@ -33,6 +35,8 @@ type (
IdentityTraitsProvider
x.CSRFProvider
config.Provider
x.TracingProvider
x.HTTPClientProvider
}
Handler struct {
r handlerDependencies
Expand Down Expand Up @@ -102,7 +106,10 @@ type getIdentitySchema struct {
// 404: errorGeneric
// default: errorGeneric
func (h *Handler) getIdentitySchema(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ss, err := h.r.IdentityTraitsSchemas(r.Context())
ctx, span := h.r.Tracer(r.Context()).Tracer().Start(r.Context(), "schema.Handler.getIdentitySchema")
defer span.End()

ss, err := h.r.IdentityTraitsSchemas(ctx)
if err != nil {
h.r.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError.WithWrap(err)))
return
Expand All @@ -123,7 +130,7 @@ func (h *Handler) getIdentitySchema(w http.ResponseWriter, r *http.Request, ps h
}
}

src, err := ReadSchema(s)
src, err := h.ReadSchema(ctx, s)
if err != nil {
h.r.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("The file for this JSON Schema ID could not be found or opened. This is a configuration issue.").WithDebugf("%+v", err)))
return
Expand Down Expand Up @@ -190,6 +197,9 @@ type identitySchemasResponse struct {
// 200: identitySchemas
// default: errorGeneric
func (h *Handler) getAll(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx, span := h.r.Tracer(r.Context()).Tracer().Start(r.Context(), "schema.Handler.getAll")
defer span.End()

page, itemsPerPage := x.ParsePagination(r)

allSchemas, err := h.r.IdentityTraitsSchemas(r.Context())
Expand All @@ -203,7 +213,7 @@ func (h *Handler) getAll(w http.ResponseWriter, r *http.Request, ps httprouter.P
var ss IdentitySchemas
for k := range schemas {
schema := schemas[k]
src, err := ReadSchema(&schema)
src, err := h.ReadSchema(ctx, &schema)
if err != nil {
h.r.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("The file for this JSON Schema ID could not be found or opened. This is a configuration issue.").WithDebugf("%+v", err)))
return
Expand All @@ -226,22 +236,25 @@ func (h *Handler) getAll(w http.ResponseWriter, r *http.Request, ps httprouter.P
h.r.Writer().Write(w, r, ss)
}

func ReadSchema(schema *Schema) (src io.ReadCloser, err error) {
func (h *Handler) ReadSchema(ctx context.Context, schema *Schema) (src io.ReadCloser, err error) {
ctx, span := h.r.Tracer(ctx).Tracer().Start(ctx, "schema.Handler.ReadSchema")
defer otelx.End(span, &err)

if schema.URL.Scheme == "file" {
src, err = os.Open(schema.URL.Host + schema.URL.Path)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStack(herodot.ErrInternalServerError.WithWrap(err).WithReason("Unable to fetch identity schema."))
}
} else if schema.URL.Scheme == "base64" {
data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(schema.RawURL, "base64://"))
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStack(herodot.ErrInternalServerError.WithWrap(err).WithReason("Unable to fetch identity schema."))
}
src = io.NopCloser(strings.NewReader(string(data)))
} else {
resp, err := http.Get(schema.URL.String())
resp, err := h.r.HTTPClient(ctx).Get(schema.URL.String())
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.WithStack(herodot.ErrInternalServerError.WithWrap(err).WithReason("Unable to fetch identity schema."))
}
src = resp.Body
}
Expand Down
4 changes: 2 additions & 2 deletions schema/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ func TestHandler(t *testing.T) {
},
})

src, err := schema.ReadSchema(&schemas[0])
src, err := reg.SchemaHandler().ReadSchema(ctx, &schemas[0])
require.NoError(t, err)
defer src.Close()

src, err = schema.ReadSchema(&schemas[1])
src, err = reg.SchemaHandler().ReadSchema(ctx, &schemas[1])
require.NoError(t, err)
defer src.Close()
})
Expand Down

0 comments on commit 8ece82f

Please sign in to comment.