Skip to content

Commit

Permalink
main: add option to disable ssl when connection now that connection w…
Browse files Browse the repository at this point in the history
…ith ssl is default
  • Loading branch information
frederikhs committed May 8, 2023
1 parent 267a776 commit 25f83e2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var listCmd = &cobra.Command{

host := args[0]

conn := database.GetDatabaseEntryConnection(host)
withSSL := getUseSSL(cmd)

conn := database.GetDatabaseEntryConnection(host, withSSL)
if conn == nil {
cmd.Println("no hosts found by that name")
os.Exit(1)
Expand Down
4 changes: 3 additions & 1 deletion cmd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var rolesCmd = &cobra.Command{

host := args[0]

conn := database.GetDatabaseEntryConnection(host)
withSSL := getUseSSL(cmd)

conn := database.GetDatabaseEntryConnection(host, withSSL)
if conn == nil {
cmd.Println("no hosts found by that name")
os.Exit(1)
Expand Down
15 changes: 14 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Execute(buildVersion string) {

func init() {
rootCmd.PersistentFlags().StringP("output", "o", "", "--output json|table (optional default table)")
rootCmd.PersistentFlags().Bool("no-ssl", false, "do not use ssl when connecting")
}

func addRequiredHostFlag(cmd *cobra.Command) {
Expand All @@ -54,7 +55,9 @@ func givenUserModification(cmd *cobra.Command, args []string, userMustExist bool

username := args[0]

conn := database.GetDatabaseEntryConnection(host)
withSSL := getUseSSL(cmd)

conn := database.GetDatabaseEntryConnection(host, withSSL)
if conn == nil {
cmd.Println("no hosts found by that name")
os.Exit(1)
Expand Down Expand Up @@ -110,3 +113,13 @@ func getOutputType(cmd *cobra.Command) OutputType {
return OutputTypeTable
}
}

func getUseSSL(cmd *cobra.Command) bool {
ssl, err := cmd.Flags().GetBool("no-ssl")
if err != nil {
cmd.Println(err)
os.Exit(1)
}

return !ssl
}
13 changes: 9 additions & 4 deletions database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ type DBConn struct {
db *sqlx.DB
}

func (config *DBConfig) Connect() *DBConn {
db, err := sqlx.Connect("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", config.Host, config.Port, config.User, config.Database, config.Pass))
func (config *DBConfig) Connect(withSSL bool) *DBConn {
ssl := "require"
if !withSSL {
ssl = "disable"
}

db, err := sqlx.Connect("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s", config.Host, config.Port, config.User, config.Database, config.Pass, ssl))
if err != nil {
log.Println("unable to connect to database:", err)
time.Sleep(time.Second * 5)
return config.Connect()
return config.Connect(withSSL)
}

if db.Ping() != nil {
log.Println("unable to connect to database:", err)
time.Sleep(time.Second * 5)
return config.Connect()
return config.Connect(withSSL)
}

return &DBConn{db: db, Config: config}
Expand Down
4 changes: 2 additions & 2 deletions database/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func HostEquals(a, b string) bool {
return false
}

func GetDatabaseEntryConnection(hostQuery string) *DBConn {
func GetDatabaseEntryConnection(hostQuery string, withSSL bool) *DBConn {
hosts, err := ListHosts()
if err != nil {
panic(err)
Expand All @@ -32,7 +32,7 @@ func GetDatabaseEntryConnection(hostQuery string) *DBConn {
}

config := MakeDbConfig(d.Hostname, d.Port, d.Username, d.Database)
return config.Connect()
return config.Connect(withSSL)
}

return nil
Expand Down

0 comments on commit 25f83e2

Please sign in to comment.