Created
August 23, 2018 09:14
-
-
Save s-l-teichmann/22e394fcf134cde2c2afeccd584de50a to your computer and use it in GitHub Desktop.
Impersonation on two database connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
"fmt" | |
"log" | |
"github.com/jackc/pgx" | |
"github.com/jackc/pgx/stdlib" | |
) | |
func OpenDB() (*sql.DB, error) { | |
// To ease SSL config ride a bit on parsing. | |
cc, err := pgx.ParseConnectionString("sslmode=prefer") | |
if err != nil { | |
return nil, err | |
} | |
// Do the rest manually to allow whitespace in user/password. | |
cc.Host = "192.168.56.101" | |
cc.Port = 5432 | |
cc.User = "meta_login" | |
cc.Password = "nai$Poo4Ph" | |
cc.Database = "gemma" | |
return stdlib.OpenDB(cc), nil | |
} | |
func must(err error) { | |
if err != nil { | |
log.Panicf("error: %v\n", err) | |
} | |
} | |
func main() { | |
db, err := OpenDB() | |
must(err) | |
defer db.Close() | |
con1, err := db.Conn(context.Background()) | |
must(err) | |
defer con1.Close() | |
con2, err := db.Conn(context.Background()) | |
must(err) | |
defer con2.Close() | |
_, err = con1.ExecContext(context.Background(), `SELECT public.setrole_plan($1)`, "sophie") | |
must(err) | |
_, err = con2.ExecContext(context.Background(), `SELECT public.setrole_plan($1)`, "penka") | |
must(err) | |
var u1, u2 string | |
err = con1.QueryRowContext(context.Background(), `SELECT current_user`).Scan(&u1) | |
must(err) | |
err = con2.QueryRowContext(context.Background(), `SELECT current_user`).Scan(&u2) | |
must(err) | |
fmt.Printf("user 1: %s\n", u1) | |
fmt.Printf("user 2: %s\n", u2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment