-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathsync.go
More file actions
312 lines (288 loc) · 10.8 KB
/
sync.go
File metadata and controls
312 lines (288 loc) · 10.8 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cmd
import (
"fmt"
"math"
"os"
"slices"
"strings"
apiAuth "github.com/cloudquery/cloudquery-api-go/auth"
"github.com/cloudquery/cloudquery/cli/internal/auth"
"github.com/cloudquery/cloudquery/cli/internal/specs/v0"
"github.com/cloudquery/plugin-pb-go/managedplugin"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
const (
syncShort = "Sync resources from configured source plugins to destinations"
syncExample = `# Sync resources from configuration in a directory
cloudquery sync ./directory
# Sync resources from directories and files
cloudquery sync ./directory ./aws.yml ./pg.yml
`
)
func NewCmdSync() *cobra.Command {
cmd := &cobra.Command{
Use: "sync [files or directories]",
Short: syncShort,
Long: syncShort,
Example: syncExample,
Args: cobra.MinimumNArgs(1),
RunE: sync,
}
cmd.Flags().Bool("no-migrate", false, "Disable auto-migration before sync. By default, sync runs a migration before syncing resources.")
cmd.Flags().String("license", "", "set offline license file")
return cmd
}
// findMaxCommonVersion finds the max common version between protocol versions supported by a plugin and those supported by the CLI.
// If all plugin versions are lower than min CLI supported version, it returns -1.
// If all plugin versions are higher than max CLI supported version, it returns -2.
// In this way it is possible tell whether the source or the CLI needs to be updated:
// if -1, the source needs to be updated or the CLI downgraded;
// if -2, the CLI needs to be updated or the source downgraded.
func findMaxCommonVersion(pluginSupported []int, cliSupported []int) int {
if len(pluginSupported) == 0 {
return -1
}
minCLISupported, maxCLISupported := math.MaxInt32, -1
for _, v := range cliSupported {
if v < minCLISupported {
minCLISupported = v
}
if v > maxCLISupported {
maxCLISupported = v
}
}
minVersion := math.MaxInt32
maxCommon := -1
for _, v := range pluginSupported {
if v < minVersion {
minVersion = v
}
if v > maxCommon && slices.Contains(cliSupported, v) {
maxCommon = v
}
}
if maxCommon == -1 && minVersion > maxCLISupported {
return -2
}
return maxCommon
}
func sync(cmd *cobra.Command, args []string) error {
cqDir, err := cmd.Flags().GetString("cq-dir")
if err != nil {
return err
}
noMigrate, err := cmd.Flags().GetBool("no-migrate")
if err != nil {
return err
}
licenseFile, err := cmd.Flags().GetString("license")
if err != nil {
return err
}
// in the cloud sync environment, we pass only the relevant environment variables to the plugin
isolatePluginEnvironment := apiAuth.NewTokenClient().GetTokenType() == apiAuth.SyncRunAPIKey
ctx := cmd.Context()
log.Info().Strs("args", args).Msg("Loading spec(s)")
fmt.Printf("Loading spec(s) from %s\n", strings.Join(args, ", "))
specReader, err := specs.NewSpecReader(args)
if err != nil {
return fmt.Errorf("failed to load spec(s) from %s. Error: %w", strings.Join(args, ", "), err)
}
sources := specReader.Sources
destinations := specReader.Destinations
sourcePluginClients := make(managedplugin.Clients, 0)
defer func() {
if err := sourcePluginClients.Terminate(); err != nil {
fmt.Println(err)
}
}()
authToken, err := auth.GetAuthTokenIfNeeded(log.Logger, sources, destinations)
if err != nil {
return fmt.Errorf("failed to get auth token: %w", err)
}
teamName, err := auth.GetTeamForToken(authToken)
if err != nil {
return fmt.Errorf("failed to get team name from token: %w", err)
}
// in a cloud sync environment, we pass only the relevant environment variables to the plugin
osEnviron := os.Environ()
for _, source := range sources {
opts := []managedplugin.Option{
managedplugin.WithLogger(log.Logger),
managedplugin.WithOtelEndpoint(source.OtelEndpoint),
managedplugin.WithAuthToken(authToken.Value),
managedplugin.WithTeamName(teamName),
managedplugin.WithLicenseFile(licenseFile),
}
if cqDir != "" {
opts = append(opts, managedplugin.WithDirectory(cqDir))
}
if disableSentry {
opts = append(opts, managedplugin.WithNoSentry())
}
if source.OtelEndpointInsecure {
opts = append(opts, managedplugin.WithOtelEndpointInsecure())
}
cfg := managedplugin.Config{
Name: source.Name,
Registry: SpecRegistryToPlugin(source.Registry),
Version: source.Version,
Path: source.Path,
DockerAuth: source.DockerRegistryAuthToken,
}
if isolatePluginEnvironment {
cfg.Environment = filterPluginEnv(osEnviron, source.Name, "source")
}
sourcePluginClient, err := managedplugin.NewClient(ctx, managedplugin.PluginSource, cfg, opts...)
if err != nil {
return enrichClientError(managedplugin.Clients{}, []bool{source.RegistryInferred()}, err)
}
sourcePluginClients = append(sourcePluginClients, sourcePluginClient)
}
destinationPluginClients := make(managedplugin.Clients, 0)
defer func() {
if err := destinationPluginClients.Terminate(); err != nil {
fmt.Println(err)
}
}()
for _, destination := range destinations {
opts := []managedplugin.Option{
managedplugin.WithLogger(log.Logger),
managedplugin.WithAuthToken(authToken.Value),
managedplugin.WithTeamName(teamName),
managedplugin.WithLicenseFile(licenseFile),
}
if cqDir != "" {
opts = append(opts, managedplugin.WithDirectory(cqDir))
}
if disableSentry {
opts = append(opts, managedplugin.WithNoSentry())
}
cfg := managedplugin.Config{
Name: destination.Name,
Registry: SpecRegistryToPlugin(destination.Registry),
Version: destination.Version,
Path: destination.Path,
DockerAuth: destination.DockerRegistryAuthToken,
}
if isolatePluginEnvironment {
cfg.Environment = filterPluginEnv(osEnviron, destination.Name, "destination")
}
destPluginClient, err := managedplugin.NewClient(ctx, managedplugin.PluginDestination, cfg, opts...)
if err != nil {
return enrichClientError(managedplugin.Clients{}, []bool{destination.RegistryInferred()}, err)
}
destinationPluginClients = append(destinationPluginClients, destPluginClient)
}
for _, source := range sources {
cl := sourcePluginClients.ClientByName(source.Name)
versions, err := cl.Versions(ctx)
if err != nil {
return fmt.Errorf("failed to get source versions: %w", err)
}
maxVersion := findMaxCommonVersion(versions, []int{0, 1, 2, 3})
var destinationClientsForSource []*managedplugin.Client
var destinationForSourceSpec []specs.Destination
var backendClientForSource *managedplugin.Client
var destinationForSourceBackendSpec *specs.Destination
for _, destination := range destinations {
if slices.Contains(source.Destinations, destination.Name) {
destinationClientsForSource = append(destinationClientsForSource, destinationPluginClients.ClientByName(destination.Name))
destinationForSourceSpec = append(destinationForSourceSpec, *destination)
continue
}
// if the destination is specified as a backend, but not used as a destination, then we initialize it separately
if source.BackendOptions != nil && strings.Contains(source.BackendOptions.Connection, "@@plugins."+destination.Name+".") {
backendClientForSource = destinationPluginClients.ClientByName(destination.Name)
destinationForSourceBackendSpec = destination
}
}
switch maxVersion {
case 3:
// for backwards-compatibility, check for old fields and move them into the spec, log a warning
warnings := specReader.GetSourceWarningsByName(source.Name)
for field, msg := range warnings {
log.Warn().Str("source", source.Name).Str("field", field).Msg(msg)
}
for _, destination := range destinationClientsForSource {
versions, err := destination.Versions(ctx)
if err != nil {
return fmt.Errorf("failed to get destination versions: %w", err)
}
if !slices.Contains(versions, 3) {
return fmt.Errorf("destination plugin %[1]s does not support CloudQuery protocol version 3, required by the %[2]s source plugin. Please upgrade to a newer version of the %[1]s destination plugin", destination.Name(), source.Name)
}
destWarnings := specReader.GetDestinationWarningsByName(source.Name)
for field, msg := range destWarnings {
log.Warn().Str("destination", destination.Name()).Str("field", field).Msg(msg)
}
}
src := v3source{
client: cl,
spec: *source,
}
dests := make([]v3destination, 0, len(destinationClientsForSource))
for i, destination := range destinationClientsForSource {
dests = append(dests, v3destination{
client: destination,
spec: destinationForSourceSpec[i],
})
}
var backend *v3destination
if backendClientForSource != nil && destinationForSourceBackendSpec != nil {
backend = &v3destination{
client: backendClientForSource,
spec: *destinationForSourceBackendSpec,
}
}
if err := syncConnectionV3(ctx, src, dests, backend, invocationUUID.String(), noMigrate); err != nil {
return fmt.Errorf("failed to sync v3 source %s: %w", cl.Name(), err)
}
case 2:
destinationsVersions := make([][]int, 0, len(destinationClientsForSource))
for _, destination := range destinationClientsForSource {
versions, err := destination.Versions(ctx)
if err != nil {
return fmt.Errorf("failed to get destination versions: %w", err)
}
if !slices.Contains(versions, 1) {
return fmt.Errorf("destination plugin %[1]s does not support CloudQuery SDK version 1. Please upgrade to a newer version of the %[1]s destination plugin", destination.Name())
}
destinationsVersions = append(destinationsVersions, versions)
}
if err := syncConnectionV2(ctx, cl, destinationClientsForSource, *source, destinationForSourceSpec, invocationUUID.String(), noMigrate, destinationsVersions); err != nil {
return fmt.Errorf("failed to sync v2 source %s: %w", cl.Name(), err)
}
case 1:
if err := syncConnectionV1(ctx, cl, destinationClientsForSource, *source, destinationForSourceSpec, invocationUUID.String(), noMigrate); err != nil {
return fmt.Errorf("failed to sync v1 source %s: %w", cl.Name(), err)
}
case 0:
return fmt.Errorf("please upgrade source %v or use an older CLI version, between v3.0.1 and v3.5.3", source.Name)
case -1:
return fmt.Errorf("please upgrade source %v or use an older CLI version, < v3.0.1", source.Name)
case -2:
return fmt.Errorf("please upgrade CLI or downgrade source to sync %v", source.Name)
default:
return fmt.Errorf("unknown source version %d", maxVersion)
}
}
return nil
}
func filterPluginEnv(environ []string, pluginName, kind string) []string {
env := make([]string, 0)
cleanName := strings.ReplaceAll(pluginName, "-", "_")
prefix := strings.ToUpper("__" + kind + "_" + cleanName + "__")
for _, v := range environ {
switch {
case strings.HasPrefix(v, "CLOUDQUERY_API_KEY="),
strings.HasPrefix(v, "_CQ_TEAM_NAME="),
strings.HasPrefix(v, "HOME="):
env = append(env, v)
case strings.HasPrefix(v, prefix):
env = append(env, strings.TrimPrefix(v, prefix))
}
}
return env
}