Skip to content

Commit

Permalink
Add openapi filter (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
wisererik authored and skdwriting committed Nov 22, 2019
1 parent 00f8b14 commit 1a18fb7
Show file tree
Hide file tree
Showing 436 changed files with 41,057 additions and 109,131 deletions.
4 changes: 3 additions & 1 deletion client/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func request(urlStr string, method string, headers HeaderOption, input interface
type receiver struct{}

func (*receiver) Recv(url string, method string, input interface{}, output interface{}) error {
return request(url, method, nil, input, output)
headers := HeaderOption{}
headers["Content-Type"] = constants.ContentType
return request(url, method, headers, input, output)
}

func NewKeystoneReceiver(auth *KeystoneAuthOptions) (Receiver, error) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/frankban/quicktest v1.6.0 // indirect
github.com/getkin/kin-openapi v0.2.0
github.com/go-ini/ini v1.41.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3C
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/frankban/quicktest v1.6.0 h1:Cd62nl66vQsx8Uv1t8M0eICyxIwZG7MxiAOrdnnUSW0=
github.com/frankban/quicktest v1.6.0/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o=
github.com/getkin/kin-openapi v0.2.0 h1:PbHHtYZpjKwZtGlIyELgA2DploRrsaXztoNNx9HjwNY=
github.com/getkin/kin-openapi v0.2.0/go.mod h1:V1z9xl9oF5Wt7v32ne4FmiF1alpS4dM6mNzoywPOXlk=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-ini/ini v1.41.0 h1:526aoxDtxRHFQKMZfcX2OG9oOI8TJ5yPLM0Mkno/uTY=
Expand Down
4 changes: 4 additions & 0 deletions install/devsds/lib/opensds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ set +o xtrace


osds:opensds:configuration(){

# Copy api spec file to configuration path
cp $OPENSDS_DIR/openapi-spec/swagger.yaml $OPENSDS_CONFIG_DIR

# Set global configuration.
cat >> $OPENSDS_CONFIG_DIR/opensds.conf << OPENSDS_GLOBAL_CONFIG_DOC
[osdsapiserver]
Expand Down
40 changes: 14 additions & 26 deletions openapi-spec/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2020,18 +2020,18 @@ components:
type: integer
format: int64
storageAccessCapability:
type: string
enum:
- Read
- Write
- Execute
type: array
items:
type: string
enum:
- Read
- Write
- Execute
IOConnectivityLoS:
description: >-
IOConnectivityLoS can be used to specify the characteristics of storage
connectivity.
type: object
required:
- accessProtocol
properties:
accessProtocol:
type: string
Expand All @@ -2040,6 +2040,7 @@ components:
- rbd
- fibre_channel
- DSWARE
- NFS
maxIOPS:
type: integer
format: int64
Expand Down Expand Up @@ -2253,12 +2254,8 @@ components:
- $ref: '#/components/schemas/BaseModel'
- type: object
required:
- tenantId
- name
- availabilityZone
- size
- profileId
- poolId
properties:
tenantId:
type: string
Expand Down Expand Up @@ -2461,8 +2458,7 @@ components:
the customized properties of profile, such as requested capabilities
including diskType, latency, deduplication, compression and so forth.
type: object
additionalProperties:
type: object
additionalProperties: {}
example:
key1: value1
key2: false
Expand All @@ -2476,12 +2472,8 @@ components:
- $ref: '#/components/schemas/BaseModel'
- type: object
required:
- tenantId
- name
- availabilityZone
- size
- profileId
- poolId
properties:
tenantId:
type: string
Expand Down Expand Up @@ -2626,11 +2618,8 @@ components:
- $ref: '#/components/schemas/BaseModel'
- type: object
required:
- tenantId
- name
- status
- volumeId
- profileId
properties:
tenantId:
type: string
Expand Down Expand Up @@ -2666,12 +2655,8 @@ components:
- $ref: '#/components/schemas/BaseModel'
- type: object
required:
- tenantId
- profiles
- name
- description
- availabilityZone
- status
properties:
tenantId:
type: string
Expand Down Expand Up @@ -2849,7 +2834,7 @@ components:
hostName:
type: string
minLength: 1
maxLength: 28
maxLength: 64
description: Only numbers, letters, '-', '_', '.' in ASCII characters are allowed.
ip:
type: string
Expand Down Expand Up @@ -2907,6 +2892,7 @@ components:
enum:
- iscsi
- fibre_channel
- nvmeof
description: Host is a consumer of volume from storage.

HostUpdateSpec:
Expand All @@ -2915,7 +2901,7 @@ components:
hostName:
type: string
minLength: 1
maxLength: 28
maxLength: 64
description: Only numbers, letters, '-', '_', '.' in ASCII characters are allowed.
ip:
type: string
Expand Down Expand Up @@ -2973,6 +2959,7 @@ components:
enum:
- iscsi
- fibre_channel
- nvmeof
description: Host is a consumer of volume from storage.
HostRespSpec:
allOf:
Expand Down Expand Up @@ -3013,5 +3000,6 @@ components:
enum:
- iscsi
- fibre_channel
- nvmeof

description: Host is a consumer of volume from storage.
68 changes: 68 additions & 0 deletions pkg/api/filter/validation/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2019 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validation

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/astaxie/beego"
bctx "github.com/astaxie/beego/context"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/golang/glog"
myctx "github.com/opensds/opensds/pkg/context"
)

// Factory returns a fiter function of api request
func Factory(filename string) beego.FilterFunc {
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromFile(filename)
if err != nil {
glog.Warningf("error loading %s api swagger file: %s", filename, err)
return func(httpCtx *bctx.Context) {}
}

// Server is not required for finding route
swagger.Servers = nil
router := openapi3filter.NewRouter().WithSwagger(swagger)
return func(httpCtx *bctx.Context) {
req := httpCtx.Request
route, pathParams, err := router.FindRoute(req.Method, req.URL)
if err != nil {
glog.Errorf("failed to find route from swagger: %s", err)
myctx.HttpError(httpCtx, http.StatusBadRequest, "failed to find route %s:%s from swagger: %s", req.Method, req.URL, err)
}

requestValidationInput := &openapi3filter.RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
}
if err := openapi3filter.ValidateRequest(context.Background(), requestValidationInput); err != nil {
errMsg := ""
switch e := err.(type) {
case *openapi3filter.RequestError:
// Retrieve first line of err message
errMsg = strings.Split(e.Error(), "\n")[0]
default:
errMsg = fmt.Sprintf("%s", err)
}
glog.Errorf("invalid request: %s", errMsg)
myctx.HttpError(httpCtx, http.StatusBadRequest, "%s", errMsg)
}
}
}
2 changes: 2 additions & 0 deletions pkg/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/opensds/opensds/pkg/api/filter/accesslog"
"github.com/opensds/opensds/pkg/api/filter/auth"
"github.com/opensds/opensds/pkg/api/filter/context"
"github.com/opensds/opensds/pkg/api/filter/validation"
cfg "github.com/opensds/opensds/pkg/utils/config"
"github.com/opensds/opensds/pkg/utils/constants"

Expand Down Expand Up @@ -77,6 +78,7 @@ func Run(apiServerCfg cfg.OsdsApiServer) {
beego.InsertFilter(pattern, beego.BeforeExec, context.Factory())
beego.InsertFilter(pattern, beego.BeforeExec, auth.Factory())
beego.InsertFilter("*", beego.BeforeExec, accesslog.Factory())
beego.InsertFilter("*", beego.BeforeExec, validation.Factory(apiServerCfg.ApiSpecPath))

// start service
beego.Run(apiServerCfg.ApiEndpoint)
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/config/config_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Default struct{}
type OsdsApiServer struct {
ApiEndpoint string `conf:"api_endpoint,localhost:50040"`
AuthStrategy string `conf:"auth_strategy,noauth"`
ApiSpecPath string `conf:"api_spec_path,/etc/opensds/swagger.yaml"`
Daemon bool `conf:"daemon,false"`
PolicyPath string `conf:"policy_path,/etc/opensds/policy.json"`
LogFlushFrequency time.Duration `conf:"log_flush_frequency,5s"` // Default value is 5s
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const (
AuthTokenHeader = "X-Auth-Token"
SubjectTokenHeader = "X-Subject-Token"

// ContentType for api
ContentType = "application/json"

// OpenSDS current api version
APIVersion = "v1beta"

Expand Down
8 changes: 5 additions & 3 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,11 @@ func prepareHost(t *testing.T) (*model.HostSpec, error) {
t.Log("Start preparing host...")
hostName, _ := os.Hostname()
var body = &model.HostSpec{
HostName: hostName,
IP: getHostIp(),
OsType: runtime.GOOS,
HostName: hostName,
IP: getHostIp(),
OsType: runtime.GOOS,
AccessMode: "agentless",
AvailabilityZones: []string{"default"},
Initiators: []*model.Initiator{
&model.Initiator{
PortName: localIqn,
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/e2ef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,11 @@ func prepareHostForTest(t *testing.T) (*model.HostSpec, error) {

hostName, _ := os.Hostname()
var body = &model.HostSpec{
HostName: hostName,
IP: getHostIp(),
OsType: runtime.GOOS,
HostName: hostName,
IP: getHostIp(),
OsType: runtime.GOOS,
AccessMode: "agentless",
AvailabilityZones: []string{"default"},
Initiators: []*model.Initiator{
&model.Initiator{
PortName: localIqn,
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/LINBIT/godrbdutils/README.md

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

6 changes: 6 additions & 0 deletions vendor/github.com/astaxie/beego/.gitignore

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

4 changes: 4 additions & 0 deletions vendor/github.com/astaxie/beego/.gosimpleignore

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

63 changes: 63 additions & 0 deletions vendor/github.com/astaxie/beego/.travis.yml

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

Loading

0 comments on commit 1a18fb7

Please sign in to comment.