Not able to access gcp secret manager from standalone go application

Viewed 47

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 Answers

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
}
Related