-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Merge firebase to master #258
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e3eb7c8
Created check security rules file and a few dummy/helper functions. (…
liminw fe30dc1
Firebase: Merge from master. (#53)
sarvaniv df8c69c
Enable ESP to invoke Firebase Security rules. (#54)
sarvaniv b27d4b9
Merge from master into firebase (#65)
sarvaniv e675ee3
Update the auth checke to use service.experimental.authorization.prov…
dee3881
Update the auth check to use service.experimental.authorization.provider
0c001df
Update the auth check to use service.experimental.authorization.provi…
wattli f90136c
Unit tests for check security rules. (#75)
sarvaniv df4b7e4
Merge from master to firebase (#143)
sarvaniv cfdb303
Merge from master to firebase (#159)
sarvaniv e00681a
Allow for HTTP based function from Firebase rules (#202)
sarvaniv 9442a83
Merge from master to firebase (#237)
sarvaniv b2b45fd
Allow HTTP functions in firebase rules to specify audience (#244)
sarvaniv 6385292
Merge branch 'firebase' of https://github.com/istio/proxy into merge_…
sarvaniv 78fee54
Remove .bazelrc.jenkins file not present in the master branch.
sarvaniv 990d8e0
Remove forward_attribute_filter.cc not present in master.
sarvaniv 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This is from Bazel's former travis setup, to avoid blowing up the RAM usage. | ||
startup --host_jvm_args=-Xmx8192m | ||
startup --host_jvm_args=-Xms8192m | ||
startup --batch | ||
|
||
# This is so we understand failures better | ||
build --verbose_failures | ||
|
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
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
230 changes: 230 additions & 0 deletions
230
contrib/endpoints/src/api_manager/check_security_rules.cc
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,230 @@ | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
#include "contrib/endpoints/src/api_manager/check_security_rules.h" | ||
#include <iostream> | ||
#include <sstream> | ||
#include "contrib/endpoints/src/api_manager/auth/lib/json_util.h" | ||
#include "contrib/endpoints/src/api_manager/firebase_rules/firebase_request.h" | ||
#include "contrib/endpoints/src/api_manager/utils/marshalling.h" | ||
|
||
using ::google::api_manager::auth::GetStringValue; | ||
using ::google::api_manager::firebase_rules::FirebaseRequest; | ||
using ::google::api_manager::utils::Status; | ||
const char kFirebaseAudience[] = | ||
"https://staging-firebaserules.sandbox.googleapis.com/" | ||
"google.firebase.rules.v1.FirebaseRulesService"; | ||
|
||
namespace google { | ||
namespace api_manager { | ||
namespace { | ||
|
||
const std::string kFailedFirebaseReleaseFetch = | ||
"Failed to fetch Firebase Release"; | ||
const std::string kFailedFirebaseTest = "Failed to execute Firebase Test"; | ||
const std::string kInvalidResponse = | ||
"Invalid JSON response from Firebase Service"; | ||
const std::string kV1 = "/v1"; | ||
const std::string kHttpGetMethod = "GET"; | ||
const std::string kProjects = "/projects"; | ||
const std::string kReleases = "/releases"; | ||
const std::string kRulesetName = "rulesetName"; | ||
const std::string kContentType = "Content-Type"; | ||
const std::string kApplication = "application/json"; | ||
|
||
std::string GetReleaseName(const context::RequestContext &context) { | ||
return context.service_context()->service_name() + ":" + | ||
context.service_context()->service().apis(0).version(); | ||
} | ||
|
||
std::string GetReleaseUrl(const context::RequestContext &context) { | ||
return context.service_context()->config()->GetFirebaseServer() + kV1 + | ||
kProjects + "/" + context.service_context()->project_id() + kReleases + | ||
"/" + GetReleaseName(context); | ||
} | ||
|
||
// An AuthzChecker object is created for every incoming request. It does | ||
// authorizaiton by calling Firebase Rules service. | ||
class AuthzChecker : public std::enable_shared_from_this<AuthzChecker> { | ||
public: | ||
// Constructor | ||
AuthzChecker(ApiManagerEnvInterface *env, | ||
auth::ServiceAccountToken *sa_token); | ||
|
||
// Check for Authorization success or failure | ||
void Check(std::shared_ptr<context::RequestContext> context, | ||
std::function<void(Status status)> continuation); | ||
|
||
private: | ||
// This method invokes the Firebase TestRuleset API endpoint as well as user | ||
// defined endpoints provided by the TestRulesetResponse. | ||
void CallNextRequest(std::function<void(Status status)> continuation); | ||
|
||
// Parse the response for GET RELEASE API call | ||
Status ParseReleaseResponse(const std::string &json_str, | ||
std::string *ruleset_id); | ||
|
||
// Invoke the HTTP call | ||
void HttpFetch(const std::string &url, const std::string &method, | ||
const std::string &request_body, | ||
auth::ServiceAccountToken::JWT_TOKEN_TYPE token_type, | ||
const std::string &audience, | ||
std::function<void(Status, std::string &&)> continuation); | ||
|
||
std::shared_ptr<AuthzChecker> GetPtr() { return shared_from_this(); } | ||
|
||
ApiManagerEnvInterface *env_; | ||
auth::ServiceAccountToken *sa_token_; | ||
std::unique_ptr<FirebaseRequest> request_handler_; | ||
}; | ||
|
||
AuthzChecker::AuthzChecker(ApiManagerEnvInterface *env, | ||
auth::ServiceAccountToken *sa_token) | ||
: env_(env), sa_token_(sa_token) {} | ||
|
||
void AuthzChecker::Check( | ||
std::shared_ptr<context::RequestContext> context, | ||
std::function<void(Status status)> final_continuation) { | ||
// TODO: Check service config to see if "useSecurityRules" is specified. | ||
// If so, call Firebase Rules service TestRuleset API. | ||
|
||
if (!context->service_context()->IsRulesCheckEnabled() || | ||
context->method() == nullptr || !context->method()->auth()) { | ||
env_->LogDebug("Skipping Firebase Rules checks since it is disabled."); | ||
final_continuation(Status::OK); | ||
return; | ||
} | ||
|
||
// Fetch the Release attributes and get ruleset name. | ||
auto checker = GetPtr(); | ||
HttpFetch(GetReleaseUrl(*context), kHttpGetMethod, "", | ||
auth::ServiceAccountToken::JWT_TOKEN_FOR_FIREBASE, | ||
kFirebaseAudience, [context, final_continuation, checker]( | ||
Status status, std::string &&body) { | ||
std::string ruleset_id; | ||
if (status.ok()) { | ||
checker->env_->LogDebug( | ||
std::string("GetReleasName succeeded with ") + body); | ||
status = checker->ParseReleaseResponse(body, &ruleset_id); | ||
} else { | ||
checker->env_->LogError(std::string("GetReleaseName for ") + | ||
GetReleaseUrl(*context.get()) + | ||
" with status " + status.ToString()); | ||
status = Status(Code::INTERNAL, kFailedFirebaseReleaseFetch); | ||
} | ||
|
||
// If the parsing of the release body is successful, then call the | ||
// Test Api for firebase rules service. | ||
if (status.ok()) { | ||
checker->request_handler_ = std::unique_ptr<FirebaseRequest>( | ||
new FirebaseRequest(ruleset_id, checker->env_, context)); | ||
checker->CallNextRequest(final_continuation); | ||
} else { | ||
final_continuation(status); | ||
} | ||
}); | ||
} | ||
|
||
void AuthzChecker::CallNextRequest( | ||
std::function<void(Status status)> continuation) { | ||
if (request_handler_->is_done()) { | ||
continuation(request_handler_->RequestStatus()); | ||
return; | ||
} | ||
|
||
auto checker = GetPtr(); | ||
firebase_rules::HttpRequest http_request = request_handler_->GetHttpRequest(); | ||
HttpFetch(http_request.url, http_request.method, http_request.body, | ||
http_request.token_type, http_request.audience, | ||
[continuation, checker](Status status, std::string &&body) { | ||
|
||
checker->env_->LogError(std::string("Response Body = ") + body); | ||
if (status.ok() && !body.empty()) { | ||
checker->request_handler_->UpdateResponse(body); | ||
checker->CallNextRequest(continuation); | ||
} else { | ||
checker->env_->LogError( | ||
std::string("Test API failed with ") + | ||
(status.ok() ? "Empty Response" : status.ToString())); | ||
status = Status(Code::INTERNAL, kFailedFirebaseTest); | ||
continuation(status); | ||
} | ||
}); | ||
} | ||
|
||
Status AuthzChecker::ParseReleaseResponse(const std::string &json_str, | ||
std::string *ruleset_id) { | ||
grpc_json *json = grpc_json_parse_string_with_len( | ||
const_cast<char *>(json_str.data()), json_str.length()); | ||
|
||
if (!json) { | ||
return Status(Code::INVALID_ARGUMENT, kInvalidResponse); | ||
} | ||
|
||
Status status = Status::OK; | ||
const char *id = GetStringValue(json, kRulesetName.c_str()); | ||
*ruleset_id = (id == nullptr) ? "" : id; | ||
|
||
if (ruleset_id->empty()) { | ||
env_->LogError("Empty ruleset Id received from firebase service"); | ||
status = Status(Code::INTERNAL, kInvalidResponse); | ||
} else { | ||
env_->LogDebug(std::string("Received ruleset Id: ") + *ruleset_id); | ||
} | ||
|
||
grpc_json_destroy(json); | ||
return status; | ||
} | ||
|
||
void AuthzChecker::HttpFetch( | ||
const std::string &url, const std::string &method, | ||
const std::string &request_body, | ||
auth::ServiceAccountToken::JWT_TOKEN_TYPE token_type, | ||
const std::string &audience, | ||
std::function<void(Status, std::string &&)> continuation) { | ||
env_->LogDebug(std::string("Issue HTTP Request to url :") + url + | ||
" method : " + method + " body: " + request_body); | ||
|
||
std::unique_ptr<HTTPRequest> request(new HTTPRequest([continuation]( | ||
Status status, std::map<std::string, std::string> &&, | ||
std::string &&body) { continuation(status, std::move(body)); })); | ||
|
||
if (!request) { | ||
continuation(Status(Code::INTERNAL, "Out of memory"), ""); | ||
return; | ||
} | ||
|
||
request->set_method(method).set_url(url).set_auth_token( | ||
sa_token_->GetAuthToken(token_type, audience)); | ||
|
||
if (!request_body.empty()) { | ||
request->set_header(kContentType, kApplication).set_body(request_body); | ||
} | ||
|
||
env_->RunHTTPRequest(std::move(request)); | ||
} | ||
|
||
} // namespace | ||
|
||
void CheckSecurityRules(std::shared_ptr<context::RequestContext> context, | ||
std::function<void(Status status)> continuation) { | ||
std::shared_ptr<AuthzChecker> checker = std::make_shared<AuthzChecker>( | ||
context->service_context()->env(), | ||
context->service_context()->service_account_token()); | ||
checker->Check(context, continuation); | ||
} | ||
|
||
} // namespace api_manager | ||
} // namespace google |
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.
this is not firebase related. where is it coming from