github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbnm/hostmanifest/whitelist.go (about) 1 package hostmanifest 2 3 import ( 4 "encoding/json" 5 "os" 6 "path/filepath" 7 ) 8 9 // whitelistPath is used for installing the whitelist as a JSON into a given 10 // path on disk. 11 type whitelistPath struct { 12 // Root is the path of the machine-global NativeMessage whitelists. 13 Root string 14 // Home is the path of the NativeMessage whitelists for regular users. 15 Home string 16 } 17 18 func (w *whitelistPath) path(u User, appID string) string { 19 if u.IsAdmin() { 20 return filepath.Join(u.PrefixPath(), w.Root, appID+".json") 21 } 22 return filepath.Join(u.PrefixPath(), w.Home, appID+".json") 23 } 24 25 func (w *whitelistPath) Install(u User, app AppManifest) error { 26 jsonPath := w.path(u, app.ID()) 27 parentDir := filepath.Dir(jsonPath) 28 29 // Make the path if it doesn't exist 30 if err := os.MkdirAll(parentDir, os.ModePerm); err != nil { 31 return wrapWriteErr(err, parentDir) 32 } 33 34 // Write the file 35 fp, err := os.OpenFile(jsonPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) 36 if err != nil { 37 return wrapWriteErr(err, jsonPath) 38 } 39 defer fp.Close() 40 41 encoder := json.NewEncoder(fp) 42 encoder.SetIndent("", " ") 43 if err := encoder.Encode(&app); err != nil { 44 return err 45 } 46 47 return fp.Sync() 48 } 49 50 func (w *whitelistPath) Uninstall(u User, app AppManifest) error { 51 jsonPath := w.path(u, app.ID()) 52 if err := os.Remove(jsonPath); err != nil && !os.IsNotExist(err) { 53 // We don't care if it doesn't exist, but other errors should escalate 54 return err 55 } 56 return nil 57 }