traefik/pkg/provider/acme/local_store_unix.go

36 lines
611 B
Go
Raw Normal View History

2017-02-04 21:21:17 +01:00
// +build !windows
package acme
import (
"fmt"
"os"
)
2020-05-11 12:06:07 +02:00
// CheckFile checks file permissions and content size.
2018-04-10 10:52:04 +02:00
func CheckFile(name string) (bool, error) {
2017-02-04 21:21:17 +01:00
f, err := os.Open(name)
if err != nil {
2018-04-10 10:52:04 +02:00
if os.IsNotExist(err) {
f, err = os.Create(name)
if err != nil {
return false, err
}
2020-07-07 14:42:03 +02:00
return false, f.Chmod(0o600)
2018-04-10 10:52:04 +02:00
}
2018-03-05 20:54:04 +01:00
return false, err
2017-02-04 21:21:17 +01:00
}
defer f.Close()
2018-03-05 20:54:04 +01:00
2017-02-04 21:21:17 +01:00
fi, err := f.Stat()
if err != nil {
2018-03-05 20:54:04 +01:00
return false, err
2017-02-04 21:21:17 +01:00
}
2018-03-05 20:54:04 +01:00
2020-07-07 14:42:03 +02:00
if fi.Mode().Perm()&0o077 != 0 {
2018-03-05 20:54:04 +01:00
return false, fmt.Errorf("permissions %o for %s are too open, please use 600", fi.Mode().Perm(), name)
2017-02-04 21:21:17 +01:00
}
2018-03-05 20:54:04 +01:00
return fi.Size() > 0, nil
2017-02-04 21:21:17 +01:00
}