@@ -23,10 +23,17 @@ namespace Extensions {
23
23
namespace HttpFilters {
24
24
namespace AwsLambda {
25
25
26
+ namespace {
26
27
struct RcDetailsValues {
27
28
const std::string FunctionNotFound = " aws_lambda_function_not_found" ;
29
+ const std::string FunctionNotFoundBody =
30
+ " no function present for AWS upstream" ;
31
+ const std::string CredentialsNotFound = " aws_lambda_credentials_not_found" ;
32
+ const std::string CredentialsNotFoundBody =
33
+ " no credentials present for AWS upstream" ;
28
34
};
29
35
typedef ConstSingleton<RcDetailsValues> RcDetails;
36
+ } // namespace
30
37
31
38
class AWSLambdaHeaderValues {
32
39
public:
@@ -47,33 +54,53 @@ const HeaderList AWSLambdaFilter::HeadersToSign =
47
54
Http::Headers::get ().ContentType });
48
55
49
56
AWSLambdaFilter::AWSLambdaFilter (Upstream::ClusterManager &cluster_manager,
50
- TimeSource &time_source)
51
- : aws_authenticator_(time_source), cluster_manager_(cluster_manager) {}
57
+ TimeSource &time_source,
58
+ AWSLambdaConfigConstSharedPtr filter_config)
59
+ : aws_authenticator_(time_source), cluster_manager_(cluster_manager),
60
+ filter_config_ (filter_config) {}
52
61
53
62
AWSLambdaFilter::~AWSLambdaFilter () {}
54
63
55
- std::string AWSLambdaFilter::functionUrlPath (const std::string &name,
56
- const std::string &qualifier) {
57
-
58
- std::stringstream val;
59
- val << " /2015-03-31/functions/" << name << " /invocations" ;
60
- if (!qualifier.empty ()) {
61
- val << " ?Qualifier=" << qualifier;
62
- }
63
- return val.str ();
64
- }
65
-
66
64
Http::FilterHeadersStatus
67
65
AWSLambdaFilter::decodeHeaders (Http::HeaderMap &headers, bool end_stream) {
68
66
69
67
protocol_options_ = Http::SoloFilterUtility::resolveProtocolOptions<
70
68
const AWSLambdaProtocolExtensionConfig>(
71
69
SoloHttpFilterNames::get ().AwsLambda , decoder_callbacks_,
72
70
cluster_manager_);
71
+
73
72
if (!protocol_options_) {
74
73
return Http::FilterHeadersStatus::Continue;
75
74
}
76
75
76
+ const std::string *access_key{};
77
+ const std::string *secret_key{};
78
+ if (protocol_options_->accessKey ().has_value () &&
79
+ protocol_options_->secretKey ().has_value ()) {
80
+ access_key = &protocol_options_->accessKey ().value ();
81
+ secret_key = &protocol_options_->secretKey ().value ();
82
+ } else if (filter_config_) {
83
+ credentials_ = filter_config_->getCredentials ();
84
+ if (credentials_) {
85
+ const absl::optional<std::string> &maybeAccessKeyId =
86
+ credentials_->accessKeyId ();
87
+ const absl::optional<std::string> &maybeSecretAccessKey =
88
+ credentials_->secretAccessKey ();
89
+ if (maybeAccessKeyId.has_value () && maybeSecretAccessKey.has_value ()) {
90
+ access_key = &maybeAccessKeyId.value ();
91
+ secret_key = &maybeSecretAccessKey.value ();
92
+ }
93
+ }
94
+ }
95
+
96
+ if ((access_key == nullptr ) || (secret_key == nullptr )) {
97
+ decoder_callbacks_->sendLocalReply (Http::Code::InternalServerError,
98
+ RcDetails::get ().CredentialsNotFoundBody ,
99
+ nullptr , absl::nullopt,
100
+ RcDetails::get ().CredentialsNotFound );
101
+ return Http::FilterHeadersStatus::StopIteration;
102
+ }
103
+
77
104
route_ = decoder_callbacks_->route ();
78
105
// great! this is an aws cluster. get the function information:
79
106
function_on_route_ =
@@ -82,20 +109,19 @@ AWSLambdaFilter::decodeHeaders(Http::HeaderMap &headers, bool end_stream) {
82
109
83
110
if (!function_on_route_) {
84
111
decoder_callbacks_->sendLocalReply (
85
- Http::Code::NotFound, " no function present for AWS upstream " , nullptr ,
86
- absl::nullopt, RcDetails::get ().FunctionNotFound );
112
+ Http::Code::InternalServerError, RcDetails::get (). FunctionNotFoundBody ,
113
+ nullptr , absl::nullopt, RcDetails::get ().FunctionNotFound );
87
114
return Http::FilterHeadersStatus::StopIteration;
88
115
}
89
116
90
- aws_authenticator_.init (&protocol_options_->accessKey (),
91
- &protocol_options_->secretKey ());
117
+ aws_authenticator_.init (access_key, secret_key);
92
118
request_headers_ = &headers;
93
119
94
120
request_headers_->insertMethod ().value ().setReference (
95
121
Http::Headers::get ().MethodValues .Post );
96
122
97
- request_headers_->insertPath ().value (functionUrlPath (
98
- function_on_route_->name (), function_on_route_-> qualifier () ));
123
+ request_headers_->insertPath ().value (). setReference (
124
+ function_on_route_->path ( ));
99
125
100
126
if (end_stream) {
101
127
lambdafy ();
0 commit comments