|
| 1 | +/* |
| 2 | +Copyright 2022 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package git |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/base64" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "os" |
| 25 | + "os/exec" |
| 26 | + "path/filepath" |
| 27 | + "strings" |
| 28 | +) |
| 29 | + |
| 30 | +type repository struct { |
| 31 | + url string |
| 32 | + username string |
| 33 | + password string |
| 34 | + directory string |
| 35 | + revision string |
| 36 | +} |
| 37 | + |
| 38 | +func resolveRepository(ctx context.Context, url, username, password, revision string) (*repository, func(), error) { |
| 39 | + urlParts := strings.Split(url, "/") |
| 40 | + repoName := urlParts[len(urlParts)-1] |
| 41 | + tmpDir, err := os.MkdirTemp("", repoName+"-*") |
| 42 | + if err != nil { |
| 43 | + return nil, func() {}, err |
| 44 | + } |
| 45 | + cleanupFunc := func() { |
| 46 | + os.RemoveAll(tmpDir) |
| 47 | + } |
| 48 | + |
| 49 | + repo := repository{ |
| 50 | + url: url, |
| 51 | + username: username, |
| 52 | + password: password, |
| 53 | + directory: tmpDir, |
| 54 | + revision: revision, |
| 55 | + } |
| 56 | + |
| 57 | + _, err = repo.execGit(ctx, "clone", repo.url, tmpDir, "--depth=1", "--no-checkout") |
| 58 | + if err != nil { |
| 59 | + if strings.Contains(err.Error(), "unable to get password from user") { |
| 60 | + err = errors.New("clone error: authentication required") |
| 61 | + } |
| 62 | + return nil, cleanupFunc, err |
| 63 | + } |
| 64 | + |
| 65 | + _, err = repo.execGit(ctx, "fetch", "origin", repo.revision, "--depth=1") |
| 66 | + if err != nil { |
| 67 | + return nil, cleanupFunc, err |
| 68 | + } |
| 69 | + |
| 70 | + _, err = repo.execGit(ctx, "checkout", "FETCH_HEAD") |
| 71 | + if err != nil { |
| 72 | + return nil, cleanupFunc, err |
| 73 | + } |
| 74 | + |
| 75 | + revisionSha, err := repo.execGit(ctx, "rev-list", "-n1", "HEAD") |
| 76 | + if err != nil { |
| 77 | + return nil, cleanupFunc, err |
| 78 | + } |
| 79 | + repo.revision = strings.TrimSpace(string(revisionSha)) |
| 80 | + |
| 81 | + return &repo, cleanupFunc, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (repo *repository) execGit(ctx context.Context, subCmd string, args ...string) ([]byte, error) { |
| 85 | + args = append([]string{subCmd}, args...) |
| 86 | + |
| 87 | + var env []string |
| 88 | + var configArgs []string |
| 89 | + if subCmd == "clone" { |
| 90 | + configArgs = []string{"-c", "credential.interactive=false"} |
| 91 | + // NOTE: Since this is only HTTP basic auth, authentication only supports http |
| 92 | + // cloning, while unauthenticated cloning works for any other protocol supported |
| 93 | + // by the git binary. |
| 94 | + if repo.username != "" && repo.password != "" { |
| 95 | + token := base64.URLEncoding.EncodeToString([]byte(repo.username + ":" + repo.password)) |
| 96 | + env = append( |
| 97 | + env, |
| 98 | + "GIT_AUTH_HEADER=Authorization=Basic "+token, |
| 99 | + ) |
| 100 | + configArgs = append(configArgs, "--config-env", "http.extraHeader=GIT_AUTH_HEADER") |
| 101 | + } |
| 102 | + } else { |
| 103 | + // If we're not cloning the repository, then we need to configure |
| 104 | + // which directory contains the cloned repository since `cd`ing |
| 105 | + // into the repository directory is not concurrency-safe |
| 106 | + configArgs = []string{"-C", repo.directory} |
| 107 | + } |
| 108 | + cmd := exec.CommandContext(ctx, "git", append(configArgs, args...)...) //nolint:gosec |
| 109 | + cmd.Env = append(cmd.Env, env...) |
| 110 | + |
| 111 | + out, err := cmd.Output() |
| 112 | + if err != nil { |
| 113 | + msg := string(out) |
| 114 | + var exitErr *exec.ExitError |
| 115 | + if errors.As(err, &exitErr) { |
| 116 | + msg = string(exitErr.Stderr) |
| 117 | + } |
| 118 | + err = fmt.Errorf("git %s error: %s: %w", subCmd, strings.TrimSpace(msg), err) |
| 119 | + } |
| 120 | + return out, err |
| 121 | +} |
| 122 | + |
| 123 | +func (repo *repository) getFileContent(path string) ([]byte, error) { |
| 124 | + if _, err := os.Stat(repo.directory); errors.Is(err, os.ErrNotExist) { |
| 125 | + return nil, fmt.Errorf("repository clone no longer exists, used after cleaned? %w", err) |
| 126 | + } |
| 127 | + fileContents, err := os.ReadFile(filepath.Join(repo.directory, path)) |
| 128 | + if err != nil { |
| 129 | + if errors.Is(err, os.ErrNotExist) { |
| 130 | + return nil, errors.New("file does not exist") |
| 131 | + } |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + return fileContents, nil |
| 135 | +} |
0 commit comments