mirror of https://github.com/grpc/grpc.git
parent
a30f2f9501
commit
fe9576c861
11 changed files with 569 additions and 2 deletions
@ -0,0 +1,39 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPCPP_ALTS_CONTEXT_H |
||||
#define GRPCPP_ALTS_CONTEXT_H |
||||
|
||||
#include <grpcpp/impl/codegen/security/auth_context.h> |
||||
#include <memory> |
||||
|
||||
namespace grpc { |
||||
|
||||
namespace gcp { |
||||
class AltsContext; |
||||
} // namespace gcp
|
||||
|
||||
// GetAltsContextFromAuthContext helps to get the AltsContext from AuthContext.
|
||||
// Please make sure the underlying protocol is ALTS before calling this
|
||||
// function. Otherwise a nullptr will be returned.
|
||||
std::unique_ptr<gcp::AltsContext> GetAltsContextFromAuthContext( |
||||
const AuthContext& auth_context); |
||||
|
||||
} // namespace grpc
|
||||
|
||||
#endif // GRPCPP_ALTS_CONTEXT_H
|
@ -0,0 +1,44 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 <grpc/grpc_security.h> |
||||
#include <grpcpp/alts_context.h> |
||||
|
||||
#include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h" |
||||
#include "src/cpp/common/secure_auth_context.h" |
||||
#include "src/proto/grpc/gcp/altscontext.pb.h" |
||||
|
||||
namespace grpc { |
||||
std::unique_ptr<gcp::AltsContext> GetAltsContextFromAuthContext( |
||||
const AuthContext& auth_context) { |
||||
std::vector<string_ref> ctx_vector = |
||||
auth_context.FindPropertyValues(TSI_ALTS_CONTEXT); |
||||
if (ctx_vector.size() != 1) { |
||||
gpr_log(GPR_ERROR, "contains zero or more than one ALTS context."); |
||||
return nullptr; |
||||
} |
||||
std::unique_ptr<gcp::AltsContext> uniq_ctx(new gcp::AltsContext()); |
||||
bool success = uniq_ctx.get()->ParseFromArray(ctx_vector[0].data(), |
||||
ctx_vector[0].size()); |
||||
if (!success) { |
||||
gpr_log(GPR_ERROR, "fails to parse ALTS context."); |
||||
return nullptr; |
||||
} |
||||
return uniq_ctx; |
||||
} |
||||
} // namespace grpc
|
@ -0,0 +1,96 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 <grpcpp/alts_context.h> |
||||
#include <grpcpp/security/auth_context.h> |
||||
|
||||
#include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h" |
||||
#include "src/cpp/common/secure_auth_context.h" |
||||
#include "src/proto/grpc/gcp/altscontext.pb.h" |
||||
#include "test/cpp/util/string_ref_helper.h" |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
using grpc::testing::ToString; |
||||
|
||||
namespace grpc { |
||||
namespace { |
||||
|
||||
TEST(AltsContextTest, EmptyAuthContext) { |
||||
SecureAuthContext context(nullptr); |
||||
std::unique_ptr<gcp::AltsContext> alts_context = |
||||
GetAltsContextFromAuthContext(context); |
||||
EXPECT_EQ(alts_context.get(), nullptr); |
||||
} |
||||
|
||||
TEST(AltsContextTest, AuthContextWithMoreThanOneAltsContext) { |
||||
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
||||
grpc_core::MakeRefCounted<grpc_auth_context>(nullptr); |
||||
SecureAuthContext context(ctx.get()); |
||||
ctx.reset(); |
||||
context.AddProperty(TSI_ALTS_CONTEXT, "context1"); |
||||
context.AddProperty(TSI_ALTS_CONTEXT, "context2"); |
||||
std::unique_ptr<gcp::AltsContext> alts_context = |
||||
GetAltsContextFromAuthContext(context); |
||||
EXPECT_EQ(alts_context.get(), nullptr); |
||||
} |
||||
|
||||
TEST(AltsContextTest, AuthContextWithBadAltsContext) { |
||||
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
||||
grpc_core::MakeRefCounted<grpc_auth_context>(nullptr); |
||||
SecureAuthContext context(ctx.get()); |
||||
ctx.reset(); |
||||
context.AddProperty(TSI_ALTS_CONTEXT, "bad context string serialization"); |
||||
std::unique_ptr<gcp::AltsContext> alts_context = |
||||
GetAltsContextFromAuthContext(context); |
||||
EXPECT_EQ(alts_context.get(), nullptr); |
||||
} |
||||
|
||||
TEST(AltsContextTest, AuthContextWithGoodAltsContext) { |
||||
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
||||
grpc_core::MakeRefCounted<grpc_auth_context>(nullptr); |
||||
SecureAuthContext context(ctx.get()); |
||||
ctx.reset(); |
||||
std::string expected_application_protocol("application protocol"); |
||||
std::string expected_record_protocol("record protocol"); |
||||
std::string expected_peer_id("peer"); |
||||
std::string expected_local_id("local"); |
||||
gcp::AltsContext expect_alts_ctx; |
||||
expect_alts_ctx.set_application_protocol(expected_application_protocol); |
||||
expect_alts_ctx.set_record_protocol(expected_record_protocol); |
||||
expect_alts_ctx.set_peer_service_account(expected_peer_id); |
||||
expect_alts_ctx.set_local_service_account(expected_local_id); |
||||
std::string serialized_alts_ctx; |
||||
bool success = expect_alts_ctx.SerializeToString(&serialized_alts_ctx); |
||||
EXPECT_TRUE(success); |
||||
context.AddProperty(TSI_ALTS_CONTEXT, serialized_alts_ctx); |
||||
std::unique_ptr<gcp::AltsContext> alts_ctx = |
||||
GetAltsContextFromAuthContext(context); |
||||
EXPECT_EQ(expected_application_protocol, alts_ctx->application_protocol()); |
||||
EXPECT_EQ(expected_record_protocol, alts_ctx->record_protocol()); |
||||
EXPECT_EQ(expected_peer_id, alts_ctx->peer_service_account()); |
||||
EXPECT_EQ(expected_local_id, alts_ctx->local_service_account()); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace grpc
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue