Skip to content

Commit 4aca037

Browse files
authored
cleanup: GUAC in examples (#13032)
1 parent 1cfed98 commit 4aca037

File tree

3 files changed

+45
-107
lines changed

3 files changed

+45
-107
lines changed

examples/grpc_credential_types.cc

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "google/cloud/iam/iam_credentials_client.h"
15+
#include "google/cloud/iam/credentials/v1/iam_credentials_client.h"
1616
#include "google/cloud/spanner/admin/instance_admin_client.h"
1717
#include "google/cloud/common_options.h"
18+
#include "google/cloud/credentials.h"
1819
#include "google/cloud/grpc_options.h"
1920
#include "google/cloud/internal/getenv.h"
2021
#include "google/cloud/log.h"
@@ -80,18 +81,10 @@ google::cloud::StatusOr<std::string> HttpGet(std::string const& url,
8081
return buffer;
8182
}
8283

83-
// TODO(#6185) - this should be done by the generated code
84-
std::set<std::string> DefaultTracingComponents() {
85-
return absl::StrSplit(
86-
google::cloud::internal::GetEnv("GOOGLE_CLOUD_CPP_ENABLE_TRACING")
87-
.value_or(""),
88-
',');
89-
}
90-
9184
google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken(
92-
google::cloud::iam::IAMCredentialsClient client,
85+
google::cloud::iam_credentials_v1::IAMCredentialsClient client,
9386
std::vector<std::string> const& argv) {
94-
namespace iam = ::google::cloud::iam;
87+
namespace iam = ::google::cloud::iam_credentials_v1;
9588
return [](iam::IAMCredentialsClient client,
9689
std::string const& service_account, std::string const& project_id) {
9790
google::protobuf::Duration duration;
@@ -109,14 +102,13 @@ google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken(
109102
<< ", which will expire around " << absl::FromChrono(expiration)
110103
<< std::endl;
111104

112-
auto credentials = grpc::CompositeChannelCredentials(
113-
grpc::SslCredentials({}),
114-
grpc::AccessTokenCredentials(token->access_token()));
105+
auto credentials = google::cloud::MakeAccessTokenCredentials(
106+
token->access_token(), expiration);
115107

116108
google::cloud::spanner_admin::InstanceAdminClient admin(
117109
google::cloud::spanner_admin::MakeInstanceAdminConnection(
118-
google::cloud::Options{}.set<google::cloud::GrpcCredentialOption>(
119-
credentials)));
110+
google::cloud::Options{}
111+
.set<google::cloud::UnifiedCredentialsOption>(credentials)));
120112
for (auto config : admin.ListInstanceConfigs(
121113
google::cloud::Project(project_id).FullName())) {
122114
if (!config) throw std::move(config).status();
@@ -127,8 +119,9 @@ google::iam::credentials::v1::GenerateAccessTokenResponse UseAccessToken(
127119
}(std::move(client), argv.at(0), argv.at(1));
128120
}
129121

130-
void UseAccessTokenUntilExpired(google::cloud::iam::IAMCredentialsClient client,
131-
std::vector<std::string> const& argv) {
122+
void UseAccessTokenUntilExpired(
123+
google::cloud::iam_credentials_v1::IAMCredentialsClient client,
124+
std::vector<std::string> const& argv) {
132125
auto token = UseAccessToken(std::move(client), argv);
133126
auto const& project_id = argv.at(1);
134127
auto const expiration =
@@ -139,13 +132,12 @@ void UseAccessTokenUntilExpired(google::cloud::iam::IAMCredentialsClient client,
139132
<< absl::FromChrono(expiration) << ")" << std::endl;
140133

141134
auto iteration = [=](bool expired) {
142-
auto credentials = grpc::CompositeChannelCredentials(
143-
grpc::SslCredentials({}),
144-
grpc::AccessTokenCredentials(token.access_token()));
135+
auto credentials = google::cloud::MakeAccessTokenCredentials(
136+
token.access_token(), expiration);
145137
google::cloud::spanner_admin::InstanceAdminClient admin(
146138
google::cloud::spanner_admin::MakeInstanceAdminConnection(
147-
google::cloud::Options{}.set<google::cloud::GrpcCredentialOption>(
148-
credentials)));
139+
google::cloud::Options{}
140+
.set<google::cloud::UnifiedCredentialsOption>(credentials)));
149141
for (auto config : admin.ListInstanceConfigs(
150142
google::cloud::Project(project_id).FullName())) {
151143
// kUnauthenticated receives special treatment, it is the error received
@@ -178,9 +170,10 @@ void UseAccessTokenUntilExpired(google::cloud::iam::IAMCredentialsClient client,
178170
}
179171
}
180172

181-
void UseIdTokenHttp(google::cloud::iam::IAMCredentialsClient client,
182-
std::vector<std::string> const& argv) {
183-
namespace iam = ::google::cloud::iam;
173+
void UseIdTokenHttp(
174+
google::cloud::iam_credentials_v1::IAMCredentialsClient client,
175+
std::vector<std::string> const& argv) {
176+
namespace iam = ::google::cloud::iam_credentials_v1;
184177
[](iam::IAMCredentialsClient client, std::string const& service_account,
185178
std::string const& hello_world_url) {
186179
auto token = client.GenerateIdToken(
@@ -203,9 +196,10 @@ void UseIdTokenHttp(google::cloud::iam::IAMCredentialsClient client,
203196
}(std::move(client), argv.at(0), argv.at(1));
204197
}
205198

206-
void UseIdTokenGrpc(google::cloud::iam::IAMCredentialsClient client,
207-
std::vector<std::string> const& argv) {
208-
namespace iam = ::google::cloud::iam;
199+
void UseIdTokenGrpc(
200+
google::cloud::iam_credentials_v1::IAMCredentialsClient client,
201+
std::vector<std::string> const& argv) {
202+
namespace iam = ::google::cloud::iam_credentials_v1;
209203
[](iam::IAMCredentialsClient client, std::string const& service_account,
210204
std::string const& url) {
211205
auto token = client.GenerateIdToken(
@@ -245,6 +239,7 @@ void UseIdTokenGrpc(google::cloud::iam::IAMCredentialsClient client,
245239

246240
void AutoRun(std::vector<std::string> const& argv) {
247241
namespace examples = ::google::cloud::testing_util;
242+
namespace iam = ::google::cloud::iam_credentials_v1;
248243
using ::google::cloud::internal::GetEnv;
249244

250245
if (!argv.empty()) throw examples::Usage{"auto"};
@@ -265,17 +260,13 @@ void AutoRun(std::vector<std::string> const& argv) {
265260
auto const hello_world_grpc_url =
266261
GetEnv("GOOGLE_CLOUD_CPP_TEST_HELLO_WORLD_GRPC_URL").value_or("");
267262

268-
auto client = google::cloud::iam::IAMCredentialsClient(
269-
google::cloud::iam::MakeIAMCredentialsConnection(
270-
google::cloud::Options{}
271-
.set<google::cloud::TracingComponentsOption>(
272-
DefaultTracingComponents())
273-
.set<google::cloud::GrpcTracingOptionsOption>(
274-
// There are some credentials returned by RPCs. On an error
275-
// these are printed. This truncates them, making the output
276-
// safe, and yet useful for debugging.
277-
google::cloud::TracingOptions{}.SetOptions(
278-
"truncate_string_field_longer_than=32"))));
263+
auto client = iam::IAMCredentialsClient(iam::MakeIAMCredentialsConnection(
264+
google::cloud::Options{}.set<google::cloud::GrpcTracingOptionsOption>(
265+
// There are some credentials returned by RPCs. On an error
266+
// these are printed. This truncates them, making the output
267+
// safe, and yet useful for debugging.
268+
google::cloud::TracingOptions{}.SetOptions(
269+
"truncate_string_field_longer_than=32"))));
279270

280271
std::cout << "\nRunning UseAccessToken() example" << std::endl;
281272
UseAccessToken(client, {test_iam_service_account, project_id});
@@ -294,9 +285,10 @@ void AutoRun(std::vector<std::string> const& argv) {
294285

295286
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
296287
using ::google::cloud::testing_util::Example;
288+
namespace iam = ::google::cloud::iam_credentials_v1;
297289

298-
using ClientCommand = std::function<void(
299-
google::cloud::iam::IAMCredentialsClient, std::vector<std::string> argv)>;
290+
using ClientCommand = std::function<void(iam::IAMCredentialsClient,
291+
std::vector<std::string> argv)>;
300292

301293
auto make_entry = [](std::string name,
302294
std::vector<std::string> const& arg_names,
@@ -308,11 +300,8 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
308300
for (auto const& a : arg_names) usage += " <" + a + ">";
309301
throw google::cloud::testing_util::Usage{std::move(usage)};
310302
}
311-
auto client = google::cloud::iam::IAMCredentialsClient(
312-
google::cloud::iam::MakeIAMCredentialsConnection(
313-
google::cloud::Options{}
314-
.set<google::cloud::TracingComponentsOption>(
315-
DefaultTracingComponents())));
303+
auto client =
304+
iam::IAMCredentialsClient(iam::MakeIAMCredentialsConnection());
316305
command(client, std::move(argv));
317306
};
318307
return google::cloud::testing_util::Commands::value_type(std::move(name),

google/cloud/bigtable/doc/bigtable-samples-grpc-credentials.dox

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ The following code shows how to use a JWT access token to connect to an Admin AP
6969

7070
The following code shows how to use a GCE credentials to connect to an Admin API endpoint.
7171

72-
@snippet bigtable_grpc_credentials.cc test gce credentials
73-
74-
One may face "Request had insufficient authentication scopes." error while running above example. This might be due to disabled "Cloud API access scope" for Bigtable. This error can be removed by providing sufficient access as explained [here][api-access-scope].
75-
7672
## Use of IAM Credentials
7773

7874
### Check IAM Policy
@@ -90,6 +86,5 @@ One may face "Request had insufficient authentication scopes." error while runni
9086
[info-google-authentication]: https://cloud.google.com/docs/authentication/getting-started
9187
[info-root-certificates]: https://github.com/googleapis/google-cloud-cpp/tree/main/google/cloud/bigtable/examples#configure-grpc-root-certificates
9288
[print-access-token]: https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/print-access-token
93-
[api-access-scope]: https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes
9489

9590
*/

google/cloud/bigtable/examples/bigtable_grpc_credentials.cc

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
#include "google/cloud/bigtable/admin/bigtable_table_admin_client.h"
1616
#include "google/cloud/bigtable/examples/bigtable_examples_common.h"
1717
#include "google/cloud/bigtable/resource_names.h"
18+
#include "google/cloud/credentials.h"
1819
#include "google/cloud/internal/getenv.h"
1920
#include "google/cloud/log.h"
21+
#include <chrono>
2022
#include <fstream>
2123
#include <sstream>
2224

@@ -30,21 +32,16 @@ void AccessToken(std::vector<std::string> const& argv) {
3032
}
3133

3234
// Create a namespace alias to make the code easier to read.
35+
namespace gc = ::google::cloud;
3336
namespace cbt = ::google::cloud::bigtable;
3437
namespace cbta = ::google::cloud::bigtable_admin;
35-
using ::google::cloud::GrpcCredentialOption;
36-
using ::google::cloud::Options;
37-
using ::google::cloud::StatusOr;
3838

3939
//! [test access token]
4040
[](std::string const& project_id, std::string const& instance_id,
4141
std::string const& access_token) {
42-
auto call_credentials = grpc::AccessTokenCredentials(access_token);
43-
auto channel_credentials =
44-
grpc::SslCredentials(grpc::SslCredentialsOptions());
45-
auto credentials = grpc::CompositeChannelCredentials(channel_credentials,
46-
call_credentials);
47-
auto options = Options{}.set<GrpcCredentialOption>(credentials);
42+
auto creds = gc::MakeAccessTokenCredentials(
43+
access_token, std::chrono::system_clock::now() + std::chrono::hours(1));
44+
auto options = gc::Options{}.set<gc::UnifiedCredentialsOption>(creds);
4845

4946
cbta::BigtableTableAdminClient admin(
5047
cbta::MakeBigtableTableAdminConnection(options));
@@ -68,11 +65,9 @@ void JWTAccessToken(std::vector<std::string> const& argv) {
6865
"<service_account_file_json>"};
6966
}
7067
// Create a namespace alias to make the code easier to read.
68+
namespace gc = ::google::cloud;
7169
namespace cbt = ::google::cloud::bigtable;
7270
namespace cbta = ::google::cloud::bigtable_admin;
73-
using ::google::cloud::GrpcCredentialOption;
74-
using ::google::cloud::Options;
75-
using ::google::cloud::StatusOr;
7671

7772
//! [test jwt access token]
7873
[](std::string const& project_id, std::string const& instance_id,
@@ -86,13 +81,8 @@ void JWTAccessToken(std::vector<std::string> const& argv) {
8681
}
8782
std::string json_key(std::istreambuf_iterator<char>{stream}, {});
8883

89-
auto call_credentials =
90-
grpc::ServiceAccountJWTAccessCredentials(json_key, 6000);
91-
auto channel_credentials =
92-
grpc::SslCredentials(grpc::SslCredentialsOptions());
93-
auto credentials = grpc::CompositeChannelCredentials(channel_credentials,
94-
call_credentials);
95-
auto options = Options{}.set<GrpcCredentialOption>(credentials);
84+
auto creds = gc::MakeServiceAccountCredentials(std::move(json_key));
85+
auto options = gc::Options{}.set<gc::UnifiedCredentialsOption>(creds);
9686

9787
cbta::BigtableTableAdminClient admin(
9888
cbta::MakeBigtableTableAdminConnection(options));
@@ -109,41 +99,6 @@ void JWTAccessToken(std::vector<std::string> const& argv) {
10999
(argv.at(0), argv.at(1), argv.at(2));
110100
}
111101

112-
void GCECredentials(std::vector<std::string> const& argv) {
113-
if (argv.size() != 2) {
114-
throw Usage{"test-gce-credentials: <project-id> <instance-id>"};
115-
}
116-
// Create a namespace alias to make the code easier to read.
117-
namespace cbt = ::google::cloud::bigtable;
118-
namespace cbta = ::google::cloud::bigtable_admin;
119-
using ::google::cloud::GrpcCredentialOption;
120-
using ::google::cloud::Options;
121-
using ::google::cloud::StatusOr;
122-
123-
//! [test gce credentials]
124-
[](std::string const& project_id, std::string const& instance_id) {
125-
auto call_credentials = grpc::GoogleComputeEngineCredentials();
126-
auto channel_credentials =
127-
grpc::SslCredentials(grpc::SslCredentialsOptions());
128-
auto credentials = grpc::CompositeChannelCredentials(channel_credentials,
129-
call_credentials);
130-
auto options = Options{}.set<GrpcCredentialOption>(credentials);
131-
132-
cbta::BigtableTableAdminClient admin(
133-
cbta::MakeBigtableTableAdminConnection(options));
134-
135-
google::bigtable::admin::v2::ListTablesRequest r;
136-
r.set_parent(cbt::InstanceName(project_id, instance_id));
137-
r.set_view(google::bigtable::admin::v2::Table::NAME_ONLY);
138-
auto tables = admin.ListTables(std::move(r));
139-
for (auto& table : tables) {
140-
if (!table) throw std::move(table).status();
141-
}
142-
}
143-
//! [test gce credentials]
144-
(argv.at(0), argv.at(1));
145-
}
146-
147102
void RunAll(std::vector<std::string> const& argv) {
148103
namespace examples = ::google::cloud::bigtable::examples;
149104

@@ -178,7 +133,6 @@ int main(int argc, char* argv[]) try {
178133
google::cloud::bigtable::examples::Commands commands = {
179134
{"test-access-token", AccessToken},
180135
{"test-jwt-access-token", JWTAccessToken},
181-
{"test-gce-credentials", GCECredentials},
182136
{"auto", RunAll},
183137
};
184138

0 commit comments

Comments
 (0)