Unable to connect to Dremio with ODBC in Golang - go

I am new to Golang and I am trying to connect to Dremio using ODBC in Golang with Dremio host, port, username and password. The following code gives me error
2022/04/19 17:36:42 missing username and password
exit status 1
import (
"database/sql"
"fmt"
"gographqlservice/graph/model"
"log"
_ "github.com/pinpt/go-dremio/driver"
)
const (
host = "dremio.xyz.abc.com"
user = "user1"
password = "user_password"
port = 32010
)
func GetAsset(id string) (*model.Asset, error) {
drminfo := fmt.Sprintf("host=%s port=%d user=%s password=%s sslmode=disable",
host, port, user, password)
db, err := sql.Open("dremio", drminfo)
if err != nil {
log.Fatal("error occurred")
log.Fatal(err)
}
rows, err := db.Query("SELECT * FROM space.xyz.\"assets\" WHERE ASSET_ID = ?", id)
if err != nil {
log.Fatal(err)
}
var asset model.Asset
defer rows.Close()
for rows.Next() {
// some code goes here
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
defer db.Close()
return &asset, err
}

It looks like you are using driver code from "Pinpoint" that is documented here:
https://pkg.go.dev/github.com/pinpt/go-dremio#section-readme
You can see their connection string should be in the following form:
db, err := sql.Open("dremio", "https://user:pass#dremio.example.com")
Your code appears to be in the form:
drminfo := fmt.Sprintf("host=%s port=%d user=%s password=%s sslmode=disable",
host, port, user, password)
To allow the Pinpoint code to parse this connection string and pull out the user and password, try the following instead:
drminfo := fmt.Sprintf("http://%s:%s#%s",
user, password, host)
The error message you're seeing comes from the following line in the Pinpoint code. In this case, it means they were not able to figure out the user name from the connection string provided.
https://github.com/pinpt/go-dremio/blob/master/driver/driver.go#L187
if u.User == nil {
return nil, fmt.Errorf("missing username and password")
}

Related

Why am I getting a timeout when executing a query against an Aurora Serverless instance from a Go lambda

This is my first attempt at using a lambda with Aurora Serverless(postgres). I wrote some code to essentially be able to check that the connection is working, but it's not clear to me why I am getting a timeout when I try to execute the query.
This code seems pretty straightforward. Is there something I'm missing about the serverless environment?
func connect() (*sql.DB, error) {
dbName := "my_db"
dbUser := "app_user"
dbHost := "REDACTED.us-east-2.rds.amazonaws.com"
dbPort := 5432
dbEndPoint := fmt.Sprintf("%s:%d", dbHost,dbPort)
region := "us-east-2"
creds := credentials.NewEnvCredentials()
authToken, err := rdsutils.BuildAuthToken(dbEndPoint, region, dbUser, creds)
if err != nil {
fmt.Printf("ERROR: %v \n", err)
return nil, err
}
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s \n",
dbHost, dbPort, dbUser, authToken, dbName,
)
return sql.Open("postgres", dsn)
}
func Healthcheck() (bool, error) {
db, err := connect()
if err != nil {
return false, err
}
defer db.Close()
var result int
err = db.QueryRow("Select 1").Scan(&result)
if err != nil {
return false, err
}
fmt.Printf("Result: %v\n", result)
return true, nil
}

How can I fix GoLang's LDAP Filter Operations Error?

So I am new to GoLang and I am trying to make a LDAP filter. However, I keep getting the following error:
LDAP Result Code 1 "Operations Error": 000020D6: SvcErr: DSID-031007E5, problem 5012 (DIR_ERROR), data 0
I read in here (LDAP Errors) that "Operations Error" happens when either not all the necessary calls have been made or the order of operation of the calls is wrong.
I am honestly not sure, what I might be missing and or what I am doing wrong.
My code is the following
import (
"crypto/tls"
"fmt"
"github.com/go-ldap/ldap/v3"
"log"
)
func ApiCaller(){
// The username and password we want to check
username := "username"
password := "password"
//Establishing connection to the LDAP server
l, err := ldap.Dial("tcp", "exampleURL.com:389")
if err != nil {
log.Fatal(err)
}
// Closing connection in case of error
defer l.Close()
// Reconnect with TLS
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
log.Fatal(err)
}
// Bind as the user to verify their password
err = l.Bind(username, password)
if err != nil {
log.Fatal(err)
}
// Setting up the search criterias with filter
searchRequest := ldap.NewSearchRequest(
"OU=OUg_Applications",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(&(OU=OUg_Applications))"),
[]string{""},
nil,
)
// Performing search in the LDAP Server
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
fmt.Println(sr.Entries)
}
The question comments helped me figure it out. My base-DN was off. It was supposed to be "DC=example,DC=com", instead of "OU=OUg_Applications".

Infinite loop when db.Ping() is called

I am attempting to create a basic connection to a database. The problem happens when I try to test the connection with db.Ping(); everything works until I get to this line. The Ping sends the program into an infinite loop (the function call never returns), and I'm not sure how to go about fixing this.
package main
import (
"database/sql"
"fmt"
"html/template"
"net/http"
_ "github.com/lib/pq"
}
type Page struct {
Name string
DBStatus bool
}
const (
host = "localhost"
port = 8080
user = "username"
password = "password"
dbname = "GoTest"
)
func main() {
templates := template.Must(template.ParseFiles("templates/index.html"))
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
p := Page{Name: "Gopher"}
if name := r.FormValue("name"); name != "" {
p.Name = name
}
p.DBStatus = db.Ping() == nil //this point is reached but never returned
if err := templates.ExecuteTemplate(w, "index.html", p); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
fmt.Println(http.ListenAndServe(":8080", nil))
}
It seems I can connect to the database fine, as the sql.Open call doesn't return an error, and if I called the Ping outside of the http server handle function, it also returns just fine.
Any help would be greatly appreciated!
Your database configurations are wrong. It's pointing to the Golang server port 8080. It should point to the pgsql port(default 5432)

CRUD operations on Redshift databases using Golang

Could you please give me some explanations and some code examples on how it would be done (ex: creating tables and inserting data) ?
Which library would you advise me to use ?
Thanks !
Please note the side-effect import of github.com/lib/pq
After this queries can be run by db.Query() or db.Exec()
https://golang.org/pkg/database/sql/#example_DB_Query
https://golang.org/pkg/database/sql/#pkg-examples
import (
_ "github.com/lib/pq"
"database/sql"
"fmt"
)
func MakeRedshfitConnection(username, password, host, port, dbName string) (*sql.DB, error) {
url := fmt.Sprintf("sslmode=require user=%v password=%v host=%v port=%v dbname=%v",
username,
password,
host,
port,
dbName)
var err error
var db *sql.DB
if db, err = sql.Open("postgres", url); err != nil {
return nil, fmt.Errorf("redshift connect error : (%v)"), err
}
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("redshift ping error : (%v)", err)
}
return db, nil
}

How to pull image from custom docker registry with golang?

Using docker sources how to pull image from custom registry? As a result of using such code
// Prepare auth registry for usage
func (app *App) PrepareRegistry() error {
app.AuthConfig = types.AuthConfig{
Username: Username,
Password: Password,
ServerAddress: DefaultServer,
}
resp, err := app.Client.RegistryLogin(context.Background(), app.AuthConfig)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
if resp.IdentityToken != "" {
app.AuthConfig.IdentityToken = resp.IdentityToken
}
app.AuthConfigEncoded, err = command.EncodeAuthToBase64(app.AuthConfig)
return err
}
func (app *App) ImagePull() error {
opts := types.ImagePullOptions{
All: true,
RegistryAuth: app.AuthConfigEncoded,
PrivilegeFunc: registryAuthentication(app.Name),
}
responseBody, err := app.Client.ImagePull(context.Background(), app.Name, opts)
defer responseBody.Close()
if err != nil {
return err
}
return nil
}
I am still getting the error
Login Succeeded
panic: Error response from daemon: Get https://registry-1.docker.io/v2/shalakhin/blender/tags/list: unauthorized: incorrect username or password
While ServerAddress is registry.gitlab.com, not registry-1.docker.io
Did you check identity token? That might cause an authentication problem.
A suggestion:
Docker client
This works fine, as I can see you did not specify the endpoint. I think you should add this info.
authConfig := types.AuthConfig{
Username: "username",
Password: "password",
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
panic(err)
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
out, err := cli.ImagePull(ctx, "alpine", types.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
panic(err)
}

Resources