Not able to access gcp secret manager from standalone go application - go

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!!

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

How to get token total supply / volume with Golang and Infura via stream?

I would like to get token total supply and/or volume(is it possible?) with Go and Infura.
Code below outputs token total supply once, but:
I need get it like in events/stream/online/websockets :)
I need get volume if it is possible
How i can improve my code for that goal?
package main
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/liyue201/erc20-go/erc20"
)
type Config struct {
Network string `yaml:"network"`
ContractAddress string `yaml:"contract_address"`
}
func main() {
conf := Config{
Network: "https://mainnet.infura.io/ws/v3/API_KEY",
ContractAddress: "0xdac17f958d2ee523a2206206994597c13d831ec7", // USDT
}
client, err := ethclient.Dial(conf.Network)
if err != nil {
fmt.Printf("Failed to connect to eth: %v", err)
return
}
token, err := erc20.NewGGToken(common.HexToAddress(conf.ContractAddress), client)
if err != nil {
fmt.Printf("Failed to instantiate a Token contract: %v", err)
return
}
name, err := token.Name(nil)
if err != nil {
fmt.Printf("Failed to get name: %v", err)
return
}
fmt.Printf("name: %v\n", name)
totalSupply, err := token.TotalSupply(nil)
if err != nil {
fmt.Printf("Failed to get name: %v", err)
return
}
fmt.Printf("totalSupply: %v\n", totalSupply.String())
}

emersion/go-imap - imap.FetchRFC822: invalid memory address or nil pointer dereference

I am trying to fetch all E-Mails from the Server with the following source Code (this function is called in the main module):
package internal
import (
"fmt"
"io"
"io/ioutil"
"log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message"
)
func FetchEMail(server string, username string, password string) error {
//Connect to Server
log.Println("Connecting to server...")
c, err := client.DialTLS(server, nil)
log.Println("Connected to " + server)
defer c.Logout()
//check if connection successful
if err != nil {
log.Println("In connection Error")
return err
}
//err = nil
//Login
log.Println("Logging in...")
err = c.Login(username, password)
log.Println("Logged in as " + username)
//check if login successful
if err != nil {
log.Println("In login Error")
return err
}
//Select INBOX
log.Println("Selecting INBOX...")
mbox, err := c.Select("INBOX", false)
log.Println("Selected INBOX")
//check if select successful
if err != nil {
return err
}
//Fetch all messages
log.Println("Fetching all messages...")
seqset := new(imap.SeqSet)
seqset.AddRange(1, mbox.Messages)
items := []imap.FetchItem{imap.FetchRFC822}
messages := make(chan *imap.Message, 10)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, items, messages)
}()
//check if fetch successful
if err := <-done; err != nil {
log.Println("In fetch Error")
return err
}
log.Println("Run Successful - Terminating...")
return nil
}
This results into the following error:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x5ee505]
goroutine 1 [running]:
I already have tried imap.FetchEvelope() which works, but for some reason imap.FetchRFC822 does not work.
My main goal is to export all E-Mail attachments (.gz, .zip, ...) from all E-Mails, that is why I need the whole E-Mail, not only the Envelope.
I think the issue was in this line items := []imap.FetchItem{imap.FetchRFC822}.
First, let's clarify what the FetchItem type is. This represents the different parts of an email that can be fetched (envelope, body, UID, flags, and so on).
Then, let's talk about the Fetch method. It expects a slice of imap.FetchItem passed in. It retrieves from the emails all of the parts specified by the slice.
So what fixes your issue is replacing this line with items := []imap.FetchItem{imap.FetchRFC822, imap.FetchEnvelope}.
I fixed and tested your program as you can see from the code snippet below:
package main
import (
"fmt"
"log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)
func FetchEMail(server string, username string, password string) error {
// Connect to Server
log.Println("Connecting to server...")
c, err := client.Dial(server)
log.Println("Connected to " + server)
defer c.Logout()
// check if connection successful
if err != nil {
log.Println("In connection Error")
return err
}
// Login
log.Println("Logging in...")
err = c.Login(username, password)
log.Println("Logged in as " + username)
// check if login successful
if err != nil {
log.Println("In login Error")
return err
}
// Select INBOX
log.Println("Selecting INBOX...")
mbox, err := c.Select("INBOX", false)
log.Println("Selected INBOX")
// check if select successful
if err != nil {
return err
}
// Fetch all messages
log.Println("Fetching all messages...")
seqset := new(imap.SeqSet)
seqset.AddRange(1, mbox.Messages)
items := []imap.FetchItem{imap.FetchRFC822, imap.FetchEnvelope}
messages := make(chan *imap.Message, 10)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, items, messages)
}()
for msg := range messages {
fmt.Printf("suject: %v\n", msg.Envelope.Subject)
}
// check if fetch successful
if err := <-done; err != nil {
log.Println("In fetch Error")
return err
}
log.Println("Run Successful - Terminating...")
return nil
}
func main() {
err := FetchEMail("xxxxxxx", "xxxxx", "xxxxx")
if err != nil {
panic(err)
}
}
Near the end, I added a for to print the subject of the retrieved emails. Here, you can replace the code with your own logic. The nil pointer dereference error disappears.
Let me know if this solves your issue!

Google Cloud Vertex AI with Golang: rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found)

I have a Vertex AI model deployed on an endpoint and want to do some prediction from my app in Golang.
To do this I create code inspired by this example : https://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1?hl=en
const file = "MY_BASE64_IMAGE"
func main() {
ctx := context.Background()
c, err := aiplatform.NewPredictionClient(cox)
if err != nil {
log.Printf("QueryVertex NewPredictionClient - Err:%s", err)
}
defer c.Close()
parameters, err := structpb.NewValue(map[string]interface{}{
"confidenceThreshold": 0.2,
"maxPredictions": 5,
})
if err != nil {
log.Printf("QueryVertex structpb.NewValue parameters - Err:%s", err)
}
instance, err := structpb.NewValue(map[string]interface{}{
"content": file,
})
if err != nil {
log.Printf("QueryVertex structpb.NewValue instance - Err:%s", err)
}
reqP := &aiplatformpb.PredictRequest{
Endpoint: "projects/PROJECT_ID/locations/LOCATION_ID/endpoints/ENDPOINT_ID",
Instances: []*structpb.Value{instance},
Parameters: parameters,
}
resp, err := c.Predict(cox, reqP)
if err != nil {
log.Printf("QueryVertex Predict - Err:%s", err)
}
log.Printf("QueryVertex Res:%+v", resp)
}
I put the path to my service account JSON file on GOOGLE_APPLICATION_CREDENTIALS environment variable.
But when I run my test app I obtain this error message:
QueryVertex Predict - Err:rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found); transport: received unexpected content-type "text/html; charset=UTF-8"
QueryVertex Res:<nil>
As #DazWilkin suggested, configure the client option to specify the specific regional endpoint with a port 443:
option.WithEndpoint("<region>-aiplatform.googleapis.com:443")
Try like below:
func main() {
ctx := context.Background()
c, err := aiplatform.NewPredictionClient(
ctx,
option.WithEndpoint("<region>-aiplatform.googleapis.com:443"),
)
if err != nil {
log.Printf("QueryVertex NewPredictionClient - Err:%s", err)
}
defer c.Close()
.
.
I'm unfamiliar with Google's (Vertex?) AI Platform and unable to test this hypothesis but it appears that the API uses location-specific endpoints.
Can you try configuring the client's ClientOption to specify the specific regional endpoint, i.e.:
url := fmt.Sprintf("https://%s-aiplatform.googleapis.com", location)
opts := []option.ClientOption{
option.WithEndpoint(url),
}
And:
package main
import (
"context"
"fmt"
"log"
"os"
aiplatform "cloud.google.com/go/aiplatform/apiv1"
"google.golang.org/api/option"
aiplatformpb "google.golang.org/genproto/googleapis/cloud/aiplatform/v1"
"google.golang.org/protobuf/types/known/structpb"
)
const file = "MY_BASE64_IMAGE"
func main() {
// Values from the environment
project := os.Getenv("PROJECT")
location := os.Getenv("LOCATION")
endpoint := os.Getenv("ENDPOINT")
ctx := context.Background()
// Configure the client with a region-specific endpoint
url := fmt.Sprintf("https://%s-aiplatform.googleapis.com", location)
opts := []option.ClientOption{
option.WithEndpoint(url),
}
c, err := aiplatform.NewPredictionClient(ctx, opts...)
if err != nil {
log.Fatal(err)
}
defer c.Close()
parameters, err := structpb.NewValue(map[string]interface{}{
"confidenceThreshold": 0.2,
"maxPredictions": 5,
})
if err != nil {
log.Fatal(err)
}
instance, err := structpb.NewValue(map[string]interface{}{
"content": file,
})
if err != nil {
log.Printf("QueryVertex structpb.NewValue instance - Err:%s", err)
}
rqst := &aiplatformpb.PredictRequest{
Endpoint: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s",
project,
location,
endpoint,
),
Instances: []*structpb.Value{
instance,
},
Parameters: parameters,
}
resp, err := c.Predict(ctx, rqst)
if err != nil {
log.Fatal(err)
}
log.Printf("QueryVertex Res:%+v", resp)
}
Try to do something like this
[...]
url := fmt.Sprintf("%s-aiplatform.googleapis.com:443", location)
[..]

Panic: runtime error: invalid memory address or nil pointer dereference only on the GAE

I am working on the golang app using gin framework. Basically it just fetch the data from firestore as JSON.
Localy it works perfectly but when I deploy it to the GAE (gcloud app deploy) there is no error during deployment but when access the page it does not work and in the logs provide an error: "panic: runtime error: invalid memory address or nil pointer dereference"
package listcollections
import (
"fmt"
"log"
"net/http"
"cloud.google.com/go/firestore"
"github.com/gin-gonic/gin"
"google.golang.org/api/iterator"
"google.golang.org/appengine"
)
func main() {
}
//GetListCollections function
func GetListCollections(c *gin.Context) {
var coll []string
ctx := appengine.NewContext(c.Request)
projectID := "XXX"
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
iter := client.Collection("collection").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
fmt.Println("ERROR")
}
coll = append(coll, doc.Data()["Title"].(string))
}
c.JSON(http.StatusOK, gin.H{
"collections": coll,
})
}
As no one has any clue where it did happen?
From Analysing your code, the only possibility which I can think off is that your
itr variable is empty.
You might need to change the check for error part and add Panic instead of just printing the error and keep runing
if err != nil {
panic("ERROR")
}

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

i'm new to golang and it may be a very basic thing but i can't seems to find the solution.
the request return json which is like this.
{"uuid":"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a22","name":"core1","owner":"systems","description":"new","creation_date":"2017-06-10T14:20:00Z"}
This is the gocode.
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Project struct {
Uuid string `json:"uuid"`
Name string `json:"name"`
Owner string `json:"owner"`
Description string `json:"description"`
Creation_date string `json:"creation_date"`
}
func main() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", "https://localhost:4443/project/core1", nil)
req.SetBasicAuth("rohit", "rohit")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("server not responding %s", err.Error())
}
var p Project
b, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
err = json.Unmarshal(b, &p)
if err != nil {
fmt.Printf("Test case failed with error %s", err.Error())
}
if resp.StatusCode != 403 {
fmt.Printf("failed %s", err.Error())
}
}
after running i'm getting this error
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x40142f]
goroutine 1 [running]:
panic(0x60c860, 0xc42000c130)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
/home/rohitk/Go_projects/src/first_program/test/main.go:41 +0x42f
exit status 2
i checked and response body has right data.
can someone please suggest what's happening here.Thanks!
As mentioned by commenters, your code is only printing errors, not handling them by altering the behavior of the program.
resp, err := client.Do(req)
if err != nil {
fmt.Printf("server not responding %s", err.Error())
}
// ...
b, err := ioutil.ReadAll(resp.Body)
In the snippet above, if there was an error then it gets printed; however, flow control proceeds as usual even though the "resp" object is probably not valid (e.g. nil).
When a library program encounters an error you should usually return it immediately without any further action. For end-user applications, you should usually display the error (typically on the stderr stream) and exit the program (typically with a nonzero exit code). For example:
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1) // Exit the program if we didn't get a response.
}
// ...
b, err := ioutil.ReadAll(resp.Body)
I am just seeing this question and I just wanted to contribute.
As mentioned by #maerics.As mentioned by commenters, your code is only printing errors, not handling them by altering the behavior of the program. My observation is also that there are two places that you are printing out errors and not handling them.
if err != nil {
fmt.Printf("server not responding %s", err.Error())
}
It should be:
if err != nil {
fmt.Printf("server not responding %s", err.Error())
return // the return statement here helps to handle the error
}
Also the second one which is :
if err != nil {
fmt.Printf("Test case failed with error %s", err.Error())
}
It should rather be :
if err != nil {
fmt.Printf("Test case failed with error %s", err.Error())
return // the return statement here helps to handle the error
}

Resources