搜尋
首頁後端開發Golang如何使用上傳(未產生)的服務帳戶金鑰向 GCP 進行身份驗證?

如何使用上传(未生成)的服务帐户密钥向 GCP 进行身份验证?

php小編草莓為您詳細介紹如何使用上傳的服務帳戶金鑰進行Google Cloud Platform(GCP)的身份驗證。在使用GCP的過程中,身分驗證是非常重要的一步,而服務帳戶金鑰則是進行身分驗證的關鍵。本文將向您解釋如何上傳服務帳戶金鑰並正確地進行身份驗證,以便您能夠順利使用GCP的各項功能和服務。無論您是初次接觸GCP還是已經有一定經驗,本文都將為您提供有用的指導和技巧,確保您能夠輕鬆地完成身份驗證的過程。閱讀本文,您將了解上傳服務帳戶金鑰的步驟和注意事項,以及如何在GCP中正確使用這些金鑰進行驗證。

問題內容

我正在撰寫一項服務,需要不同租用戶存取 GCP 服務。 我的微服務將在不同的主機上運行(無 GCP)。 我面臨的限制之一是使用服務帳戶金鑰授權 api 呼叫。 第二個約束是我的微服務負責為服務帳戶產生金鑰對並與租用戶共用公鑰,以便他們可以將其上傳到他們的服務帳戶。

我目前的問題是,我找不到任何文件來解釋如何使用我擁有的金鑰對對 GCP golang 庫進行身份驗證。我讀過的所有文件始終解決服務帳戶金鑰驗證問題,假設您將讓 GCP 為您產生金鑰對,並且您已經擁有包含所需所有詳細資訊的 json 檔案。

我審閱過的文件:

- https://cloud.google.com/docs/authentication/client-libraries#adc
- https://cloud.google.com/docs/authentication#service-accounts
- https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key
- https://github.com/GoogleCloudPlatform/golang-samples/blob/main/auth/id_token_from_service_account.go

Google 圖書館使用名為「應用程式預設憑證」的機制,可讓其所有圖書館找到用於身分驗證的憑證。具體來說,在這種情況下,ADC 始終需要 json 檔案才能取得憑證的詳細資訊。

json 檔案如下所示:

<code>{
    "type": "service_account",
    "project_id": "my-project",
    "private_key_id": "1ed3aae07f98f3e6da78ad308b41232133d006cf",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...==\n-----END PRIVATE KEY-----\n",
    "client_email": "<EMAIL HERE>",
    "client_id": "1234567890102",
    "auth_uri": "<GOOGLE AUTH URI HERE>",
    "token_uri": "<GOOGLE TOKEN URI HERE>",
    "auth_provider_x509_cert_url": "<SOME GOOGLE URL HERE>",
    "client_x509_cert_url": "<MORE GOOGLE URL HERE>",
    "universe_domain": "googleapis.com"
}
</code>

所以我的問題是,如何從生成的金鑰對建立這個 json 檔案? 如果這是不可能的(GCP 限制),那麼為什麼我能夠上傳金鑰對? 有沒有人可以指出我的例子來幫助我解決這種情況?

非常感謝任何幫助

我已經查看了所有 GCP 官方文件、API 參考和 Github 腳本範例。它們都沒有引用上傳的服務帳戶金鑰

解決方法

花了一些時間才回到這個問題。感謝 @guillaume-blaquiere 的回答,我開始使用最少的所需文件進行測試。

最後想出了這個:

{
  "type": "service_account",
  "private_key": "<Your generated private_key.pem>",
  "client_email": "<service account email>",
}

這些是 Google SDK 成功通過 GCP 驗證所需的最低屬性

私鑰必須是用於產生上傳到 GCP 服務帳號的公鑰的私鑰。

就golang程式碼而言,可以將這些資訊放入一個結構體中,然後將該結構體解析為json:

type JSONFile struct {
  Type     string `json:"type"`
  PrivKey  []byte `json:"private_key"`
  Email    string `json:"client_email"`
}

然後:

file := JSONFile{
    Type:        "service_account",
    PrivKey:  "<PK bytes here>",
    Email: "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="7d0e180f0b141e18501c1e1e120813095018101c14113d1a1e0d531e1210">[email&#160;protected]</a>",
}
result, err := json.Marshal(file)
if err != nil {
    panic(1)
}
credentials, err := google.CredentialsFromJSON(context.Background(), result, secretmanager.DefaultAuthScopes()...)
if err != nil {
    panic(1)
}
projectsClient, err := resourcemanager.NewProjectsClient(context.Background(), option.WithCredentials(credentials))

以上是如何使用上傳(未產生)的服務帳戶金鑰向 GCP 進行身份驗證?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除
去其他語言:比較分析去其他語言:比較分析Apr 28, 2025 am 12:17 AM

goisastrongchoiceforprojectsneedingsimplicity,績效和引發性,butitmaylackinadvancedfeatures and ecosystemmaturity.1)

比較以其他語言的靜態初始化器中的初始化功能比較以其他語言的靜態初始化器中的初始化功能Apr 28, 2025 am 12:16 AM

Go'sinitfunctionandJava'sstaticinitializersbothservetosetupenvironmentsbeforethemainfunction,buttheydifferinexecutionandcontrol.Go'sinitissimpleandautomatic,suitableforbasicsetupsbutcanleadtocomplexityifoverused.Java'sstaticinitializersoffermorecontr

GO中初始功能的常見用例GO中初始功能的常見用例Apr 28, 2025 am 12:13 AM

thecommonusecasesfortheinitfunctionoare:1)加載configurationfilesbeforeThemainProgramStarts,2)初始化的globalvariables和3)runningpre-checkSorvalidationsbeforEtheprofforeTheProgrecce.TheInitFunctionIsautefunctionIsautomentycalomationalmatomatimationalycalmatemationalcalledbebeforethemainfuniinfuninfuntuntion

GO中的頻道:掌握際際交流GO中的頻道:掌握際際交流Apr 28, 2025 am 12:04 AM

ChannelsarecrucialingoforenablingsafeandefficityCommunicationBetnewengoroutines.theyfacilitateSynChronizationAndManageGoroutIneLifeCycle,EssentialforConcurrentProgramming.ChannelSallSallSallSallSallowSallowsAllowsEnderDendingAndReceivingValues,ActassignalsignalsforsynChronization,and actassignalsynChronization and andsupppor

包裝錯誤:將上下文添加到錯誤鏈中包裝錯誤:將上下文添加到錯誤鏈中Apr 28, 2025 am 12:02 AM

在Go中,可以通過errors.Wrap和errors.Unwrap方法來包裝錯誤並添加上下文。 1)使用errors包的新功能,可以在錯誤傳播過程中添加上下文信息。 2)通過fmt.Errorf和%w包裝錯誤,幫助定位問題。 3)自定義錯誤類型可以創建更具語義化的錯誤,增強錯誤處理的表達能力。

使用GO開發時的安全考慮使用GO開發時的安全考慮Apr 27, 2025 am 12:18 AM

Gooffersrobustfeaturesforsecurecoding,butdevelopersmustimplementsecuritybestpracticeseffectively.1)UseGo'scryptopackageforsecuredatahandling.2)Manageconcurrencywithsynchronizationprimitivestopreventraceconditions.3)SanitizeexternalinputstoavoidSQLinj

了解GO的錯誤接口了解GO的錯誤接口Apr 27, 2025 am 12:16 AM

Go的錯誤接口定義為typeerrorinterface{Error()string},允許任何實現Error()方法的類型被視為錯誤。使用步驟如下:1.基本檢查和記錄錯誤,例如iferr!=nil{log.Printf("Anerroroccurred:%v",err)return}。 2.創建自定義錯誤類型以提供更多信息,如typeMyErrorstruct{MsgstringDetailstring}。 3.使用錯誤包裝(自Go1.13起)來添加上下文而不丟失原始錯誤信息,

並發程序中的錯誤處理並發程序中的錯誤處理Apr 27, 2025 am 12:13 AM

對效率的Handleerrorsinconcurrentgopragrs,UsechannelstocommunicateErrors,enplionErrorWatchers,Instertimeout,UsebufferedChannels和Provideclearrormessages.1)USEchannelelStopassErtopassErrorsErtopassErrorsErrorsErrorsFromGoroutInestOthemainFunction.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具