forked from rexray/gocsi
-
Notifications
You must be signed in to change notification settings - Fork 9
/
middleware.go
252 lines (226 loc) · 7.69 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package gocsi
import (
"strconv"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
csictx "github.com/dell/gocsi/context"
"github.com/dell/gocsi/middleware/logging"
"github.com/dell/gocsi/middleware/requestid"
"github.com/dell/gocsi/middleware/serialvolume"
"github.com/dell/gocsi/middleware/serialvolume/etcd"
"github.com/dell/gocsi/middleware/specvalidator"
"github.com/dell/gocsi/utils"
)
func (sp *StoragePlugin) initInterceptors(ctx context.Context) {
sp.Interceptors = append(sp.Interceptors, sp.injectContext)
log.Debug("enabled context injector")
var (
withReqLogging = sp.getEnvBool(ctx, EnvVarReqLogging)
withRepLogging = sp.getEnvBool(ctx, EnvVarRepLogging)
withDisableLogVolCtx = sp.getEnvBool(ctx, EnvVarLoggingDisableVolCtx)
withSerialVol = sp.getEnvBool(ctx, EnvVarSerialVolAccess)
withSpec = sp.getEnvBool(ctx, EnvVarSpecValidation)
withStgTgtPath = sp.getEnvBool(ctx, EnvVarRequireStagingTargetPath)
withVolContext = sp.getEnvBool(ctx, EnvVarRequireVolContext)
withPubContext = sp.getEnvBool(ctx, EnvVarRequirePubContext)
withCreds = sp.getEnvBool(ctx, EnvVarCreds)
withCredsNewVol = sp.getEnvBool(ctx, EnvVarCredsCreateVol)
withCredsDelVol = sp.getEnvBool(ctx, EnvVarCredsDeleteVol)
withCredsCtrlrPubVol = sp.getEnvBool(ctx, EnvVarCredsCtrlrPubVol)
withCredsCtrlrUnpubVol = sp.getEnvBool(ctx, EnvVarCredsCtrlrUnpubVol)
withCredsNodeStgVol = sp.getEnvBool(ctx, EnvVarCredsNodeStgVol)
withCredsNodePubVol = sp.getEnvBool(ctx, EnvVarCredsNodePubVol)
withDisableFieldLen = sp.getEnvBool(ctx, EnvVarDisableFieldLen)
)
// Enable all cred requirements if the general option is enabled.
if withCreds {
withCredsNewVol = true
withCredsDelVol = true
withCredsCtrlrPubVol = true
withCredsCtrlrUnpubVol = true
withCredsNodeStgVol = true
withCredsNodePubVol = true
}
// Initialize request & response validation to the global validaiton value.
var (
withSpecReq = withSpec
withSpecRep = withSpec
)
log.WithField("withSpec", withSpec).Debug("init req & rep validation")
// If request validation is not enabled explicitly, check to see if it
// should be enabled implicitly.
if !withSpecReq {
withSpecReq = withCreds ||
withStgTgtPath ||
withVolContext ||
withPubContext
log.WithField("withSpecReq", withSpecReq).Debug(
"init implicit req validation")
}
// Check to see if spec request or response validation are overridden.
if v, ok := csictx.LookupEnv(ctx, EnvVarSpecReqValidation); ok {
withSpecReq, _ = strconv.ParseBool(v)
log.WithField("withSpecReq", withSpecReq).Debug("init req validation")
}
if v, ok := csictx.LookupEnv(ctx, EnvVarSpecRepValidation); ok {
withSpecRep, _ = strconv.ParseBool(v)
log.WithField("withSpecRep", withSpecRep).Debug("init rep validation")
}
// Configure logging.
if withReqLogging || withRepLogging {
// Automatically enable request ID injection if logging
// is enabled.
sp.Interceptors = append(sp.Interceptors,
requestid.NewServerRequestIDInjector())
log.Debug("enabled request ID injector")
var (
loggingOpts []logging.Option
w = newLogger(log.Infof)
)
if withDisableLogVolCtx {
loggingOpts = append(loggingOpts, logging.WithDisableLogVolumeContext())
log.Debug("disabled logging of VolumeContext field")
}
if withReqLogging {
loggingOpts = append(loggingOpts, logging.WithRequestLogging(w))
log.Debug("enabled request logging")
}
if withRepLogging {
loggingOpts = append(loggingOpts, logging.WithResponseLogging(w))
log.Debug("enabled response logging")
}
sp.Interceptors = append(sp.Interceptors,
logging.NewServerLogger(loggingOpts...))
}
if withSpecReq || withSpecRep {
var specOpts []specvalidator.Option
if withSpecReq {
specOpts = append(
specOpts,
specvalidator.WithRequestValidation())
log.Debug("enabled spec validator opt: request validation")
}
if withSpecRep {
specOpts = append(
specOpts,
specvalidator.WithResponseValidation())
log.Debug("enabled spec validator opt: response validation")
}
if withCredsNewVol {
specOpts = append(specOpts,
specvalidator.WithRequiresControllerCreateVolumeSecrets())
log.Debug("enabled spec validator opt: requires creds: " +
"CreateVolume")
}
if withCredsDelVol {
specOpts = append(specOpts,
specvalidator.WithRequiresControllerDeleteVolumeSecrets())
log.Debug("enabled spec validator opt: requires creds: " +
"DeleteVolume")
}
if withCredsCtrlrPubVol {
specOpts = append(specOpts,
specvalidator.WithRequiresControllerPublishVolumeSecrets())
log.Debug("enabled spec validator opt: requires creds: " +
"ControllerPublishVolume")
}
if withCredsCtrlrUnpubVol {
specOpts = append(specOpts,
specvalidator.WithRequiresControllerUnpublishVolumeSecrets())
log.Debug("enabled spec validator opt: requires creds: " +
"ControllerUnpublishVolume")
}
if withCredsNodeStgVol {
specOpts = append(specOpts,
specvalidator.WithRequiresNodeStageVolumeSecrets())
log.Debug("enabled spec validator opt: requires creds: " +
"NodeStageVolume")
}
if withCredsNodePubVol {
specOpts = append(specOpts,
specvalidator.WithRequiresNodePublishVolumeSecrets())
log.Debug("enabled spec validator opt: requires creds: " +
"NodePublishVolume")
}
if withStgTgtPath {
specOpts = append(specOpts,
specvalidator.WithRequiresStagingTargetPath())
log.Debug("enabled spec validator opt: " +
"requires starging target path")
}
if withVolContext {
specOpts = append(specOpts,
specvalidator.WithRequiresVolumeContext())
log.Debug("enabled spec validator opt: requires vol context")
}
if withPubContext {
specOpts = append(specOpts,
specvalidator.WithRequiresPublishContext())
log.Debug("enabled spec validator opt: requires pub context")
}
if withDisableFieldLen {
specOpts = append(specOpts,
specvalidator.WithDisableFieldLenCheck())
log.Debug("disabled spec validator opt: field length check")
}
sp.Interceptors = append(sp.Interceptors,
specvalidator.NewServerSpecValidator(specOpts...))
}
if _, ok := csictx.LookupEnv(ctx, EnvVarPluginInfo); ok {
log.Debug("enabled GetPluginInfo interceptor")
sp.Interceptors = append(sp.Interceptors, sp.getPluginInfo)
}
if withSerialVol {
var (
opts []serialvolume.Option
fields = map[string]interface{}{}
)
// Get serial provider's timeout.
if v, _ := csictx.LookupEnv(
ctx, EnvVarSerialVolAccessTimeout); v != "" {
if t, err := time.ParseDuration(v); err == nil {
fields["serialVol.timeout"] = t
opts = append(opts, serialvolume.WithTimeout(t))
}
}
// Check for etcd
if csictx.Getenv(ctx, EnvVarSerialVolAccessEtcdEndpoints) != "" {
p, err := etcd.New(ctx, "", 0, nil)
if err != nil {
log.Fatal(err)
}
opts = append(opts, serialvolume.WithLockProvider(p))
}
sp.Interceptors = append(sp.Interceptors, serialvolume.New(opts...))
log.WithFields(fields).Debug("enabled serial volume access")
}
return
}
func (sp *StoragePlugin) injectContext(
ctx context.Context,
req interface{},
_ *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
return handler(csictx.WithLookupEnv(ctx, sp.lookupEnv), req)
}
func (sp *StoragePlugin) getPluginInfo(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
if sp.pluginInfo.Name == "" {
return handler(ctx, req)
}
_, service, method, err := utils.ParseMethod(info.FullMethod)
if err != nil {
return nil, err
}
if service != "Identity" || method != "GetPluginInfo" {
return handler(ctx, req)
}
return &sp.pluginInfo, nil
}