Skip to content

Commit

Permalink
kots run command works without identity enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin committed Aug 4, 2021
1 parent 55dee34 commit c0fe9c4
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/apiserver/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
identity "github.com/replicatedhq/kots/pkg/kotsadmidentity"
identitystore "github.com/replicatedhq/kots/pkg/kotsadmidentity/store"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/store"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -53,7 +54,7 @@ func bootstrapClusterToken(autoCreateClusterToken string) error {
}

func bootstrapIdentity() error {
err := identity.CreateDexPostgresDatabase("dex", "dex", os.Getenv("DEX_PGPASSWORD"))
err := identitystore.GetStore().CreateDexDatabase("dex", "dex", os.Getenv("DEX_PGPASSWORD"))
if err != nil {
return errors.Wrap(err, "failed to create identity db")
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/kotsadmidentity/identityconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/kotsadmidentity/store"
"github.com/replicatedhq/kots/pkg/util"
"github.com/segmentio/ksuid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -20,7 +21,7 @@ func AppIdentityNeedsBootstrap(appSlug string) (bool, error) {
user := fmt.Sprintf("%s-dex", appSlug)
exists, err := postgresUserExists(user)
if err != nil {
return false, errors.Wrap(err, "failed to create dex postgres database")
return false, errors.Wrapf(err, "failed to check %s user exists", user)
}

if exists {
Expand Down Expand Up @@ -51,9 +52,9 @@ func InitAppIdentityConfig(appSlug string, storage kotsv1beta1.Storage, cipher c

database := fmt.Sprintf("%s-dex", appSlug)
user := fmt.Sprintf("%s-dex", appSlug)
err := CreateDexPostgresDatabase(database, user, postgresPassword)
err := store.GetStore().CreateDexDatabase(database, user, postgresPassword)
if err != nil {
return "", errors.Wrap(err, "failed to create dex postgres database")
return "", errors.Wrap(err, "failed to create dex database")
}

identityConfig := &kotsv1beta1.IdentityConfig{
Expand Down
6 changes: 6 additions & 0 deletions pkg/kotsadmidentity/store/interface.go
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)
}
75 changes: 75 additions & 0 deletions pkg/kotsadmidentity/store/postgres.go
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
}
15 changes: 15 additions & 0 deletions pkg/kotsadmidentity/store/sqlite.go
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
}
32 changes: 32 additions & 0 deletions pkg/kotsadmidentity/store/store.go
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
}
6 changes: 4 additions & 2 deletions pkg/store/kotsstore/version_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,15 @@ func (s *KOTSStore) GetAppVersion(appID string, sequence int64) (*versiontypes.A
row := db.QueryRow(query, appID, sequence)

var status sql.NullString
var deployedAt sql.NullTime
var deployedAt persistence.NullStringTime
var createdAt persistence.NullStringTime
var installationSpec sql.NullString
var kotsAppSpec sql.NullString

v := versiontypes.AppVersion{
AppID: appID,
}
if err := row.Scan(&v.Sequence, &v.CreatedOn, &status, &deployedAt, &installationSpec, &kotsAppSpec); err != nil {
if err := row.Scan(&v.Sequence, &createdAt, &status, &deployedAt, &installationSpec, &kotsAppSpec); err != nil {
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
Expand All @@ -569,6 +570,7 @@ func (s *KOTSStore) GetAppVersion(appID string, sequence int64) (*versiontypes.A
}
}

v.CreatedOn = createdAt.Time
if deployedAt.Valid {
v.DeployedAt = &deployedAt.Time
}
Expand Down

0 comments on commit c0fe9c4

Please sign in to comment.