-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kots run command works without identity enabled
- Loading branch information
Showing
7 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package store | ||
|
||
type DexStore interface { | ||
CreateDexDatabase(database string, user string, password string) error | ||
DatabaseUserExists(user string) (bool, error) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package store | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/kots/pkg/persistence" | ||
) | ||
|
||
type PostgresStore struct{} | ||
|
||
func (s *PostgresStore) DatabaseUserExists(user string) (bool, error) { | ||
db := persistence.MustGetDBSession() | ||
|
||
query := "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = $1" | ||
row := db.QueryRow(query, user) | ||
|
||
var exists bool | ||
err := row.Scan(&exists) | ||
if err == sql.ErrNoRows { | ||
return false, nil | ||
} else if err != nil { | ||
return false, errors.Wrap(err, "failed to query user") | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (s *PostgresStore) CreateDexDatabase(database string, user string, password string) error { | ||
db := persistence.MustGetDBSession() | ||
|
||
databaseQ := pq.QuoteIdentifier(database) | ||
userQ := pq.QuoteIdentifier(user) | ||
|
||
query := "SELECT 1 FROM pg_database WHERE datname = $1" | ||
row := db.QueryRow(query, database) | ||
var exists bool | ||
err := row.Scan(&exists) | ||
if err == sql.ErrNoRows { | ||
query := fmt.Sprintf("CREATE DATABASE %s", databaseQ) | ||
_, err := db.Exec(query) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create database") | ||
} | ||
} else if err != nil { | ||
return errors.Wrap(err, "failed to query database") | ||
} | ||
|
||
exists, err = s.DatabaseUserExists(user) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to query user") | ||
} | ||
|
||
if !exists { | ||
query := fmt.Sprintf("CREATE USER %s", userQ) | ||
_, err := db.Exec(query) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create user") | ||
} | ||
} | ||
|
||
query = fmt.Sprintf( | ||
`ALTER USER %s WITH PASSWORD '%s'; | ||
GRANT ALL PRIVILEGES ON DATABASE %s TO %s;`, | ||
userQ, password, databaseQ, userQ, | ||
) | ||
_, err = db.Exec(query) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to grant user privileges") | ||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package store | ||
|
||
type SQLiteStore struct { | ||
dbFilename string | ||
} | ||
|
||
func (s *SQLiteStore) DatabaseUserExists(user string) (bool, error) { | ||
// SQLite has no notion of db users | ||
return true, nil | ||
} | ||
|
||
func (s *SQLiteStore) CreateDexDatabase(database string, user string, password string) error { | ||
// SQLite database is a file on disk that does not need to be created ahead of time | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package store | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/replicatedhq/kots/pkg/persistence" | ||
) | ||
|
||
var ( | ||
hasStore = false | ||
globalStore DexStore | ||
) | ||
|
||
var _ DexStore = (*PostgresStore)(nil) | ||
var _ DexStore = (*SQLiteStore)(nil) | ||
|
||
func GetStore() DexStore { | ||
if hasStore { | ||
return globalStore | ||
} | ||
|
||
hasStore = true | ||
if persistence.IsSQlite() { | ||
globalStore = &SQLiteStore{ | ||
dbFilename: filepath.Join(filepath.Dir(persistence.SQLiteURI), "dex.db"), | ||
} | ||
} else { | ||
globalStore = &PostgresStore{} | ||
} | ||
|
||
return globalStore | ||
} |
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