From 1e8bc3a643055ea61de36afc8e62596038419760 Mon Sep 17 00:00:00 2001 From: Richard LT Date: Tue, 23 Jun 2020 17:09:46 +0200 Subject: [PATCH] fix(api): resync repo managers to compute template default params (#5260) * fix(api): resync repo managers to compute template default params * refactor: move resync to cdsctl workflow init --- cli/cdsctl/workflow_init.go | 36 ++++++++++++++++++++++++---- engine/api/router_middleware_auth.go | 3 ++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/cli/cdsctl/workflow_init.go b/cli/cdsctl/workflow_init.go index 5ecef73cbd..419ee91e06 100644 --- a/cli/cdsctl/workflow_init.go +++ b/cli/cdsctl/workflow_init.go @@ -197,6 +197,30 @@ func searchRepository(pkey, repoManagerName, repoFullname string) (string, error return "", fmt.Errorf("unable to find repository %s from %s: please check your credentials", repoFullname, repoManagerName) } +// Check for given repo URL that it exists in project vcs. +// This func will resync the repositories list if not found at first time. +func checkRepositoryExists(proj sdk.Project, repoURL string) error { + for _, vcs := range proj.VCSServers { + var resync bool + for { + repos, err := client.RepositoriesList(proj.Key, vcs.Name, resync) + if err != nil { + return fmt.Errorf("unable to list repositories from %s: %v", vcs.Name, err) + } + for _, r := range repos { + if repoURL == r.HTTPCloneURL || repoURL == r.SSHCloneURL { + return nil + } + } + if resync { + break + } + resync = true + } + } + return fmt.Errorf("unable to find repository %s for project: please check project vcs configuration", repoURL) +} + func interactiveChoosePipeline(pkey, defaultPipeline string) (string, *sdk.Pipeline, error) { // Try to find pipeline or create a new pipeline pips, err := client.PipelineList(pkey) @@ -427,9 +451,9 @@ func craftPipelineFile(proj *sdk.Project, existingPip *sdk.Pipeline, pipName, de func workflowInitRun(c cli.Values) error { path := "." - gitRepo, errRepo := repo.New(path) - if errRepo != nil { - return errRepo + gitRepo, err := repo.New(path) + if err != nil { + return err } pkey, err := interactiveChooseProject(gitRepo, c.GetString(_ProjectKey)) @@ -476,7 +500,7 @@ func workflowInitRun(c cli.Values) error { if len(files) > 0 { if c.GetString("pipeline") != "" { - return errors.New("you can't set a pipeline name while files alerady exists in .cds/ folder") + return errors.New("you can't set a pipeline name while files already exists in .cds/ folder") } } @@ -536,6 +560,10 @@ func workflowInitRun(c cli.Values) error { files = append(files, pipFilePath) } } else { + // Check that current repository is accessible for CDS, also resync repositories + if err := checkRepositoryExists(*proj, fetchURL); err != nil { + return err + } fmt.Println("Reading files:") for _, f := range files { fmt.Printf(" * %s", cli.Magenta(f)) diff --git a/engine/api/router_middleware_auth.go b/engine/api/router_middleware_auth.go index 9125643195..8592687e2b 100644 --- a/engine/api/router_middleware_auth.go +++ b/engine/api/router_middleware_auth.go @@ -233,7 +233,8 @@ func (api *API) xsrfMiddleware(ctx context.Context, w http.ResponseWriter, req * // else if its a read request we want to reuse a cached XSRF token or generate one if rc.PermissionLevel > sdk.PermissionRead { if !existXSRFTokenInCache || xsrfToken != existingXSRFToken { - return ctx, sdk.WithStack(sdk.ErrUnauthorized) + // We want to return a forbidden to allow the user to retry with a new token. + return ctx, sdk.WithStack(sdk.ErrForbidden) } newXSRFToken, err := authentication.NewSessionXSRFToken(api.Cache, sessionID)