-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chris Roche <croche@lyft.com>
- Loading branch information
Chris Roche
committed
May 9, 2018
1 parent
5180b24
commit a34fde7
Showing
7 changed files
with
162 additions
and
123 deletions.
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,22 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_cc_library", | ||
"envoy_package", | ||
) | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
name = "attribute_context_lib", | ||
hdrs = ["attribute_context.h"], | ||
srcs = ["attribute_context.cc"], | ||
deps = [ | ||
"//include/envoy/http:filter_interface", | ||
"//include/envoy/http:header_map_interface", | ||
"//source/common/http:utility_lib", | ||
"//source/common/network:utility_lib", | ||
"@envoy_api//envoy/service/auth/v2alpha:attribute_context_cc", | ||
], | ||
) |
91 changes: 91 additions & 0 deletions
91
source/extensions/filters/common/auth/attribute_context.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,91 @@ | ||
#include "extensions/filters/common/auth/attribute_context.h" | ||
|
||
#include "common/http/utility.h" | ||
#include "common/network/utility.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Filters { | ||
namespace Common { | ||
namespace Auth { | ||
|
||
void AttributeContextUtils::setSourcePeer(envoy::service::auth::v2alpha::AttributeContext& context, | ||
const Network::Connection& connection, | ||
const std::string& service) { | ||
setPeer(*context.mutable_source(), connection, service, false); | ||
} | ||
|
||
void AttributeContextUtils::setDestinationPeer( | ||
envoy::service::auth::v2alpha::AttributeContext& context, | ||
const Network::Connection& connection) { | ||
setPeer(*context.mutable_destination(), connection, "", true); | ||
} | ||
|
||
void AttributeContextUtils::setPeer(envoy::service::auth::v2alpha::AttributeContext_Peer& peer, | ||
const Network::Connection& connection, | ||
const std::string& service, const bool local) { | ||
|
||
auto addr = local ? connection.localAddress() : connection.remoteAddress(); | ||
Envoy::Network::Utility::addressToProtobufAddress(*addr, *peer.mutable_address()); | ||
|
||
if (!service.empty()) { | ||
peer.set_service(service); | ||
} | ||
|
||
auto ssl = const_cast<Ssl::Connection*>(connection.ssl()); | ||
if (ssl) { | ||
std::string principal = local ? ssl->uriSanLocalCertificate() : ssl->uriSanPeerCertificate(); | ||
if (principal.empty()) { | ||
principal = local ? ssl->subjectLocalCertificate() : ssl->subjectPeerCertificate(); | ||
} | ||
peer.set_principal(principal); | ||
} | ||
} | ||
|
||
void AttributeContextUtils::setHttpRequest( | ||
envoy::service::auth::v2alpha::AttributeContext& context, | ||
const Envoy::Http::StreamDecoderFilterCallbacks* callbacks, | ||
const Envoy::Http::HeaderMap& headers) { | ||
|
||
auto req = context.mutable_request()->mutable_http(); | ||
auto sdfc = const_cast<Envoy::Http::StreamDecoderFilterCallbacks*>(callbacks); | ||
|
||
auto start = ProtobufUtil::TimeUtil::MicrosecondsToTimestamp( | ||
sdfc->requestInfo().startTime().time_since_epoch().count()); | ||
context.mutable_request()->mutable_time()->MergeFrom(start); | ||
|
||
auto proto = sdfc->requestInfo().protocol(); | ||
if (proto) { | ||
req->set_protocol(Envoy::Http::Utility::getProtocolString(proto.value())); | ||
} | ||
|
||
req->set_id(std::to_string(sdfc->streamId())); | ||
req->set_size(sdfc->requestInfo().bytesReceived()); | ||
|
||
req->set_method(getHeaderStr(headers.Method())); | ||
req->set_path(getHeaderStr(headers.Path())); | ||
req->set_host(getHeaderStr(headers.Host())); | ||
req->set_scheme(getHeaderStr(headers.Scheme())); | ||
// TODO(rodaine): add query & fragment fields | ||
|
||
auto mutable_headers = req->mutable_headers(); | ||
headers.iterate( | ||
[](const Envoy::Http::HeaderEntry& e, void* ctx) { | ||
auto mutable_headers = static_cast< | ||
Envoy::Protobuf::Map<Envoy::ProtobufTypes::String, Envoy::ProtobufTypes::String>*>(ctx); | ||
(*mutable_headers)[std::string(e.key().getStringView())] = | ||
std::string(e.value().getStringView()); | ||
return Envoy::Http::HeaderMap::Iterate::Continue; | ||
}, | ||
mutable_headers); | ||
} | ||
|
||
std::string AttributeContextUtils::getHeaderStr(const Envoy::Http::HeaderEntry* entry) { | ||
return entry ? std::string(entry->value().getStringView()) : ""; | ||
} | ||
|
||
} // namespace Auth | ||
} // namespace Common | ||
} // namespace Filters | ||
} // namespace Extensions | ||
} // namespace Envoy |
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,38 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "envoy/http/filter.h" | ||
#include "envoy/http/header_map.h" | ||
#include "envoy/network/connection.h" | ||
#include "envoy/service/auth/v2alpha/attribute_context.pb.h" | ||
|
||
namespace Envoy { | ||
namespace Extensions { | ||
namespace Filters { | ||
namespace Common { | ||
namespace Auth { | ||
|
||
class AttributeContextUtils { | ||
public: | ||
static void setSourcePeer(envoy::service::auth::v2alpha::AttributeContext& context, | ||
const Network::Connection& connection, const std::string& service); | ||
static void setDestinationPeer(envoy::service::auth::v2alpha::AttributeContext& context, | ||
const Network::Connection& connection); | ||
static void setHttpRequest(envoy::service::auth::v2alpha::AttributeContext& context, | ||
const Envoy::Http::StreamDecoderFilterCallbacks* callbacks, | ||
const Envoy::Http::HeaderMap& headers); | ||
|
||
private: | ||
static void setPeer(envoy::service::auth::v2alpha::AttributeContext_Peer& peer, | ||
const Network::Connection& connection, const std::string& service, | ||
const bool local); | ||
|
||
static std::string getHeaderStr(const Envoy::Http::HeaderEntry* entry); | ||
}; | ||
|
||
} // namespace Auth | ||
} // namespace Common | ||
} // namespace Filters | ||
} // namespace Extensions | ||
} // namespace Envoy |
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