-
Notifications
You must be signed in to change notification settings - Fork 3
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
Authenticate endpoints and validate user state_code in metrics requests #546
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
16f3133
Guard metric api behind auth
terryttsai 03dcea1
verify matching state code when retrieving metrics when auth is enabl…
terryttsai d739491
add tests
terryttsai 643f214
fix tests
terryttsai f6027dd
fix lint
terryttsai 823b079
remove unneeded env variable and move to constants
terryttsai 552814e
Pass in rootStore to fetchMetrics in order to get token in fetch.
terryttsai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
GOOGLE_APPLICATION_CREDENTIALS={PATH_TO_CREDENTIALS_FILE} | ||
METRIC_BUCKET={GCS_BUCKET_NAME} | ||
AUTH_ENABLED={AUTH_ENABLED} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Recidiviz - a data platform for criminal justice reform | ||
// Copyright (C) 2020 Recidiviz, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// ============================================================================= | ||
|
||
const { fetchMetricsByName } = require("../core/metricsApi"); | ||
const { AUTH0_APP_METADATA_KEY } = require("../utils/constants"); | ||
const { metricsByName } = require("./api"); | ||
|
||
jest.mock("../core/metricsApi"); | ||
|
||
// mocking the node env is esoteric, see https://stackoverflow.com/a/48042799 | ||
const ORIGINAL_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
fetchMetricsByName.mockImplementation( | ||
(tenantId, metrics, isDemoMode, responder) => { | ||
responder(undefined, "passed"); | ||
} | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = ORIGINAL_ENV; | ||
}); | ||
|
||
test("retrieves metrics if auth is disabled", async () => { | ||
process.env.AUTH_ENABLED = "false"; | ||
const mockFn = jest.fn(); | ||
metricsByName( | ||
{ | ||
params: { | ||
tenantId: "US_ND", | ||
}, | ||
body: { | ||
metrics: ["test_metric"], | ||
}, | ||
}, | ||
{ | ||
send: mockFn, | ||
} | ||
); | ||
expect(mockFn).toHaveBeenCalledWith("passed"); | ||
}); | ||
|
||
test("returns 401 if there is no metrics array in the request body", async () => { | ||
process.env.AUTH_ENABLED = "false"; | ||
const mockSendFn = jest.fn(); | ||
const mockStatusFn = jest.fn(); | ||
metricsByName( | ||
{ | ||
params: { | ||
tenantId: "US_ND", | ||
}, | ||
body: {}, | ||
}, | ||
{ | ||
status: () => ({ json: mockStatusFn }), | ||
send: mockSendFn, | ||
} | ||
); | ||
expect(mockSendFn).not.toHaveBeenCalled(); | ||
expect(mockStatusFn).toHaveBeenCalledWith({ | ||
error: "request is missing metrics array parameter", | ||
}); | ||
}); | ||
|
||
test("retrieves metrics if auth is enabled and user state code is 'recidiviz'", async () => { | ||
process.env.AUTH_ENABLED = "true"; | ||
const mockFn = jest.fn(); | ||
metricsByName( | ||
{ | ||
params: { | ||
tenantId: "US_ND", | ||
}, | ||
body: { | ||
metrics: ["test_metric"], | ||
}, | ||
user: { | ||
[AUTH0_APP_METADATA_KEY]: { | ||
state_code: "recidiviz", | ||
}, | ||
}, | ||
}, | ||
{ | ||
send: mockFn, | ||
} | ||
); | ||
expect(mockFn).toHaveBeenCalledWith("passed"); | ||
}); | ||
|
||
test("retrieves metrics if auth is enabled and user state code matches the request param", async () => { | ||
process.env.AUTH_ENABLED = "true"; | ||
const mockFn = jest.fn(); | ||
metricsByName( | ||
{ | ||
params: { | ||
tenantId: "US_ND", | ||
}, | ||
body: { | ||
metrics: ["test_metric"], | ||
}, | ||
user: { | ||
[AUTH0_APP_METADATA_KEY]: { | ||
state_code: "us_nd", | ||
}, | ||
}, | ||
}, | ||
{ | ||
send: mockFn, | ||
} | ||
); | ||
expect(mockFn).toHaveBeenCalledWith("passed"); | ||
}); | ||
|
||
test("returns 401 if auth is enabled and user state code doesn't match the request param", async () => { | ||
process.env.AUTH_ENABLED = "true"; | ||
const mockStatusFn = jest.fn(); | ||
const mockSendFn = jest.fn(); | ||
metricsByName( | ||
{ | ||
params: { | ||
tenantId: "US_PA", | ||
}, | ||
body: { | ||
metrics: ["test_metric"], | ||
}, | ||
user: { | ||
[AUTH0_APP_METADATA_KEY]: { | ||
state_code: "us_nd", | ||
}, | ||
}, | ||
}, | ||
{ | ||
status: () => ({ json: mockStatusFn }), | ||
send: mockSendFn, | ||
} | ||
); | ||
expect(mockSendFn).not.toHaveBeenCalled(); | ||
expect(mockStatusFn).toHaveBeenCalledWith({ | ||
error: "User is not a member of the requested tenant US_PA", | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const AUTH0_APP_METADATA_KEY = "https://recidiviz.org/app_metadata"; | ||
|
||
module.exports = { | ||
AUTH0_APP_METADATA_KEY, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should throw different error messages in these different cases:
Might be helpful for debugging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I can assert that
AUTH0_APP_METADATA_KEY
is defined and we'll see the error in the logs if it's not? If any of the rest is empty we know the user app_metadata hasn't been configured properly. We could have different logs though I would want to return the same 401 error to the client.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep sounds good, whatever you think is best! Just thought I'd point it out