Skip to content

Implement scm package for handling ignored files#50

Merged
onyxraven merged 14 commits intoIbotta:masterfrom
randrusiak:feature/add-file-to-scm-ignores
Sep 28, 2022
Merged

Implement scm package for handling ignored files#50
onyxraven merged 14 commits intoIbotta:masterfrom
randrusiak:feature/add-file-to-scm-ignores

Conversation

@randrusiak
Copy link
Contributor

Story/Issue Link

Fixes #22 - sopstool add should add the file to SCM ignores

Background

I like the idea that sopstool is inspired by the blackbox project. One of the blackbox features is handling the .gitignore file.

I think there is no need to deeply explain why it is important. In short, it prevents secrets leaks.

How does it work?
Very simple, it adds the origin filename to .gitingore if you add a new secret and removes the origin filename from .gitignore if you remove a secret. That's all. :)

Versioning

Additional Requests to Reviewers

Tasks

  • Specs written
  • Manual testing

@randrusiak randrusiak requested a review from onyxraven as a code owner August 22, 2022 14:28
Copy link
Member

@onyxraven onyxraven left a comment

Choose a reason for hiding this comment

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

This looks great so far! This is definitely one of those that has been brought up internally.

Like the other PR, I'm going to review this a bit more and ensure this gets through testing.

Thanks!

Copy link
Member

@onyxraven onyxraven left a comment

Choose a reason for hiding this comment

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

Comments here are mostly about avoiding loading the ignore file into memory. The chances that the file is big enough to worry about is near-nothing though, so I'm OK if that work is deferred.

@randrusiak
Copy link
Contributor Author

Thank you for your feedback. As you noticed, the gitignore file is usually small, but I think it's always a good idea to write better code.
I'll try to figure it out next weekend. :)

@randrusiak
Copy link
Contributor Author

Sorry for the delay. I made suggested fixes, and now it works without loading unnecessary data into memory :)

onyxraven
onyxraven previously approved these changes Sep 26, 2022
Copy link
Member

@onyxraven onyxraven left a comment

Choose a reason for hiding this comment

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

couple small notes, otherwise looks good.

}

// Override target file with tempFile
err = os.Rename(tempFile.Name(), filename)
Copy link
Member

Choose a reason for hiding this comment

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

I can't remember, do you need to fsync or close the tempFile before you rename it here? It might be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, it works so maybe it's handled internally but I'm not sure how to do it properly.

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 can call file.Sync() before renaming. But what about closing a file?
I think I can close the file exactly before renaming (without deferring) or defer the anonymous function, which closes a file and rename it.
Which approach do you think is better?

Copy link
Member

Choose a reason for hiding this comment

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

I'll do a little looking around, but I think this should be OK for now.

I believe in linux type OSs this should be fine, because the file handle and the filename are not the same, so there should be no problem, so I think this is probably fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks. :)

Copy link
Member

Choose a reason for hiding this comment

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

reading through recommendations, this may be safe on unix-like fd systems (you can rename a file that your user has open) but may have some undefined behavior, especially if there are any errors. But: it is specifically NOT allowed on windows machines, and there may be other things in between.

There's also some discussion about handling defer Close in this article thats relevant.

From all that, I think its both safe and prudent to (with error checks):

  1. open the source (read); defer Close
  2. open the tempfile (write); defer Close
  3. write to tempfile
  4. Sync the tempfile
  5. Close the tempfile
  6. Close the source file
  7. rename tempfile over the old file.

The deferred Close's will handle any closing after any other return/error in the process. They'll actually throw an error (see the source) because we're calling Close twice, but we're ok with ignoring it at that point.

Copy link
Contributor Author

@randrusiak randrusiak Sep 28, 2022

Choose a reason for hiding this comment

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

Great article, it helped me understand how we should do it correctly. So according to your suggestions, I added sync and close methods before renaming tempFile. I hope this will be the final solution 😄

Copy link
Member

Choose a reason for hiding this comment

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

yeah, this has been a great exercise to learn some of these details for me too. Thanks for continuing to tweak!

Copy link
Member

@onyxraven onyxraven left a comment

Choose a reason for hiding this comment

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

looks good. I'm gunna add a couple more tests to cover the 'file doesnt exist' cases.

@onyxraven onyxraven merged commit 476cd6d into Ibotta:master Sep 28, 2022
@onyxraven onyxraven mentioned this pull request Sep 28, 2022
2 tasks
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.

sopstool add should add the file to SCM ignores

2 participants