Skip to content

Commit 1a18fb7

Browse files
wisererikskdwriting
authored andcommitted
Add openapi filter (#1078)
1 parent 00f8b14 commit 1a18fb7

436 files changed

Lines changed: 41057 additions & 109131 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

client/receiver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ func request(urlStr string, method string, headers HeaderOption, input interface
152152
type receiver struct{}
153153

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

158160
func NewKeystoneReceiver(auth *KeystoneAuthOptions) (Receiver, error) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
1414
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
1515
github.com/frankban/quicktest v1.6.0 // indirect
16+
github.com/getkin/kin-openapi v0.2.0
1617
github.com/go-ini/ini v1.41.0
1718
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
1819
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3C
4343
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
4444
github.com/frankban/quicktest v1.6.0 h1:Cd62nl66vQsx8Uv1t8M0eICyxIwZG7MxiAOrdnnUSW0=
4545
github.com/frankban/quicktest v1.6.0/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o=
46+
github.com/getkin/kin-openapi v0.2.0 h1:PbHHtYZpjKwZtGlIyELgA2DploRrsaXztoNNx9HjwNY=
47+
github.com/getkin/kin-openapi v0.2.0/go.mod h1:V1z9xl9oF5Wt7v32ne4FmiF1alpS4dM6mNzoywPOXlk=
4648
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
4749
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
4850
github.com/go-ini/ini v1.41.0 h1:526aoxDtxRHFQKMZfcX2OG9oOI8TJ5yPLM0Mkno/uTY=

install/devsds/lib/opensds.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ set +o xtrace
2121

2222

2323
osds:opensds:configuration(){
24+
25+
# Copy api spec file to configuration path
26+
cp $OPENSDS_DIR/openapi-spec/swagger.yaml $OPENSDS_CONFIG_DIR
27+
2428
# Set global configuration.
2529
cat >> $OPENSDS_CONFIG_DIR/opensds.conf << OPENSDS_GLOBAL_CONFIG_DOC
2630
[osdsapiserver]

openapi-spec/swagger.yaml

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,18 +2020,18 @@ components:
20202020
type: integer
20212021
format: int64
20222022
storageAccessCapability:
2023-
type: string
2024-
enum:
2025-
- Read
2026-
- Write
2027-
- Execute
2023+
type: array
2024+
items:
2025+
type: string
2026+
enum:
2027+
- Read
2028+
- Write
2029+
- Execute
20282030
IOConnectivityLoS:
20292031
description: >-
20302032
IOConnectivityLoS can be used to specify the characteristics of storage
20312033
connectivity.
20322034
type: object
2033-
required:
2034-
- accessProtocol
20352035
properties:
20362036
accessProtocol:
20372037
type: string
@@ -2040,6 +2040,7 @@ components:
20402040
- rbd
20412041
- fibre_channel
20422042
- DSWARE
2043+
- NFS
20432044
maxIOPS:
20442045
type: integer
20452046
format: int64
@@ -2253,12 +2254,8 @@ components:
22532254
- $ref: '#/components/schemas/BaseModel'
22542255
- type: object
22552256
required:
2256-
- tenantId
22572257
- name
2258-
- availabilityZone
22592258
- size
2260-
- profileId
2261-
- poolId
22622259
properties:
22632260
tenantId:
22642261
type: string
@@ -2461,8 +2458,7 @@ components:
24612458
the customized properties of profile, such as requested capabilities
24622459
including diskType, latency, deduplication, compression and so forth.
24632460
type: object
2464-
additionalProperties:
2465-
type: object
2461+
additionalProperties: {}
24662462
example:
24672463
key1: value1
24682464
key2: false
@@ -2476,12 +2472,8 @@ components:
24762472
- $ref: '#/components/schemas/BaseModel'
24772473
- type: object
24782474
required:
2479-
- tenantId
24802475
- name
2481-
- availabilityZone
24822476
- size
2483-
- profileId
2484-
- poolId
24852477
properties:
24862478
tenantId:
24872479
type: string
@@ -2626,11 +2618,8 @@ components:
26262618
- $ref: '#/components/schemas/BaseModel'
26272619
- type: object
26282620
required:
2629-
- tenantId
26302621
- name
2631-
- status
26322622
- volumeId
2633-
- profileId
26342623
properties:
26352624
tenantId:
26362625
type: string
@@ -2666,12 +2655,8 @@ components:
26662655
- $ref: '#/components/schemas/BaseModel'
26672656
- type: object
26682657
required:
2669-
- tenantId
26702658
- profiles
26712659
- name
2672-
- description
2673-
- availabilityZone
2674-
- status
26752660
properties:
26762661
tenantId:
26772662
type: string
@@ -2849,7 +2834,7 @@ components:
28492834
hostName:
28502835
type: string
28512836
minLength: 1
2852-
maxLength: 28
2837+
maxLength: 64
28532838
description: Only numbers, letters, '-', '_', '.' in ASCII characters are allowed.
28542839
ip:
28552840
type: string
@@ -2907,6 +2892,7 @@ components:
29072892
enum:
29082893
- iscsi
29092894
- fibre_channel
2895+
- nvmeof
29102896
description: Host is a consumer of volume from storage.
29112897

29122898
HostUpdateSpec:
@@ -2915,7 +2901,7 @@ components:
29152901
hostName:
29162902
type: string
29172903
minLength: 1
2918-
maxLength: 28
2904+
maxLength: 64
29192905
description: Only numbers, letters, '-', '_', '.' in ASCII characters are allowed.
29202906
ip:
29212907
type: string
@@ -2973,6 +2959,7 @@ components:
29732959
enum:
29742960
- iscsi
29752961
- fibre_channel
2962+
- nvmeof
29762963
description: Host is a consumer of volume from storage.
29772964
HostRespSpec:
29782965
allOf:
@@ -3013,5 +3000,6 @@ components:
30133000
enum:
30143001
- iscsi
30153002
- fibre_channel
3003+
- nvmeof
30163004

30173005
description: Host is a consumer of volume from storage.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2019 The OpenSDS Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package validation
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"net/http"
21+
"strings"
22+
23+
"github.com/astaxie/beego"
24+
bctx "github.com/astaxie/beego/context"
25+
"github.com/getkin/kin-openapi/openapi3"
26+
"github.com/getkin/kin-openapi/openapi3filter"
27+
"github.com/golang/glog"
28+
myctx "github.com/opensds/opensds/pkg/context"
29+
)
30+
31+
// Factory returns a fiter function of api request
32+
func Factory(filename string) beego.FilterFunc {
33+
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromFile(filename)
34+
if err != nil {
35+
glog.Warningf("error loading %s api swagger file: %s", filename, err)
36+
return func(httpCtx *bctx.Context) {}
37+
}
38+
39+
// Server is not required for finding route
40+
swagger.Servers = nil
41+
router := openapi3filter.NewRouter().WithSwagger(swagger)
42+
return func(httpCtx *bctx.Context) {
43+
req := httpCtx.Request
44+
route, pathParams, err := router.FindRoute(req.Method, req.URL)
45+
if err != nil {
46+
glog.Errorf("failed to find route from swagger: %s", err)
47+
myctx.HttpError(httpCtx, http.StatusBadRequest, "failed to find route %s:%s from swagger: %s", req.Method, req.URL, err)
48+
}
49+
50+
requestValidationInput := &openapi3filter.RequestValidationInput{
51+
Request: req,
52+
PathParams: pathParams,
53+
Route: route,
54+
}
55+
if err := openapi3filter.ValidateRequest(context.Background(), requestValidationInput); err != nil {
56+
errMsg := ""
57+
switch e := err.(type) {
58+
case *openapi3filter.RequestError:
59+
// Retrieve first line of err message
60+
errMsg = strings.Split(e.Error(), "\n")[0]
61+
default:
62+
errMsg = fmt.Sprintf("%s", err)
63+
}
64+
glog.Errorf("invalid request: %s", errMsg)
65+
myctx.HttpError(httpCtx, http.StatusBadRequest, "%s", errMsg)
66+
}
67+
}
68+
}

pkg/api/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/opensds/opensds/pkg/api/filter/accesslog"
2929
"github.com/opensds/opensds/pkg/api/filter/auth"
3030
"github.com/opensds/opensds/pkg/api/filter/context"
31+
"github.com/opensds/opensds/pkg/api/filter/validation"
3132
cfg "github.com/opensds/opensds/pkg/utils/config"
3233
"github.com/opensds/opensds/pkg/utils/constants"
3334

@@ -77,6 +78,7 @@ func Run(apiServerCfg cfg.OsdsApiServer) {
7778
beego.InsertFilter(pattern, beego.BeforeExec, context.Factory())
7879
beego.InsertFilter(pattern, beego.BeforeExec, auth.Factory())
7980
beego.InsertFilter("*", beego.BeforeExec, accesslog.Factory())
81+
beego.InsertFilter("*", beego.BeforeExec, validation.Factory(apiServerCfg.ApiSpecPath))
8082

8183
// start service
8284
beego.Run(apiServerCfg.ApiEndpoint)

pkg/utils/config/config_define.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Default struct{}
2121
type OsdsApiServer struct {
2222
ApiEndpoint string `conf:"api_endpoint,localhost:50040"`
2323
AuthStrategy string `conf:"auth_strategy,noauth"`
24+
ApiSpecPath string `conf:"api_spec_path,/etc/opensds/swagger.yaml"`
2425
Daemon bool `conf:"daemon,false"`
2526
PolicyPath string `conf:"policy_path,/etc/opensds/policy.json"`
2627
LogFlushFrequency time.Duration `conf:"log_flush_frequency,5s"` // Default value is 5s

pkg/utils/constants/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const (
2828
AuthTokenHeader = "X-Auth-Token"
2929
SubjectTokenHeader = "X-Subject-Token"
3030

31+
// ContentType for api
32+
ContentType = "application/json"
33+
3134
// OpenSDS current api version
3235
APIVersion = "v1beta"
3336

test/e2e/e2e_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,11 @@ func prepareHost(t *testing.T) (*model.HostSpec, error) {
412412
t.Log("Start preparing host...")
413413
hostName, _ := os.Hostname()
414414
var body = &model.HostSpec{
415-
HostName: hostName,
416-
IP: getHostIp(),
417-
OsType: runtime.GOOS,
415+
HostName: hostName,
416+
IP: getHostIp(),
417+
OsType: runtime.GOOS,
418+
AccessMode: "agentless",
419+
AvailabilityZones: []string{"default"},
418420
Initiators: []*model.Initiator{
419421
&model.Initiator{
420422
PortName: localIqn,

0 commit comments

Comments
 (0)