check user's GH host for compatibility

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-05 09:53:36 -07:00
parent 625c4412d4
commit af90403ecb
6 changed files with 42 additions and 3 deletions

View file

@ -0,0 +1,17 @@
package auth
import (
"errors"
"github.com/cli/go-gh/v2/pkg/auth"
)
var ErrUnsupportedHost = errors.New("The GH_HOST environment variable is set to a custom GitHub host. gh attestation does not currently support custom GitHub Enterprise hosts")
func IsHostSupported() error {
host, _ := auth.DefaultHost()
if host != "github.com" {
return ErrUnsupportedHost
}
return nil
}

View file

@ -7,6 +7,7 @@ import (
"github.com/cli/cli/v2/pkg/cmd/attestation/api"
"github.com/cli/cli/v2/pkg/cmd/attestation/artifact"
"github.com/cli/cli/v2/pkg/cmd/attestation/auth"
"github.com/cli/cli/v2/pkg/cmd/attestation/logging"
"github.com/cli/cli/v2/pkg/cmdutil"
@ -75,8 +76,12 @@ func NewDownloadCmd(f *cmdutil.Factory) *cobra.Command {
// when RunE is used, the command usage will be printed
// We only want to print the error, not usage
Run: func(cmd *cobra.Command, args []string) {
if err := auth.IsHostSupported(); err != nil {
opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error()))
os.Exit(1)
}
if err := RunDownload(opts); err != nil {
opts.Logger.Println(opts.Logger.IO.Out, opts.Logger.ColorScheme.Redf("Failed to download the artifact's trusted metadata: %s", err.Error()))
opts.Logger.Println(opts.Logger.ColorScheme.Redf("Failed to download the artifact's trusted metadata: %s", err.Error()))
os.Exit(1)
}
},

View file

@ -6,6 +6,7 @@ import (
"os"
"github.com/cli/cli/v2/pkg/cmd/attestation/artifact"
"github.com/cli/cli/v2/pkg/cmd/attestation/auth"
"github.com/cli/cli/v2/pkg/cmd/attestation/logging"
"github.com/cli/cli/v2/pkg/cmd/attestation/verification"
"github.com/cli/cli/v2/pkg/cmdutil"
@ -69,6 +70,10 @@ func NewInspectCmd(f *cmdutil.Factory) *cobra.Command {
// when RunE is used, the command usage will be printed
// We only want to print the error, not usage
Run: func(cmd *cobra.Command, args []string) {
if err := auth.IsHostSupported(); err != nil {
opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error()))
os.Exit(1)
}
if err := RunInspect(opts); err != nil {
opts.Logger.Println(opts.Logger.ColorScheme.Redf("Failed to inspect the artifact and bundle: %s", err.Error()))
os.Exit(1)

View file

@ -36,7 +36,7 @@ func NewSystemLogger() *Logger {
return NewDefaultLogger(iostreams.System())
}
// Printf writes the formatted arguments to the stdout writer.
// Printf writes the formatted arguments to the stderr writer.
func (l *Logger) Printf(f string, v ...interface{}) (int, error) {
if l.quiet || !l.IO.IsStdoutTTY() {
return 0, nil
@ -44,7 +44,7 @@ func (l *Logger) Printf(f string, v ...interface{}) (int, error) {
return fmt.Fprintf(l.IO.ErrOut, f, v...)
}
// Println writes the arguments to the stdout writer with a newline at the end.
// Println writes the arguments to the stderr writer with a newline at the end.
func (l *Logger) Println(v ...interface{}) (int, error) {
if l.quiet || !l.IO.IsStdoutTTY() {
return 0, nil

View file

@ -8,6 +8,7 @@ import (
"github.com/cli/cli/v2/pkg/cmd/attestation/api"
"github.com/cli/cli/v2/pkg/cmd/attestation/artifact"
"github.com/cli/cli/v2/pkg/cmd/attestation/auth"
"github.com/cli/cli/v2/pkg/cmd/attestation/logging"
"github.com/cli/cli/v2/pkg/cmd/attestation/verification"
"github.com/cli/cli/v2/pkg/cmdutil"
@ -96,6 +97,10 @@ func NewVerifyCmd(f *cmdutil.Factory) *cobra.Command {
// when RunE is used, the command usage will be printed
// We only want to print the error, not usage
Run: func(cmd *cobra.Command, args []string) {
if err := auth.IsHostSupported(); err != nil {
opts.Logger.Println(opts.Logger.ColorScheme.Red(err.Error()))
os.Exit(1)
}
if err := RunVerify(opts); err != nil {
opts.Logger.Println(opts.Logger.ColorScheme.Redf("Failed to verify the artifact: %s", err.Error()))
os.Exit(1)

View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"github.com/cli/cli/v2/pkg/cmd/attestation/auth"
"github.com/cli/cli/v2/pkg/cmd/attestation/logging"
"github.com/cli/cli/v2/pkg/cmd/attestation/verification"
"github.com/cli/cli/v2/pkg/cmdutil"
@ -35,6 +36,12 @@ func NewVerifyTUFRootCmd(f *cmdutil.Factory) *cobra.Command {
`),
Run: func(cmd *cobra.Command, args []string) {
logger := logging.NewDefaultLogger(f.IOStreams)
if err := auth.IsHostSupported(); err != nil {
fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Red(err.Error()))
os.Exit(1)
}
if err := verifyTUFRoot(mirror, root); err != nil {
fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Redf("Failed to verify the TUF repository: %s", err))
os.Exit(1)