-
Notifications
You must be signed in to change notification settings - Fork 168
/
gcp.go
194 lines (169 loc) · 5.16 KB
/
gcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package datasource
import (
"context"
"fmt"
"net/url"
"os"
"strings"
"time"
"cloud.google.com/go/bigquery"
cloudspanner "cloud.google.com/go/spanner"
"github.com/k1LoW/duration"
"github.com/k1LoW/tbls/drivers/bq"
"github.com/k1LoW/tbls/drivers/spanner"
"github.com/k1LoW/tbls/schema"
"golang.org/x/oauth2"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
)
const defaultImpersonateServiceAccountLifetimeStr = "300sec"
// AnalyzeBigquery analyze `bq://`
func AnalyzeBigquery(urlstr string) (*schema.Schema, error) {
s := &schema.Schema{}
ctx := context.Background()
client, projectID, datasetID, err := NewBigqueryClient(ctx, urlstr)
if err != nil {
return s, err
}
defer client.Close()
s.Name = fmt.Sprintf("%s:%s", projectID, datasetID)
driver, err := bq.New(ctx, client, datasetID)
if err != nil {
return s, err
}
err = driver.Analyze(s)
if err != nil {
return s, err
}
return s, nil
}
// NewBigqueryClient returns new bigquery.Client
func NewBigqueryClient(ctx context.Context, urlstr string) (*bigquery.Client, string, string, error) {
u, err := url.Parse(urlstr)
if err != nil {
return nil, "", "", err
}
splitted := strings.Split(u.Path, "/")
projectID := u.Host
datasetID := splitted[1]
var options []option.ClientOption
// Setup credential
values := u.Query()
if err := setEnvGoogleApplicationCredentials(values); err != nil {
return nil, "", "", err
}
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" && os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON") != "" {
options = append(options, option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON"))))
}
// Setup impersonate service account configuration
impersonateServiceAccount := getImpersonateServiceAccount()
if impersonateServiceAccount != "" {
lifetime, err := getImpersonateServiceAccountLifetime()
if err != nil {
return nil, "", "", err
}
ts, err := createImpersonationTokenSource(ctx, impersonateServiceAccount, lifetime)
if err != nil {
return nil, "", "", err
}
options = append(options, option.WithTokenSource(ts))
}
client, err := bigquery.NewClient(ctx, projectID, options...)
if err != nil {
return nil, "", "", err
}
return client, projectID, datasetID, err
}
// AnalyzeSpanner analyze `spanner://`
func AnalyzeSpanner(urlstr string) (*schema.Schema, error) {
s := &schema.Schema{}
ctx := context.Background()
client, db, err := NewSpannerClient(ctx, urlstr)
if err != nil {
return s, err
}
defer client.Close() //nolint
s.Name = db
driver, err := spanner.New(ctx, client)
if err != nil {
return s, err
}
err = driver.Analyze(s)
if err != nil {
return s, err
}
return s, nil
}
// NewSpannerClient returns new cloudspanner.Client
func NewSpannerClient(ctx context.Context, urlstr string) (*cloudspanner.Client, string, error) {
u, err := url.Parse(urlstr)
if err != nil {
return nil, "", err
}
splitted := strings.Split(u.Path, "/")
projectID := u.Host
instanceID := splitted[1]
databaseID := splitted[2]
db := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectID, instanceID, databaseID)
var options []option.ClientOption
// Setup credential
values := u.Query()
if err := setEnvGoogleApplicationCredentials(values); err != nil {
return nil, "", err
}
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" && os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON") != "" {
options = append(options, option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON"))))
}
// Setup impersonate service account configuration
impersonateServiceAccount := getImpersonateServiceAccount()
if impersonateServiceAccount != "" {
lifetime, err := getImpersonateServiceAccountLifetime()
if err != nil {
return nil, "", err
}
ts, err := createImpersonationTokenSource(ctx, impersonateServiceAccount, lifetime)
if err != nil {
return nil, "", err
}
options = append(options, option.WithTokenSource(ts))
}
client, err := cloudspanner.NewClient(ctx, db, options...)
if err != nil {
return nil, "", err
}
return client, db, nil
}
func setEnvGoogleApplicationCredentials(values url.Values) error {
keys := []string{
"google_application_credentials",
"credentials",
"creds",
}
for _, k := range keys {
if values.Get(k) != "" {
return os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", values.Get(k))
}
}
return nil
}
func createImpersonationTokenSource(ctx context.Context, impersonateServiceAccount string, impersonateServiceAccountLifetime time.Duration) (oauth2.TokenSource, error) {
return impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: impersonateServiceAccount,
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
Lifetime: impersonateServiceAccountLifetime,
})
}
func getImpersonateServiceAccount() string {
return os.Getenv("GOOGLE_IMPERSONATE_SERVICE_ACCOUNT")
}
func getImpersonateServiceAccountLifetime() (time.Duration, error) {
durationStr := os.Getenv("GOOGLE_IMPERSONATE_SERVICE_ACCOUNT_LIFETIME")
if durationStr == "" {
durationStr = defaultImpersonateServiceAccountLifetimeStr
}
d, err := duration.Parse(durationStr)
if err != nil {
return 0, err
}
return d, nil
}