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

profiler: add API key check at initialization of Profiler object #718

Merged
merged 12 commits into from
Sep 10, 2020

Conversation

sanchda
Copy link
Contributor

@sanchda sanchda commented Sep 2, 2020

This change adds a new check which verifies the sanity of the user provided API key and returns an error if the key is invalid.

ref: PROF-1571

Validation only triggers when the user specifies a key using WithAPIKey.
No other actions are taken, no error is returned, and the specified key
will be used.
Moved the API key check to profiler/profiler.go.  An API key with an
invalid format will now cause newProfiler to throw an error.
@sanchda sanchda requested a review from AlexJF September 2, 2020 19:30
My mistake, didn't know we had gofmt as part of our ci.
Refactored the error message as well, which was too specific; hopefully
users onboarding into a configuration where an API key is necessary
either don't exist or can refer to documentation.
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

Thanks for the work. Left a few pointers.

profiler/options.go Outdated Show resolved Hide resolved
profiler/options.go Outdated Show resolved Hide resolved
profiler/options.go Outdated Show resolved Hide resolved
profiler/options.go Outdated Show resolved Hide resolved
profiler/profiler.go Outdated Show resolved Hide resolved
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

Since this is an open-source project, please provide a proper description of the work done, the motivations, etc. in the description of the PR. An internal ticket number has very little meaning to an external contributor.

@sanchda sanchda added this to the Unplanned milestone Sep 3, 2020
Added a preliminary attempt at sanitizing API keys.  This supposes that
API keys are secret, and even invalid API keys may contain a valid API
key and must be de-identified a little bit before display.  None of this
is relevant if we don't care...

Address a few other concerns from #718 review
profiler/options.go Outdated Show resolved Hide resolved
Instead, just don't print the API key.
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

This is looking good now! Can we please also have tests for isAPIKeyValid? Table-tests is what you want here, it will make it easy. See https://github.com/golang/go/wiki/TableDrivenTests

We should also test that the error is returned, as it should, when trying to create a profiler (newProfiler) with a bad key.

@@ -37,18 +37,20 @@ func TestOptions(t *testing.T) {
})

t.Run("WithAPIKey", func(t *testing.T) {
var validKey = "12345678901234567890123456789012"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var validKey = "12345678901234567890123456789012"
validKey := "12345678901234567890123456789012"

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, maybe we can have just one global constant testAPIKey

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great suggestions.

Writing the tests revealed an issue with non-ASCII glyphs returning true by unicode.IsLower(), so this already paid off.

Also added a simple positive and negative check for Profiler instantiation.

Erroneous passes were possible with inputs from the extended ASCII space
(and beyond), when those glyphs were characterized (ha) as lowercase by
unicode.IsLower.  This patch explicitly filters for the ASCII range.
Just a few tests to cover some obvious edge-cases.  There may be many
more.
Creating a profiler should fail if the API key validity check fails, and
it should succeed if it succeeds (equivalently, if the API key isn't
needed).  Added simple tests for these conditions.
gbbr
gbbr previously approved these changes Sep 10, 2020
Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

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

Fantastic 👍

profiler/profiler.go Outdated Show resolved Hide resolved
As per @gbbr's suggestion

Co-authored-by: Gabriel Aszalos <gabriel.aszalos@gmail.com>
@gbbr gbbr modified the milestones: Triage, 1.27.0 Sep 10, 2020
@gbbr gbbr added the profiler label Sep 10, 2020
@gbbr
Copy link
Contributor

gbbr commented Sep 10, 2020

Thanks!

@gbbr gbbr merged commit 7e9fc2b into v1 Sep 10, 2020
@gbbr gbbr deleted the sanchda/profiling-apicheck branch September 10, 2020 12:24
@sanchda
Copy link
Contributor Author

sanchda commented Sep 10, 2020

Woohoo! Many thanks for the help and guidance on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants