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

add filestatus command #545

Merged
merged 2 commits into from
Jun 24, 2024
Merged

Conversation

endorama
Copy link
Contributor

@endorama endorama commented Oct 8, 2019

Closes #460

Add filestatus command, reporting if the file is in encrypted or unencrypted state.

I reused ensureNoMetadata logic, thus the command would return 0 when the file is not encrypted and 203 when the file is encrypted (respecting the codes.FileAlreadyEncrypted error code)

It does not output the error message as returned by ensureNoMetadata, preferring a simpler output: "File is encrypted" or "File is unencrypted".

As I'm not so fond on sops internals, I'm sure there are multiple things to review, I'll gladly update the code to reflect any suggestion.

cmd/sops/main.go Outdated Show resolved Hide resolved
cmd/sops/main.go Outdated
return common.NewExitError("File is encrypted", codes.FileAlreadyEncrypted)
}

fmt.Println("File is unencrypted")
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 fmt the proper way to output such an informative message?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks fine to me. I'm not sure whether we should use a machine-parseable and extensible format (probably JSON), though. It would let us put more status information there in the future, e.g. whether the MAC is okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would something like {"encrypted": true|false} be ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me.

cmd/sops/main.go Outdated Show resolved Hide resolved
Copy link
Contributor

@autrilla autrilla left a comment

Choose a reason for hiding this comment

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

Looks pretty good :) just a few comments.

cmd/sops/main.go Outdated Show resolved Hide resolved
cmd/sops/main.go Outdated
return common.NewExitError(fmt.Sprintf("Error unmarshalling file: %s", err), codes.CouldNotReadInputFile)
}
if err := ensureNoMetadata(opts, branches[0]); err != nil {
return common.NewExitError("File is encrypted", codes.FileAlreadyEncrypted)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm, I'm wondering whether we should return an error when the file is not encrypted, or the other way around. What was your reasoning for choosing this over the alternative?

Copy link
Contributor Author

@endorama endorama Oct 10, 2019

Choose a reason for hiding this comment

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

Mainly because there was an error code for "file is already encrypted" and not the other way round. So I would expect users to be already used to such an error.

IMO none is an error, and using JSON output like you requested in a comment above everything makes more sense and this error can probably be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I agree. Let's make none of them be an error for now.

@codecov-io
Copy link

codecov-io commented Oct 10, 2019

Codecov Report

Merging #545 into develop will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #545   +/-   ##
========================================
  Coverage    36.46%   36.46%           
========================================
  Files           20       20           
  Lines         2863     2863           
========================================
  Hits          1044     1044           
  Misses        1725     1725           
  Partials        94       94

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b5b7ae...0fba7e1. Read the comment docs.

Comment on lines 15 to 19
// OutputStore sops.Store
InputPath string
// KeyServices []keyservice.KeyServiceClient
// KeyGroups []sops.KeyGroup
// GroupThreshold int
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the commented lines since they're not used

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 in commit 80686ab


// Status rapresent file status
type Status struct {
Encrypted bool
Copy link
Contributor

Choose a reason for hiding this comment

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

// Encrypted represents whether the file provided is encrypted by SOPS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved in 80686ab

func checkMetadata(branch sops.TreeBranch) bool {
for _, b := range branch {
if b.Key == "sops" {
return true
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't that a bit weak as a test? I'd suggest at least verifying that a mac is set and one key exists under the various sections.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's indeed a bit weak. It's what we do elsewhere as an easy test to check whether the file is encrypted, but maybe as you say we should have a stricter test here.

Copy link
Contributor Author

@endorama endorama Oct 28, 2019

Choose a reason for hiding this comment

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

May I propose to go with this version for consistency, and work on a more complex test (that we can discuss more) on a separate one? I'm willing to work on that.
We could also extend a bit the scope and extract the check so both places can use the same code.

@kowalczykp
Copy link

Any update on that? It would be a really nice feature

@mazdack
Copy link

mazdack commented Aug 3, 2020

any update? would be really nice to have this command

@autrilla
Copy link
Contributor

autrilla commented Aug 3, 2020

No updates. The review comments haven't been addressed in this PR, plus there's merge conflicts. We'd be happy to review a PR that builds on this one an handles the review comments.

@endorama
Copy link
Contributor Author

@autrilla May you let me know your opinion on #545 (comment)?

I was waiting on a reply there, other concerns should have been addressed as requested.

@autrilla
Copy link
Contributor

autrilla commented Aug 10, 2020

@autrilla May you let me know your opinion on #545 (comment)?

It wouldn't take a lot of work to have a slightly better check by doing what jvehent suggests, e.g. ensuring there's a mac field. Bonus points for reusing this better check in the place where we already check if a file is encrypted :)

@endorama endorama force-pushed the add-filestatus-command branch 2 times, most recently from 3568c88 to ff0888f Compare August 11, 2020 10:02
@codecov-commenter
Copy link

Codecov Report

Merging #545 into develop will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #545   +/-   ##
========================================
  Coverage    36.44%   36.44%           
========================================
  Files           22       22           
  Lines         3205     3205           
========================================
  Hits          1168     1168           
  Misses        1918     1918           
  Partials       119      119           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4bd640e...ff0888f. Read the comment docs.

@endorama
Copy link
Contributor Author

endorama commented Aug 11, 2020

@autrilla I resolved conflicts and rebased onto latest develop so this should be now safe to merge again.

I started working on how to improve the check and I found out that using an implementation of EncryptedFileLoader.LoadEncryptedFile(in []byte) (Tree, error) on a non-encrypted file results in a MetadataNotFound error. Would this be ok as a deeper test? It feels "right" from my point of view. Is it enough or once metadata are found should it perform some other check?

@autrilla
Copy link
Contributor

autrilla commented Sep 2, 2020

@autrilla I resolved conflicts and rebased onto latest develop so this should be now safe to merge again.

I started working on how to improve the check and I found out that using an implementation of EncryptedFileLoader.LoadEncryptedFile(in []byte) (Tree, error) on a non-encrypted file results in a MetadataNotFound error. Would this be ok as a deeper test? It feels "right" from my point of view. Is it enough or once metadata are found should it perform some other check?

Trying to load the file sounds good to me.

@onedr0p
Copy link
Contributor

onedr0p commented Nov 19, 2020

@autrilla any status here, or are you waiting on the author to rebase this PR again? It seems like he addressed the issues before but now there's conflicts again. What's the hold up?

@onedr0p
Copy link
Contributor

onedr0p commented Jan 19, 2021

Bump ❤️

@autrilla
Copy link
Contributor

Not all the issues are addressed. In particular, I asked for a better test that the file is encrypted. It's also missing documentation and tests, and as you mention, there's conflicts.

@endorama
Copy link
Contributor Author

endorama commented Mar 19, 2021

Hello,

I have redone the implementation.
The check is performed in the function cfs and:

  • tries to load the encrypted file
  • if it fails, report as not encrypted
  • if encryption works, checks for version and mac keys to be present

filestatus subcommand now output JSON as requested ({"encrypted": true|false}).

I rebased onto master, so I'm not sure why it's still complaining about the conflict.

@autrilla is this implementation ok? I also added some tests for it. The cfs function is private in the filestatus package, let me know where is the most appropriate place to put it into so it can be reused in other areas as well.

Thanks and sorry for the long wait!

@ajvb ajvb self-requested a review March 24, 2021 20:58
@ajvb ajvb added this to the v3.7.3 milestone Mar 3, 2022
@ajvb ajvb modified the milestones: v3.7.3, v3.8.0 Apr 4, 2022
@Pehesi97
Copy link

Pehesi97 commented May 1, 2023

How is the progress in this PR? Do you need any help?

@aDingil
Copy link

aDingil commented May 12, 2023

@ajvb
alt text

@ggordan
Copy link

ggordan commented Jun 5, 2023

Hi @ajvb @autrilla @jvehent, I see that roughly a year ago the milestone for this issue was changed to v3.8.0.

Would that suggest that this PR is "complete" meaning that when v3.8.0 is released, this will be included, or are there still things that need to be addressed for the maintainers to be happy to merge this? If the answer is no, can we get a indication of what is needed, I'd be more than happy to help out.

Subsequently, is there a rough estimate on when v3.8.0 is expected to land?

Thanks,
Gordan

@felixfontein
Copy link
Contributor

@endorama would you mind rebasing another time and signing off your commits? Would be great if we could finally get this merged :)

@davinkevin
Copy link

Can't wait to use it!

Signed-off-by: Edoardo Tenani <[email protected]>
Signed-off-by: Edoardo Tenani <[email protected]>
@endorama
Copy link
Contributor Author

Hello everybody and sorry for the long wait! I'd love to see this finally being merge and I have some time to dedicate to it.

Rebasing the old work was quite complex, so given the overall addition in this PR was small I re-implemented it on top of the latest master branch. I kept the implementation the same with the same tests.

(The second force push is due to the DCO, which I didn't sign the first time)

Waiting on the review ❤

@felixfontein
Copy link
Contributor

I did some extensive tests, and it looks good! The method of detecting encrypted files is pretty good; obviously you can fake it with fake values, like the follwing YAML file:

sops:
    lastmodified: "2020-01-01T00:00:00Z"
    mac: x
    pgp:
        - created_at: "2020-01-01T00:00:00Z"
          enc: |-
            x
          fp: x
    version: x

(and similar variants for other storages), but I think that's acceptable, and it also seems to agree to what all reviewers so far asked for.

If nobody objects (especially from @getsops/maintainers side), I propose to merge this soon!

Copy link

@sabre1041 sabre1041 left a comment

Choose a reason for hiding this comment

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

LGTM

@felixfontein felixfontein merged commit 15d30ed into getsops:main Jun 24, 2024
9 checks passed
@felixfontein
Copy link
Contributor

@endorama thanks a lot for contributing this!

Also thanks a lot to everyone who reviewed this and commented on this over all these years! I'm glad this finally made it :)

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.

Implement some kind of query/status option