This is an automated email from the ASF dual-hosted git repository.

srijeet0406 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 44299e6b7d t3c to add safe.directory exception (#7594)
44299e6b7d is described below

commit 44299e6b7d5de4b58d3656740b8b7513b343f71a
Author: Joe Pappano <[email protected]>
AuthorDate: Wed Jun 28 17:40:10 2023 -0400

    t3c to add safe.directory exception (#7594)
    
    * fixed issue with git repo ownership.
    
    * fixed issue with git repo ownership
    
    * fixed formatting errors
---
 CHANGELOG.md                           |  1 +
 cache-config/t3c-apply/t3c-apply.go    |  4 ++++
 cache-config/t3c-apply/util/gitutil.go | 31 +++++++++++++++++++++++++++++++
 3 files changed, 36 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd3b6df5cf..87ef292d53 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -120,6 +120,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#6385](https://github.com/apache/trafficcontrol/issues/6385) *Traffic Ops* 
Reserved consistentHashQueryParameters cause internal server error
 - [#7471](https://github.com/apache/trafficcontrol/pull/7471) *Traffic Control 
Cache Config (t3c)* Fixed issue with MSO non topo origins from multiple cache 
groups.
 - [#4393](https://github.com/apache/trafficcontrol/issues/4393) *Traffic Ops* 
Fixed the error code and alert structure when TO is queried for a delivery 
service with no ssl keys.
+- [#7590](https://github.com/apache/trafficcontrol/issues/7590) *Traffic 
Control Cache Config (t3c)* Fixed issue with git detected dubious ownership in 
repository.
 
 ### Removed
 - [#7271](https://github.com/apache/trafficcontrol/pull/7271) Remove 
components in `infrastructre/docker/`, not in use as cdn-in-a-box performs the 
same functionality.
diff --git a/cache-config/t3c-apply/t3c-apply.go 
b/cache-config/t3c-apply/t3c-apply.go
index 1382da3529..28cc44962f 100644
--- a/cache-config/t3c-apply/t3c-apply.go
+++ b/cache-config/t3c-apply/t3c-apply.go
@@ -165,6 +165,10 @@ func Main() int {
                                log.Errorf("couldn't remove git lock file: %v", 
err.Error())
                        }
                }
+               log.Infoln("Checking git for safe directory config")
+               if err := util.GetGitConfigSafeDir(cfg); err != nil {
+                       log.Warnln("error checking git for safe directory 
config: " + err.Error())
+               }
                // commit anything someone else changed when we weren't looking,
                // with a keyword indicating it wasn't our change
                if err := util.MakeGitCommitAll(cfg, util.GitChangeNotSelf, 
true); err != nil {
diff --git a/cache-config/t3c-apply/util/gitutil.go 
b/cache-config/t3c-apply/util/gitutil.go
index 33d05b0529..c85d546e1e 100644
--- a/cache-config/t3c-apply/util/gitutil.go
+++ b/cache-config/t3c-apply/util/gitutil.go
@@ -79,6 +79,37 @@ func EnsureConfigDirIsGitRepo(cfg config.Cfg) (bool, error) {
        return true, nil
 }
 
+const GitSafeDir = "safe.directory"
+
+// GetGitConfigSafeDir checks that TsConfigDir has been configured as
+// a safe directory. if not it will be added to the git config
+// this will prevent the fatal: detected dubious ownership error
+func GetGitConfigSafeDir(cfg config.Cfg) error {
+       safeDir := GitSafeDir + "=" + cfg.TsConfigDir
+       cmd := exec.Command("/usr/bin/git", "config", "-l")
+       cmd.Dir = cfg.TsConfigDir
+       output, err := cmd.CombinedOutput()
+       if err != nil {
+               return fmt.Errorf("git config returned err %v", string(output))
+       }
+       if !bytes.Contains(output, []byte(safeDir)) {
+               if err := addGitSafeDir(GitSafeDir, cfg.TsConfigDir); err != 
nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+func addGitSafeDir(safeDir string, path string) error {
+       cmd := exec.Command("/usr/bin/git", "config", "--global", "--add", 
GitSafeDir, path)
+       cmd.Dir = path
+       output, err := cmd.CombinedOutput()
+       if err != nil {
+               return fmt.Errorf("git config add '%v' returned err %v", 
safeDir, string(output))
+       }
+       return nil
+}
+
 func makeConfigDirGitRepo(cfg config.Cfg) error {
        cmd := exec.Command("git", "init")
        cmd.Dir = cfg.TsConfigDir

Reply via email to