Skip to content

Commit 0a56fec

Browse files
Add support for service account impersonation
1 parent e00129f commit 0a56fec

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

pkg/bigquery/http_client.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,35 @@ func getMiddleware(settings types.BigQuerySettings, routePath string) (httpclien
5050
var provider tokenprovider.TokenProvider
5151
switch settings.AuthenticationType {
5252
case "gce":
53-
provider = tokenprovider.NewGceAccessTokenProvider(providerConfig)
53+
if settings.UsingImpersonation {
54+
providerConfig.TargetPrincipal = settings.ServiceAccountToImpersonate
55+
provider = tokenprovider.NewImpersonatedGceAccessTokenProvider(providerConfig)
56+
} else {
57+
provider = tokenprovider.NewGceAccessTokenProvider(providerConfig)
58+
}
59+
5460
case "jwt":
5561
err := validateDataSourceSettings(settings)
5662

5763
if err != nil {
5864
return nil, err
5965
}
60-
61-
providerConfig.JwtTokenConfig = &tokenprovider.JwtTokenConfig{
62-
Email: settings.ClientEmail,
63-
URI: settings.TokenUri,
64-
PrivateKey: []byte(settings.PrivateKey),
66+
if settings.UsingImpersonation {
67+
providerConfig.TargetPrincipal = settings.ServiceAccountToImpersonate
68+
providerConfig.JwtTokenConfig = &tokenprovider.JwtTokenConfig{
69+
Email: settings.ClientEmail,
70+
URI: settings.TokenUri,
71+
PrivateKey: []byte(settings.PrivateKey),
72+
}
73+
provider = tokenprovider.NewImpersonatedJwtAccessTokenProvider(providerConfig)
74+
} else {
75+
providerConfig.JwtTokenConfig = &tokenprovider.JwtTokenConfig{
76+
Email: settings.ClientEmail,
77+
URI: settings.TokenUri,
78+
PrivateKey: []byte(settings.PrivateKey),
79+
}
80+
provider = tokenprovider.NewJwtAccessTokenProvider(providerConfig)
6581
}
66-
provider = tokenprovider.NewJwtAccessTokenProvider(providerConfig)
6782
}
6883

6984
return tokenprovider.AuthMiddleware(provider), nil

pkg/bigquery/types/types.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import (
77
)
88

99
type BigQuerySettings struct {
10-
DatasourceId int64 `json:"datasourceId"`
11-
ClientEmail string `json:"clientEmail"`
12-
DefaultProject string `json:"defaultProject"`
13-
FlatRateProject string `json:"flatRateProject"`
14-
TokenUri string `json:"tokenUri"`
15-
QueryPriority string `json:"queryPriority"`
16-
ProcessingLocation string `json:"processingLocation"`
17-
MaxBytesBilled int64 `json:"MaxBytesBilled,omitempty"`
18-
Updated time.Time
19-
AuthenticationType string `json:"authenticationType"`
20-
PrivateKeyPath string `json:"privateKeyPath"`
21-
ServiceEndpoint string `json:"serviceEndpoint"`
10+
DatasourceId int64 `json:"datasourceId"`
11+
ClientEmail string `json:"clientEmail"`
12+
DefaultProject string `json:"defaultProject"`
13+
FlatRateProject string `json:"flatRateProject"`
14+
TokenUri string `json:"tokenUri"`
15+
QueryPriority string `json:"queryPriority"`
16+
ProcessingLocation string `json:"processingLocation"`
17+
MaxBytesBilled int64 `json:"MaxBytesBilled,omitempty"`
18+
Updated time.Time
19+
AuthenticationType string `json:"authenticationType"`
20+
PrivateKeyPath string `json:"privateKeyPath"`
21+
ServiceEndpoint string `json:"serviceEndpoint"`
22+
UsingImpersonation bool `json:"usingImpersonation"`
23+
ServiceAccountToImpersonate string `json:"serviceAccountToImpersonate"`
2224

2325
// Saved in secure JSON
2426
PrivateKey string `json:"-"`

0 commit comments

Comments
 (0)