Skip to content

Commit

Permalink
Merge pull request #51 from intergral/auth
Browse files Browse the repository at this point in the history
fix(auth): propagate headers from source request
  • Loading branch information
Umaaz authored May 6, 2024
2 parents 7b6554d + d6248e4 commit bd97429
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<!-- 1.0.4 Start -->

# 1.0.4 (06/05/2024)

- **[BUGFIX]**: Fix auth propagation when using cookie auth [#51](https://github.com/intergral/deep/pull/51) [@Umaaz](https://github.com/Umaaz)

<!-- 1.0.4 END -->

<!-- 1.0.3 Start -->

# 1.0.3 (17/04/2024)
Expand Down
37 changes: 24 additions & 13 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ func (d *DeepDatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
var res backend.DataResponse
switch q.QueryType {
case "deepql":
res = d.queryDeepQl(ctx, req.PluginContext, q)
res = d.queryDeepQl(ctx, req, q)
case "byid":
res = d.queryByID(ctx, req.PluginContext, q)
res = d.queryByID(ctx, req, q)
case "tracepoint":
res = d.queryTracepoint(ctx, req.PluginContext, q)
res = d.queryTracepoint(ctx, req, q)
}

// save the response in a hashmap
Expand All @@ -126,25 +126,26 @@ type queryModel struct {
Query string `json:"query"`
}

func (d *DeepDatasource) queryTracepoint(_ context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
func (d *DeepDatasource) queryTracepoint(_ context.Context, req *backend.QueryDataRequest, query backend.DataQuery) backend.DataResponse {

url := fmt.Sprintf("%v/api/tracepoints", pCtx.DataSourceInstanceSettings.URL)
url := fmt.Sprintf("%v/api/tracepoints", req.PluginContext.DataSourceInstanceSettings.URL)

request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("cannot build request: %v", err.Error()))
}
attachHeaders(request, req.Headers)
request.Header.Set("Accept", "application/protobuf")

response, err := d.client.Do(request)
if err != nil {
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("request failed: %v", err.Error()))
}

return d.processProtoTracepointResponse(query, response, pCtx)
return d.processProtoTracepointResponse(query, response, req.PluginContext)
}

func (d *DeepDatasource) queryByID(_ context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
func (d *DeepDatasource) queryByID(_ context.Context, req *backend.QueryDataRequest, query backend.DataQuery) backend.DataResponse {

// Unmarshal the JSON into our queryModel.
var qm queryModel
Expand All @@ -154,12 +155,13 @@ func (d *DeepDatasource) queryByID(_ context.Context, pCtx backend.PluginContext
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("json unmarshal: %v", err.Error()))
}

url := fmt.Sprintf("%v/api/snapshots/%v", pCtx.DataSourceInstanceSettings.URL, qm.Query)
url := fmt.Sprintf("%v/api/snapshots/%v", req.PluginContext.DataSourceInstanceSettings.URL, qm.Query)

request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("cannot build request: %v", err.Error()))
}
attachHeaders(request, req.Headers)
request.Header.Set("Accept", "application/protobuf")

response, err := d.client.Do(request)
Expand Down Expand Up @@ -413,6 +415,7 @@ func (d *DeepDatasource) CheckHealth(_ context.Context, req *backend.CheckHealth
}, nil
}

attachHeaders(request, req.Headers)
response, err := d.client.Do(request)

if err != nil {
Expand Down Expand Up @@ -454,7 +457,7 @@ func (d *DeepDatasource) CheckHealth(_ context.Context, req *backend.CheckHealth
}, nil
}

func (d *DeepDatasource) queryDeepQl(ctx context.Context, pluginContext backend.PluginContext, query backend.DataQuery) backend.DataResponse {
func (d *DeepDatasource) queryDeepQl(ctx context.Context, req *backend.QueryDataRequest, query backend.DataQuery) backend.DataResponse {

// Unmarshal the JSON into our queryModel.
var qm queryModel
Expand All @@ -464,12 +467,14 @@ func (d *DeepDatasource) queryDeepQl(ctx context.Context, pluginContext backend.
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("json unmarshal: %v", err.Error()))
}

url := fmt.Sprintf("%v/api/search", pluginContext.DataSourceInstanceSettings.URL)
url := fmt.Sprintf("%v/api/search", req.PluginContext.DataSourceInstanceSettings.URL)

request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("cannot build request: %v", err.Error()))
}
attachHeaders(request, req.Headers)

values := request.URL.Query()
values.Set("q", qm.Query)
values.Set("limit", strconv.Itoa(int(qm.Limit)))
Expand Down Expand Up @@ -519,7 +524,7 @@ func (d *DeepDatasource) queryDeepQl(ctx context.Context, pluginContext backend.
}

if snapResp.Snapshots != nil {
frame, err := searchResultToFrame(snapResp.Snapshots, pluginContext)
frame, err := searchResultToFrame(snapResp.Snapshots, req.PluginContext)
if err != nil {
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("convertion failed: %v", err.Error()))
}
Expand All @@ -537,16 +542,22 @@ func (d *DeepDatasource) queryDeepQl(ctx context.Context, pluginContext backend.
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("response failed: %v", err.Error()))
}
if pollResponse.Type == "list" {
return d.processTracepointResponse(query, pollResponse.Affected, pluginContext)
return d.processTracepointResponse(query, pollResponse.Affected, req.PluginContext)
}
return d.processTracepointResponse(query, pollResponse.All, pluginContext)
return d.processTracepointResponse(query, pollResponse.All, req.PluginContext)
}

return backend.DataResponse{
Frames: []*data.Frame{},
}
}

func attachHeaders(request *http.Request, headers map[string]string) {
for k, v := range headers {
request.Header.Add(k, v)
}
}

func (d *DeepDatasource) processProtoTracepointResponse(query backend.DataQuery, response *http.Response, pluginContext backend.PluginContext) backend.DataResponse {

defer func(Body io.ReadCloser) {
Expand Down

0 comments on commit bd97429

Please sign in to comment.