Support all services types in the curl subcommand
Control plane access depends on enabling Auth0 Device Flow.
package cmd
import (
+ "errors"
"fmt"
"log"
"os"
"strings"
"github.com/spf13/cobra"
+ "github.com/vespa-engine/vespa/client/go/auth0"
"github.com/vespa-engine/vespa/client/go/curl"
+ "github.com/vespa-engine/vespa/client/go/vespa"
)
var curlDryRun bool
+var curlService string
func init() {
rootCmd.AddCommand(curlCmd)
curlCmd.Flags().BoolVarP(&curlDryRun, "dry-run", "n", false, "Print the curl command that would be executed")
+ curlCmd.Flags().StringVarP(&curlService, "service", "s", "query", "Which service to query")
}
var curlCmd = &cobra.Command{
@@ -42,26 +47,46 @@ $ vespa curl -- -v --data-urlencode "yql=select * from music where album contain
if err != nil {
return err
}
- privateKeyFile, err := cfg.PrivateKeyPath(app)
+ service, err := getService(curlService, 0, "")
if err != nil {
return err
}
- certificateFile, err := cfg.CertificatePath(app)
+ url := joinURL(service.BaseURL, args[len(args)-1])
+ rawArgs := args[:len(args)-1]
+ c, err := curl.RawArgs(url, rawArgs...)
if err != nil {
return err
}
- service, err := getService("query", 0, "")
+ switch curlService {
+ case "deploy":
+ if !vespa.Auth0AccessTokenEnabled() {
+ return errors.New("accessing control plane using curl subcommand is only supported for Auth0 device flow")
+ }
+ t, err := getTarget()
if err != nil {
return err
}
- url := joinURL(service.BaseURL, args[len(args)-1])
- rawArgs := args[:len(args)-1]
- c, err := curl.RawArgs(url, rawArgs...)
+ if t.Type() == "cloud" {
+ if err := addCloudAuth0Authentication(cfg, c); err != nil {
+ return err
+ }
+ }
+ case "document":
+ fallthrough
+ case "query":
+ privateKeyFile, err := cfg.PrivateKeyPath(app)
+ if err != nil {
+ return err
+ }
+ certificateFile, err := cfg.CertificatePath(app)
if err != nil {
return err
}
c.PrivateKey = privateKeyFile
c.Certificate = certificateFile
+ default:
+ return fmt.Errorf("service not found: %s", curlService)
+ }
if curlDryRun {
log.Print(c.String())
@@ -74,6 +99,20 @@ $ vespa curl -- -v --data-urlencode "yql=select * from music where album contain
},
}
+func addCloudAuth0Authentication(cfg *Config, c *curl.Command) error {
+ a, err := auth0.GetAuth0(cfg.AuthConfigPath(), getSystemName(), getApiURL())
+ if err != nil {
+ return err
+ }
+
+ system, err := a.PrepareSystem(auth0.ContextWithCancel())
+ if err != nil {
+ return err
+ }
+ c.Header("Authorization", "Bearer "+system.AccessToken)
+ return nil
+}
+
func joinURL(baseURL, path string) string {
baseURL = strings.TrimSuffix(baseURL, "/")
path = strings.TrimPrefix(path, "/")
