inkxk
July 16, 2024, 10:34am
1
Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):
go 1.22.4
github.com/opensearch-project/opensearch-go/v4 v4.1.0
Describe the issue :
infoResp, err := client.Info(context.Background(), nil)
this line of code always get 401 unauthorized
but I can curl to OpenSearch with the same address, username, and password
and response is 200 success
Relevant Logs or Screenshots :
Hi @inkxk ,
Could you please share your code in a text format?
inkxk
July 16, 2024, 10:49am
3
func InitSession() {
client, err := opensearchapi.NewClient(
opensearchapi.Config{
Client: opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{config.GetEnv(`OPS_LOG_ADDR`)},
Username: config.GetEnv(`OPS_LOG_USER`),
Password: config.GetEnv(`OPS_LOG_PASS`),
},
},
)
if err != nil {
fmt.Println("Error creating the client:", err)
return
}
// Perform a basic request to check connectivity
infoResp, err := client.Info(context.Background(), nil)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
fmt.Printf("Cluster INFO:\n Cluster Name: %s\n Cluster UUID: %s\n Version Number: %s\n", infoResp.ClusterName, infoResp.ClusterUUID, infoResp.Version.Number)
}
this is my full code
pablo
July 16, 2024, 10:56am
4
@inkxk I’ve got it working with the following code.
client, err := opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{"https://localhost:9200"},
Username: "admin", // For testing only. Don't store credentials in code.
Password: "Eliatra123",
})
if err != nil {
fmt.Println("cannot initialize", err)
os.Exit(1)
}
inkxk
July 16, 2024, 11:03am
5
I have tried this and got same error 401
Is it possible that vpc setting when creating the instance will have an effect?
pablo
July 16, 2024, 1:01pm
6
@inkxk How did you deploy your cluster?
Do you see any errors in the OpenSearch logs?
Does your password contain any special characters? If so could you try something simpler i.e. “Eliatra123”
Just to double-check that you get the correct username, password, and address from your ENV variables, please add the following lines to your code:
fmt.Println("OPS_LOG_ADDR:", config.GetEnv(`OPS_LOG_ADDR`))
fmt.Println("OPS_LOG_USER:", config.GetEnv(`OPS_LOG_USER`))
fmt.Println("OPS_LOG_PASS:", config.GetEnv(`OPS_LOG_PASS`))
inkxk
July 17, 2024, 2:19am
8
yeah, I check it and it’s correct
@inkxk Do you see any errors in the OpenSearch logs?