github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/verify/step_verify_git.go (about)

     1  package verify
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/auth"
     9  
    10  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
    11  
    12  	"github.com/jenkins-x/jx-logging/pkg/log"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    15  	"github.com/olli-ai/jx/v2/pkg/gits"
    16  	"github.com/olli-ai/jx/v2/pkg/util"
    17  	"github.com/pkg/errors"
    18  	"github.com/spf13/cobra"
    19  )
    20  
    21  const (
    22  	optionGitHubAppOwner = "github-app-owner"
    23  )
    24  
    25  // StepVerifyGitOptions contains the command line flags
    26  type StepVerifyGitOptions struct {
    27  	step.StepOptions
    28  
    29  	GitHubAppOwner string
    30  }
    31  
    32  // NewCmdStepVerifyGit creates the `jx step verify pod` command
    33  func NewCmdStepVerifyGit(commonOpts *opts.CommonOptions) *cobra.Command {
    34  	options := &StepVerifyGitOptions{
    35  		StepOptions: step.StepOptions{
    36  			CommonOptions: commonOpts,
    37  		},
    38  	}
    39  
    40  	cmd := &cobra.Command{
    41  		Use: "git",
    42  		Run: func(cmd *cobra.Command, args []string) {
    43  			options.Cmd = cmd
    44  			options.Args = args
    45  			err := options.Run()
    46  			helper.CheckErr(err)
    47  		},
    48  	}
    49  
    50  	cmd.Flags().StringVarP(&options.GitHubAppOwner, optionGitHubAppOwner, "g", "", "The owner (organisation or user name) if using GitHub App based tokens")
    51  	return cmd
    52  }
    53  
    54  // Run implements this command
    55  func (o *StepVerifyGitOptions) Run() error {
    56  	log.Logger().Infof("Verifying the git config\n")
    57  
    58  	gha, err := o.IsGitHubAppMode()
    59  	if err != nil {
    60  		return errors.Wrap(err, "while checking if github app mode is enabled")
    61  	}
    62  
    63  	if gha && o.GitHubAppOwner == "" {
    64  		log.Logger().Infof("this command does nothing if using github app mode and no %s option specified", optionGitHubAppOwner)
    65  		return nil
    66  	}
    67  
    68  	var authSvc auth.ConfigService
    69  	if gha {
    70  		authSvc, err = o.GitAuthConfigServiceGitHubAppMode("github")
    71  		if err != nil {
    72  			return errors.Wrap(err, "when creating auth config service using GitAuthConfigServiceGitHubAppMode")
    73  		}
    74  	} else {
    75  		authSvc, err = o.GitAuthConfigService()
    76  		if err != nil {
    77  			return errors.Wrap(err, "when creating auth config service using GitAuthConfigService")
    78  		}
    79  	}
    80  
    81  	config := authSvc.Config()
    82  	if config == nil {
    83  		return fmt.Errorf("git auth config is empty")
    84  	}
    85  
    86  	servers := config.Servers
    87  	if len(servers) == 0 {
    88  		return fmt.Errorf("no git servers found in the auth configuration")
    89  	}
    90  	info := util.ColorInfo
    91  	pipeUserValid := false
    92  	for _, server := range servers {
    93  		for _, userAuth := range server.Users {
    94  			log.Logger().Infof("Verifying username %s at git server %s at %s\n",
    95  				info(userAuth.Username), info(server.Name), info(server.URL))
    96  
    97  			provider, err := gits.CreateProvider(server, userAuth, o.Git())
    98  			if err != nil {
    99  				return errors.Wrapf(err, "creating git provider for %s at git server %s",
   100  					userAuth.Username, server.URL)
   101  			}
   102  
   103  			if strings.HasSuffix(provider.CurrentUsername(), "[bot]") {
   104  				pipeUserValid = true
   105  				continue
   106  			}
   107  
   108  			orgs, err := provider.ListOrganisations()
   109  			if err != nil {
   110  				return errors.Wrapf(err, "listing the organisations for %s at git server %s",
   111  					userAuth.Username, server.URL)
   112  			}
   113  			orgNames := []string{}
   114  			for _, org := range orgs {
   115  				orgNames = append(orgNames, org.Login)
   116  			}
   117  			sort.Strings(orgNames)
   118  			log.Logger().Infof("Found %d organisations in git server %s: %s\n",
   119  				len(orgs), info(server.URL), info(strings.Join(orgNames, ", ")))
   120  			if config.PipeLineServer == server.URL && config.PipeLineUsername == userAuth.Username {
   121  				pipeUserValid = true
   122  			}
   123  		}
   124  	}
   125  
   126  	if pipeUserValid {
   127  		log.Logger().Infof("Validated pipeline user %s on git server %s", util.ColorInfo(config.PipeLineUsername), util.ColorInfo(config.PipeLineServer))
   128  	} else {
   129  		return errors.Errorf("pipeline user %s on git server %s not valid", util.ColorError(config.PipeLineUsername), util.ColorError(config.PipeLineServer))
   130  	}
   131  
   132  	log.Logger().Infof("Git tokens seem to be setup correctly\n")
   133  	return nil
   134  }