|
| 1 | +package backup |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/codeskyblue/go-sh" |
| 6 | + "github.com/pkg/errors" |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | + "github.com/stefanprodan/mgob/pkg/config" |
| 9 | + "os" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +func encrypt(file string, encryptedFile string, plan config.Plan, conf *config.AppConfig) (string, error) { |
| 15 | + if plan.Encryption.Gpg != nil { |
| 16 | + if !conf.HasGpg { |
| 17 | + return "", errors.Errorf("GPG configuration is present, but no GPG binary is found! Uploading unencrypted backup.") |
| 18 | + } |
| 19 | + return gpgEncrypt(file, encryptedFile, plan) |
| 20 | + } |
| 21 | + |
| 22 | + return "", errors.Errorf("Encryption config is not valid!") |
| 23 | +} |
| 24 | + |
| 25 | +func removeUnencrypted(file string, encryptedFile string) { |
| 26 | + // Check if encrypted file exists and remove original |
| 27 | + stat, err := os.Stat(encryptedFile) |
| 28 | + if err == nil && stat.Size() > 0 { |
| 29 | + os.Remove(file) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func gpgEncrypt(file string, encryptedFile string, plan config.Plan) (string, error) { |
| 34 | + output := "" |
| 35 | + recipient := "" |
| 36 | + |
| 37 | + recipients := plan.Encryption.Gpg.Recipients |
| 38 | + |
| 39 | + keyFile := plan.Encryption.Gpg.KeyFile |
| 40 | + if keyFile != "" { |
| 41 | + keyFileStat, err := os.Stat(keyFile) |
| 42 | + if err == nil && !keyFileStat.IsDir() { |
| 43 | + // import key from file |
| 44 | + importCmd := fmt.Sprintf("gpg --batch --import %v", keyFile) |
| 45 | + |
| 46 | + result, err := sh.Command("/bin/sh", "-c", importCmd).CombinedOutput() |
| 47 | + if len(result) > 0 { |
| 48 | + output += strings.Replace(string(result), "\n", " ", -1) |
| 49 | + } |
| 50 | + if err != nil { |
| 51 | + return "", errors.Wrapf(err, "Importing encryption key for plan %v failed %s", plan.Name, output) |
| 52 | + } |
| 53 | + if !strings.Contains(output, "imported: 1") && !strings.Contains(output, "unchanged: 1") { |
| 54 | + return "", errors.Errorf("Importing encryption key failed %v", output) |
| 55 | + } |
| 56 | + |
| 57 | + re := regexp.MustCompile(`key ([0-9A-F]+):`) |
| 58 | + keyMatch := re.FindStringSubmatch(output) |
| 59 | + log.WithField("plan", plan.Name).Debugf("Import output: %v", output) |
| 60 | + log.WithField("plan", plan.Name).Debugf("Parsed key id: %v", keyMatch[1]) |
| 61 | + if keyMatch != nil { |
| 62 | + recipients = append(recipients, keyMatch[1]) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + recipient = strings.Join(recipients, " -r ") |
| 68 | + |
| 69 | + if recipient == "" { |
| 70 | + return "", errors.Errorf("GPG configuration is present, but no encryption key is configured! %v", output) |
| 71 | + } |
| 72 | + |
| 73 | + keyServer := plan.Encryption.Gpg.KeyServer |
| 74 | + if keyServer == "" { |
| 75 | + keyServer = "hkps://keys.openpgp.org" |
| 76 | + } |
| 77 | + |
| 78 | + // encrypt file |
| 79 | + encryptCmd := fmt.Sprintf( |
| 80 | + "gpg -v --batch --yes --trust-model always --auto-key-locate local,%v -e -r %v -o %v %v", |
| 81 | + keyServer, recipient, encryptedFile, file) |
| 82 | + |
| 83 | + result, err := sh.Command("/bin/sh", "-c", encryptCmd).CombinedOutput() |
| 84 | + if len(result) > 0 { |
| 85 | + output += strings.Replace(string(result), "\n", " ", -1) |
| 86 | + } |
| 87 | + if err != nil { |
| 88 | + return "", errors.Wrapf(err, "Encryption for plan %v failed %s", plan.Name, output) |
| 89 | + } |
| 90 | + |
| 91 | + return output, nil |
| 92 | +} |
0 commit comments