Skip to content

Commit

Permalink
Start validation work in shim API
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Jul 9, 2024
1 parent e184eda commit d0ed227
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 25 deletions.
2 changes: 1 addition & 1 deletion hack/ccp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ gen-web:
npm --prefix=$(CURDIR)/web run build

gen-shim: # @HELP Generate Go server boilerplate from OpenAPIv3 spec.
gen-shim: SPEC_COMMIT := b0b9af20
gen-shim: SPEC_COMMIT := 65e58266
gen-shim: SPEC_URL := https://raw.githubusercontent.com/artefactual-labs/archivematica-api-specification/$(SPEC_COMMIT)/typespec/tsp-output/@typespec/openapi3/openapi.v1.yaml
gen-shim: $(OAPI_CODEGEN)
@echo "Downloading Archivematica API specification ($(SPEC_COMMIT))..."
Expand Down
3 changes: 3 additions & 0 deletions hack/ccp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/ikawaha/httpcheck v1.12.3
github.com/jellydator/ttlcache/v3 v3.2.0
github.com/mikespook/gearman-go v0.0.0-20220520031403-2a518e866145
github.com/oapi-codegen/runtime v1.1.1
Expand Down Expand Up @@ -83,6 +84,8 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/itchyny/gojq v0.12.16 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions hack/ccp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,16 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA=
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/ikawaha/httpcheck v1.12.3 h1:OCDe3yCTZWKhubT5rNOef26ulykUxQRx/GqSGm5lYMs=
github.com/ikawaha/httpcheck v1.12.3/go.mod h1:K+Oyw7JUcLcRGOvIj2kTEzwPpxMqpXaafHiKb7WsJ4Q=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps=
github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g=
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
github.com/jdkato/prose v1.2.1 h1:Fp3UnJmLVISmlc57BgKUzdjr0lOtjqTZicL3PaYy6cU=
github.com/jdkato/prose v1.2.1/go.mod h1:AiRHgVagnEx2JbQRQowVBKjG0bcs/vtkGCH1dYAL1rA=
github.com/jellydator/ttlcache/v3 v3.2.0 h1:6lqVJ8X3ZaUwvzENqPAobDsXNExfUJd61u++uW8a3LE=
Expand Down Expand Up @@ -259,8 +265,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/microsoft/kiota-abstractions-go v1.6.0 h1:qbGBNMU0/o5myKbikCBXJFohVCFrrpx2cO15Rta2WyA=
github.com/microsoft/kiota-abstractions-go v1.6.0/go.mod h1:7YH20ZbRWXGfHSSvdHkdztzgCB9mRdtFx13+hrYIEpo=
Expand Down Expand Up @@ -340,6 +346,8 @@ github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSz
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
Expand Down
2 changes: 1 addition & 1 deletion hack/ccp/internal/cmd/servercmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *Server) Run() error {

if s.config.shim.Enabled {
s.logger.V(1).Info("Creating Archivematica API shim.")
s.shim = shim.NewServer(s.logger.WithName("shim"), s.config.shim)
s.shim = shim.NewServer(s.logger.WithName("shim"), s.config.shim, s.store)
if err := s.shim.Run(); err != nil {
return fmt.Errorf("error creating shim API: %v", err)
}
Expand Down
16 changes: 14 additions & 2 deletions hack/ccp/internal/shim/gen/shim.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions hack/ccp/internal/shim/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package shim

import (
"context"
"net/http"

"github.com/artefactual/archivematica/hack/ccp/internal/shim/gen"
"github.com/artefactual/archivematica/hack/ccp/internal/store"
)

const (
archivematicaVersion = "dev"
)

func infoMiddleware(store store.Store) func(next gen.StrictHandlerFunc, _ string) gen.StrictHandlerFunc {
pipelineID := ""

return func(next gen.StrictHandlerFunc, _ string) gen.StrictHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (response interface{}, err error) {
if pipelineID == "" {
if id, err := store.ReadPipelineID(ctx); err != nil {
return nil, err
} else {
pipelineID = id.String()
}
}
w.Header().Set("X-Archivematica-Version", archivematicaVersion)
w.Header().Set("x-Archivematica-ID", pipelineID)
return next(ctx, w, r, request)
}
}
}

type contextKey string

const (
requestContextKey contextKey = "requestObject"
)

func contextMiddleware(next gen.StrictHandlerFunc, _ string) gen.StrictHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (response interface{}, err error) {
ctx = context.WithValue(ctx, requestContextKey, r)
return next(ctx, w, r, request)
}
}

func requestFromContext(ctx context.Context) *http.Request {
if req, ok := ctx.Value(requestContextKey).(*http.Request); ok {
return req
}

return nil
}
32 changes: 29 additions & 3 deletions hack/ccp/internal/shim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@ import (
"github.com/go-logr/logr"

"github.com/artefactual/archivematica/hack/ccp/internal/shim/gen"
"github.com/artefactual/archivematica/hack/ccp/internal/store"
)

type Server struct {
logger logr.Logger
config Config
store store.Store
server *http.Server
ln net.Listener
}

var _ gen.StrictServerInterface = (*Server)(nil)

func NewServer(logger logr.Logger, config Config) *Server {
func NewServer(logger logr.Logger, config Config, store store.Store) *Server {
return &Server{
logger: logger,
config: config,
store: store,
}
}

func (s *Server) Run() error {
middleware := []gen.StrictMiddlewareFunc{
infoMiddleware(s.store),
contextMiddleware,
}

s.server = &http.Server{
Handler: gen.Handler(gen.NewStrictHandler(s, []gen.StrictMiddlewareFunc{})),
Handler: gen.Handler(gen.NewStrictHandler(s, middleware)),
ReadHeaderTimeout: time.Second,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute,
Expand Down Expand Up @@ -167,7 +175,25 @@ func (s *Server) TasksRead(ctx context.Context, request gen.TasksReadRequestObje
}

func (s *Server) ValidateCreate(ctx context.Context, request gen.ValidateCreateRequestObject) (gen.ValidateCreateResponseObject, error) {
return nil, nil
validator, err := loadValidator(request.Validator)
if err != nil {
return gen.ValidateCreate404JSONResponse{
Error: true,
Message: err.Error(),
}, nil
}

if err := validateContentType(requestFromContext(ctx)); err != nil {
return nil, err
}

err = validator.validate(request.Body)
if err != nil {
// TODO: gen.ValidateCreate400JSONResponse doesn't seem workable atm.
return nil, err
}

return gen.ValidateCreate200JSONResponse{Valid: true}, nil
}

func (s *Server) Close(ctx context.Context) error {
Expand Down
Loading

0 comments on commit d0ed227

Please sign in to comment.