@@ -7,16 +7,15 @@ package grafana
7
7
8
8
import (
9
9
"context"
10
- "fmt"
11
10
"strings"
12
11
12
+ "github.com/cockroachdb/cockroach/pkg/roachprod/promhelperclient"
13
13
"github.com/cockroachdb/cockroach/pkg/roachprod/roachprodutil"
14
14
"github.com/cockroachdb/cockroach/pkg/util/httputil"
15
15
"github.com/cockroachdb/errors"
16
16
"github.com/go-openapi/strfmt"
17
17
grafana "github.com/grafana/grafana-openapi-client-go/client"
18
18
"github.com/grafana/grafana-openapi-client-go/models"
19
- "google.golang.org/api/idtoken"
20
19
)
21
20
22
21
// newGrafanaClient is a helper function that creates an HTTP client to
@@ -26,30 +25,37 @@ import (
26
25
func newGrafanaClient (
27
26
ctx context.Context , host string , secure bool ,
28
27
) (* grafana.GrafanaHTTPAPI , error ) {
29
- headers := map [string ]string {}
30
28
scheme := "http"
31
29
30
+ // Use the default HTTP client for unsecure Grafana calls.
31
+ grafanaHttpClient := httputil .DefaultClient .Client
32
+
32
33
if secure {
33
34
scheme = "https"
34
35
35
- // Read in the service account key and audience, so we can retrieve the identity token.
36
- if _ , err := roachprodutil . SetServiceAccountCredsEnv ( ctx , false ); err != nil {
37
- return nil , err
38
- }
39
-
40
- token , err := roachprodutil . GetServiceAccountToken ( ctx , idtoken . NewTokenSource )
36
+ // Grafana annotations currently use the same service account
37
+ // and OAuth client ID as the prometheus helper service.
38
+ iapTokenSource , err := roachprodutil . NewIAPTokenSource (roachprodutil. IAPTokenSourceOptions {
39
+ OAuthClientID : promhelperclient . OAuthClientID ,
40
+ ServiceAccountEmail : promhelperclient . ServiceAccountEmail ,
41
+ } )
41
42
if err != nil {
42
43
return nil , err
43
44
}
44
- headers ["Authorization" ] = fmt .Sprintf ("Bearer %s" , token )
45
+
46
+ // Override the default HTTP client with the one
47
+ // that has the IAP token source.
48
+ grafanaHttpClient = iapTokenSource .GetHTTPClient ()
45
49
}
46
50
47
- headers [httputil .ContentTypeHeader ] = httputil .JSONContentType
48
51
cfg := & grafana.TransportConfig {
49
- Host : host ,
50
- BasePath : "/api" ,
51
- Schemes : []string {scheme },
52
- HTTPHeaders : headers ,
52
+ Host : host ,
53
+ BasePath : "/api" ,
54
+ Schemes : []string {scheme },
55
+ HTTPHeaders : map [string ]string {
56
+ httputil .ContentTypeHeader : httputil .JSONContentType ,
57
+ },
58
+ Client : grafanaHttpClient ,
53
59
}
54
60
55
61
return grafana .NewHTTPClientWithConfig (strfmt .Default , cfg ), nil
0 commit comments