66 "encoding/json"
77 "fmt"
88 "os"
9+ "path/filepath"
910 "time"
1011
1112 "github.com/obolnetwork/charon/app/errors"
@@ -26,28 +27,56 @@ var (
2627
2728// New returns new private key locking service. It errors if a recently-updated private key lock file exists.
2829func New (privKeyFilePath , clusterLockFilePath , command string ) (Service , error ) {
30+ // Sanity check: private key file exists
31+ info , err := os .Stat (privKeyFilePath )
32+ if err != nil {
33+ if errors .Is (err , os .ErrNotExist ) {
34+ return Service {}, errors .New ("private key file does not exist, please check the path" , z .Str ("path" , privKeyFilePath ))
35+ }
36+
37+ return Service {}, errors .Wrap (err , "fetch file info for private key file" , z .Str ("path" , privKeyFilePath ))
38+ }
39+
40+ if info .IsDir () {
41+ return Service {}, errors .New ("private key file path is a directory, please check the path" , z .Str ("path" , privKeyFilePath ))
42+ }
43+
44+ privKeyFileLockPath := privKeyFilePath + ".lock"
45+
46+ // Sanity check: we can write to the directory
47+ parentDir := filepath .Dir (privKeyFileLockPath )
48+ if info , err := os .Stat (parentDir ); err != nil {
49+ if errors .Is (err , os .ErrNotExist ) {
50+ return Service {}, errors .New ("parent directory, fetched from private key lock file path, does not exist" , z .Str ("parent_directory" , parentDir ), z .Str ("lock_path" , privKeyFileLockPath ))
51+ }
52+
53+ return Service {}, errors .Wrap (err , "check parent directory" , z .Str ("dir" , parentDir ))
54+ } else if ! info .IsDir () {
55+ return Service {}, errors .New ("parent path is not a directory" , z .Str ("path" , parentDir ))
56+ } else if info .Mode ().Perm ()& 0o200 == 0 {
57+ return Service {}, errors .New ("parent directory is not writable" , z .Str ("dir" , parentDir ))
58+ }
59+
2960 clusterLockHash , err := readClusterLockHash (clusterLockFilePath )
3061 if err != nil {
3162 return Service {}, err
3263 }
3364
34- privKeyFilePath += ".lock"
35-
36- content , err := os .ReadFile (privKeyFilePath )
65+ content , err := os .ReadFile (privKeyFileLockPath )
3766 if errors .Is (err , os .ErrNotExist ) { //nolint:revive // Empty block is fine.
3867 // No file, we will create it in run
3968 } else if err != nil {
40- return Service {}, errors .Wrap (err , "read private key lock file" , z .Str ("path" , privKeyFilePath ))
69+ return Service {}, errors .Wrap (err , "read private key lock file" , z .Str ("path" , privKeyFileLockPath ))
4170 } else {
4271 var meta metadata
4372 if err := json .Unmarshal (content , & meta ); err != nil {
44- return Service {}, errors .Wrap (err , "decode private key lock file" , z .Str ("path" , privKeyFilePath ))
73+ return Service {}, errors .Wrap (err , "decode private key lock file" , z .Str ("path" , privKeyFileLockPath ))
4574 }
4675
4776 if time .Since (meta .Timestamp ) <= staleDuration {
4877 return Service {}, errors .New (
4978 "existing private key lock file found, another charon instance may be running on your machine" ,
50- z .Str ("path" , privKeyFilePath ),
79+ z .Str ("path" , privKeyFileLockPath ),
5180 z .Str ("command" , meta .Command ),
5281 z .Str ("cluster_lock_hash" , meta .ClusterLockHash ),
5382 )
@@ -61,7 +90,7 @@ func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error)
6190
6291 return Service {}, errors .New (
6392 errText ,
64- z .Str ("path" , privKeyFilePath ),
93+ z .Str ("path" , privKeyFileLockPath ),
6594 z .Str ("command" , meta .Command ),
6695 z .Str ("existing_cluster_lock_hash" , meta .ClusterLockHash ),
6796 z .Str ("current_cluster_lock_hash" , clusterLockHash ),
@@ -71,14 +100,14 @@ func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error)
71100 }
72101 }
73102
74- if err := writeFile ( privKeyFilePath , clusterLockHash , command , time .Now ()); err != nil {
103+ if err := writePrivateKeyLockFile ( privKeyFileLockPath , clusterLockHash , command , time .Now ()); err != nil {
75104 return Service {}, err
76105 }
77106
78107 return Service {
79108 clusterLockHash : clusterLockHash ,
109+ lockFilePath : privKeyFileLockPath ,
80110 command : command ,
81- path : privKeyFilePath ,
82111 updatePeriod : updatePeriod ,
83112 quit : make (chan struct {}),
84113 done : make (chan struct {}),
@@ -88,8 +117,8 @@ func New(privKeyFilePath, clusterLockFilePath, command string) (Service, error)
88117// Service is a private key locking service.
89118type Service struct {
90119 clusterLockHash string
120+ lockFilePath string
91121 command string
92- path string
93122 updatePeriod time.Duration
94123 quit chan struct {} // Quit exits the Run goroutine if closed.
95124 done chan struct {} // Done is closed when Run exits, which exits the Close goroutine.
@@ -105,14 +134,14 @@ func (s Service) Run() error {
105134 for {
106135 select {
107136 case <- s .quit :
108- if err := os .Remove (s .path ); err != nil {
137+ if err := os .Remove (s .lockFilePath ); err != nil {
109138 return errors .Wrap (err , "delete private key lock file" )
110139 }
111140
112141 return nil
113142 case <- tick .C :
114143 // Overwrite lockfile with new metadata
115- if err := writeFile (s .path , s .clusterLockHash , s .command , time .Now ()); err != nil {
144+ if err := writePrivateKeyLockFile (s .lockFilePath , s .clusterLockHash , s .command , time .Now ()); err != nil {
116145 return err
117146 }
118147 }
@@ -133,15 +162,22 @@ type metadata struct {
133162 ClusterLockHash string `json:"cluster_lock_hash,omitempty"`
134163}
135164
136- // writeFile creates or updates the file with the latest metadata.
137- func writeFile (path , clusterLockHash , command string , now time.Time ) error {
165+ // writePrivateKeyLockFile creates or updates the lock file with the latest metadata.
166+ func writePrivateKeyLockFile (path , clusterLockHash , command string , now time.Time ) error {
138167 b , err := json .Marshal (metadata {Command : command , Timestamp : now , ClusterLockHash : clusterLockHash })
139168 if err != nil {
140169 return errors .Wrap (err , "marshal private key lock file" )
141170 }
142171
143172 //nolint:gosec // Readable and writable for all users is fine for this file.
144173 if err := os .WriteFile (path , b , 0o666 ); err != nil {
174+ if errors .Is (err , os .ErrPermission ) {
175+ return errors .New (
176+ "permission denied while writing lock file, please check file permissions" ,
177+ z .Str ("path" , path ),
178+ )
179+ }
180+
145181 return errors .Wrap (err , "write private key lock file" , z .Str ("path" , path ))
146182 }
147183
0 commit comments