Skip to content

Commit 34795b4

Browse files
Merge pull request grpc#23665 from dhc3800/evalargs_util
CEL evaluation argument utilility
2 parents 2c766b1 + 03ca0e3 commit 34795b4

File tree

6 files changed

+173
-0
lines changed

6 files changed

+173
-0
lines changed

BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,9 +1855,11 @@ grpc_cc_library(
18551855
name = "grpc_authorization_engine",
18561856
srcs = [
18571857
"src/core/lib/security/authorization/authorization_engine.cc",
1858+
"src/core/lib/security/authorization/evaluate_args.cc",
18581859
],
18591860
hdrs = [
18601861
"src/core/lib/security/authorization/authorization_engine.h",
1862+
"src/core/lib/security/authorization/evaluate_args.h",
18611863
"src/core/lib/security/authorization/mock_cel/activation.h",
18621864
"src/core/lib/security/authorization/mock_cel/cel_value.h",
18631865
],
@@ -1866,6 +1868,7 @@ grpc_cc_library(
18661868
"envoy_ads_upb",
18671869
"google_api_upb",
18681870
"grpc_base",
1871+
"grpc_secure",
18691872
],
18701873
)
18711874

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8411,6 +8411,7 @@ if(gRPC_BUILD_TESTS)
84118411

84128412
add_executable(authorization_engine_test
84138413
src/core/lib/security/authorization/authorization_engine.cc
8414+
src/core/lib/security/authorization/evaluate_args.cc
84148415
test/core/security/authorization_engine_test.cc
84158416
third_party/googletest/googletest/src/gtest-all.cc
84168417
third_party/googletest/googlemock/src/gmock-all.cc

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11652,6 +11652,7 @@ endif
1165211652

1165311653
AUTHORIZATION_ENGINE_TEST_SRC = \
1165411654
src/core/lib/security/authorization/authorization_engine.cc \
11655+
src/core/lib/security/authorization/evaluate_args.cc \
1165511656
test/core/security/authorization_engine_test.cc \
1165611657

1165711658
AUTHORIZATION_ENGINE_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(AUTHORIZATION_ENGINE_TEST_SRC))))
@@ -11685,6 +11686,8 @@ endif
1168511686

1168611687
$(OBJDIR)/$(CONFIG)/src/core/lib/security/authorization/authorization_engine.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libaddress_sorting.a $(LIBDIR)/$(CONFIG)/libupb.a
1168711688

11689+
$(OBJDIR)/$(CONFIG)/src/core/lib/security/authorization/evaluate_args.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libaddress_sorting.a $(LIBDIR)/$(CONFIG)/libupb.a
11690+
1168811691
$(OBJDIR)/$(CONFIG)/test/core/security/authorization_engine_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libaddress_sorting.a $(LIBDIR)/$(CONFIG)/libupb.a
1168911692

1169011693
deps_authorization_engine_test: $(AUTHORIZATION_ENGINE_TEST_OBJS:.o=.dep)

build_autogenerated.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4771,10 +4771,12 @@ targets:
47714771
language: c++
47724772
headers:
47734773
- src/core/lib/security/authorization/authorization_engine.h
4774+
- src/core/lib/security/authorization/evaluate_args.h
47744775
- src/core/lib/security/authorization/mock_cel/activation.h
47754776
- src/core/lib/security/authorization/mock_cel/cel_value.h
47764777
src:
47774778
- src/core/lib/security/authorization/authorization_engine.cc
4779+
- src/core/lib/security/authorization/evaluate_args.cc
47784780
- test/core/security/authorization_engine_test.cc
47794781
deps:
47804782
- grpc_test_util
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//
2+
//
3+
// Copyright 2020 gRPC authors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
//
18+
19+
#include <grpc/support/port_platform.h>
20+
21+
#include "src/core/lib/security/authorization/evaluate_args.h"
22+
23+
#include "src/core/lib/slice/slice_utils.h"
24+
25+
namespace grpc_core {
26+
27+
absl::string_view EvaluateArgs::GetPath() const {
28+
absl::string_view path;
29+
if (metadata_ != nullptr && metadata_->idx.named.path != nullptr) {
30+
grpc_linked_mdelem* elem = metadata_->idx.named.path;
31+
const grpc_slice& val = GRPC_MDVALUE(elem->md);
32+
path = StringViewFromSlice(val);
33+
}
34+
return path;
35+
}
36+
37+
absl::string_view EvaluateArgs::GetHost() const {
38+
absl::string_view host;
39+
if (metadata_ != nullptr && metadata_->idx.named.host != nullptr) {
40+
grpc_linked_mdelem* elem = metadata_->idx.named.host;
41+
const grpc_slice& val = GRPC_MDVALUE(elem->md);
42+
host = StringViewFromSlice(val);
43+
}
44+
return host;
45+
}
46+
47+
absl::string_view EvaluateArgs::GetMethod() const {
48+
absl::string_view method;
49+
if (metadata_ != nullptr && metadata_->idx.named.method != nullptr) {
50+
grpc_linked_mdelem* elem = metadata_->idx.named.method;
51+
const grpc_slice& val = GRPC_MDVALUE(elem->md);
52+
method = StringViewFromSlice(val);
53+
}
54+
return method;
55+
}
56+
57+
std::multimap<absl::string_view, absl::string_view> EvaluateArgs::GetHeaders()
58+
const {
59+
std::multimap<absl::string_view, absl::string_view> headers;
60+
if (metadata_ == nullptr) {
61+
return headers;
62+
}
63+
for (grpc_linked_mdelem* elem = metadata_->list.head; elem != nullptr;
64+
elem = elem->next) {
65+
const grpc_slice& key = GRPC_MDKEY(elem->md);
66+
const grpc_slice& val = GRPC_MDVALUE(elem->md);
67+
headers.emplace(StringViewFromSlice(key), StringViewFromSlice(val));
68+
}
69+
return headers;
70+
}
71+
72+
absl::string_view EvaluateArgs::GetSpiffeId() const {
73+
absl::string_view spiffe_id;
74+
if (auth_context_ == nullptr) {
75+
return spiffe_id;
76+
}
77+
grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name(
78+
auth_context_, GRPC_PEER_SPIFFE_ID_PROPERTY_NAME);
79+
const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it);
80+
if (prop == nullptr) return spiffe_id;
81+
if (strncmp(prop->value, GRPC_PEER_SPIFFE_ID_PROPERTY_NAME,
82+
prop->value_length) != 0) {
83+
return spiffe_id;
84+
}
85+
if (grpc_auth_property_iterator_next(&it) != nullptr) return spiffe_id;
86+
spiffe_id = absl::string_view(
87+
reinterpret_cast<const char*>(prop->value, prop->value_length));
88+
return spiffe_id;
89+
}
90+
91+
absl::string_view EvaluateArgs::GetCertServerName() const {
92+
absl::string_view name;
93+
if (auth_context_ == nullptr) {
94+
return name;
95+
}
96+
grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name(
97+
auth_context_, GRPC_X509_CN_PROPERTY_NAME);
98+
const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it);
99+
if (prop == nullptr) return name;
100+
if (strncmp(prop->value, GRPC_X509_CN_PROPERTY_NAME, prop->value_length) !=
101+
0) {
102+
return name;
103+
}
104+
if (grpc_auth_property_iterator_next(&it) != nullptr) return name;
105+
name = absl::string_view(
106+
reinterpret_cast<const char*>(prop->value, prop->value_length));
107+
return name;
108+
}
109+
110+
} // namespace grpc_core
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
//
3+
// Copyright 2020 gRPC authors.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
//
18+
19+
#ifndef GRPC_CORE_LIB_SECURITY_AUTHORIZATION_EVALUATE_ARGS_H
20+
#define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_EVALUATE_ARGS_H
21+
22+
#include <grpc/support/port_platform.h>
23+
24+
#include <map>
25+
26+
#include "src/core/lib/iomgr/endpoint.h"
27+
#include "src/core/lib/security/context/security_context.h"
28+
#include "src/core/lib/transport/metadata_batch.h"
29+
30+
namespace grpc_core {
31+
32+
class EvaluateArgs {
33+
public:
34+
EvaluateArgs(grpc_metadata_batch* metadata, grpc_auth_context* auth_context,
35+
grpc_endpoint* endpoint);
36+
37+
absl::string_view GetPath() const;
38+
absl::string_view GetHost() const;
39+
absl::string_view GetMethod() const;
40+
std::multimap<absl::string_view, absl::string_view> GetHeaders() const;
41+
absl::string_view GetSpiffeId() const;
42+
absl::string_view GetCertServerName() const;
43+
44+
// TODO: Add a getter function for source.principal
45+
46+
private:
47+
grpc_metadata_batch* metadata_;
48+
grpc_auth_context* auth_context_;
49+
grpc_endpoint* endpoint_;
50+
};
51+
52+
} // namespace grpc_core
53+
54+
#endif // GRPC_CORE_LIB_SECURITY_AUTHORIZATION_EVALUATE_ARGS_H

0 commit comments

Comments
 (0)