aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/cmd/config.go
blob: 447bad14444bd15f20eea8ab59f17aad5f3f751f (plain) (blame)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa config command
// author: bratseth

package cmd

import (
	"crypto/tls"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"

	"github.com/fatih/color"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/vespa-engine/vespa/client/go/auth/auth0"
	"github.com/vespa-engine/vespa/client/go/util"
	"github.com/vespa-engine/vespa/client/go/vespa"
)

const (
	configName = "config"
	configType = "yaml"
)

func newConfigCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "config",
		Short: "Configure persistent values for global flags",
		Long: `Configure persistent values for global flags.

This command allows setting a persistent value for a given global flag. On
future invocations the flag can then be omitted as it is read from the config
file instead.

Configuration is written to $HOME/.vespa by default. This path can be
overridden by setting the VESPA_CLI_HOME environment variable.`,
		DisableAutoGenTag: true,
		SilenceUsage:      false,
		Args:              cobra.MinimumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return fmt.Errorf("invalid command: %s", args[0])
		},
	}
}

func newConfigSetCmd(cli *CLI) *cobra.Command {
	return &cobra.Command{
		Use:   "set option-name value",
		Short: "Set a configuration option.",
		Example: `# Set the target to Vespa Cloud
$ vespa config set target cloud

# Set application, without a specific instance. The instance will be named "default"
$ vespa config set application my-tenant.my-application

# Set application with a specific instance
$ vespa config set application my-tenant.my-application.my-instance

# Set the instance explicitly. This will take precedence over an instance specified as part of the application option.
$ vespa config set instance other-instance`,
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		Args:              cobra.ExactArgs(2),
		RunE: func(cmd *cobra.Command, args []string) error {
			if err := cli.config.set(args[0], args[1]); err != nil {
				return err
			}
			return cli.config.write()
		},
	}
}

func newConfigGetCmd(cli *CLI) *cobra.Command {
	return &cobra.Command{
		Use:   "get [option-name]",
		Short: "Show given configuration option, or all configuration options",
		Example: `$ vespa config get
$ vespa config get target`,
		Args:              cobra.MaximumNArgs(1),
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 { // Print all values
				var flags []string
				for flag := range cli.config.bindings.flag {
					flags = append(flags, flag)
				}
				sort.Strings(flags)
				for _, flag := range flags {
					cli.config.printOption(flag)
				}
			} else {
				cli.config.printOption(args[0])
			}
			return nil
		},
	}
}

type Config struct {
	homeDir     string
	cacheDir    string
	environment map[string]string
	bindings    ConfigBindings
	createDirs  bool
}

type ConfigBindings struct {
	flag        map[string]*cobra.Command
	environment map[string]string
}

type KeyPair struct {
	KeyPair         tls.Certificate
	CertificateFile string
	PrivateKeyFile  string
}

func NewConfigBindings() ConfigBindings {
	return ConfigBindings{
		flag:        make(map[string]*cobra.Command),
		environment: make(map[string]string),
	}
}

func (b *ConfigBindings) bindFlag(name string, command *cobra.Command) {
	b.flag[name] = command
}

func (b *ConfigBindings) bindEnvironment(flagName string, variable string) {
	b.environment[flagName] = variable
}

func loadConfig(environment map[string]string, bindings ConfigBindings) (*Config, error) {
	home, err := vespaCliHome(environment)
	if err != nil {
		return nil, fmt.Errorf("could not detect config directory: %w", err)
	}
	cacheDir, err := vespaCliCacheDir(environment)
	if err != nil {
		return nil, fmt.Errorf("could not detect cache directory: %w", err)
	}
	c := &Config{
		homeDir:     home,
		cacheDir:    cacheDir,
		environment: environment,
		bindings:    bindings,
		createDirs:  true,
	}
	if err := c.load(); err != nil {
		return nil, fmt.Errorf("could not load config: %w", err)
	}
	return c, nil
}

func (c *Config) write() error {
	if err := os.MkdirAll(c.homeDir, 0700); err != nil {
		return err
	}
	configFile := filepath.Join(c.homeDir, configName+"."+configType)
	if !util.PathExists(configFile) {
		if _, err := os.Create(configFile); err != nil {
			return err
		}
	}
	return viper.WriteConfig()
}

func (c *Config) targetType() (string, error) {
	targetType, ok := c.get(targetFlag)
	if !ok {
		return "", fmt.Errorf("target is unset")
	}
	return targetType, nil
}

func (c *Config) application() (vespa.ApplicationID, error) {
	app, ok := c.get(applicationFlag)
	if !ok {
		return vespa.ApplicationID{}, errHint(fmt.Errorf("no application specified"), "Try the --"+applicationFlag+" flag")
	}
	application, err := vespa.ApplicationFromString(app)
	if err != nil {
		return vespa.ApplicationID{}, errHint(err, "application format is <tenant>.<app>[.<instance>]")
	}
	instance, ok := c.get(instanceFlag)
	if ok {
		application.Instance = instance
	}
	return application, nil
}

func (c *Config) deploymentIn(zoneName string, system vespa.System) (vespa.Deployment, error) {
	zone := system.DefaultZone
	var err error
	if zoneName != "" {
		zone, err = vespa.ZoneFromString(zoneName)
		if err != nil {
			return vespa.Deployment{}, err
		}
	}
	app, err := c.application()
	if err != nil {
		return vespa.Deployment{}, err
	}
	return vespa.Deployment{System: system, Application: app, Zone: zone}, nil
}

func (c *Config) certificatePath(app vespa.ApplicationID) (string, error) {
	if override, ok := c.environment["VESPA_CLI_DATA_PLANE_CERT_FILE"]; ok {
		return override, nil
	}
	return c.applicationFilePath(app, "data-plane-public-cert.pem")
}

func (c *Config) privateKeyPath(app vespa.ApplicationID) (string, error) {
	if override, ok := c.environment["VESPA_CLI_DATA_PLANE_KEY_FILE"]; ok {
		return override, nil
	}
	return c.applicationFilePath(app, "data-plane-private-key.pem")
}

func (c *Config) x509KeyPair(app vespa.ApplicationID) (KeyPair, error) {
	cert, certOk := c.environment["VESPA_CLI_DATA_PLANE_CERT"]
	key, keyOk := c.environment["VESPA_CLI_DATA_PLANE_KEY"]
	if certOk && keyOk {
		// Use key pair from environment
		kp, err := tls.X509KeyPair([]byte(cert), []byte(key))
		return KeyPair{KeyPair: kp}, err
	}
	privateKeyFile, err := c.privateKeyPath(app)
	if err != nil {
		return KeyPair{}, err
	}
	certificateFile, err := c.certificatePath(app)
	if err != nil {
		return KeyPair{}, err
	}
	kp, err := tls.LoadX509KeyPair(certificateFile, privateKeyFile)
	if err != nil {
		return KeyPair{}, err
	}
	return KeyPair{
		KeyPair:         kp,
		CertificateFile: certificateFile,
		PrivateKeyFile:  privateKeyFile,
	}, nil
}

func (c *Config) apiKeyPath(tenantName string) string {
	if override, ok := c.get(apiKeyFileFlag); ok {
		return override
	}
	return filepath.Join(c.homeDir, tenantName+".api-key.pem")
}

func (c *Config) authConfigPath() string {
	return filepath.Join(c.homeDir, "auth.json")
}

func (c *Config) readAPIKey(tenantName string) ([]byte, error) {
	if override, ok := c.get(apiKeyFlag); ok {
		return []byte(override), nil
	}
	return os.ReadFile(c.apiKeyPath(tenantName))
}

// useAPIKey returns true if an API key should be used when authenticating with system.
func (c *Config) useAPIKey(cli *CLI, system vespa.System, tenantName string) bool {
	if _, ok := c.get(apiKeyFlag); ok {
		return true
	}
	if _, ok := c.get(apiKeyFileFlag); ok {
		return true
	}
	// If no Auth0 token is created, fall back to tenant api key, but warn that this functionality is deprecated
	// TODO: Remove this when users have had time to migrate over to Auth0 device flow authentication
	if !cli.isCI() {
		a, err := auth0.New(c.authConfigPath(), system.Name, system.URL)
		if err != nil || !a.HasCredentials() {
			cli.printWarning("Use of API key is deprecated", "Authenticate with Auth0 instead: 'vespa auth login'")
			return util.PathExists(c.apiKeyPath(tenantName))
		}
	}
	return false
}

func (c *Config) readSessionID(app vespa.ApplicationID) (int64, error) {
	sessionPath, err := c.applicationFilePath(app, "session_id")
	if err != nil {
		return 0, err
	}
	b, err := os.ReadFile(sessionPath)
	if err != nil {
		return 0, err
	}
	return strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
}

func (c *Config) writeSessionID(app vespa.ApplicationID, sessionID int64) error {
	sessionPath, err := c.applicationFilePath(app, "session_id")
	if err != nil {
		return err
	}
	return os.WriteFile(sessionPath, []byte(fmt.Sprintf("%d\n", sessionID)), 0600)
}

func (c *Config) applicationFilePath(app vespa.ApplicationID, name string) (string, error) {
	appDir := filepath.Join(c.homeDir, app.String())
	if c.createDirs {
		if err := os.MkdirAll(appDir, 0700); err != nil {
			return "", err
		}
	}
	return filepath.Join(appDir, name), nil
}

func (c *Config) load() error {
	viper.SetConfigName(configName)
	viper.SetConfigType(configType)
	viper.AddConfigPath(c.homeDir)
	for option, command := range c.bindings.flag {
		viper.BindPFlag(option, command.PersistentFlags().Lookup(option))
	}
	err := viper.ReadInConfig()
	if _, ok := err.(viper.ConfigFileNotFoundError); ok {
		return nil
	}
	return err
}

func (c *Config) get(option string) (string, bool) {
	if envVar, ok := c.bindings.environment[option]; ok {
		if value, ok := c.environment[envVar]; ok {
			return value, true
		}
	}
	value := viper.GetString(option)
	if value == "" {
		return "", false
	}
	return value, true
}

func (c *Config) set(option, value string) error {
	switch option {
	case targetFlag:
		switch value {
		case vespa.TargetLocal, vespa.TargetCloud, vespa.TargetHosted:
			viper.Set(option, value)
			return nil
		}
		if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
			viper.Set(option, value)
			return nil
		}
	case applicationFlag:
		app, err := vespa.ApplicationFromString(value)
		if err != nil {
			return err
		}
		viper.Set(option, app.String())
		return nil
	case instanceFlag:
		viper.Set(option, value)
		return nil
	case waitFlag:
		if n, err := strconv.Atoi(value); err != nil || n < 0 {
			return fmt.Errorf("%s option must be an integer >= 0, got %q", option, value)
		}
		viper.Set(option, value)
		return nil
	case colorFlag:
		switch value {
		case "auto", "never", "always":
			viper.Set(option, value)
			return nil
		}
	case quietFlag:
		switch value {
		case "true", "false":
			viper.Set(option, value)
			return nil
		}
	case apiKeyFileFlag:
		viper.Set(option, value)
		return nil
	}
	return fmt.Errorf("invalid option or value: %q: %q", option, value)
}

func (c *Config) printOption(option string) {
	value, ok := c.get(option)
	if !ok {
		faintColor := color.New(color.FgWhite, color.Faint)
		value = faintColor.Sprint("<unset>")
	} else {
		value = color.CyanString(value)
	}
	log.Printf("%s = %s", option, value)
}

func vespaCliHome(env map[string]string) (string, error) {
	home := env["VESPA_CLI_HOME"]
	if home == "" {
		userHome, err := os.UserHomeDir()
		if err != nil {
			return "", err
		}
		home = filepath.Join(userHome, ".vespa")
	}
	if err := os.MkdirAll(home, 0700); err != nil {
		return "", err
	}
	return home, nil
}

func vespaCliCacheDir(env map[string]string) (string, error) {
	cacheDir := env["VESPA_CLI_CACHE_DIR"]
	if cacheDir == "" {
		userCacheDir, err := os.UserCacheDir()
		if err != nil {
			return "", err
		}
		cacheDir = filepath.Join(userCacheDir, "vespa")
	}
	if err := os.MkdirAll(cacheDir, 0755); err != nil {
		return "", err
	}
	return cacheDir, nil
}