Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow some commands to be run without authenticating against the API #364

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"

Expand All @@ -37,4 +38,10 @@ var accountCmd = &cobra.Command{

printer.Account(account)
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}
6 changes: 6 additions & 0 deletions cmd/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func Backups() *cobra.Command {
Use: "backups",
Aliases: []string{"b"},
Short: "Display backups",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

backupsCmd.AddCommand(backupsList, backupsGet)
Expand Down
6 changes: 6 additions & 0 deletions cmd/bareMetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func BareMetal() *cobra.Command {
Aliases: []string{"bm"},
Long: bareMetalLong,
Example: bareMetalExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

bareMetalCmd.AddCommand(
Expand Down
6 changes: 6 additions & 0 deletions cmd/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func Billing() *cobra.Command {
Short: "Display billing information",
Long: billingLong,
Example: billingExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

historyCmd := &cobra.Command{
Expand Down
6 changes: 6 additions & 0 deletions cmd/blockStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func BlockStorageCmd() *cobra.Command {
Aliases: []string{"bs"},
Short: "block storage commands",
Long: `block-storage is used to interact with the block-storage api`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

bsCmd.AddCommand(bsAttach, bsCreate, bsDelete, bsDetach, bsLabelSet, bsList, bsGet, bsResize)
Expand Down
6 changes: 6 additions & 0 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func Database() *cobra.Command { //nolint:funlen
Short: "commands to interact with managed databases on vultr",
Long: databaseLong,
Example: databaseExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

databaseCmd.AddCommand(databaseList, databaseCreate, databaseInfo, databaseUpdate, databaseDelete)
Expand Down
8 changes: 8 additions & 0 deletions cmd/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"errors"

"github.com/spf13/cobra"
)

Expand All @@ -24,6 +26,12 @@ func DNS() *cobra.Command {
Use: "dns",
Short: "dns is used to access dns commands",
Long: ``,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

dnsCmd.AddCommand(DNSDomain())
Expand Down
8 changes: 8 additions & 0 deletions cmd/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"errors"

"github.com/spf13/cobra"
)

Expand All @@ -25,6 +27,12 @@ func Firewall() *cobra.Command {
Short: "firewall is used to access firewall commands",
Long: ``,
Aliases: []string{"fw"},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

firewallCmd.AddCommand(FirewallGroup(), FirewallRule())
Expand Down
6 changes: 6 additions & 0 deletions cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func Instance() *cobra.Command { //nolint: funlen,gocyclo
Short: "commands to interact with instances on vultr",
Long: instanceLong,
Example: instanceExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

instanceCmd.AddCommand(
Expand Down
6 changes: 6 additions & 0 deletions cmd/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func ISO() *cobra.Command {
Use: "iso",
Short: "iso is used to access iso commands",
Long: ``,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

isoCmd.AddCommand(isoCreate, isoDelete, isoPrivateGet, isoPrivateList, isoPublic)
Expand Down
6 changes: 6 additions & 0 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ func Kubernetes() *cobra.Command { //nolint: funlen
Short: "kubernetes is used to access kubernetes commands",
Long: kubernetesLong,
Example: kubernetesExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

kubernetesCmd.AddCommand(k8Create, k8Get, k8List, k8GetConfig, k8Update, k8Delete, k8DeleteWithResources, k8GetVersions)
Expand Down
6 changes: 6 additions & 0 deletions cmd/loadBalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func LoadBalancer() *cobra.Command { //nolint: funlen
Short: "load balancer commands",
Long: lbLong,
Example: lbExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

lbCmd.AddCommand(lbCreate, lbDelete, lbGet, lbList, lbUpdate)
Expand Down
6 changes: 6 additions & 0 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func Network() *cobra.Command {
Short: "network interacts with network actions",
Long: netLong,
Deprecated: "Use vpc instead.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

networkCmd.AddCommand(networkGet, networkList, networkDelete, networkCreate)
Expand Down
6 changes: 6 additions & 0 deletions cmd/objectStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func ObjectStorageCmd() *cobra.Command {
Aliases: []string{"objStorage"},
Short: "object storage commands",
Long: `object-storage is used to interact with the object-storage api`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

objStorageCmd.AddCommand(
Expand Down
6 changes: 6 additions & 0 deletions cmd/reservedIP.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func ReservedIP() *cobra.Command {
Short: "reserved-ip lets you interact with reserved-ip ",
Long: reservedIPLong,
Example: reservedIPExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

reservedIPCmd.AddCommand(
Expand Down
33 changes: 23 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ import (
const (
userAgent = "vultr-cli/" + version
perPageDefault int = 100
//nolint: gosec
apiKeyError string = `
Please export your VULTR API key as an environment variable or add 'api-key' to your config file, eg:
export VULTR_API_KEY='<api_key_from_vultr_account>'
`
)

// ctxAuthKey is the context key for the authorized token check
type ctxAuthKey struct{}

var cfgFile string
var client *govultr.Client

Expand All @@ -47,7 +55,6 @@ var rootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Expand Down Expand Up @@ -83,11 +90,13 @@ func init() {
rootCmd.AddCommand(User())
rootCmd.AddCommand(VPC())
rootCmd.AddCommand(VPC2())
cobra.OnInitialize(initConfig)

ctx := initConfig()
rootCmd.SetContext(ctx)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
func initConfig() context.Context {
var token string
configPath := viper.GetString("config")

Expand All @@ -112,18 +121,22 @@ func initConfig() {
token = os.Getenv("VULTR_API_KEY")
}

ctx := context.Background()

if token == "" {
fmt.Println("Please export your VULTR API key as an environment variable or add `api-key` to your config file, eg:")
fmt.Println("export VULTR_API_KEY='<api_key_from_vultr_account>'")
os.Exit(1)
client = govultr.NewClient(nil)
ctx = context.WithValue(ctx, ctxAuthKey{}, false)
} else {
config := &oauth2.Config{}
ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: token})
client = govultr.NewClient(oauth2.NewClient(ctx, ts))
ctx = context.WithValue(ctx, ctxAuthKey{}, true)
}

config := &oauth2.Config{}
ts := config.TokenSource(context.Background(), &oauth2.Token{AccessToken: token})
client = govultr.NewClient(oauth2.NewClient(context.Background(), ts))

client.SetRateLimit(1 * time.Second)
client.SetUserAgent(userAgent)

return ctx
}

func getPaging(cmd *cobra.Command) *govultr.ListOptions {
Expand Down
6 changes: 6 additions & 0 deletions cmd/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func Script() *cobra.Command {
Aliases: []string{"ss"},
Short: "startup script commands",
Long: `script is used to access startup script commands`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

cmd.AddCommand(scriptCreate, scriptGet, scriptDelete, scriptList, scriptUpdate)
Expand Down
6 changes: 6 additions & 0 deletions cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func Snapshot() *cobra.Command {
Aliases: []string{"sn"},
Short: "snapshot commands",
Long: `snapshot is used to access snapshot commands`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

cmd.AddCommand(snapshotCreate, snapshotCreateFromURL, snapshotGet, snapshotDelete, snapshotList)
Expand Down
6 changes: 6 additions & 0 deletions cmd/sshKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func SSHKey() *cobra.Command {
Aliases: []string{"ssh"},
Short: "ssh-key commands",
Long: `ssh-key is used to access SSH key commands`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

cmd.AddCommand(sshCreate, sshDelete, sshGet, sshList, sshUpdate)
Expand Down
6 changes: 6 additions & 0 deletions cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func User() *cobra.Command {
Aliases: []string{"u"},
Short: "user commands",
Long: `user is used to access user commands`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

cmd.AddCommand(userCreate, userDelete, userGet, userList, userUpdate)
Expand Down
6 changes: 6 additions & 0 deletions cmd/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func VPC() *cobra.Command {
Short: "Interact with VPCs",
Long: vpcLong,
Example: vpcExample,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

vpcCmd.AddCommand(vpcGet, vpcList, vpcDelete, vpcCreate, vpcUpdate)
Expand Down
6 changes: 6 additions & 0 deletions cmd/vpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func VPC2() *cobra.Command {
Short: "commands to interact with vpc2 on vultr",
Long: vpc2Long,
Example: vpc2Example,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Context().Value(ctxAuthKey{}).(bool) {
return errors.New(apiKeyError)
}
return nil
},
}

vpc2Cmd.AddCommand(vpc2List, vpc2Create, vpc2Info, vpc2Update, vpc2Delete)
Expand Down
Loading