LoadFileAtPath validates that the file at path exists, can be read by the current process, and has the correct permission bits set. Parses the contents and returns the bytes if file permissions are valid and reading is successful; otherwise returns an error.
(path string)
| 60 | // contents and returns the bytes if file permissions are valid and |
| 61 | // reading is successful; otherwise returns an error. |
| 62 | func (l *FileLoader) LoadFileAtPath(path string) ([]byte, error) { |
| 63 | // Check if file exists and we can access it |
| 64 | if _, err := l.Fs.Stat(path); err != nil { |
| 65 | return nil, fmt.Errorf("failed to describe the file at path: %w", err) |
| 66 | } |
| 67 | |
| 68 | // Validate that file has correct permission bits set |
| 69 | if err := NewPermsChecker(l.Fs).CheckPerm(path, []fs.FileMode{l.RequiredPerm}, "", ""); err != nil { |
| 70 | return nil, fmt.Errorf("policy file has insecure permissions: %w", err) |
| 71 | } |
| 72 | |
| 73 | // Read file contents |
| 74 | afs := &afero.Afero{Fs: l.Fs} |
| 75 | content, err := afs.ReadFile(path) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | return content, nil |
| 80 | } |
| 81 | |
| 82 | // Dump writes the bytes in fileBytes to the filepath |
| 83 | func (l *FileLoader) Dump(fileBytes []byte, path string) error { |
no test coverage detected