Skip to content

Commit

Permalink
Merge pull request grpc#3765 from jboeuf/core_creds_api_change
Browse files Browse the repository at this point in the history
Core credentials API refactoring
  • Loading branch information
jboeuf committed Nov 18, 2015
2 parents 1505b69 + 25e46bf commit 731c83f
Show file tree
Hide file tree
Showing 125 changed files with 1,822 additions and 1,437 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ E = @echo
Q = @
endif

VERSION = 0.11.0.0
VERSION = 0.12.0.0

CPPFLAGS_NO_ARCH += $(addprefix -I, $(INCLUDES)) $(addprefix -D, $(DEFINES))
CPPFLAGS += $(CPPFLAGS_NO_ARCH) $(ARCH_FLAGS)
Expand Down
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ settings:
'#': The public version number of the library.
version:
major: 0
minor: 11
minor: 12
micro: 0
build: 0
filegroups:
Expand Down
8 changes: 4 additions & 4 deletions doc/grpc-auth-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ authenticate the server and encrypt all data.
```cpp
SslCredentialsOptions ssl_opts; // Options to override SSL params, empty by default
// Create the credentials object by providing service account key in constructor
std::unique_ptr<Credentials> creds = CredentialsFactory::SslCredentials(ssl_opts);
std::shared_ptr<ChannelCredentials> creds = SslCredentials(ssl_opts);
// Create a channel using the credentials created in the previous step
std::shared_ptr<ChannelInterface> channel = CreateChannel(server_name, creds, channel_args);
std::shared_ptr<Channel> channel = CreateChannel(server_name, creds);
// Create a stub on the channel
std::unique_ptr<Greeter::Stub> stub(Greeter::NewStub(channel));
// Make actual RPC calls on the stub.
Expand All @@ -55,9 +55,9 @@ passed to the factory method.
gRPC applications can use a simple API to create a credential that works in various deployment scenarios.
```cpp
std::unique_ptr<Credentials> creds = CredentialsFactory::GoogleDefaultCredentials();
std::shared_ptr<ChannelCredentials> creds = GoogleDefaultCredentials();
// Create a channel, stub and make RPC calls (same as in the previous example)
std::shared_ptr<ChannelInterface> channel = CreateChannel(server_name, creds, channel_args);
std::shared_ptr<Channel> channel = CreateChannel(server_name, creds);
std::unique_ptr<Greeter::Stub> stub(Greeter::NewStub(channel));
grpc::Status s = stub->sayHello(&context, *request, response);
```
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/cpptutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ To call service methods, we first need to create a *stub*.
First we need to create a gRPC *channel* for our stub, specifying the server address and port we want to connect to without SSL:

```cpp
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials());
grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials());
```
Now we can use the channel to create our stub using the `NewStub` method provided in the `RouteGuide` class we generated from our .proto.
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ $ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
arguments as follows

```
auto channel = CreateChannel("localhost:50051", InsecureCredentials());
auto channel = CreateChannel("localhost:50051", InsecureChannelCredentials());
```
- Create a stub. A stub implements the rpc methods of a service and in the
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/helloworld/greeter_async_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ int main(int argc, char** argv) {
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureCredentials()).
GreeterClient greeter(
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials()));
// (use of InsecureChannelCredentials()).
GreeterClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
std::string user("world");
std::string reply = greeter.SayHello(user); // The actual RPC call!
std::cout << "Greeter received: " << reply << std::endl;
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/helloworld/greeter_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ int main(int argc, char** argv) {
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureCredentials()).
GreeterClient greeter(
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials()));
// (use of InsecureChannelCredentials()).
GreeterClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
std::string user("world");
std::string reply = greeter.SayHello(user);
std::cout << "Greeter received: " << reply << std::endl;
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/route_guide/route_guide_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ int main(int argc, char** argv) {
// Expect only arg: --db_path=path/to/route_guide_db.json.
std::string db = routeguide::GetDbFileContent(argc, argv);
RouteGuideClient guide(
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials()),
grpc::CreateChannel("localhost:50051",
grpc::InsecureChannelCredentials()),
db);

std::cout << "-------------- GetFeature --------------" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions include/grpc++/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace grpc {
class CallOpSetInterface;
class ChannelArguments;
class CompletionQueue;
class Credentials;
class SecureCredentials;
class ChannelCredentials;
class SecureChannelCredentials;

template <class R>
class ClientReader;
Expand Down
6 changes: 3 additions & 3 deletions include/grpc++/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace grpc {

class Channel;
class CompletionQueue;
class Credentials;
class CallCredentials;
class RpcMethod;
template <class R>
class ClientReader;
Expand Down Expand Up @@ -245,7 +245,7 @@ class ClientContext {
/// call.
///
/// \see https://github.com/grpc/grpc/blob/master/doc/grpc-auth-support.md
void set_credentials(const std::shared_ptr<Credentials>& creds) {
void set_credentials(const std::shared_ptr<CallCredentials>& creds) {
creds_ = creds;
}

Expand Down Expand Up @@ -321,7 +321,7 @@ class ClientContext {
bool call_canceled_;
gpr_timespec deadline_;
grpc::string authority_;
std::shared_ptr<Credentials> creds_;
std::shared_ptr<CallCredentials> creds_;
mutable std::shared_ptr<const AuthContext> auth_context_;
struct census_context* census_context_;
std::multimap<grpc::string, grpc::string> send_initial_metadata_;
Expand Down
6 changes: 4 additions & 2 deletions include/grpc++/create_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ namespace grpc {
/// an object or is invalid, a lame channel is returned.
/// \param args Options for channel creation.
std::shared_ptr<Channel> CreateChannel(
const grpc::string& target, const std::shared_ptr<Credentials>& creds);
const grpc::string& target,
const std::shared_ptr<ChannelCredentials>& creds);

/// Create a new \em custom \a Channel pointing to \a target
///
Expand All @@ -61,7 +62,8 @@ std::shared_ptr<Channel> CreateChannel(
/// an object or is invalid, a lame channel is returned.
/// \param args Options for channel creation.
std::shared_ptr<Channel> CreateCustomChannel(
const grpc::string& target, const std::shared_ptr<Credentials>& creds,
const grpc::string& target,
const std::shared_ptr<ChannelCredentials>& creds,
const ChannelArguments& args);

} // namespace grpc
Expand Down
88 changes: 59 additions & 29 deletions include/grpc++/security/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,60 @@
namespace grpc {
class ChannelArguments;
class Channel;
class SecureCredentials;

/// A credentials object encapsulates all the state needed by a client to
/// authenticate with a server and make various assertions, e.g., about the
/// client’s identity, role, or whether it is authorized to make a particular
/// call.
class SecureChannelCredentials;
class CallCredentials;
class SecureCallCredentials;

/// A channel credentials object encapsulates all the state needed by a client
/// to authenticate with a server for a given channel.
/// It can make various assertions, e.g., about the client’s identity, role
/// for all the calls on that channel.
///
/// \see https://github.com/grpc/grpc/blob/master/doc/grpc-auth-support.md
class Credentials : public GrpcLibrary {
class ChannelCredentials : public GrpcLibrary {
public:
~Credentials() GRPC_OVERRIDE;

/// Apply this instance's credentials to \a call.
virtual bool ApplyToCall(grpc_call* call) = 0;
~ChannelCredentials() GRPC_OVERRIDE;

protected:
friend std::shared_ptr<Credentials> CompositeCredentials(
const std::shared_ptr<Credentials>& creds1,
const std::shared_ptr<Credentials>& creds2);
friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
const std::shared_ptr<ChannelCredentials>& channel_creds,
const std::shared_ptr<CallCredentials>& call_creds);

virtual SecureCredentials* AsSecureCredentials() = 0;
virtual SecureChannelCredentials* AsSecureCredentials() = 0;

private:
friend std::shared_ptr<Channel> CreateCustomChannel(
const grpc::string& target, const std::shared_ptr<Credentials>& creds,
const grpc::string& target,
const std::shared_ptr<ChannelCredentials>& creds,
const ChannelArguments& args);

virtual std::shared_ptr<Channel> CreateChannel(
const grpc::string& target, const ChannelArguments& args) = 0;
};

/// A call credentials object encapsulates the state needed by a client to
/// authenticate with a server for a given call on a channel.
///
/// \see https://github.com/grpc/grpc/blob/master/doc/grpc-auth-support.md
class CallCredentials : public GrpcLibrary {
public:
~CallCredentials() GRPC_OVERRIDE;

/// Apply this instance's credentials to \a call.
virtual bool ApplyToCall(grpc_call* call) = 0;

protected:
friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
const std::shared_ptr<ChannelCredentials>& channel_creds,
const std::shared_ptr<CallCredentials>& call_creds);

friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
const std::shared_ptr<CallCredentials>& creds1,
const std::shared_ptr<CallCredentials>& creds2);

virtual SecureCallCredentials* AsSecureCredentials() = 0;
};

/// Options used to build SslCredentials.
struct SslCredentialsOptions {
/// The buffer containing the PEM encoding of the server root certificates. If
Expand Down Expand Up @@ -106,10 +129,10 @@ struct SslCredentialsOptions {
/// Using these credentials to connect to any other service may result in this
/// service being able to impersonate your client for requests to Google
/// services.
std::shared_ptr<Credentials> GoogleDefaultCredentials();
std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();

/// Builds SSL Credentials given SSL specific options
std::shared_ptr<Credentials> SslCredentials(
std::shared_ptr<ChannelCredentials> SslCredentials(
const SslCredentialsOptions& options);

/// Builds credentials for use when running in GCE
Expand All @@ -118,14 +141,14 @@ std::shared_ptr<Credentials> SslCredentials(
/// Using these credentials to connect to any other service may result in this
/// service being able to impersonate your client for requests to Google
/// services.
std::shared_ptr<Credentials> GoogleComputeEngineCredentials();
std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();

/// Builds Service Account JWT Access credentials.
/// json_key is the JSON key string containing the client's private key.
/// token_lifetime_seconds is the lifetime in seconds of each Json Web Token
/// (JWT) created with this credentials. It should not exceed
/// grpc_max_auth_token_lifetime or will be cropped to this value.
std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials(
std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
const grpc::string& json_key, long token_lifetime_seconds);

/// Builds refresh token credentials.
Expand All @@ -136,7 +159,7 @@ std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials(
/// Using these credentials to connect to any other service may result in this
/// service being able to impersonate your client for requests to Google
/// services.
std::shared_ptr<Credentials> GoogleRefreshTokenCredentials(
std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
const grpc::string& json_refresh_token);

/// Builds access token credentials.
Expand All @@ -147,7 +170,7 @@ std::shared_ptr<Credentials> GoogleRefreshTokenCredentials(
/// Using these credentials to connect to any other service may result in this
/// service being able to impersonate your client for requests to Google
/// services.
std::shared_ptr<Credentials> AccessTokenCredentials(
std::shared_ptr<CallCredentials> AccessTokenCredentials(
const grpc::string& access_token);

/// Builds IAM credentials.
Expand All @@ -156,17 +179,24 @@ std::shared_ptr<Credentials> AccessTokenCredentials(
/// Using these credentials to connect to any other service may result in this
/// service being able to impersonate your client for requests to Google
/// services.
std::shared_ptr<Credentials> GoogleIAMCredentials(
std::shared_ptr<CallCredentials> GoogleIAMCredentials(
const grpc::string& authorization_token,
const grpc::string& authority_selector);

/// Combines two credentials objects into a composite credentials
std::shared_ptr<Credentials> CompositeCredentials(
const std::shared_ptr<Credentials>& creds1,
const std::shared_ptr<Credentials>& creds2);
/// Combines a channel credentials and a call credentials into a composite
/// channel credentials.
std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
const std::shared_ptr<ChannelCredentials>& channel_creds,
const std::shared_ptr<CallCredentials>& call_creds);


/// Combines two call credentials objects into a composite call credentials.
std::shared_ptr<CallCredentials> CompositeCallCredentials(
const std::shared_ptr<CallCredentials>& creds1,
const std::shared_ptr<CallCredentials>& creds2);

/// Credentials for an unencrypted, unauthenticated channel
std::shared_ptr<Credentials> InsecureCredentials();
std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();

// User defined metadata credentials.
class MetadataCredentialsPlugin {
Expand All @@ -183,7 +213,7 @@ class MetadataCredentialsPlugin {
std::multimap<grpc::string, grpc::string>* metadata) = 0;
};

std::shared_ptr<Credentials> MetadataCredentialsFromPlugin(
std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
std::unique_ptr<MetadataCredentialsPlugin> plugin);

} // namespace grpc
Expand Down
2 changes: 1 addition & 1 deletion include/grpc++/support/channel_arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ChannelArguments {
void SetString(const grpc::string& key, const grpc::string& value);

private:
friend class SecureCredentials;
friend class SecureChannelCredentials;
friend class testing::ChannelArgumentsTest;

// Returns empty string when it is not set.
Expand Down
Loading

0 comments on commit 731c83f

Please sign in to comment.