Skip to content

Fix/create temp url escaping#1994

Merged
jtopjian merged 14 commits intogophercloud:masterfrom
Lirt:fix/create-temp-url-escaping
Aug 5, 2020
Merged

Fix/create temp url escaping#1994
jtopjian merged 14 commits intogophercloud:masterfrom
Lirt:fix/create-temp-url-escaping

Conversation

@Lirt
Copy link
Copy Markdown
Contributor

@Lirt Lirt commented Jul 24, 2020

For #1993

Fix issue with generating temporary URLs, which are invalid when conatiner or object name contains characters, which are subject to query escaping.

- #1993
- URL MUST NOT be encoded prior to hashing
- From Swift docs - Do not URL-encode the path when you generate the HMAC-SHA1 signature

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Jul 24, 2020

For tests to work, time.Now() in function CreateTempURL must always return consistent value https://github.com/gophercloud/gophercloud/blob/master/openstack/objectstorage/v1/objects/requests.go#L467.

I know that time cannot be mocked, but there are other techniques that could be done. Can you help me with this @kayrus?

Also let me know what you think about this approach to unit test. If you think we can test this easier, I can rewrite it.

Edit: Also I can add one more test with slashes in object name (which caused the original issue)

- This test should generate URL with consistent signature and expiry and
  then be asserted

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 24, 2020

Build succeeded.

@kayrus
Copy link
Copy Markdown
Contributor

kayrus commented Jul 24, 2020

For tests to work, time.Now() in function CreateTempURL must always return consistent value https://github.com/gophercloud/gophercloud/blob/master/openstack/objectstorage/v1/objects/requests.go#L467.

I'd split this func and use the major logic func in unit tests.

I wonder how terraform functional tests passed this, and noticed that object name doesn't contain the slash: https://github.com/terraform-providers/terraform-provider-openstack/blob/152286f9ae3d016cb38d53ae2c0d1920d967d368/openstack/resource_openstack_objectstorage_tempurl_v1_test.go#L14
In addition it would be great to have functional tests in gophercloud as well.

Lirt added 4 commits July 24, 2020 22:37
Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
This gives enough flexibility to stub time and perform unit tests

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Jul 24, 2020

For tests to work, time.Now() in function CreateTempURL must always return consistent value https://github.com/gophercloud/gophercloud/blob/master/openstack/objectstorage/v1/objects/requests.go#L467.

I'd split this func and use the major logic func in unit tests.

I'm sorry, but I don't see way how to split it and use it without adding new argument for time to be able to stub it.

What I came up after some research is allowing to change function of time at package level (https://labs.yulrizka.com/en/stubbing-time-dot-now-in-golang/) - f4b9e4b.

Feel free to update the MR with how you see it.

…added

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@coveralls
Copy link
Copy Markdown

coveralls commented Jul 24, 2020

Coverage Status

Coverage increased (+0.1%) to 79.512% when pulling c93e05f on Lirt:fix/create-temp-url-escaping into bd999d0 on gophercloud:master.

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 25, 2020

Build failed.

}
duration := time.Duration(opts.TTL) * time.Second
expiry := time.Now().Add(duration).Unix()
expiry := timeNow().Add(duration).Unix()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder how did it work before without UTC()

Copy link
Copy Markdown
Contributor Author

@Lirt Lirt Jul 25, 2020

Choose a reason for hiding this comment

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

Good question, I didn't think about that. I was reading docs, and it says Unix() should be consistent in any way ⬇️.

Unix() The result does not depend on the location associated with t. Unix-like operating systems
https://golang.org/pkg/time/#Time.Unix

So I can actually revert back to Now() if it looks better for you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure what should be used. @jtopjian please advice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can we resolve as discussed below?
It was commited here 9a500de

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 25, 2020

Build succeeded.

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 25, 2020

Build succeeded.

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 25, 2020

Build succeeded.

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 27, 2020

Build succeeded.

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Jul 27, 2020

Had to fix some issues with Time packaging, but it works now correctly. Question is if this way is OK for you maintainers. As I said I didn't find better solution yet, but I am open to suggestions.

I will also add 1 integration test, to cover this function better, but I think it will take some time, since it will be my first.

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 30, 2020

Build failed.


resp, err := http.Get(objURLs[i])
th.AssertNoErr(t, err)
defer resp.Body.Close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not critical, but better to call resp.Body.Close() at the end of the loop. otherwise go will close the connection only when the test function exits.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 22628a7

Lirt added 2 commits July 30, 2020 09:09
Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
Split string
}

type nowFuncT func() time.Time
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if add the date into the CreateTempURLOpts struct? If it is nil, then a default time.Now() is used. If not, the provided value is used. This will avoid hacking around the ResetClockImplementation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me

Copy link
Copy Markdown
Contributor Author

@Lirt Lirt Jul 30, 2020

Choose a reason for hiding this comment

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

Please check 9a500de

Notice I didn't use pointer to time.Time because as documentation says:

Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time.

But I used isZero() to check for null value.

https://golang.org/pkg/time/#Time

Copy link
Copy Markdown
Contributor Author

@Lirt Lirt Jul 30, 2020

Choose a reason for hiding this comment

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

I looks much cleaner when passing time as opts :-)

- This way is less hacky than packaging time

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 30, 2020

Build failed.

Signed-off-by: Ondrej Vasko <ondrej.vaskoo@gmail.com>
@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Jul 30, 2020

Build failed.

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Aug 3, 2020

Acceptance test for the function was successful, but some other failed, doing recheck.

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Aug 3, 2020

recheck

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Aug 3, 2020

Build failed.

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Aug 3, 2020

@kayrus Can you please look at the pipeline? I'm not sure if I should just run recheck, or there is something wrong. I think it is failing on something different than my code.

2020-08-03 10:06:35.176116 | ubuntu-bionic | --- SKIP: TestMessageDelete (0.19s)
2020-08-03 10:06:35.176174 | ubuntu-bionic |     messages_test.go:91: No messages were found
2020-08-03 10:06:35.176219 | ubuntu-bionic | PASS
2020-08-03 10:06:35.177855 | ubuntu-bionic | ok  	github.com/gophercloud/gophercloud/acceptance/openstack/sharedfilesystems/v2/messages	0.531s
2020-08-03 10:06:35.201255 | ubuntu-bionic | + ./script/acceptancetest:main:101         :   [[ 0 != 0 ]]
2020-08-03 10:06:35.204759 | ubuntu-bionic | + ./script/acceptancetest:main:107         :   [[ -n 1 ]]
2020-08-03 10:06:35.206948 | ubuntu-bionic | + ./script/acceptancetest:main:108         :   exit 1
2020-08-03 10:06:39.396148 | ubuntu-bionic | ERROR
2020-08-03 10:06:39.397095 | ubuntu-bionic | {
2020-08-03 10:06:39.397221 | ubuntu-bionic |   "delta": "0:45:12.849894",
2020-08-03 10:06:39.397348 | ubuntu-bionic |   "end": "2020-08-03 10:06:38.006579",
2020-08-03 10:06:39.397430 | ubuntu-bionic |   "msg": "non-zero return code",
2020-08-03 10:06:39.397508 | ubuntu-bionic |   "rc": 1,
2020-08-03 10:06:39.397628 | ubuntu-bionic |   "start": "2020-08-03 09:21:25.156685"
2020-08-03 10:06:39.397715 | ubuntu-bionic | }

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Aug 3, 2020

recheck

@theopenlab-ci
Copy link
Copy Markdown

theopenlab-ci bot commented Aug 3, 2020

Build failed.

@jtopjian
Copy link
Copy Markdown
Contributor

jtopjian commented Aug 3, 2020

The acceptance test failure should be fixed when #2000 is merged.

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Aug 3, 2020

@jtopjian Thank you.

I'm pretty much done with this MR. You can do review when you have time for it.

@jtopjian
Copy link
Copy Markdown
Contributor

jtopjian commented Aug 5, 2020

@Lirt Thanks! This looks good to me.

@kayrus are you OK with this?

@kayrus
Copy link
Copy Markdown
Contributor

kayrus commented Aug 5, 2020

@jtopjian LGTM as well. @Lirt, thanks a lot for your contribution.

@Lirt
Copy link
Copy Markdown
Contributor Author

Lirt commented Aug 5, 2020

Thank you for review and help with code @jtopjian and @kayrus.

Copy link
Copy Markdown
Contributor

@jtopjian jtopjian left a comment

Choose a reason for hiding this comment

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

LGTM - thank you!

@jtopjian jtopjian merged commit e4a74d0 into gophercloud:master Aug 5, 2020
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.

4 participants