github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/transferconfig/utils.go (about) 1 package transferconfig 2 3 import ( 4 "archive/zip" 5 "bytes" 6 "compress/flate" 7 "errors" 8 "io" 9 "os" 10 "path/filepath" 11 "strings" 12 13 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 14 15 "github.com/jfrog/jfrog-client-go/utils/errorutils" 16 "github.com/jfrog/jfrog-client-go/utils/log" 17 ) 18 19 // Needed files for config import in SaaS 20 var neededFiles = []string{ 21 "artifactory.properties", 22 filepath.Join("etc", "access.bootstrap.json"), 23 filepath.Join("etc", "security", "artifactory.key"), 24 filepath.Join("etc", "security", "url.signing.key"), 25 } 26 27 // Archive the exportPath directory and the input artifactory.config.xml. 28 func archiveConfig(exportPath string, configXml string) (buffer *bytes.Buffer, retErr error) { 29 buffer = &bytes.Buffer{} 30 writer := zip.NewWriter(buffer) 31 writer.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) { 32 return flate.NewWriter(out, flate.BestCompression) 33 }) 34 defer func() { 35 retErr = errors.Join(retErr, errorutils.CheckError(writer.Close())) 36 }() 37 38 err := handleTypoInAccessBootstrap(exportPath) 39 if err != nil { 40 return nil, err 41 } 42 43 for _, neededFile := range neededFiles { 44 neededFilePath := filepath.Join(exportPath, neededFile) 45 log.Debug("Archiving " + neededFile) 46 fileContent, err := os.ReadFile(neededFilePath) 47 if err != nil { 48 if os.IsNotExist(err) && strings.Contains(neededFile, "security") { 49 log.Info(neededFile + " file is missing in the source Artifactory server. Skipping...") 50 continue 51 } 52 return nil, errorutils.CheckError(err) 53 } 54 fileWriter, err := writer.Create(neededFile) 55 if err != nil { 56 return nil, errorutils.CheckError(err) 57 } 58 if _, retErr = fileWriter.Write(fileContent); errorutils.CheckError(retErr) != nil { 59 return 60 } 61 } 62 fileWriter, err := writer.Create("artifactory.config.xml") 63 if err != nil { 64 return buffer, errorutils.CheckError(err) 65 } 66 _, retErr = fileWriter.Write([]byte(configXml)) 67 return 68 } 69 70 // In some versions of Artifactory, the file access.bootstrap.json has a typo in its name: access.boostrap.json. 71 // If this is the case, rename the file to the right name. 72 func handleTypoInAccessBootstrap(exportPath string) error { 73 accessBootstrapPath := filepath.Join(exportPath, "etc", "access.bootstrap.json") 74 accessBootstrapExists, err := fileutils.IsFileExists(accessBootstrapPath, false) 75 if err != nil { 76 return err 77 } 78 if !accessBootstrapExists { 79 err = fileutils.MoveFile(filepath.Join(exportPath, "etc", "access.boostrap.json"), accessBootstrapPath) 80 if err != nil { 81 if os.IsNotExist(err) { 82 return errorutils.CheckErrorf("%s: the file was not found or is not accessible", accessBootstrapPath) 83 } 84 return err 85 } 86 } 87 return nil 88 }