Skip to content

Commit

Permalink
Merge pull request #73 from spowelljr/iam
Browse files Browse the repository at this point in the history
Add ability to authenticate using IAM
  • Loading branch information
medyagh authored Aug 3, 2023
2 parents 8c2592e + a72e32f commit 5dff30a
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 76 deletions.
18 changes: 15 additions & 3 deletions cmd/dbbrowser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ import (
"github.com/medyagh/gopogh/pkg/db"
)

var dbPath = flag.String("db_path", "", "path to postgres db in the form of 'host=HOST_NAME user=DB_USER dbname=DB_NAME password=DB_PASS'")
var dbPath = flag.String("db_path", "", "path to postgres db in the form of 'user=DB_USER dbname=DB_NAME password=DB_PASS'")
var dbHost = flag.String("db_host", "", "host of the db")
var useCloudSQL = flag.Bool("use_cloudsql", false, "whether the database is a cloudsql db")
var useIAMAuth = flag.Bool("use_iam_auth", false, "whether to use IAM to authenticate with the cloudsql db")

func main() {
flag.Parse()
if *dbPath == "" {
log.Fatalf("db_path not specified")
log.Fatalf("The db_path flag is required")
}
db, err := db.FromEnv(*dbPath, "postgres", *useCloudSQL)
if *dbHost == "" {
log.Fatalf("The db_host flag is required")
}
flagValues := db.FlagValues{
Backend: "postgres",
Host: *dbHost,
Path: *dbPath,
UseCloudSQL: *useCloudSQL,
UseIAMAuth: *useIAMAuth,
}
db, err := db.FromEnv(flagValues)
if err != nil {
log.Fatal(err)
}
Expand Down
37 changes: 29 additions & 8 deletions cmd/gopogh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"

"github.com/medyagh/gopogh/pkg/db"
"github.com/medyagh/gopogh/pkg/models"
"github.com/medyagh/gopogh/pkg/parser"
"github.com/medyagh/gopogh/pkg/report"
Expand All @@ -15,9 +16,11 @@ import (
var Build string

var (
dbPath = flag.String("db_path", "", "path to sql database/database file. if using postgres in the form of 'host=HOST_NAME user=DB_USER dbname=DB_NAME password=DB_PASS'")
useCloudSQL = flag.Bool("use_cloudsql", false, "whether the database is a cloudsql db")
dbBackend = flag.String("db_backend", "", "sql database driver. 'sqlite' for file output")
dbHost = flag.String("db_host", "", "host of the db")
dbPath = flag.String("db_path", "", "path to sql database/database file. if using postgres in the form of 'user=DB_USER dbname=DB_NAME password=DB_PASS'")
useCloudSQL = flag.Bool("use_cloudsql", false, "whether the database is a cloudsql db")
useIAMAuth = flag.Bool("use_iam_auth", false, "whether to use IAM to authenticate with the cloudsql db")
reportName = flag.String("name", "", "report name")
reportPR = flag.String("pr", "", "Pull request number")
reportDetails = flag.String("details", "", "report details (for example test args...)")
Expand Down Expand Up @@ -62,8 +65,15 @@ func main() {
os.Exit(1)
}

if dbVarProvided(*dbPath, *dbBackend) {
if err := c.SQL(*dbPath, *dbBackend, *useCloudSQL); err != nil {
if dbVarProvided(*dbPath, *dbBackend, *dbHost) {
flagValues := db.FlagValues{
Backend: *dbBackend,
Host: *dbHost,
Path: *dbPath,
UseCloudSQL: *useCloudSQL,
UseIAMAuth: *useIAMAuth,
}
if err := c.SQL(flagValues); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down Expand Up @@ -101,8 +111,19 @@ func main() {
}

// dbVarProvided checks whether any of the database flags/environment variables are set
func dbVarProvided(dbPath string, dbBackend string) bool {
return (dbPath != "" || os.Getenv("DB_PATH") != "") ||
(dbBackend != "" || os.Getenv("DB_BACKEND") != "")

func dbVarProvided(dbPath, dbBackend, dbHost string) bool {
values := []string{
dbBackend,
dbHost,
dbPath,
os.Getenv("DB_BACKEND"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PATH"),
}
for _, v := range values {
if v != "" {
return true
}
}
return false
}
33 changes: 21 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/medyagh/gopogh
go 1.20

require (
cloud.google.com/go/cloudsqlconn v1.4.0
github.com/GoogleCloudPlatform/cloudsql-proxy v1.33.8
github.com/jackc/pgx/v4 v4.18.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
modernc.org/sqlite v1.23.1
Expand All @@ -17,7 +19,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/tools v0.7.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
Expand All @@ -31,25 +33,32 @@ require (
)

require (
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.10.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/api v0.130.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 // indirect
google.golang.org/grpc v1.56.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading

0 comments on commit 5dff30a

Please sign in to comment.