MCPcopy
hub / github.com/openpubkey/opkssh / LoadFileAtPath

Method LoadFileAtPath

policy/files/fileloader.go:62–80  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

60// contents and returns the bytes if file permissions are valid and
61// reading is successful; otherwise returns an error.
62func (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
83func (l *FileLoader) Dump(fileBytes []byte, path string) error {

Callers 3

LoadPolicyAtPathMethod · 0.80
LoadHomePolicyMethod · 0.80
LoadProviderPolicyMethod · 0.80

Calls 4

NewPermsCheckerFunction · 0.85
StatMethod · 0.65
CheckPermMethod · 0.65
ReadFileMethod · 0.65

Tested by

no test coverage detected