Sfoglia il codice sorgente

Added getter method within alts_context.cc to plumb through peer attributes. Included Test in alts_util

Danny Reidenbach 5 anni fa
parent
commit
863ae0c124

+ 3 - 0
include/grpcpp/security/alts_context.h

@@ -23,6 +23,7 @@
 #include <grpcpp/security/auth_context.h>
 
 #include <memory>
+#include <map>
 
 struct grpc_gcp_AltsContext;
 
@@ -50,6 +51,7 @@ class AltsContext {
   std::string local_service_account() const;
   grpc_security_level security_level() const;
   RpcProtocolVersions peer_rpc_versions() const;
+  std::map<std::string, std::string> peer_attributes();
 
  private:
   // TODO(ZhenLian): Also plumb field peer_attributes when it is in use
@@ -59,6 +61,7 @@ class AltsContext {
   std::string local_service_account_;
   grpc_security_level security_level_ = GRPC_SECURITY_NONE;
   RpcProtocolVersions peer_rpc_versions_ = {{0, 0}, {0, 0}};
+  std::map<std::string, std::string> peer_attributes_map;
 };
 
 }  // namespace experimental

+ 16 - 0
src/cpp/common/alts_context.cc

@@ -80,6 +80,18 @@ AltsContext::AltsContext(const grpc_gcp_AltsContext* ctx) {
     security_level_ = static_cast<grpc_security_level>(
         grpc_gcp_AltsContext_security_level(ctx));
   }
+  if (grpc_gcp_AltsContext_has_peer_attributes(ctx)) {
+    // std::map<std::string, std::string> peer_attributes_map;
+    size_t iter = UPB_MAP_BEGIN;
+    const grpc_gcp_AltsContext_PeerAttributesEntry* peer_attributes_entry= grpc_gcp_AltsContext_peer_attributes_next(ctx, &iter);
+    while ( peer_attributes_entry != nullptr) {
+      upb_strview key = grpc_gcp_AltsContext_PeerAttributesEntry_key(peer_attributes_entry);
+      upb_strview val = grpc_gcp_AltsContext_PeerAttributesEntry_value(peer_attributes_entry);
+      peer_attributes_map[std::string(key.data, key.size)] = std::string(val.data, val.size);
+      peer_attributes_entry = grpc_gcp_AltsContext_peer_attributes_next(ctx, &iter);
+    }
+
+  }
 }
 
 std::string AltsContext::application_protocol() const {
@@ -104,5 +116,9 @@ AltsContext::RpcProtocolVersions AltsContext::peer_rpc_versions() const {
   return peer_rpc_versions_;
 }
 
+std::map<std::string, std::string> AltsContext::peer_attributes() {
+  return peer_attributes_map;
+}
+
 }  // namespace experimental
 }  // namespace grpc

+ 13 - 0
test/cpp/common/alts_util_test.cc

@@ -83,6 +83,11 @@ TEST(AltsUtilTest, AuthContextWithGoodAltsContextWithoutRpcVersions) {
   std::string expected_rp("record protocol");
   std::string expected_peer("peer");
   std::string expected_local("local");
+
+  //doggo
+  std::string expected_peer_atrributes_key("peer");
+  std::string expected_peer_atrributes_value("attributes");
+
   grpc_security_level expected_sl = GRPC_INTEGRITY_ONLY;
   upb::Arena context_arena;
   grpc_gcp_AltsContext* context = grpc_gcp_AltsContext_new(context_arena.ptr());
@@ -96,6 +101,12 @@ TEST(AltsUtilTest, AuthContextWithGoodAltsContextWithoutRpcVersions) {
   grpc_gcp_AltsContext_set_local_service_account(
       context,
       upb_strview_make(expected_local.data(), expected_local.length()));
+
+grpc_gcp_AltsContext_peer_attributes_set(context,
+      upb_strview_make(expected_peer_atrributes_key.data(), expected_peer_atrributes_key.length()),
+      upb_strview_make(expected_peer_atrributes_value.data(), expected_peer_atrributes_value.length()),
+      context_arena.ptr()); 
+
   size_t serialized_ctx_length;
   char* serialized_ctx = grpc_gcp_AltsContext_serialize(
       context, context_arena.ptr(), &serialized_ctx_length);
@@ -117,6 +128,8 @@ TEST(AltsUtilTest, AuthContextWithGoodAltsContextWithoutRpcVersions) {
   EXPECT_EQ(0, rpc_protocol_versions.max_rpc_version.minor_version);
   EXPECT_EQ(0, rpc_protocol_versions.min_rpc_version.major_version);
   EXPECT_EQ(0, rpc_protocol_versions.min_rpc_version.minor_version);
+
+  EXPECT_EQ(expected_peer_atrributes_value, alts_context->peer_attributes()[expected_peer_atrributes_key]);
 }
 
 TEST(AltsUtilTest, AuthContextWithGoodAltsContext) {