Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed all unwraps/panics for all code except testing #36

Merged
merged 2 commits into from
Feb 13, 2019

Conversation

leonjia0112
Copy link
Contributor

@leonjia0112 leonjia0112 commented Dec 17, 2018

  1. Removed all the unwraps for all the scripts under src/ directory except the testing suite.
  2. Changes functions' return type to Result<> after removing the unwraps, which has better error handling.
  3. Add a script to CI for catch a list of banned function that could possibly be panicking the thread and terminate the process. Both the shell script and the banned list are both .travis/ directory.

The main change in main.rs is a loop scope is used to wrap around the request, once an error is encountered early exit the loop scope with the response that has the corresponding content regarding the error.

Now there should be now unwraps/panics in the repo and prevent further use of these panic function in this repo.

let secure_dir = format!("{}{}", common::WORK_DIR, "/secure");
match check_mount(&secure_dir) {
Ok(b) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match on Ok(false) and Ok(true) rather than having an if inside the match.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

// Create directory if the directory is not exist. The
// directory permission is set to 448.
if !secure_dir_path.exists() {
match fs::create_dir(secure_dir_path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use an if let here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

@leonjia0112 leonjia0112 force-pushed the update-secure-mount branch 4 times, most recently from 84fc754 to 70fea43 Compare January 2, 2019 17:18
@leonjia0112
Copy link
Contributor Author

Requested change implemented.

Test passed. Ready for a review.
Thanks!

@leonjia0112
Copy link
Contributor Author

This is PR that remove all the unwrap in the code base, except the testing suite.

In main, I used loop scope to wrap around the whole function, the function will exit the loop if either finish executing all the code or encounter an error.

In common, the function json_response_content, which was generating a new response this function is called, is changed to set_response_content that a response is borrowed and edit its content based on the given parameters.

In TPM, I simply use a match or if let to remove the unwrap, but many functions' return types are not Result which doesn't support error returning. Therefore, those functions need to be updated with Result return type in the future PR. An issue has been opened regarding this change.

Test passed. Ready for a review.
Thanks!

@leonjia0112 leonjia0112 force-pushed the update-secure-mount branch 2 times, most recently from 083ceee to 91d1d4d Compare January 9, 2019 22:31
@leonjia0112 leonjia0112 changed the title Add error handling support for secure mount function Removed all unwraps/panics for all code except testing Jan 17, 2019
@leonjia0112
Copy link
Contributor Author

Add a bash script for catching panic call in CI.

Test Passed. Ready for a review.
Thanks!

.travis/panic-test.sh Outdated Show resolved Hide resolved
secure_dir
);
if tokens[0] != "tmpfs" {
return emsg(format!("secure storage location {} already mounted on wrong file system type: {}. Unmount to continue.", secure_dir, tokens[0]).as_str(), None::<String>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way you can wrap this better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the message string too long? How about using a shorter message instead?

src/secure_mount.rs Outdated Show resolved Hide resolved
@@ -47,34 +48,35 @@ pub static MOUNT_SECURE: bool = true;
* 2. There is no space in the json content, but python version response
* content contains white space in between keys.
*/
pub fn json_response_content(
pub fn set_response_content(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being renamed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I changed the function's implementation from creating a new response each time to modifying the response body. I think the new name is more appropriate for the function.

src/tpm.rs Outdated Show resolved Hide resolved
src/tpm.rs Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
.travis/panic-test.sh Outdated Show resolved Hide resolved
@leonjia0112 leonjia0112 force-pushed the update-secure-mount branch 7 times, most recently from 37b0145 to a7fafa7 Compare January 28, 2019 17:52
@leonjia0112
Copy link
Contributor Author

Reimplemented the GET/POST request handler in main.rs into two separate functions that are more adequate for error handling using Result.
Test Passed. Ready for a review.
Thanks.

src/secure_mount.rs Outdated Show resolved Hide resolved
src/secure_mount.rs Outdated Show resolved Hide resolved
src/tpm.rs Outdated Show resolved Hide resolved
src/main.rs Outdated
@@ -44,7 +46,7 @@ fn main() {
info!("Starting server...");

/* Should be port 3000 eventually */
let addr = "127.0.0.1:1337".parse().unwrap();
let addr = ([127, 0, 0, 1], 3000).into();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing port number is not in scope for this PR and would need the comment above removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed changed of port number

src/tpm.rs Outdated Show resolved Hide resolved

# Output result
if [ "$panic_line" -gt 0 ]
then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes on the previous line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious. Would it cause any difference or this is what people normally do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did it yourself earlier in the file. It's also pretty normal because you get a block structure similar to C (or Rust) - "then" corresponds to "{", and "fi" to "}".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks.

done < "$banned_function"

output=""
panic_line=0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 isn't a good sentinel. Consider what'll happen if a banned function appears on the first line of a file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to using boolean as a sentinel instead of using 0.

@leonjia0112
Copy link
Contributor Author

Test Passed. Ready for a review.
Thanks.

Copy link
Contributor

@frozencemetery frozencemetery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review. You still have outstanding comments from previous review.

.travis/panic-test.sh Outdated Show resolved Hide resolved
.travis/panic-test.sh Outdated Show resolved Hide resolved
src/tpm.rs Outdated Show resolved Hide resolved
src/tpm.rs Outdated Show resolved Hide resolved
src/tpm.rs Outdated Show resolved Hide resolved
src/tpm.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@leonjia0112 leonjia0112 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change request

src/tpm.rs Outdated Show resolved Hide resolved
src/tpm.rs Outdated Show resolved Hide resolved
This code needs cleanup.

[rharwood@redhat.com: squash, commit message, some cleanup]
@frozencemetery frozencemetery merged commit c087297 into keylime:master Feb 13, 2019
@frozencemetery
Copy link
Contributor

As I am quite tired of looking at this PR; I have rewritten the checker in python and squished the results. I'm curious if you even ran the old code; $true and $false aren't built-ins in bash.

Some effort will be spent going forward on a cohesive codebase cleanup.

@leonjia0112
Copy link
Contributor Author

My apologies. Sorry for making you have this feeling. Will be more careful next time.

@lukehinds lukehinds self-requested a review March 8, 2019 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants