-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check file access information in conformance tests (#5102)
* Add handler with file access info - Use permissions to determine whether we can read or write to file/directory. - Validate response body for file access information Signed-off-by: Shash Reddy <shashwathireddy@gmail.com> * Address comments * Address comments - Thanks to Markus for suggestion about simplify the logic to get excluded file paths * Address comment
- Loading branch information
1 parent
356bb51
commit 8fb75cc
Showing
4 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
test/test_images/runtime/handlers/file_access_attempt.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package handlers | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
"knative.dev/serving/test/types" | ||
) | ||
|
||
type permissionBits uint32 | ||
|
||
const ( | ||
otherRead permissionBits = 1 << (2 - iota) | ||
otherWrite | ||
) | ||
|
||
func (p permissionBits) hasPermission(mode permissionBits) bool { | ||
return p&mode == mode | ||
} | ||
|
||
func fileAccessAttempt(filePaths ...string) map[string]types.FileAccessInfo { | ||
files := map[string]types.FileAccessInfo{} | ||
for _, path := range filePaths { | ||
accessInfo := types.FileAccessInfo{} | ||
|
||
if err := checkReadable(path); err != nil { | ||
accessInfo.ReadErr = err.Error() | ||
} | ||
|
||
if err := checkWritable(path); err != nil { | ||
accessInfo.WriteErr = err.Error() | ||
} | ||
|
||
files[path] = accessInfo | ||
} | ||
return files | ||
} | ||
|
||
// checkReadable function checks whether path file or directory is readable | ||
func checkReadable(path string) error { | ||
file, err := os.Stat(path) // It should only return error only if file does not exist | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// We aren't expected to be able to read, so just exit | ||
perm := permissionBits(file.Mode().Perm()) | ||
if !perm.hasPermission(otherRead) { | ||
return nil | ||
} | ||
|
||
if file.IsDir() { | ||
_, err := ioutil.ReadDir(path) | ||
return err | ||
} | ||
|
||
readFile, err := os.OpenFile(path, os.O_RDONLY, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return readFile.Close() | ||
} | ||
|
||
// checkWritable function checks whether path file or directory is writable | ||
func checkWritable(path string) error { | ||
file, err := os.Stat(path) // It should only return error only if file does not exist | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// We aren't expected to be able to write, so just exits | ||
perm := permissionBits(file.Mode().Perm()) | ||
if !perm.hasPermission(otherWrite) { | ||
return nil | ||
} | ||
|
||
if file.IsDir() { | ||
writeFile, err := ioutil.TempFile(path, "random") | ||
if writeFile != nil { | ||
os.Remove(writeFile.Name()) | ||
} | ||
return err | ||
} | ||
|
||
writeFile, err := os.OpenFile(path, os.O_APPEND, 0) | ||
if writeFile != nil { | ||
defer writeFile.Close() | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters