{"id":3139,"date":"2023-09-05T13:52:03","date_gmt":"2023-09-05T13:52:03","guid":{"rendered":"https:\/\/blog.designed79.co.uk\/?p=3139"},"modified":"2023-09-05T13:55:39","modified_gmt":"2023-09-05T13:55:39","slug":"google-cloud-setting-up-gcloud-with-service-account-credentials","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=3139","title":{"rendered":"Google Cloud \u2013 Setting up Gcloud with Service Account Credentials"},"content":{"rendered":"<p>In this article, we will download and install the Google gcloud CLI. Then we will set up gcloud with Google Service Account credentials. This article is for Windows-based systems but the same principles apply to Linux and Mac systems.<\/p>\n<h5>Step 1 \u2013 Download gcloud<\/h5>\n<p><a href=\"https:\/\/dl.google.com\/dl\/cloudsdk\/channels\/rapid\/GoogleCloudSDKInstaller.exe\">Google Cloud SDK Installer<\/a><\/p>\n<h5>Step 2 \u2013 Launch the installer<\/h5>\n<p>At the<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Completing the Google Cloud SDK Setup Wizard<\/div><\/div>\n<p>, deselect<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Run gcloud init<\/div><\/div>\n<p>to configure the Cloud SDK. The reason is that we only want to use Service Account credentials.<\/p>\n<h5>Step 3 \u2013 Access a Google public bucket<\/h5>\n<div id=\"urvanov-syntax-highlighter-64e6a82868146103538767\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-toolbar\" data-settings=\"\">\n<div class=\"crayon-tools\">\n<div class=\"crayon-button urvanov-syntax-highlighter-popup-button\" title=\"Open Code In New Window\">\n<div class=\"urvanov-syntax-highlighter-button-icon\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"urvanov-syntax-highlighter-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"urvanov-syntax-highlighter-nums-content\">\n<div class=\"crayon-num\" data-line=\"urvanov-syntax-highlighter-64e6a82868146103538767-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\">\n<pre id=\"urvanov-syntax-highlighter-64e6a82868146103538767-1\" class=\"crayon-line\"><span class=\"crayon-e\">gsutil <\/span><span class=\"crayon-e\">ls <\/span><span class=\"crayon-v\">gs<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-c\">\/\/gcp-public-data-landsat<\/span><\/pre>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This command should succeed and provide a listing of the files in this bucket. This command verifies that the CLI is installed. We have not set up credentials yet.<\/p>\n<h5>Step 4 \u2013 Access one of your own private buckets<\/h5>\n<p>This step will verify that you have no credentials. Change the bucket name to a private bucket that you own.<\/p>\n<div id=\"urvanov-syntax-highlighter-64e6a8286814f039156236\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"urvanov-syntax-highlighter-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"urvanov-syntax-highlighter-nums-content\">\n<div class=\"crayon-num\" data-line=\"urvanov-syntax-highlighter-64e6a8286814f039156236-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\">\n<pre id=\"urvanov-syntax-highlighter-64e6a8286814f039156236-1\" class=\"crayon-line\"><span class=\"crayon-e\">gsutil <\/span><span class=\"crayon-e\">ls <\/span><span class=\"crayon-v\">gs<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-c\">\/\/mybucket<\/span><\/pre>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This command should fail. If it succeeds you have a public bucket that anyone can access.<\/p>\n<h5>Step 5 \u2013 Create Google Service Account credentials.<\/h5>\n<p>You can skip this step if you already have credentials to use.<\/p>\n<p>In this example, we will only grant<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Storage Admin<\/div><\/div>\n<p>to these credentials.<\/p>\n<ol>\n<li>Go to\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">IAM &amp;amp; admin<\/div><\/div>\n<p>\u00a0-&gt;\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Service accounts<\/div><\/div>\n<\/li>\n<li>Click\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">CREATE SERVICE ACCOUNT<\/div><\/div>\n<\/li>\n<li>Enter a\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Service account name<\/div><\/div>\n<p>\u00a0and\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Service account description<\/div><\/div>\n<\/li>\n<li>Click\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">CREATE<\/div><\/div>\n<\/li>\n<li>In the next screen\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Service account permissions<\/div><\/div>\n<p>, select a role.<\/li>\n<li>Select\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Storage<\/div><\/div>\n<p>\u00a0-&gt;\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Storage Admin<\/div><\/div>\n<\/li>\n<li>Click\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">CONTINUE<\/div><\/div>\n<\/li>\n<li>Click\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Create key<\/div><\/div>\n<\/li>\n<li>Check the\u00a0\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">JSON<\/div><\/div>\n<p>\u00a0radio button for the\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">Key type<\/div><\/div>\n<\/li>\n<li>Save the json file to your local computer.<\/li>\n<\/ol>\n<p>Make note of the email address that Google Cloud created for these credentials.<\/p>\n<h5>Step 6- Configure gcloud with the Google Service Account credentials<\/h5>\n<p>In this example, the email address is:\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">test@development-123456.iam.gserviceaccount.com<\/div><\/div>\n<p>The credentials file is:\u00a0<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">test_google_account.json<\/div><\/div>\n<p>Modify these items to what you created in step 5.<\/p>\n<div id=\"urvanov-syntax-highlighter-64e6a82868151769657621\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate urvanov-syntax-highlighter-wrapped\" data-settings=\" touchscreen minimize scroll-mouseover wrap\">\n<div class=\"urvanov-syntax-highlighter-plain-wrap\"><\/div>\n<div class=\"urvanov-syntax-highlighter-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"urvanov-syntax-highlighter-nums-content\">\n<div class=\"crayon-num\" data-line=\"urvanov-syntax-highlighter-64e6a82868151769657621-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\">\n<pre id=\"urvanov-syntax-highlighter-64e6a82868151769657621-1\" class=\"crayon-line\"><span class=\"crayon-e\">gcloud <\/span><span class=\"crayon-e\">auth <\/span><span class=\"crayon-v\">activate<\/span><span class=\"crayon-o\">-<\/span><span class=\"crayon-v\">service<\/span><span class=\"crayon-o\">-<\/span><span class=\"crayon-e\">account <\/span><span class=\"crayon-v\">test<\/span><span class=\"crayon-sy\">@<\/span><span class=\"crayon-v\">development<\/span><span class=\"crayon-o\">-<\/span><span class=\"crayon-cn\">123456.iam.gserviceaccount.com<\/span> <span class=\"crayon-o\">--<\/span><span class=\"crayon-v\">key<\/span><span class=\"crayon-o\">-<\/span><span class=\"crayon-v\">file<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-v\">test_google_account<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">json<\/span><\/pre>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h5><\/h5>\n<h5>Step 7 \u2013 Verify that the credentials work<\/h5>\n<p>Change the bucket name to a private bucket that you own.<\/p>\n<div id=\"urvanov-syntax-highlighter-64e6a82868153887672902\" class=\"urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"urvanov-syntax-highlighter-plain-wrap\"><\/div>\n<div class=\"urvanov-syntax-highlighter-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"urvanov-syntax-highlighter-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"urvanov-syntax-highlighter-nums-content\">\n<div class=\"crayon-num\" data-line=\"urvanov-syntax-highlighter-64e6a82868153887672902-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"urvanov-syntax-highlighter-code\">\n<div class=\"crayon-pre\">\n<pre id=\"urvanov-syntax-highlighter-64e6a82868153887672902-1\" class=\"crayon-line\"><span class=\"crayon-e\">gsutil <\/span><span class=\"crayon-e\">ls <\/span><span class=\"crayon-v\">gs<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-c\">\/\/mybucket<\/span><\/pre>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This command should now succeed.<\/p>\n<p>You have now successfully configured gcloud to work with Google Service Account credentials.<\/p>\n<p>There are two commands that generate access tokens:<\/p>\n<ul>\n<li>gcloud auth print-access-token<\/li>\n<li>gcloud auth application-default print-access-token<\/li>\n<\/ul>\n<h5>Important Detail<\/h5>\n<p>Configuring<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">gcloud<\/div><\/div>\n<p>with a service account sets up the tool\u2019s credentials and does not create credentials for Application Default Credentials (ADC).<\/p>\n<h5>More information<\/h5>\n<ul>\n<li><a href=\"https:\/\/cloud.google.com\/sdk\/gcloud\/reference\/auth\/print-access-token\" target=\"_blank\" rel=\"noopener\">gcloud auth print-access-token<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/sdk\/gcloud\/reference\/auth\/application-default\/print-access-token\" target=\"_blank\" rel=\"noopener\">gcloud auth application-default print-access-token<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/sdk\/gcloud\/reference\/auth\/activate-service-account\" target=\"_blank\" rel=\"noopener\">gcloud auth activate-service-account<\/a><\/li>\n<\/ul>\n<p>In future articles, I will show you how to use these same credentials when programming, for example, in C++, Python, etc. Then we will cover in detail what Google Service Account credentials are and how to programmatically generate Access Tokens from these credentials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will download and install the Google gcloud CLI. Then we will set up gcloud with Google Service Account [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3139","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3139"}],"version-history":[{"count":5,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3139\/revisions"}],"predecessor-version":[{"id":3144,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3139\/revisions\/3144"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}