maybeDeleteFilesystem wraps util.DeleteFilesystem with a guard to hopefully stop folks from unwittingly deleting their entire root directory.
(logger log.Func, force bool)
| 1572 | // maybeDeleteFilesystem wraps util.DeleteFilesystem with a guard to hopefully stop |
| 1573 | // folks from unwittingly deleting their entire root directory. |
| 1574 | func maybeDeleteFilesystem(logger log.Func, force bool) error { |
| 1575 | // We always expect the magic directory to be set to the default, signifying that |
| 1576 | // the user is running envbuilder in a container. |
| 1577 | // If this is set to anything else we should bail out to prevent accidental data loss. |
| 1578 | // defaultWorkingDir := workingdir.WorkingDir("") |
| 1579 | kanikoDir, ok := os.LookupEnv("KANIKO_DIR") |
| 1580 | if !ok || strings.TrimSpace(kanikoDir) != workingdir.Default.Path() { |
| 1581 | if !force { |
| 1582 | logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", workingdir.Default.Path()) |
| 1583 | logger(log.LevelError, "To bypass this check, set FORCE_SAFE=true.") |
| 1584 | return errors.New("safety check failed") |
| 1585 | } |
| 1586 | bailoutSecs := 10 |
| 1587 | logger(log.LevelWarn, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!") |
| 1588 | logger(log.LevelWarn, "You have %d seconds to bail out!", bailoutSecs) |
| 1589 | for i := bailoutSecs; i > 0; i-- { |
| 1590 | logger(log.LevelWarn, "%d...", i) |
| 1591 | <-time.After(time.Second) |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | return util.DeleteFilesystem() |
| 1596 | } |
| 1597 | |
| 1598 | func fileExists(fs billy.Filesystem, path string) bool { |
| 1599 | fi, err := fs.Stat(path) |