mirror of
https://github.com/safedep/vet.git
synced 2025-12-10 13:43:01 -06:00
Fix golint issue Docs update Docs update Use community URL if community mode enabled Update docs for community mode Update docs for community mode Show community mode msg for auth verify command
132 lines
3.0 KiB
Go
132 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"syscall"
|
|
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/term"
|
|
|
|
"github.com/safedep/vet/internal/auth"
|
|
"github.com/safedep/vet/internal/ui"
|
|
)
|
|
|
|
var (
|
|
authInsightApiBaseUrl string
|
|
authControlPlaneApiBaseUrl string
|
|
authTrialEmail string
|
|
authCommunity bool
|
|
)
|
|
|
|
func newAuthCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "auth",
|
|
Short: "Configure and verify Insights API authentication",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return errors.New("a valid sub-command is required")
|
|
},
|
|
}
|
|
|
|
cmd.PersistentFlags().StringVarP(&authControlPlaneApiBaseUrl, "control-plane", "",
|
|
auth.DefaultControlPlaneApiUrl(), "Base URL of Control Plane API")
|
|
|
|
cmd.AddCommand(configureAuthCommand())
|
|
cmd.AddCommand(verifyAuthCommand())
|
|
cmd.AddCommand(trialsRegisterCommand())
|
|
|
|
return cmd
|
|
}
|
|
|
|
func configureAuthCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "configure",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var key []byte
|
|
var err error
|
|
|
|
if !authCommunity {
|
|
fmt.Print("Enter API Key: ")
|
|
key, err = term.ReadPassword(syscall.Stdin)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
authInsightApiBaseUrl = auth.DefaultCommunityApiUrl()
|
|
}
|
|
|
|
err = auth.Configure(auth.Config{
|
|
ApiUrl: authInsightApiBaseUrl,
|
|
ApiKey: string(key),
|
|
ControlPlaneApiUrl: authControlPlaneApiBaseUrl,
|
|
Community: authCommunity,
|
|
})
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
os.Exit(1)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&authInsightApiBaseUrl, "api", "", auth.DefaultApiUrl(),
|
|
"Base URL of Insights API")
|
|
cmd.Flags().BoolVarP(&authCommunity, "community", "", false,
|
|
"Use community API endpoint for Insights")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
func verifyAuthCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "verify",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if auth.CommunityMode() {
|
|
ui.PrintSuccess("Running in Community Mode")
|
|
}
|
|
|
|
failOnError("auth/verify", auth.Verify(&auth.VerifyConfig{
|
|
ControlPlaneApiUrl: authControlPlaneApiBaseUrl,
|
|
}))
|
|
|
|
ui.PrintSuccess("Authentication key is valid!")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
return cmd
|
|
}
|
|
|
|
func trialsRegisterCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "trial",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
client := auth.NewTrialRegistrationClient(auth.TrialConfig{
|
|
Email: authTrialEmail,
|
|
ControlPlaneApiUrl: authControlPlaneApiBaseUrl,
|
|
})
|
|
|
|
res, err := client.Execute()
|
|
if err != nil {
|
|
fmt.Printf("Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("Trial registration successful with Id:%s\n", res.Id)
|
|
fmt.Printf("Check your email (%s) for API key and usage instructions\n", authTrialEmail)
|
|
fmt.Printf("The trial API key will expire on %s\n", res.ExpiresAt.String())
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&authTrialEmail, "email", "", "",
|
|
"Email address to use for sending trial API key")
|
|
|
|
return cmd
|
|
}
|