diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 2f735365..877ef28d 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -32,10 +32,9 @@ - [ ] My change requires a change to the documentation. - [ ] If you've changed APIs, describe what needs to be updated in the documentation. - [ ] I have updated the documentation accordingly. -- [ ] Modules and vendor dependencies have been updated; run `go mod tidy && go mod vendor` in applicable directories. +- [ ] Modules dependencies have been updated; run `go mod tidy` in applicable directories. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. - [ ] Check your code additions will not fail linting checks: - [ ] `gofmt -s -w .` - - [ ] `go vet ./...` - [ ] `golangci-lint run` diff --git a/.golangci.yaml b/.golangci.yaml index 3a67d618..f0210363 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -7,6 +7,13 @@ issues: max-same-issues: 0 new-from-rev: origin/master + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + # Exclude some linters from running on test files. + - path: _test\.go + linters: + - dupl + linters-settings: dogsled: max-blank-identifiers: 2 diff --git a/Makefile b/Makefile index ad7cbd0c..da0b1317 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ swagger-client: check-swagger clean: ## Clean up the temp and output directories, and any built binaries. This will cause everything to get rebuilt. > rm -rf ./bin +> rm -rf ./integration/outputs > go clean > cd mservctl > go clean diff --git a/api/api.go b/api/api.go index 8a4688b2..ada23c61 100644 --- a/api/api.go +++ b/api/api.go @@ -3,6 +3,8 @@ package api import ( "bufio" + "context" + "errors" "fmt" "io" "io/ioutil" @@ -21,7 +23,6 @@ import ( coprocess "github.com/TykTechnologies/mserv/coprocess/bindings/go" "github.com/TykTechnologies/mserv/storage" "github.com/TykTechnologies/mserv/util/logger" - "github.com/TykTechnologies/mserv/util/storage/errors" ) const ( @@ -41,24 +42,24 @@ type API struct { store storage.MservStore } -func (a *API) HandleUpdateBundle(filePath string, bundleName string) (*storage.MW, error) { - mw, err := a.store.GetMWByID(bundleName) +func (a *API) HandleUpdateBundle(ctx context.Context, filePath, bundleName string) (*storage.MW, error) { + mw, err := a.store.GetMWByID(ctx, bundleName) if err != nil { - return nil, err + return nil, fmt.Errorf("get mw by id error: %w", err) } - err = a.store.DeleteMW(mw.UID) + err = a.store.DeleteMW(ctx, mw.UID) if err != nil { - return nil, err + return nil, fmt.Errorf("delete mw error: %w", err) } - return a.HandleNewBundle(filePath, mw.APIID, bundleName) + return a.HandleNewBundle(ctx, filePath, mw.APIID, bundleName) } -func (a *API) HandleDeleteBundle(bundleName string) error { - mw, err := a.store.GetMWByID(bundleName) +func (a *API) HandleDeleteBundle(ctx context.Context, bundleName string) error { + mw, err := a.store.GetMWByID(ctx, bundleName) if err != nil { - return err + return fmt.Errorf("get mw by id error: %w", err) } fStore, err := GetFileStore() @@ -124,10 +125,14 @@ func (a *API) HandleDeleteBundle(bundleName string) error { return fmt.Errorf("could not remove container '%s': %w", pluginContainerID, errRC) } - return a.store.DeleteMW(mw.UID) + if err := a.store.DeleteMW(ctx, mw.UID); err != nil { + return fmt.Errorf("delete mw error: %w", err) + } + + return nil } -func (a *API) HandleNewBundle(filePath string, apiID, bundleName string) (*storage.MW, error) { +func (a *API) HandleNewBundle(ctx context.Context, filePath, apiID, bundleName string) (*storage.MW, error) { // Read the zip file raw data bData, err := ioutil.ReadFile(filePath) if err != nil { @@ -283,7 +288,7 @@ func (a *API) HandleNewBundle(filePath string, apiID, bundleName string) (*stora // a.LoadMWIntoDispatcher(mw, bdl.Path) // store in mongo - _, err = a.store.CreateMW(mw) + _, err = a.store.CreateMW(ctx, mw) if err != nil { return mw, err } @@ -303,7 +308,7 @@ func (a *API) HandleNewBundle(filePath string, apiID, bundleName string) (*stora } // Will only store the bundle file into our store so we can pull it from a gateway if necessary -func (a *API) StoreBundleOnly(filePath string, apiID, bundleName string) (*storage.MW, error) { +func (a *API) StoreBundleOnly(ctx context.Context, filePath, apiID, bundleName string) (*storage.MW, error) { // create DB record of the bundle mw := &storage.MW{ UID: bundleName, @@ -358,9 +363,9 @@ func (a *API) StoreBundleOnly(filePath string, apiID, bundleName string) (*stora log.Info("completed storage") // store in mongo - _, err = a.store.CreateMW(mw) + _, err = a.store.CreateMW(ctx, mw) if err != nil { - return mw, err + return mw, fmt.Errorf("create mw error: %w", err) } // clean up @@ -371,12 +376,22 @@ func (a *API) StoreBundleOnly(filePath string, apiID, bundleName string) (*stora return mw, nil } -func (a *API) GetMWByID(id string) (*storage.MW, error) { - return a.store.GetMWByID(id) +func (a *API) GetMWByID(ctx context.Context, id string) (*storage.MW, error) { + mw, err := a.store.GetMWByID(ctx, id) + if err != nil { + return nil, fmt.Errorf("get mw by id error: %w", err) + } + + return mw, nil } -func (a *API) GetAllActiveMW() ([]*storage.MW, error) { - return a.store.GetAllActive() +func (a *API) GetAllActiveMW(ctx context.Context) ([]*storage.MW, error) { + list, err := a.store.GetAllActive(ctx) + if err != nil { + return nil, fmt.Errorf("get all active error: %w", err) + } + + return list, nil } func (a *API) LoadMWIntoDispatcher(mw *storage.MW, pluginPath string) (*storage.MW, error) { diff --git a/go.mod b/go.mod index 3ce3fc01..a4c92a17 100644 --- a/go.mod +++ b/go.mod @@ -25,9 +25,9 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.6.2 - golang.org/x/net v0.0.0-20191009170851-d66e71096ffb + go.mongodb.org/mongo-driver v1.13.0 + golang.org/x/net v0.18.0 google.golang.org/grpc v1.21.1 - gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 ) require ( @@ -40,6 +40,7 @@ require ( github.com/aws/aws-sdk-go v1.28.11 // indirect github.com/aws/aws-sdk-go-v2 v0.7.0 // indirect github.com/clbanning/mxj v1.8.4 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db // indirect github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect github.com/fsnotify/fsnotify v1.4.7 // indirect @@ -48,7 +49,8 @@ require ( github.com/go-openapi/jsonreference v0.19.2 // indirect github.com/go-openapi/loads v0.19.3 // indirect github.com/go-openapi/spec v0.19.3 // indirect - github.com/go-stack/stack v1.8.0 // indirect + github.com/golang/snappy v0.0.1 // indirect + github.com/google/uuid v1.4.0 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-multierror v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -58,6 +60,7 @@ require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect + github.com/klauspost/compress v1.13.6 // indirect github.com/lonelycode/go-uuid v0.0.0-20141202165402-ed3ca8a15a93 // indirect github.com/lonelycode/osin v0.0.0-20160423095202-da239c9dacb6 // indirect github.com/magiconair/properties v1.8.1 // indirect @@ -68,6 +71,7 @@ require ( github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mitchellh/go-wordwrap v1.0.0 // indirect github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/onsi/ginkgo v1.12.0 // indirect github.com/onsi/gomega v1.9.0 // indirect github.com/pelletier/go-toml v1.6.0 // indirect @@ -79,16 +83,22 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect + github.com/xdg-go/pbkdf2 v1.0.0 // indirect + github.com/xdg-go/scram v1.1.2 // indirect + github.com/xdg-go/stringprep v1.0.4 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xeipuuv/gojsonschema v1.2.0 // indirect + github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect github.com/zclconf/go-cty v1.2.1 // indirect - go.mongodb.org/mongo-driver v1.1.1 // indirect - golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect - golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect - golang.org/x/text v0.3.2 // indirect + golang.org/x/crypto v0.15.0 // indirect + golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect + golang.org/x/sys v0.14.0 // indirect + golang.org/x/term v0.14.0 // indirect + golang.org/x/text v0.14.0 // indirect google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/ini.v1 v1.52.0 // indirect + gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/go.sum b/go.sum index 002346d0..f5a28e67 100644 --- a/go.sum +++ b/go.sum @@ -180,7 +180,6 @@ github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2K github.com/go-openapi/validate v0.19.3 h1:PAH/2DylwWcIU1s0Y7k3yNmeAgWOcKrNE2Q7Ww/kCg4= github.com/go-openapi/validate v0.19.3/go.mod h1:90Vh6jjkTn+OT1Eefm0ZixWNFjhtOH7vS9k0lo6zwJo= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= @@ -202,12 +201,15 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -216,6 +218,8 @@ github.com/google/readahead v0.0.0-20161222183148-eaceba169032/go.mod h1:qYysrqQ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gophercloud/gophercloud v0.0.0-20190208042652-bc37892e1968/go.mod h1:3WdhXV3rUYy9p6AUW8d94kr+HS62Y4VL9mBnFxsD8q4= @@ -300,6 +304,8 @@ github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba/go.mod h1:ghbZscTyKdM07+Fw3KSi0hcJm+AlEUWj8QLlPtijN/M= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= @@ -365,6 +371,8 @@ github.com/mitchellh/prefixedio v0.0.0-20190213213902-5733675afd51/go.mod h1:kB1 github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/ncw/swift v1.0.49/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= @@ -461,7 +469,6 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/svanharmelen/jsonapi v0.0.0-20180618144545-0c0828c3f16d/go.mod h1:BSTlc8jOjh0niykqEGVXOLXdi9o0r0kR8tCYiMvjFgw= github.com/terraform-providers/terraform-provider-openstack v1.15.0/go.mod h1:2aQ6n/BtChAl1y2S60vebhyJyZXBsuAI5G4+lHrT1Ew= -github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -474,6 +481,12 @@ github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6Ac github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= +github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= +github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= +github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= @@ -484,6 +497,9 @@ github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xlab/treeprint v0.0.0-20161029104018-1d6e34225557/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8= @@ -492,8 +508,9 @@ github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q github.com/zclconf/go-cty-yaml v1.0.1/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= -go.mongodb.org/mongo-driver v1.1.1 h1:Sq1fR+0c58RME5EoqKdjkiQAmPjmfHlZOoRI6fTUOcs= go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= +go.mongodb.org/mongo-driver v1.13.0 h1:67DgFFjYOCMWdtTEmKFpV3ffWlFnh+CYZ8ZS/tXWUfY= +go.mongodb.org/mongo-driver v1.13.0/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= @@ -511,8 +528,11 @@ golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190617133340-57b3e21c3d56/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= @@ -522,6 +542,7 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -544,8 +565,12 @@ golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191009170851-d66e71096ffb h1:TR699M2v0qoKTOHxeLgp6zPqaQNs74f01a/ob9W0qko= golang.org/x/net v0.0.0-20191009170851-d66e71096ffb/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -554,6 +579,8 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -576,12 +603,28 @@ golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= +golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -598,8 +641,11 @@ golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190617190820-da514acc4774/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= diff --git a/http_funcs/api_handlers.go b/http_funcs/api_handlers.go index 0429b193..07a5df9f 100644 --- a/http_funcs/api_handlers.go +++ b/http_funcs/api_handlers.go @@ -74,11 +74,13 @@ func (h *HttpServ) ExtractBundleFromPost(r *http.Request) (string, error) { // Expects a zipped file bundle in the `uploadfile` form field. // // Security: -// api_key: +// +// api_key: // // Responses: -// 200: mwIDResponse -// 500: genericErrorResponse +// +// 200: mwIDResponse +// 500: genericErrorResponse func (h *HttpServ) AddMW(w http.ResponseWriter, r *http.Request) { apiID := r.FormValue("api_id") @@ -100,7 +102,7 @@ func (h *HttpServ) AddMW(w http.ResponseWriter, r *http.Request) { restartNeeded = false } - mw, err := processor(tmpFileLoc, apiID, uuid.NewV4().String()) + mw, err := processor(r.Context(), tmpFileLoc, apiID, uuid.NewV4().String()) if err != nil { h.HandleError(err, w, r) return @@ -128,11 +130,13 @@ func (h *HttpServ) AddMW(w http.ResponseWriter, r *http.Request) { // Expects a file bundle in `uploadfile` form field. // // Security: -// api_key: +// +// api_key: // // Responses: -// 200: mwIDResponse -// 500: genericErrorResponse +// +// 200: mwIDResponse +// 500: genericErrorResponse func (h *HttpServ) UpdateMW(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, ok := vars["id"] @@ -148,7 +152,7 @@ func (h *HttpServ) UpdateMW(w http.ResponseWriter, r *http.Request) { return } - mw, err := h.api.HandleUpdateBundle(tmpFileLoc, id) + mw, err := h.api.HandleUpdateBundle(r.Context(), tmpFileLoc, id) if err != nil { h.HandleError(err, w, r) return @@ -169,11 +173,13 @@ func (h *HttpServ) UpdateMW(w http.ResponseWriter, r *http.Request) { // Deletes a middleware specified by {id}. // // Security: -// api_key: +// +// api_key: // // Responses: -// 200: mwIDResponse -// 500: genericErrorResponse +// +// 200: mwIDResponse +// 500: genericErrorResponse func (h *HttpServ) DeleteMW(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, ok := vars["id"] @@ -182,7 +188,7 @@ func (h *HttpServ) DeleteMW(w http.ResponseWriter, r *http.Request) { return } - if err := h.api.HandleDeleteBundle(id); err != nil { + if err := h.api.HandleDeleteBundle(r.Context(), id); err != nil { h.HandleError(err, w, r) return } @@ -198,11 +204,13 @@ func (h *HttpServ) DeleteMW(w http.ResponseWriter, r *http.Request) { // Fetches a middleware specified by {id}. // // Security: -// api_key: +// +// api_key: // // Responses: -// 200: mwResponse -// 500: genericErrorResponse +// +// 200: mwResponse +// 500: genericErrorResponse func (h *HttpServ) FetchMW(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, ok := vars["id"] @@ -211,7 +219,7 @@ func (h *HttpServ) FetchMW(w http.ResponseWriter, r *http.Request) { return } - dat, err := h.api.GetMWByID(id) + dat, err := h.api.GetMWByID(r.Context(), id) if err != nil { h.HandleError(err, w, r) return @@ -227,11 +235,13 @@ func (h *HttpServ) FetchMW(w http.ResponseWriter, r *http.Request) { // - application/octet-stream // // Security: -// api_key: +// +// api_key: // // Responses: -// 200: mwBundleResponse -// 500: genericErrorResponse +// +// 200: mwBundleResponse +// 500: genericErrorResponse func (h *HttpServ) FetchBundleFile(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, ok := vars["id"] @@ -240,7 +250,7 @@ func (h *HttpServ) FetchBundleFile(w http.ResponseWriter, r *http.Request) { return } - dat, err := h.api.GetMWByID(id) + dat, err := h.api.GetMWByID(r.Context(), id) if err != nil { h.HandleError(err, w, r) return @@ -260,13 +270,15 @@ func (h *HttpServ) FetchBundleFile(w http.ResponseWriter, r *http.Request) { // Lists all middleware. // // Security: -// api_key: +// +// api_key: // // Responses: -// 200: mwListResponse -// 500: genericErrorResponse +// +// 200: mwListResponse +// 500: genericErrorResponse func (h *HttpServ) FetchAllActiveMW(w http.ResponseWriter, r *http.Request) { - mws, err := h.api.GetAllActiveMW() + mws, err := h.api.GetAllActiveMW(r.Context()) if err != nil { h.HandleError(err, w, r) return diff --git a/main.go b/main.go index 22e7f658..10755d02 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "io" "net" @@ -59,7 +60,7 @@ func prog(state overseer.State) { log.Fatal("store does not implement MservStore") } - err = store.InitMservStore(conf.Mserv.StorageTag) + err = store.InitMservStore(context.Background(), conf.Mserv.StorageTag) if err != nil { log.Fatal("store failed to init: ", err) } @@ -92,7 +93,8 @@ func prog(state overseer.State) { // First run, fetch all plugins so we can init properly // log.Warning("SKIPPING PLUGIN FETCH AND INIT") log.Warning("fetching latest plugin list") - alPLs, err := store.GetAllActive() + + alPLs, err := store.GetAllActive(context.Background()) if err != nil { log.Fatal(err) } @@ -132,7 +134,7 @@ func pollForActiveMWs(store storage.MservStore) { for { time.Sleep(interval) - alPLs, err := store.GetAllActive() + alPLs, err := store.GetAllActive(context.Background()) if err != nil { log.Error(err) } diff --git a/mserv_example.conf b/mserv_example.conf index 21708003..26c5f5d9 100644 --- a/mserv_example.conf +++ b/mserv_example.conf @@ -12,17 +12,7 @@ }, "MongoStore": { "default": { - "ConnStr": "mongodb://mongodb:27017/admin", - "UseTLS": false, - "Crypto": { - "Enabled": false, - "KeySets": { - "default": { - "PrivateKeyFile": "", - "PublicKeyFile": "" - } - } - } + "ConnStr": "mongodb://mongodb:27017/admin" } } } diff --git a/mserv_slave.json b/mserv_slave.json index 42908961..c693cc8c 100644 --- a/mserv_slave.json +++ b/mserv_slave.json @@ -12,17 +12,7 @@ }, "MongoStore": { "default": { - "ConnStr": "mongodb://admin:finglebobcatflurplefray@159.89.48.218:27017/admin", - "UseTLS": false, - "Crypto": { - "Enabled": false, - "KeySets": { - "default": { - "PrivateKeyFile": "", - "PublicKeyFile": "" - } - } - } + "ConnStr": "mongodb://admin:finglebobcatflurplefray@159.89.48.218:27017/admin" } }, "ServiceStore": { diff --git a/storage/config_storage.go b/storage/config_storage.go index 24b24e77..4176db72 100644 --- a/storage/config_storage.go +++ b/storage/config_storage.go @@ -1,6 +1,7 @@ package storage import ( + "context" "time" "github.com/TykTechnologies/tyk/apidef" @@ -29,11 +30,11 @@ type MW struct { } type MservStore interface { - GetMWByID(id string) (*MW, error) - GetMWByAPIID(APIID string) (*MW, error) - GetAllActive() ([]*MW, error) - CreateMW(mw *MW) (string, error) - UpdateMW(mw *MW) (string, error) - DeleteMW(id string) error - InitMservStore(tag string) error + GetMWByID(ctx context.Context, id string) (*MW, error) + GetMWByAPIID(ctx context.Context, APIID string) (*MW, error) + GetAllActive(ctx context.Context) ([]*MW, error) + CreateMW(ctx context.Context, mw *MW) (string, error) + UpdateMW(ctx context.Context, mw *MW) (string, error) + DeleteMW(ctx context.Context, id string) error + InitMservStore(ctx context.Context, tag string) error } diff --git a/storage/errors.go b/storage/errors.go deleted file mode 100644 index da692795..00000000 --- a/storage/errors.go +++ /dev/null @@ -1,5 +0,0 @@ -package storage - -import "errors" - -var ErrEmptyUID = errors.New("UID cannot be empty") diff --git a/storage/runtime_storage.go b/storage/runtime_storage.go index e64d8ab4..6303404f 100644 --- a/storage/runtime_storage.go +++ b/storage/runtime_storage.go @@ -2,13 +2,13 @@ package storage import ( + "errors" "fmt" "sync" "github.com/TykTechnologies/tyk/apidef" coprocess "github.com/TykTechnologies/mserv/coprocess/bindings/go" - "github.com/TykTechnologies/mserv/util/storage/errors" ) var GlobalRtStore *RuntimeStore diff --git a/util/storage/errors/errors.go b/util/storage/errors/errors.go deleted file mode 100644 index 187a29ce..00000000 --- a/util/storage/errors/errors.go +++ /dev/null @@ -1,11 +0,0 @@ -package errors - -import "errors" - -var ( - ErrNotFound = errors.New("not found") -) - -func New(s string) error { - return errors.New(s) -} diff --git a/util/storage/mock/mock.go b/util/storage/mock/mock.go index b495d38e..a0baeb3d 100644 --- a/util/storage/mock/mock.go +++ b/util/storage/mock/mock.go @@ -2,47 +2,57 @@ package mock import ( + "context" + "errors" + "github.com/TykTechnologies/mserv/storage" ) +// ErrEmptyUID is returned when middleware UID is empty. +var ErrEmptyUID = errors.New("UID cannot be empty") + // Storage is a mock store for testing mserv. type Storage struct{} // GetMWByID is a test mock. -func (s *Storage) GetMWByID(id string) (*storage.MW, error) { +func (s *Storage) GetMWByID(_ context.Context, id string) (*storage.MW, error) { + if id == "" { + return nil, ErrEmptyUID + } + return &storage.MW{UID: id}, nil } // GetMWByAPIID is a test mock. -func (s *Storage) GetMWByAPIID(apiID string) (*storage.MW, error) { +func (s *Storage) GetMWByAPIID(_ context.Context, _ string) (*storage.MW, error) { panic("TODO: Implement") } // GetAllActive is a test mock. -func (s *Storage) GetAllActive() ([]*storage.MW, error) { +func (s *Storage) GetAllActive(_ context.Context) ([]*storage.MW, error) { panic("TODO: Implement") } // CreateMW is a test mock. -func (s *Storage) CreateMW(mw *storage.MW) (string, error) { +func (s *Storage) CreateMW(_ context.Context, mw *storage.MW) (string, error) { if mw.UID == "" { - return "", storage.ErrEmptyUID + return "", ErrEmptyUID } return mw.UID, nil } // UpdateMW is a test mock. -func (s *Storage) UpdateMW(mw *storage.MW) (string, error) { +func (s *Storage) UpdateMW(_ context.Context, mw *storage.MW) (string, error) { panic("TODO: Implement") } // DeleteMW is a test mock. -func (s *Storage) DeleteMW(id string) error { +func (s *Storage) DeleteMW(_ context.Context, id string) error { return nil } // InitMservStore is a test mock. -func (s *Storage) InitMservStore(tag string) error { +func (s *Storage) InitMservStore(_ context.Context, tag string) error { panic("TODO: Implement") } diff --git a/util/storage/mongo/conf.go b/util/storage/mongo/conf.go index 88f6294b..fc066c9f 100644 --- a/util/storage/mongo/conf.go +++ b/util/storage/mongo/conf.go @@ -2,21 +2,22 @@ package mongo import ( "encoding/json" + "github.com/TykTechnologies/mserv/util/conf" ) -type MgoStoreConf struct { - ConnStr string - UseTLS bool -} +type ( + MgoStoreConf struct { + ConnStr string + } -type Config struct { - MongoStore map[string]*MgoStoreConf -} + Config struct { + MongoStore map[string]*MgoStoreConf + } +) var sconf *Config -// Variable so we can override var GetConf = func() *Config { if sconf == nil { sconf = &Config{} @@ -25,13 +26,7 @@ var GetConf = func() *Config { if err != nil { log.Fatal("Failed to unmarshal mongo driver config: ", err) } - - SetDefaults() } return sconf } - -func SetDefaults() { - // Set Defaults? -} diff --git a/util/storage/mongo/mgo_models/mgo_models.go b/util/storage/mongo/mgo_models/mgo_models.go deleted file mode 100644 index 84ad5ca5..00000000 --- a/util/storage/mongo/mgo_models/mgo_models.go +++ /dev/null @@ -1,11 +0,0 @@ -package mgo_models - -import ( - mservStorage "github.com/TykTechnologies/mserv/storage" - "gopkg.in/mgo.v2/bson" -) - -type MgoMW struct { - *mservStorage.MW - MID bson.ObjectId `bson:"_id"` -} diff --git a/util/storage/mongo/models.go b/util/storage/mongo/models.go new file mode 100644 index 00000000..c9cf081b --- /dev/null +++ b/util/storage/mongo/models.go @@ -0,0 +1,22 @@ +package mongo + +import ( + "errors" + + "go.mongodb.org/mongo-driver/bson/primitive" + + mservStorage "github.com/TykTechnologies/mserv/storage" +) + +type mgoMW struct { + *mservStorage.MW + MID primitive.ObjectID `bson:"_id"` +} + +var ( + // ErrEmptyUID is returned when middleware UID is empty. + ErrEmptyUID = errors.New("UID cannot be empty") + + // ErrNotFound is returned when middleware is not found. + ErrNotFound = errors.New("middleware not found") +) diff --git a/util/storage/mongo/mongo.go b/util/storage/mongo/mongo.go index 906b14cd..8e1cfd99 100644 --- a/util/storage/mongo/mongo.go +++ b/util/storage/mongo/mongo.go @@ -1,13 +1,15 @@ package mongo import ( - "crypto/tls" + "context" "fmt" - "net" "time" "github.com/patrickmn/go-cache" - "gopkg.in/mgo.v2" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/mongo/readpref" + "go.mongodb.org/mongo-driver/x/mongo/driver/connstring" "github.com/TykTechnologies/mserv/util/logger" ) @@ -17,7 +19,7 @@ const ( ) type Store struct { - ms *mgo.Session + db *mongo.Database conf *MgoStoreConf objCache *cache.Cache tag string @@ -48,34 +50,37 @@ func (m *Store) Init() error { m.conf = c log.Info("initialising mgo store") - var session *mgo.Session - var err error - if m.conf.UseTLS { - log.Info("TLS enabled") - dialInfo, mErr := mgo.ParseURL(m.conf.ConnStr) - if mErr != nil { - return mErr - } - - dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) { - return tls.Dial("tcp", addr.String(), &tls.Config{}) - } - - session, err = mgo.DialWithInfo(dialInfo) - } else { - session, err = mgo.Dial(m.conf.ConnStr) + cs, err := connstring.ParseAndValidate(m.conf.ConnStr) + if err != nil { + return fmt.Errorf("error validating mongo connection string: %w", err) + } + + opts := options.Client(). + ApplyURI(m.conf.ConnStr) + + if err := opts.Validate(); err != nil { + return fmt.Errorf("error validating mongodb settings: %w", err) } + // Connect to MongoDB. + mgo, err := mongo.Connect(context.Background(), opts) if err != nil { - log.Error(err) - return err + return fmt.Errorf("error connectiong to mongodb: %w", err) } - m.ms = session + // Verify that we have active DB connection. + if err := mgo.Ping(context.Background(), readpref.Primary()); err != nil { + return fmt.Errorf("error pinging mongodb: %w", err) + } + + // Connect to default database. + m.db = mgo.Database(cs.Database) log.Info("Initialising cache") m.objCache = cache.New(1*time.Minute, 5*time.Minute) + m.initialised = true + return nil } diff --git a/util/storage/mongo/mserv_store.go b/util/storage/mongo/mserv_store.go index 661473b9..e3b0a69f 100644 --- a/util/storage/mongo/mserv_store.go +++ b/util/storage/mongo/mserv_store.go @@ -1,42 +1,59 @@ +// Package mongo implements Mserv MongoDB storage. package mongo import ( - "gopkg.in/mgo.v2/bson" + "context" + "fmt" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "github.com/TykTechnologies/mserv/storage" - "github.com/TykTechnologies/mserv/util/storage/mongo/mgo_models" ) -// GetMWByID gets middleware from the store based on its UID. -func (m *Store) GetMWByID(id string) (*storage.MW, error) { - return m.getByID("mw.uid", id) -} +// InitMservStore initializes Mserv storage. +func (m *Store) InitMservStore(_ context.Context, tag string) error { + m.tag = tag -// GetMWByAPIID gets middleware from the store based on its API ID. -func (m *Store) GetMWByAPIID(apiID string) (*storage.MW, error) { - return m.getByID("mw.apiid", apiID) + return m.Init() } -func (m *Store) getByID(key, value string) (*storage.MW, error) { - s := m.ms.Copy() - defer s.Close() +// getByKey func fetches middleware from database for give key/value pair. +func (m *Store) getByKey(ctx context.Context, key, value string) (*storage.MW, error) { + mm := mgoMW{} - mm := &mgo_models.MgoMW{} - if err := s.DB("").C(mservCol).Find(bson.M{key: value}).One(mm); err != nil { + f := bson.M{key: value} + + if err := m.db.Collection(mservCol).FindOne(ctx, f).Decode(&mm); err != nil { return nil, err } return mm.MW, nil } +// GetMWByID gets middleware from the store based on its UID. +func (m *Store) GetMWByID(ctx context.Context, id string) (*storage.MW, error) { + return m.getByKey(ctx, "mw.uid", id) +} + +// GetMWByAPIID gets middleware from the store based on its API ID. +func (m *Store) GetMWByAPIID(ctx context.Context, apiID string) (*storage.MW, error) { + return m.getByKey(ctx, "mw.apiid", apiID) +} + // GetAllActive returns all active middleware from the store. -func (m *Store) GetAllActive() ([]*storage.MW, error) { - s := m.ms.Copy() - defer s.Close() +func (m *Store) GetAllActive(ctx context.Context) ([]*storage.MW, error) { + mm := make([]mgoMW, 0) - mm := make([]mgo_models.MgoMW, 0) - if err := s.DB("").C(mservCol).Find(bson.M{"mw.active": true}).All(&mm); err != nil { - return nil, err + f := bson.M{"mw.active": true} + + cur, err := m.db.Collection(mservCol).Find(ctx, f) + if err != nil { + return nil, fmt.Errorf("collect error: %w", err) + } + + if err := cur.All(ctx, &mm); err != nil { + return nil, fmt.Errorf("cursor fetch error: %w", err) } mws := make([]*storage.MW, len(mm)) @@ -47,71 +64,73 @@ func (m *Store) GetAllActive() ([]*storage.MW, error) { return mws, nil } -// UpdateMW will update the given middleware in-place in storage. -func (m *Store) UpdateMW(mw *storage.MW) (string, error) { - s := m.ms.Copy() - defer s.Close() - +// CreateMW stores the given middleware. +func (m *Store) CreateMW(ctx context.Context, mw *storage.MW) (string, error) { if mw.UID == "" { - return "", storage.ErrEmptyUID + return "", ErrEmptyUID } - mMw := &mgo_models.MgoMW{} - if err := s.DB("").C(mservCol).Find(bson.M{"mw.uid": mw.UID}).One(mMw); err != nil { - return "", err + mMw := mgoMW{ + MID: primitive.NewObjectID(), + MW: mw, } - mMw.MW = mw - - if err := s.DB("").C(mservCol).Update(bson.M{"mw.uid": mw.UID}, mMw); err != nil { - return "", err + if _, err := m.db.Collection(mservCol).InsertOne(ctx, mMw); err != nil { + return "", fmt.Errorf("insert error: %w", err) } return mw.UID, nil } -// CreateMW stores the given middleware. -func (m *Store) CreateMW(mw *storage.MW) (string, error) { - s := m.ms.Copy() - defer s.Close() - +// UpdateMW will update the given middleware in-place in storage. +func (m *Store) UpdateMW(ctx context.Context, mw *storage.MW) (string, error) { if mw.UID == "" { - return "", storage.ErrEmptyUID + return "", ErrEmptyUID } - mMw := &mgo_models.MgoMW{ - MID: bson.NewObjectId(), - MW: mw, + mMw := mgoMW{} + + f := bson.M{"mw.uid": mw.UID} + + if err := m.db.Collection(mservCol).FindOne(ctx, f).Decode(&mMw); err != nil { + return "", fmt.Errorf("find error: %w", err) } - if err := s.DB("").C(mservCol).Insert(mMw); err != nil { - return "", err + mMw.MW = mw + + update := bson.M{ + "$set": mMw, + } + + res, err := m.db.Collection(mservCol).UpdateOne(ctx, f, update) + if err != nil { + return "", fmt.Errorf("update error: %w", err) + } + + if res.MatchedCount == 0 { + return "", ErrNotFound } return mw.UID, nil } -func (m *Store) DeleteMW(id string) error { - s := m.ms.Copy() - defer s.Close() - +// DeleteMW removes given middleware. +func (m *Store) DeleteMW(ctx context.Context, id string) error { if id == "" { - return storage.ErrEmptyUID + return ErrEmptyUID } - mMw := &mgo_models.MgoMW{} - if err := s.DB("").C(mservCol).Find(bson.M{"mw.uid": id}).One(mMw); err != nil { - return err + mMw := mgoMW{} + + f := bson.M{"mw.uid": id} + + if err := m.db.Collection(mservCol).FindOne(ctx, f).Decode(&mMw); err != nil { + return fmt.Errorf("find error: %w", err) } - if err := s.DB("").C(mservCol).Remove(bson.M{"mw.uid": id}); err != nil { - return err + if _, err := m.db.Collection(mservCol).DeleteOne(ctx, f); err != nil { + return fmt.Errorf("delete error: %w", err) } return nil } - -func (m *Store) InitMservStore(tag string) error { - m.tag = tag - return m.Init() -} diff --git a/util/storage/mongo/mserv_store_test.go b/util/storage/mongo/mserv_store_test.go new file mode 100644 index 00000000..4b0c26c1 --- /dev/null +++ b/util/storage/mongo/mserv_store_test.go @@ -0,0 +1,682 @@ +package mongo + +import ( + "context" + "testing" + + "github.com/google/uuid" + "github.com/matryer/is" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo/integration/mtest" + + "github.com/TykTechnologies/mserv/storage" +) + +var errMsg = "test error" + +func Test_getByKey(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("get middleware, with error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute getByKey(). + _, err := m.getByKey( + context.Background(), + "key", + "val", + ) + + // Assert errors. + eval.Equal(err.Error(), errMsg) // Expected test error. + }) + + mt.Run("get middleware, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute getByKey(). + res, err := m.getByKey( + context.Background(), + "key", + "val", + ) + + // Assert responses. + eval.NoErr(err) // Expected no errors. + eval.Equal(res.APIID, "api-1") + eval.Equal(res.OrgID, "org-1") + eval.Equal(res.UID, "uid-1") + }) +} + +func Test_GetMWByID(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("get middleware, with error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetMWByID(). + _, err := m.GetMWByID( + context.Background(), + "id", + ) + + // Assert errors. + eval.Equal(err.Error(), errMsg) // Expected test error. + }) + + mt.Run("get middleware, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetMWByID(). + res, err := m.GetMWByID( + context.Background(), + "id", + ) + + // Assert responses. + eval.NoErr(err) // Expected no errors. + eval.Equal(res.APIID, "api-1") + eval.Equal(res.OrgID, "org-1") + eval.Equal(res.UID, "uid-1") + }) +} + +func Test_GetMWByAPIID(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("get middleware, with error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetMWByAPIID(). + _, err := m.GetMWByAPIID( + context.Background(), + "id", + ) + + // Assert errors. + eval.Equal(err.Error(), errMsg) // Expected test error. + }) + + mt.Run("get middleware, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetMWByAPIID(). + res, err := m.GetMWByAPIID( + context.Background(), + "id", + ) + + // Assert responses. + eval.NoErr(err) // Expected no errors. + eval.Equal(res.APIID, "api-1") + eval.Equal(res.OrgID, "org-1") + eval.Equal(res.UID, "uid-1") + }) +} + +func Test_GetAllActive(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("get all middlewares, with error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetAllActive(). + _, err := m.GetAllActive(context.Background()) + + // Assert errors. + eval.Equal(err.Error(), "collect error: "+errMsg) // Expected test error. + }) + + mt.Run("get all middlewares, with cursor error", func(mt *mtest.T) { + mt.Parallel() + + // Create invalid subscription to force cursor error. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: "invalid"}, + } + + // Defined mocked response. + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetAllActive(). + _, err := m.GetAllActive(context.Background()) + + // Assert response. + eval.Equal( + err.Error(), + "cursor fetch error: error decoding key mw: cannot decode string into a storage.MW", + ) // Expected decoding error. + }) + + mt.Run("get all middlewares, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute GetAllActive(). + res, err := m.GetAllActive(context.Background()) + + // Assert responses. + eval.NoErr(err) // Expected no errors. + eval.Equal(res[0].APIID, "api-1") + eval.Equal(res[0].OrgID, "org-1") + eval.Equal(res[0].UID, "uid-1") + }) +} + +func Test_CreateMW(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("create middleware, with empty id", func(mt *mtest.T) { + mt.Parallel() + + // Create test store. + m := Store{} + + // Execute CreateMW(). + _, err := m.CreateMW(context.Background(), &storage.MW{}) + + // Assert errors. + eval.Equal(err, ErrEmptyUID) // Expected empty UID error. + }) + + mt.Run("create middleware, with error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + mw := storage.MW{ + UID: uuid.NewString(), + } + + // Execute CreateMW(). + _, err := m.CreateMW(context.Background(), &mw) + + // Assert errors. + eval.Equal(err.Error(), "insert error: "+errMsg) // Expected test error. + }) + + mt.Run("create middleware, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + mw := storage.MW{ + UID: "uid-1", + } + + // Execute CreateMW(). + id, err := m.CreateMW(context.Background(), &mw) + + // Assert responses. + eval.NoErr(err) // Expected no errors. + eval.Equal(id, "uid-1") + }) +} + +func Test_UpdateMW(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("update middleware, error empty id", func(mt *mtest.T) { + mt.Parallel() + + // Create test store. + m := Store{} + + // Execute UpdateMW(). + _, err := m.UpdateMW(context.Background(), &storage.MW{}) + + // Assert errors. + eval.Equal(err, ErrEmptyUID) // Expected empty UID error. + }) + + mt.Run("update middleware, with find error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Test middleware. + mw := storage.MW{ + UID: "uid-1", + } + + // Execute UpdateMW(). + _, err := m.UpdateMW(context.Background(), &mw) + + // Assert errors. + eval.Equal(err.Error(), "find error: "+errMsg) // Expected test error. + }) + + mt.Run("update middleware, with update error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Test middleware. + mw := storage.MW{ + UID: "uid-1", + } + + // Execute UpdateMW(). + _, err := m.UpdateMW(context.Background(), &mw) + + // Assert errors. + eval.Equal(err.Error(), "update error: "+errMsg) // Expected test error. + }) + + mt.Run("update middleware, with error not found", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + mtest.CreateSuccessResponse(bson.E{Key: "n", Value: 0}), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Test middleware. + mw := storage.MW{ + UID: "uid-1", + } + + // Execute UpdateMW(). + _, err := m.UpdateMW(context.Background(), &mw) + + // Assert errors. + eval.Equal(err, ErrNotFound) // Expected error not found. + }) + + mt.Run("update middleware, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + mtest.CreateSuccessResponse(bson.E{Key: "n", Value: 1}), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Test middleware. + mw := storage.MW{ + UID: "uid-1", + } + + // Execute UpdateMW(). + id, err := m.UpdateMW(context.Background(), &mw) + + // Assert errors. + eval.NoErr(err) // Expected no error. + eval.Equal(id, "uid-1") + }) +} + +func Test_DeleteMW(t *testing.T) { + t.Parallel() + + eval := is.New(t) + + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) + + mt.Run("delete middleware, error empty id", func(mt *mtest.T) { + mt.Parallel() + + // Create test store. + m := Store{} + + // Execute DeleteMW(). + err := m.DeleteMW(context.Background(), "") + + // Assert errors. + eval.Equal(err, ErrEmptyUID) // Expected empty UID error. + }) + + mt.Run("delete middleware, with find error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked response. + mt.AddMockResponses( + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute DeleteMW(). + err := m.DeleteMW(context.Background(), "uid-1") + + // Assert errors. + eval.Equal(err.Error(), "find error: "+errMsg) // Expected test error. + }) + + mt.Run("delete middleware, with delete error", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + mtest.CreateCommandErrorResponse( + mtest.CommandError{ + Code: 11, + Message: errMsg, + }, + ), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute DeleteMW(). + err := m.DeleteMW(context.Background(), "uid-1") + + // Assert errors. + eval.Equal(err.Error(), "delete error: "+errMsg) // Expected test error. + }) + + mt.Run("delete middleware, with success", func(mt *mtest.T) { + mt.Parallel() + + // Define mocked responses. + doc := bson.D{ + {Key: "_id", Value: primitive.NewObjectID()}, + {Key: "mw", Value: bson.D{ + {Key: "apiid", Value: "api-1"}, + {Key: "orgid", Value: "org-1"}, + {Key: "uid", Value: "uid-1"}, + }}, + } + + mt.AddMockResponses( + mtest.CreateCursorResponse(0, "test.mserv", mtest.FirstBatch, doc), + mtest.CreateSuccessResponse(bson.E{Key: "n", Value: 1}), + ) + + // Create test store. + m := Store{ + db: mt.Client.Database("test"), + } + + // Execute DeleteMW(). + err := m.DeleteMW(context.Background(), "uid-1") + + // Assert errors. + eval.NoErr(err) // Expected no error. + }) +} diff --git a/util/storage/slave/slave.go b/util/storage/slave/slave.go index 4543885c..4bfa5049 100644 --- a/util/storage/slave/slave.go +++ b/util/storage/slave/slave.go @@ -1,6 +1,7 @@ package slave import ( + "context" "errors" "fmt" "net/url" @@ -34,7 +35,7 @@ func (c *Client) defaultAuth() runtime.ClientAuthInfoWriter { return httptransport.APIKeyAuth("X-Api-Key", "header", c.conf.Secret) } -func (c *Client) GetMWByID(id string) (*storage.MW, error) { +func (c *Client) GetMWByID(_ context.Context, id string) (*storage.MW, error) { params := mw.NewMwFetchParams().WithID(id) resp, err := c.mservapi.Mw.MwFetch(params, c.defaultAuth()) if err != nil { @@ -45,11 +46,11 @@ func (c *Client) GetMWByID(id string) (*storage.MW, error) { } // GetMWByAPIID is not yet implemented. -func (c *Client) GetMWByAPIID(apiID string) (*storage.MW, error) { +func (c *Client) GetMWByAPIID(_ context.Context, _ string) (*storage.MW, error) { return nil, errors.New("not implemented") } -func (c *Client) GetAllActive() ([]*storage.MW, error) { +func (c *Client) GetAllActive(_ context.Context) ([]*storage.MW, error) { resp, err := c.mservapi.Mw.MwListAll(mw.NewMwListAllParams(), c.defaultAuth()) if err != nil { return nil, err @@ -67,19 +68,19 @@ func (c *Client) GetAllActive() ([]*storage.MW, error) { return mws, nil } -func (c *Client) CreateMW(mw *storage.MW) (string, error) { +func (c *Client) CreateMW(_ context.Context, _ *storage.MW) (string, error) { return "", errors.New("not implemented") } -func (c *Client) UpdateMW(mw *storage.MW) (string, error) { +func (c *Client) UpdateMW(_ context.Context, _ *storage.MW) (string, error) { return "", errors.New("not implemented") } -func (c *Client) DeleteMW(id string) error { +func (c *Client) DeleteMW(_ context.Context, _ string) error { return errors.New("not implemented") } -func (c *Client) InitMservStore(tag string) error { +func (c *Client) InitMservStore(_ context.Context, tag string) error { c.tag = tag cnf, ok := GetConf().ServiceStore[tag] if !ok { diff --git a/util/storage/storage.go b/util/storage/storage.go index ce756878..9f58a3ee 100644 --- a/util/storage/storage.go +++ b/util/storage/storage.go @@ -22,28 +22,6 @@ type Store interface { var StorageMap = make(map[string]interface{}) -// GetStore is a convenience function to return a composite Store type -func GetStore(name config.StorageDriver, tag string) (Store, error) { - _, ok := StorageMap[tag] - if ok { - log.Debugf("store already initialised for tag: %v", tag) - st, typOk := StorageMap[tag].(Store) - if !typOk { - return nil, fmt.Errorf("store with tag %v does not implement the complete Store interface", tag) - } - - return st, nil - } - - ist, err := GetSpecificStoreType(name, tag) - st, ok := ist.(Store) - if !ok { - return nil, errors.New("driver does not fulfill Store interface") - } - - return st, err -} - // GetSpecificStoreType is used to get a sub-type of the Store interface e.g. DashboardStore, // the storage specific init function must be called by the caller though. func GetSpecificStoreType(name config.StorageDriver, tag string) (interface{}, error) { @@ -79,15 +57,3 @@ func GetSpecificStoreType(name config.StorageDriver, tag string) (interface{}, e return nil, errors.New("no storage driver set") } - -// GetClone is useful if you need to adjust contextual settings in the storage driver (e.g. crypto) -// without having to dial a new connection -func GetClone(st Store) Store { - ni := st.Clone() - newST, ok := ni.(Store) - if !ok { - return nil - } - - return newST -}