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

Replace all remaining time.ParseDurations with parseutil.ParseDurationSeconds #21357

Conversation

VioletHynes
Copy link
Contributor

@VioletHynes VioletHynes commented Jun 20, 2023

Vault's duration string format supports a strict superset of options compared to Go's duration string format: https://developer.hashicorp.com/vault/docs/concepts/duration-format

In particular, we support days, e.g. 5d, whereas time.ParseDuration does not.

We use parseutil.ParseDurationSeconds everywhere in the code we can, to support our advertised duration format and to be consistent with ourselves. The majority of our code already uses parseutil.ParseDurationSeconds, but there were a few places I found that time.ParseDuration was still being used as part of poking around, so I thought I'd fix them all at once.

I'm not worried about the fact this change changes a lot of files. ParseDurationSeconds is battle-tested, and there's no valid duration that time.ParseDuration supports that we don't also support.

@VioletHynes VioletHynes changed the title Replace all time.ParseDurations with parseutil.ParseDurationSeconds Replace all remaining time.ParseDurations with parseutil.ParseDurationSeconds Jun 20, 2023
@@ -989,33 +989,3 @@ func (d *timeValue) Get() interface{} { return *d.target }
func (d *timeValue) String() string { return (*d.target).String() }
func (d *timeValue) Example() string { return "time" }
func (d *timeValue) Hidden() bool { return d.hidden }

// -- helpers
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These three functions were unused. I found them as part of my search for time.ParseDuration, and thought it better to remove them.

@VioletHynes VioletHynes added this to the 1.15 milestone Jun 20, 2023
@VioletHynes VioletHynes marked this pull request as ready for review June 20, 2023 17:29
@VioletHynes VioletHynes requested review from a team June 20, 2023 17:29
Copy link
Contributor

@hghaf099 hghaf099 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 to me!

@@ -19,6 +19,8 @@ import (
"strings"
"time"

"github.com/hashicorp/go-secure-stdlib/parseutil"

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be good to remove this extra 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.

Good call, it was added by my IDE but it definitely doesn't look correct there, looking at the full file. I'll fix these!

Copy link
Contributor

@raskchanky raskchanky left a comment

Choose a reason for hiding this comment

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

The change itself looks good. I notice almost every file modified though included extra lines in the imports. I wonder if you'd mind collapsing all of those empty lines so that we have 2 import groups in each of the modified files?

I also wonder if it's worth introducing a semgrep rule to prevent future uses of time.ParseDuration. Thoughts?

@VioletHynes
Copy link
Contributor Author

The change itself looks good. I notice almost every file modified though included extra lines in the imports. I wonder if you'd mind collapsing all of those empty lines so that we have 2 import groups in each of the modified files?

Yeah! Apologies, my IDE took the liberty of adding these, but they should be fixed now.

I also wonder if it's worth introducing a semgrep rule to prevent future uses of time.ParseDuration. Thoughts?

This is a good idea, and there's no worries about preventing it from applying to older code since I just removed it from older code. I'll have a look to see how hard this would be to add

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raskchanky Added a semgrep rule here! Thanks for the suggestion. As part of CI, we run all configs in the ci directory, which should include this one. We get no findings on the existing codebase, but hits like this when I add something in to test it:

    ../../physical/raft/raft.go
       time-parse-duration
          Usage of time.ParseDuration. Use parseutil.ParseDurationSeconds,
          instead!

          878┆ time.ParseDuration("abc")

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome! Thanks for doing that 🙂

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.

3 participants