Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/envoy/mixer/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2016 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.
#
################################################################################
#

package(default_visibility = ["//visibility:public"])

cc_binary(
name = "envoy_esp",

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 suffix _esp?

it would be nice to export a cc_library, and a cc_binary so it can be linked together.

srcs = [
"http_control.cc",
"http_control.h",
"http_filter.cc",
],
linkstatic = 1,
deps = [
"//external:mixer_client_lib",
"@envoy_git//:envoy-common",
"@envoy_git//:envoy-main",
],
)
71 changes: 71 additions & 0 deletions src/envoy/mixer/envoy-esp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"listeners": [
{
"port": 9090,
"bind_to_port": true,
"filters": [
{
"type": "read",
"name": "http_connection_manager",
"config": {
"codec_type": "auto",
"stat_prefix": "ingress_http",
"route_config": {
"virtual_hosts": [
{
"name": "backend",
"domains": ["*"],
"routes": [
{
"timeout_ms": 0,
"prefix": "/",
"cluster": "service1"
}
]
}
]
},
"access_log": [
{
"path": "/dev/stdout"
}
],
"filters": [
{
"type": "both",
"name": "esp",

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.

rename to "mixerclient"

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.

changed to mixer.

"config": {
"mixer_server": "localhost:9091"
}
},
{
"type": "decoder",
"name": "router",
"config": {}
}
]
}
}
]
}
],
"admin": {
"access_log_path": "/dev/stdout",
"port": 9001
},
"cluster_manager": {
"clusters": [
{
"name": "service1",
"connect_timeout_ms": 5000,
"type": "strict_dns",
"lb_type": "round_robin",
"hosts": [
{
"url": "tcp://localhost:8080"
}
]
}
]
}
}
162 changes: 162 additions & 0 deletions src/envoy/mixer/http_control.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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 "src/envoy/mixer/http_control.h"
#include "common/common/utility.h"
#include "common/http/utility.h"

using ::google::protobuf::util::Status;
using ::istio::mixer_client::Attributes;
using ::istio::mixer_client::DoneFunc;

namespace Http {
namespace Mixer {
namespace {

const std::string kProxyPeerID = "Istio/Proxy";
const std::string kEnvNameTargetService = "TARGET_SERVICE";

// Define lower case string for X-Forwarded-Host.
const LowerCaseString kHeaderNameXFH("x-forwarded-host", false);

// Define attribute names
const std::string kAttrNamePeerId = "peerId";
const std::string kAttrNameURL = "url";
const std::string kAttrNameHttpMethod = "httpMethod";

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.

add x-request-id as an attribute since it's very useful for understanding dataflow.

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.

I added all http headers. It looks like these

HTTP-request-header::authority: (STRING): localhost:9090
HTTP-request-header::method: (STRING): POST
HTTP-request-header::path: (STRING): /echo?key=API-KEY
HTTP-request-header:accept: (STRING): /
HTTP-request-header:content-length: (STRING): 11
HTTP-request-header:content-type: (STRING): application/x-www-form-urlencoded
HTTP-request-header:user-agent: (STRING): curl/7.35.0
HTTP-request-header:x-forwarded-proto: (STRING): http
HTTP-request-header:x-request-id: (STRING): fe6cd0cf-31b6-40c0-b352-3b5186daec2c
HTTP-request-param:key: (STRING): API-KEY
HTTP-response-header::status: (STRING): 200
HTTP-response-header:content-length: (STRING): 11
HTTP-response-header:content-type: (STRING): application/x-www-form-urlencoded
HTTP-response-header:date: (STRING): Sat, 04 Feb 2017 00:22:44 GMT
HTTP-response-header:server: (STRING): envoy
HTTP-response-header:x-envoy-upstream-service-time: (STRING): 0
peerId: (STRING): Istio/Proxy
requestSize: (INT64): 11
responseSize: (INT64): 11
responseTime: (INT64): 3
targetService: (STRING): abdcecee

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.

@mandarjog, @geeknoid is there some namespace for HTTP header attributes? Or should they be translated to some canonical attribute name?

const std::string kAttrNameRequestSize = "requestSize";
const std::string kAttrNameResponseSize = "responseSize";
const std::string kAttrNameLogMessage = "logMessage";
const std::string kAttrNameResponseTime = "responseTime";
const std::string kAttrNameOriginIp = "originIp";
const std::string kAttrNameOriginHost = "originHost";
const std::string kAttrNameTargetService = "targetService";

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.

rename to source and destination?

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.

let us keep it as target for now.


Attributes::Value StringValue(const std::string& str) {
Attributes::Value v;
v.type = Attributes::Value::STRING;
v.str_v = str;
return v;
}

Attributes::Value Int64Value(int64_t value) {
Attributes::Value v;
v.type = Attributes::Value::INT64;
v.value.int64_v = value;
return v;
}

void SetStringAttribute(const std::string& name, const std::string& value,
Attributes* attr) {
if (!value.empty()) {
attr->attributes[name] = StringValue(value);
}
}

std::string GetFirstForwardedFor(const HeaderMap& header_map) {
if (!header_map.ForwardedFor()) {
return "";
}
std::vector<std::string> xff_address_list =
StringUtil::split(header_map.ForwardedFor()->value().c_str(), ',');
if (xff_address_list.empty()) {
return "";
}
return xff_address_list.front();
}

std::string GetLastForwardedHost(const HeaderMap& header_map) {
const HeaderEntry* entry = header_map.get(kHeaderNameXFH);
if (entry == nullptr) {
return "";
}
auto xff_list = StringUtil::split(entry->value().c_str(), ',');
if (xff_list.empty()) {
return "";
}
return xff_list.back();
}

} // namespace

HttpControl::HttpControl(const std::string& mixer_server) {
::istio::mixer_client::MixerClientOptions options;
options.mixer_server = mixer_server;
mixer_client_ = ::istio::mixer_client::CreateMixerClient(options);

auto target_service = getenv(kEnvNameTargetService.c_str());
if (target_service) {
target_service_ = target_service;
}
}

void HttpControl::FillCheckAttributes(const HeaderMap& header_map,
Attributes* attr) {
// Pass in all headers
header_map.iterate(
[](const HeaderEntry& header, void* context) -> void {
auto attr = static_cast<Attributes*>(context);
attr->attributes[header.key().c_str()] =
StringValue(header.value().c_str());
},
attr);

// Pass in all Query parameters.
auto path = header_map.Path();
if (path != nullptr) {
for (const auto& it : Utility::parseQueryString(path->value().c_str())) {
attr->attributes[it.first] = StringValue(it.second);
}
}

SetStringAttribute(kAttrNameOriginIp, GetFirstForwardedFor(header_map), attr);
SetStringAttribute(kAttrNameOriginHost, GetLastForwardedHost(header_map),
attr);

SetStringAttribute(kAttrNameTargetService, target_service_, attr);
attr->attributes[kAttrNamePeerId] = StringValue(kProxyPeerID);
}

void HttpControl::FillReportAttributes(const AccessLog::RequestInfo& info,
Attributes* attr) {
if (info.bytesReceived() >= 0) {
attr->attributes[kAttrNameRequestSize] = Int64Value(info.bytesReceived());
}
if (info.bytesSent() >= 0) {
attr->attributes[kAttrNameResponseSize] = Int64Value(info.bytesSent());
}
if (info.duration().count() >= 0) {
attr->attributes[kAttrNameResponseTime] =
Int64Value(info.duration().count());
}
}

void HttpControl::Check(HttpRequestDataPtr request_data, HeaderMap& headers,
DoneFunc on_done) {
FillCheckAttributes(headers, &request_data->attributes);
log().debug("Send Check: {}", request_data->attributes.DebugString());
mixer_client_->Check(request_data->attributes, on_done);
}

void HttpControl::Report(HttpRequestDataPtr request_data,
const AccessLog::RequestInfo& request_info,
DoneFunc on_done) {
// Use all Check attributes for Report.
// Add additional Report attributes.
FillReportAttributes(request_info, &request_data->attributes);
log().debug("Send Report: {}", request_data->attributes.DebugString());
mixer_client_->Report(request_data->attributes, on_done);
}

} // namespace Mixer
} // namespace Http
64 changes: 64 additions & 0 deletions src/envoy/mixer/http_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright 2016 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.
*/

#pragma once

#include "precompiled/precompiled.h"

#include "common/common/logger.h"
#include "common/http/headers.h"
#include "envoy/http/access_log.h"
#include "include/client.h"

namespace Http {
namespace Mixer {

// Store data from Check to report
struct HttpRequestData {
::istio::mixer_client::Attributes attributes;
};
typedef std::shared_ptr<HttpRequestData> HttpRequestDataPtr;

// The mixer client class to control HTTP requests.
// It has Check() to validate if a request can be processed.
// At the end of request, call Report().
class HttpControl final : public Logger::Loggable<Logger::Id::http> {
public:
// The constructor.
HttpControl(const std::string& mixer_server);

// Make mixer check call.
void Check(HttpRequestDataPtr request_data, HeaderMap& headers,
::istio::mixer_client::DoneFunc on_done);

// Make mixer report call.
void Report(HttpRequestDataPtr request_data,
const AccessLog::RequestInfo& request_info,
::istio::mixer_client::DoneFunc on_done);

private:
void FillCheckAttributes(const HeaderMap& header_map,
::istio::mixer_client::Attributes* attr);
void FillReportAttributes(const AccessLog::RequestInfo& info,
::istio::mixer_client::Attributes* attr);

// The mixer client
std::unique_ptr<::istio::mixer_client::MixerClient> mixer_client_;
// Target service
std::string target_service_;
};

} // namespace Mixer
} // namespace Http
Loading