66 "fmt"
77 "io/ioutil"
88 "net/http"
9+ "net/http/httptest"
910 "strings"
1011
1112 log "github.com/sirupsen/logrus"
@@ -17,6 +18,7 @@ import (
1718 "github.com/appbaseio/arc/middleware/validate"
1819 "github.com/appbaseio/arc/model/acl"
1920 "github.com/appbaseio/arc/model/category"
21+ "github.com/appbaseio/arc/model/index"
2022 "github.com/appbaseio/arc/model/op"
2123 "github.com/appbaseio/arc/model/permission"
2224 "github.com/appbaseio/arc/plugins/auth"
@@ -49,7 +51,7 @@ func list() []middleware.Middleware {
4951 validate .ACL (),
5052 validate .Operation (),
5153 validate .PermissionExpiry (),
52- transformRequest ,
54+ intercept ,
5355 }
5456}
5557
@@ -123,7 +125,7 @@ func classifyOp(h http.HandlerFunc) http.HandlerFunc {
123125 }
124126}
125127
126- func transformRequest (h http.HandlerFunc ) http.HandlerFunc {
128+ func intercept (h http.HandlerFunc ) http.HandlerFunc {
127129 return func (w http.ResponseWriter , req * http.Request ) {
128130 ctx := req .Context ()
129131 reqACL , err := acl .FromContext (ctx )
@@ -138,75 +140,104 @@ func transformRequest(h http.HandlerFunc) http.HandlerFunc {
138140 reqPermission , err := permission .FromContext (ctx )
139141 if err != nil {
140142 log .Errorln (logTag , ":" , err )
141- h (w , req )
142- return
143- }
144- sources := make (map [string ]interface {})
145- var Includes , Excludes []string
146- Includes = reqPermission .Includes
147- Excludes = reqPermission .Excludes
148- if len (Includes ) > 0 {
149- sources ["includes" ] = Includes
150- }
151- if len (Excludes ) > 0 {
152- sources ["excludes" ] = Excludes
153- }
154- _ , isExcludesPresent := sources ["excludes" ]
155- isEmpty := len (Includes ) == 0 && len (Excludes ) == 0
156- isDefaultInclude := len (Includes ) > 0 && Includes [0 ] == "*"
157- shouldApplyFilters := ! isEmpty && (! isDefaultInclude || isExcludesPresent )
158- if shouldApplyFilters {
159- if isMsearch {
160- // Handle the _msearch requests
161- body , err := ioutil .ReadAll (req .Body )
162- if err != nil {
163- log .Errorln (logTag , ":" , err )
164- util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
165- return
166- }
167- var reqBodyString = string (body )
168- splitReq := strings .Split (reqBodyString , "\n " )
169- var modifiedBodyString string
170- for index , element := range splitReq {
171- if index % 2 == 1 { // even lines
172- var reqBody = make (map [string ]interface {})
173- err := json .Unmarshal ([]byte (element ), & reqBody )
174- if err != nil {
175- log .Errorln (logTag , ":" , err )
176- util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
177- return
178- }
179- reqBody ["_source" ] = sources
180- raw , err := json .Marshal (reqBody )
181- if err != nil {
182- log .Errorln (logTag , ":" , err )
183- util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
184- return
143+ } else {
144+ sources := make (map [string ]interface {})
145+ var Includes , Excludes []string
146+ Includes = reqPermission .Includes
147+ Excludes = reqPermission .Excludes
148+ if len (Includes ) > 0 {
149+ sources ["includes" ] = Includes
150+ }
151+ if len (Excludes ) > 0 {
152+ sources ["excludes" ] = Excludes
153+ }
154+ _ , isExcludesPresent := sources ["excludes" ]
155+ isEmpty := len (Includes ) == 0 && len (Excludes ) == 0
156+ isDefaultInclude := len (Includes ) > 0 && Includes [0 ] == "*"
157+ shouldApplyFilters := ! isEmpty && (! isDefaultInclude || isExcludesPresent )
158+ if shouldApplyFilters {
159+ if isMsearch {
160+ // Handle the _msearch requests
161+ body , err := ioutil .ReadAll (req .Body )
162+ if err != nil {
163+ log .Errorln (logTag , ":" , err )
164+ util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
165+ return
166+ }
167+ var reqBodyString = string (body )
168+ splitReq := strings .Split (reqBodyString , "\n " )
169+ var modifiedBodyString string
170+ for index , element := range splitReq {
171+ if index % 2 == 1 { // even lines
172+ var reqBody = make (map [string ]interface {})
173+ err := json .Unmarshal ([]byte (element ), & reqBody )
174+ if err != nil {
175+ log .Errorln (logTag , ":" , err )
176+ util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
177+ return
178+ }
179+ reqBody ["_source" ] = sources
180+ raw , err := json .Marshal (reqBody )
181+ if err != nil {
182+ log .Errorln (logTag , ":" , err )
183+ util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
184+ return
185+ }
186+ modifiedBodyString += string (raw )
187+ } else {
188+ modifiedBodyString += element
185189 }
186- modifiedBodyString += string (raw )
187- } else {
188- modifiedBodyString += element
190+ modifiedBodyString += "\n "
189191 }
190- modifiedBodyString += "\n "
191- }
192- modifiedBody := []byte (modifiedBodyString )
193- req .Body = ioutil .NopCloser (bytes .NewReader (modifiedBody ))
194- } else {
195- body , err := ioutil .ReadAll (req .Body )
196- if err != nil {
197- log .Errorln (logTag , ":" , err )
198- util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
199- return
192+ modifiedBody := []byte (modifiedBodyString )
193+ req .Body = ioutil .NopCloser (bytes .NewReader (modifiedBody ))
194+ } else {
195+ body , err := ioutil .ReadAll (req .Body )
196+ if err != nil {
197+ log .Errorln (logTag , ":" , err )
198+ util .WriteBackError (w , err .Error (), http .StatusInternalServerError )
199+ return
200+ }
201+ d := json .NewDecoder (ioutil .NopCloser (bytes .NewReader (body )))
202+ reqBody := make (map [string ]interface {})
203+ d .Decode (& reqBody )
204+ reqBody ["_source" ] = sources
205+ modifiedBody , _ := json .Marshal (reqBody )
206+ req .Body = ioutil .NopCloser (bytes .NewReader (modifiedBody ))
200207 }
201- d := json .NewDecoder (ioutil .NopCloser (bytes .NewReader (body )))
202- reqBody := make (map [string ]interface {})
203- d .Decode (& reqBody )
204- reqBody ["_source" ] = sources
205- modifiedBody , _ := json .Marshal (reqBody )
206- req .Body = ioutil .NopCloser (bytes .NewReader (modifiedBody ))
207208 }
208209 }
210+
209211 }
210- h (w , req )
212+
213+ resp := httptest .NewRecorder ()
214+ indices , err := index .FromContext (req .Context ())
215+ h (resp , req )
216+
217+ // Copy the response to writer
218+ for k , v := range resp .Header () {
219+ w .Header ()[k ] = v
220+ }
221+
222+ result := resp .Result ()
223+ body , err2 := ioutil .ReadAll (result .Body )
224+ if err2 != nil {
225+ log .Errorln (logTag , ":" , err2 )
226+ util .WriteBackError (w , "error reading response body" , http .StatusInternalServerError )
227+ return
228+ }
229+ for _ , index := range indices {
230+ alias := classify .GetIndexAlias (index )
231+ if alias != "" {
232+ body = bytes .Replace (body , []byte (index ), []byte (alias ), - 1 )
233+ continue
234+ }
235+ // if alias is present in url get index name from cache
236+ indexName := classify .GetAliasIndex (index )
237+ if indexName != "" {
238+ body = bytes .Replace (body , []byte (indexName ), []byte (index ), - 1 )
239+ }
240+ }
241+ util .WriteBackRaw (w , body , http .StatusOK )
211242 }
212243}
0 commit comments