Skip to content

Commit

Permalink
go fmt (sorry)
Browse files Browse the repository at this point in the history
  • Loading branch information
toresbe committed Apr 22, 2021
1 parent 31d9394 commit 1dd36ba
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 88 deletions.
57 changes: 32 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const (

func main() {
logger, _ = zap.NewDevelopment()
smellTest()
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":8080", nil)
smellTest()
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":8080", nil)
//logger, _ = zap.NewProduction()
defer logger.Sync()
logger.Info("Assuming the position...")
Expand Down Expand Up @@ -99,17 +99,17 @@ func handleConnection(frontConn net.Conn) {
return
}
startupFields, _ := startupMessage.parse()
if startupFields["encryptionRequest"] == "true" {
logger.Debug("Sending encryption downgrade request packet")
frontConn.Write([]byte{0x4e})
handleConnection(frontConn)
return
}
servAddr, err := getRearEnd(startupFields["database"])
if err != nil {
sendErrorPacket(err.Error(), frontConn)
return
}
if startupFields["encryptionRequest"] == "true" {
logger.Debug("Sending encryption downgrade request packet")
frontConn.Write([]byte{0x4e})
handleConnection(frontConn)
return
}
servAddr, err := getRearEnd(startupFields["database"])
if err != nil {
sendErrorPacket(err.Error(), frontConn)
return
}
rearConn, err := openWide(frontConn, servAddr)
if err != nil {
sendErrorPacket("Rear end not ready", frontConn)
Expand Down Expand Up @@ -161,17 +161,24 @@ func (startupMessage *pgStartupMessage) parse() (map[string]string, error) {

res["protoMajor"] = strconv.FormatUint(uint64(protoVer>>16), 10)
res["protoMinor"] = strconv.FormatUint(uint64(protoVer&0xFFFF), 10)
if (res["protoMajor"] == "1234") && (res["protoMinor"] == "5679") {
logger.Debug("Startup message is SSLRequest")
res["encryptionRequest"] = "true"
return res, nil
} else if (res["protoMajor"] == "1234") && (res["protoMinor"] == "5680") {
logger.Debug("Startup message is GSSEncRequest")
res["encryptionRequest"] = "true"
return res, nil
} else {
res["encryptionRequest"] = "false"
}

if res["protoMajor"] == "1234" {
if res["protoMinor"] == "5679" {
logger.Info("Startup message is SSLRequest")
res["encryptionRequest"] = "true"
return res, nil
}

if res["protoMinor"] == "5680" {
logger.Info("Startup message is GSSEncRequest")
res["encryptionRequest"] = "true"
return res, nil
}

logger.Warn("Almost certainly received a request to start a session with an as-of-yet unsupported encryption")
}

res["encryptionRequest"] = "false"

for {
key, _ := reader.ReadString(0)
Expand Down
128 changes: 65 additions & 63 deletions posterior.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,90 @@
package main

import (
"errors"
"strconv"
"os"
"database/sql"
_ "go.uber.org/zap"
"fmt"
_ "github.com/lib/pq"
"database/sql"
"errors"
"fmt"
_ "github.com/lib/pq"
_ "go.uber.org/zap"
"os"
"strconv"
)

const (
host = "localhost"
port = 5433
user = "herlighet"
password = "herlighet"
confdb = "herlighet"
host = "localhost"
port = 5433
user = "herlighet"
password = "herlighet"
confdb = "herlighet"
)

func openDb() *sql.DB {
psqlInfo := getDbConfString()
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
return db
psqlInfo := getDbConfString()
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
logger.Error("Failed to connect to herlighet database")
panic(err)
}
err = db.Ping()
if err != nil {
logger.Error("Failed to connect to herlighet database")
panic(err)
}
return db
}

func smellTest() {
var count int
db := openDb()
row := db.QueryRow("SELECT COUNT(*) FROM databases;")
err := row.Scan(&count)
if err != nil {
panic(err)
}
logger.Info(fmt.Sprintf("%d databases in access list", count))
db.Close()
var count int
db := openDb()
row := db.QueryRow("SELECT COUNT(*) FROM databases;")
err := row.Scan(&count)
if err != nil {
panic(err)
}
logger.Info(fmt.Sprintf("%d databases in access list", count))
db.Close()
}

func getDbConfField(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
os.Setenv(key, fallback)
logger.Info(fmt.Sprintf(`"%s" is not defined; using fallback value.`, key))
return fallback
if value, ok := os.LookupEnv(key); ok {
return value
}
os.Setenv(key, fallback)
logger.Info(fmt.Sprintf(`"%s" is not defined; using fallback value.`, key))
return fallback
}

func getDbConfString() string {
user := getDbConfField("HERLIGHET_DBUSER", "herlighet")
pass := getDbConfField("HERLIGHET_DBPASS", "herlighet")
host := getDbConfField("HERLIGHET_DBHOST", "localhost")
port, _ := strconv.Atoi(getDbConfField("HERLIGHET_DBPORT", "5433"))
dbname := getDbConfField("HERLIGHET_DBNAME", "herlighet")
logger.Info(fmt.Sprintf("Connecting as %s to %s:%d/%s", user, host, port, dbname))
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, pass, dbname)
user := getDbConfField("HERLIGHET_DBUSER", "herlighet")
pass := getDbConfField("HERLIGHET_DBPASS", "herlighet")
host := getDbConfField("HERLIGHET_DBHOST", "localhost")
port, _ := strconv.Atoi(getDbConfField("HERLIGHET_DBPORT", "5433"))
dbname := getDbConfField("HERLIGHET_DBNAME", "herlighet")
logger.Info(fmt.Sprintf("Connecting as %s to %s:%d/%s", user, host, port, dbname))
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, pass, dbname)
}

func getRearEnd(dbname string) (string, error) {
db := openDb()
defer db.Close()
db := openDb()
defer db.Close()

sqlStatement := `SELECT hostname, naisdevice FROM databases WHERE dbname=$1;`
sqlStatement := `SELECT hostname, naisdevice FROM databases WHERE dbname=$1;`

var hostname string
var naisdevice bool
var hostname string
var naisdevice bool

row := db.QueryRow(sqlStatement, dbname)
switch err := row.Scan(&hostname, &naisdevice); err {
case sql.ErrNoRows:
return "", errors.New("naisdevice access not configured for this database; please ask #postgres-på-laptop for help with updating database-iac.")
case nil:
if !naisdevice {
return "", errors.New("naisdevice access disabled for this database; please update database-iac.")
} else {
return hostname, nil
}
default:
return "", err
}
row := db.QueryRow(sqlStatement, dbname)
switch err := row.Scan(&hostname, &naisdevice); err {
case sql.ErrNoRows:
return "", errors.New("naisdevice access not configured for this database; please ask #postgres-på-laptop for help with updating database-iac.")
case nil:
if !naisdevice {
return "", errors.New("naisdevice access disabled for this database; please update database-iac.")
} else {
return hostname, nil
}
default:
return "", err
}
}

0 comments on commit 1dd36ba

Please sign in to comment.