Datasources: provide generic function to extract custom headers (#66738)

This commit is contained in:
Jean-Philippe Quéméner
2023-04-19 17:04:30 +02:00
committed by GitHub
parent 772ddbc3c0
commit 42cdec369d
6 changed files with 97 additions and 3 deletions
+11 -2
View File
@@ -561,8 +561,8 @@ func (s *Service) getCustomHeaders(jsonData *simplejson.Json, decryptedValues ma
index := 0
for {
index++
headerNameSuffix := fmt.Sprintf("httpHeaderName%d", index)
headerValueSuffix := fmt.Sprintf("httpHeaderValue%d", index)
headerNameSuffix := fmt.Sprintf("%s%d", datasources.CustomHeaderName, index)
headerValueSuffix := fmt.Sprintf("%s%d", datasources.CustomHeaderValue, index)
key := jsonData.Get(headerNameSuffix).MustString()
if key == "" {
@@ -651,3 +651,12 @@ func readQuotaConfig(cfg *setting.Cfg) (*quota.Map, error) {
limits.Set(orgQuotaTag, cfg.Quota.Org.DataSource)
return limits, nil
}
// CustomerHeaders returns the custom headers specified in the datasource. The context is used for the decryption operation that might use the store, so consider setting an acceptable timeout for your use case.
func (s *Service) CustomHeaders(ctx context.Context, ds *datasources.DataSource) (map[string]string, error) {
values, err := s.SecretsService.DecryptJsonData(ctx, ds.SecureJsonData)
if err != nil {
return nil, fmt.Errorf("failed to get custom headers: %w", err)
}
return s.getCustomHeaders(ds.JsonData, values), nil
}