无法从独立的Go应用程序访问GCP密钥管理器。

huangapple go评论138阅读模式
英文:

Not able to access gcp secret manager from standalone go application

问题

我正在尝试从Google Cloud Function访问GCP Secret Manager。以下是代码片段:

// Package p contains an HTTP Cloud Function.
package p

import (
	"context"
	"fmt"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)

func main() {

	ctx := context.Background()
	client, err := secretmanager.NewClient(ctx)
	if err != nil {
		fmt.Errorf("failed to create secretmanager client: %v", err)
	}
	defer client.Close()
	name := "projects/485126440943/secrets/APIAdminServiceAccountLookupKey"
	// Build the request.
	req := &secretmanagerpb.GetSecretRequest{
		Name: name,
	}

	// Call the API.
	result, err := client.GetSecret(ctx, req)
	if err != nil {
		fmt.Errorf("failed to get secret: %v", err)
	}

	replication := result.Replication.Replication
	fmt.Printf("Found secret %s with replication policy %s\n", result.Name, replication)
	//return nil

}

但是我得到的secretManagerClientnil。这就是为什么在运行代码后我会得到以下错误:

panic: runtime error: invalid memory address or nil pointer dereference
panic: runtime error: invalid memory address or nil pointer dereference

如何解决这个问题?提前感谢!

英文:

I am trying to access GCP secret manager from google cloud function. Below is the code snippet

// Package p contains an HTTP Cloud Function.
 package p

 import (
    "context"
	"fmt"
	
	

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
        secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)
   
 func main() {
	
	ctx := context.Background()
        client, err := secretmanager.NewClient(ctx)
        if err != nil {
                fmt.Errorf("failed to create secretmanager client: %v", err)
        }
        defer client.Close()
		name := "projects/485126440943/secrets/APIAdminServiceAccountLookupKey"
        // Build the request.
        req := &secretmanagerpb.GetSecretRequest{
                Name: name,
        }

        // Call the API.
        result, err := client.GetSecret(ctx, req)
        if err != nil {
                fmt.Errorf("failed to get secret: %v", err)
        }

        replication := result.Replication.Replication
        fmt.Printf("Found secret %s with replication policy %s\n", result.Name, replication)
        //return nil

	}





But i am getting secretManagerClient as Nil ]. Thats why after running code I am getting below error:

panic: runtime error: invalid memory address or nil pointer dereference
panic: runtime error: invalid memory address or nil pointer dereference

How to solve this issue?
Thanks in advance!!

答案1

得分: 2

从你的代码中可以看出,当出现错误时,你忘记停止程序了。这会导致你访问了*secretmanager.Client中的空指针方法client.GetSecret(ctx, req),从而引发空指针恐慌。要解决这个问题,你只需要添加panicreturn语句即可。实施后,你将不再遇到空指针恐慌,并且你将从secretmanager.NewClient(ctx)中得到一个错误。

package main

import (
	"context"
	"fmt"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)

func main() {
	ctx := context.Background()
	client, err := secretmanager.NewClient(ctx)
	if err != nil {
		// 停止程序执行下一行
		panic(fmt.Errorf("failed to create secretmanager client: %v", err))

		// 你也可以使用 return 语句
		//
		// log.Println(fmt.Errorf("failed to create secretmanager client: %v", err))
		// return
	}
	defer client.Close()

	name := "projects/485126440943/secrets/APIAdminServiceAccountLookupKey"
	// 构建请求。
	req := &secretmanagerpb.GetSecretRequest{
		Name: name,
	}

	// 调用 API。
	result, err := client.GetSecret(ctx, req)
	if err != nil {
		panic(fmt.Errorf("failed to get secret: %v", err))
	}

	replication := result.Replication.Replication
	fmt.Printf("Found secret %s with replication policy %s\n", result.Name, replication)
	//return nil
}
英文:

From your codes, we can see that you forgot to stop the program when getting an error. It will lead you to the nil pointer panic because you access the method of nil *secretmanager.Client in client.GetSecret(ctx, req). To solve this, you just need to add panic or return statement. After implementing it, you will no longer get a nil pointer panic and you will face an error from secretmanager.NewClient(ctx).

package main

import (
	"context"
	"fmt"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)

func main() {
	ctx := context.Background()
	client, err := secretmanager.NewClient(ctx)
	if err != nil {
		// stop your program from executing the next lines
		panic(fmt.Errorf("failed to create secretmanager client: %v", err))

		// you can also use return statement
		//
		// log.Println(fmt.Errorf("failed to create secretmanager client: %v", err))
		// return
	}
	defer client.Close()

	name := "projects/485126440943/secrets/APIAdminServiceAccountLookupKey"
	// Build the request.
	req := &secretmanagerpb.GetSecretRequest{
		Name: name,
	}

	// Call the API.
	result, err := client.GetSecret(ctx, req)
	if err != nil {
		panic(fmt.Errorf("failed to get secret: %v", err))
	}

	replication := result.Replication.Replication
	fmt.Printf("Found secret %s with replication policy %s\n", result.Name, replication)
	//return nil
}

huangapple
  • 本文由 发表于 2022年9月16日 19:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/73744504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定