Explorar el Código

no auth for single docker container option using sqlite

sunguroku hace 5 años
padre
commit
2a3d855527

+ 2 - 0
.gitignore

@@ -1,3 +1,5 @@
 .DS_Store
 .env
 app
+*.db
+

+ 4 - 1
cmd/app/main.go

@@ -5,6 +5,7 @@ import (
 	"log"
 	"net/http"
 
+	"github.com/gorilla/sessions"
 	"github.com/porter-dev/porter/internal/repository/gorm"
 
 	"github.com/porter-dev/porter/server/api"
@@ -30,7 +31,9 @@ func main() {
 
 	repo := gorm.NewRepository(db)
 
-	store, _ := sessionstore.NewStore(repo, appConf.Server)
+	// declare as Store interface (methods Get, New, Save)
+	var store sessions.Store
+	store, _ = sessionstore.NewStore(repo, appConf.Server)
 
 	validator := vr.New()
 

+ 1 - 0
cmd/migrate/main.go

@@ -25,6 +25,7 @@ func main() {
 	err = db.AutoMigrate(
 		&models.User{},
 		&models.ClusterConfig{},
+		&models.Session{},
 	)
 
 	if err != nil {

+ 1 - 1
dashboard/src/main/Login.tsx

@@ -40,7 +40,7 @@ export default class Login extends Component<PropsType, StateType> {
         password: password
       }, {}, (err: any, res: any) => {
         // TODO: case and set credential error
-        err ? setCurrentError(JSON.stringify(err)) : authenticate();
+        err ? setCurrentError(err.response.data.errors[0]) : authenticate();
       });
     }
   }

+ 1 - 2
dashboard/src/main/Register.tsx

@@ -48,8 +48,7 @@ export default class Register extends Component<PropsType, StateType> {
         email: email,
         password: password
       }, {}, (err: any, res: any) => {
-        console.log('err',err)
-        err ? setCurrentError(JSON.stringify(err)) : authenticate();
+        err ? setCurrentError(err.response.data.errors[0]) : authenticate();
       });
     } 
   };

+ 2 - 2
dashboard/src/main/home/sidebar/Sidebar.tsx

@@ -84,8 +84,8 @@ export default class Sidebar extends Component<PropsType, StateType> {
     api.logOutUser('<token>', {}, {}, (err: any, res: any) => {
       // TODO: case and set logout error
       
-      err ? setCurrentError(JSON.stringify(err)) : logOut();
-    });
+      err ? setCurrentError(err.response.data.errors[0]) : logOut();
+    }); 
   }
 
   // SidebarBg is separate to cover retracted drawer

+ 0 - 1
dashboard/src/shared/baseApi.tsx

@@ -39,7 +39,6 @@ export const baseApi = <T extends {}, S = {}>(requestType: string, endpoint: ((p
       });
     } else {
       axios.get(endpointString, {
-        withCredentials: true,
         params
       })
       .then(res => {

+ 1 - 0
docker-compose.dev.yaml

@@ -21,6 +21,7 @@ services:
       - ./cmd:/porter/cmd
       - ./internal:/porter/internal
       - ./server:/porter/server
+      - $HOME/.kube/config:/porter/.kubeconfig
   postgres:
     image: postgres:latest
     container_name: postgres

+ 3 - 1
docker/.env

@@ -10,4 +10,6 @@ DB_PORT=5432
 DB_USER=porter
 DB_PASS=porter
 DB_NAME=porter
-COOKIE_SECRETS=secret
+COOKIE_SECRETS=secret
+
+ENABLE_AUTH=false

+ 1 - 0
go.mod

@@ -32,6 +32,7 @@ require (
 	gopkg.in/go-playground/validator.v9 v9.31.0
 	gopkg.in/yaml.v2 v2.3.0
 	gorm.io/driver/postgres v1.0.2
+	gorm.io/driver/sqlite v1.1.3
 	gorm.io/gorm v1.20.2
 	k8s.io/apimachinery v0.19.2
 	k8s.io/client-go v0.0.0-20200917000235-cba7285b7f29

+ 4 - 0
go.sum

@@ -289,6 +289,8 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
 github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
 github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
+github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA=
+github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -644,6 +646,8 @@ gorm.io/driver/postgres v1.0.1 h1:jRfDNUxpxNrea/97kbcscAQGmiks4UCKAYXsvh4rhOQ=
 gorm.io/driver/postgres v1.0.1/go.mod h1:pv4dVhHvEVrP7k/UYqdBIllbdbpB5VTz89X1O0uOrCA=
 gorm.io/driver/postgres v1.0.2 h1:mB5JjD4QglbCTdMT1aZDxQzHr87XDK1qh0MKIU3P96g=
 gorm.io/driver/postgres v1.0.2/go.mod h1:FvRSYfBI9jEp6ZSjlpS9qNcSjxwYxFc03UOTrHdvvYA=
+gorm.io/driver/sqlite v1.1.3 h1:BYfdVuZB5He/u9dt4qDpZqiqDJ6KhPqs5QUqsr/Eeuc=
+gorm.io/driver/sqlite v1.1.3/go.mod h1:AKDgRWk8lcSQSw+9kxCJnX/yySj8G3rdwYlU57cB45c=
 gorm.io/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
 gorm.io/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
 gorm.io/gorm v1.20.1 h1:+hOwlHDqvqmBIMflemMVPLJH7tZYK4RxFDBHEfJTup0=

+ 9 - 1
internal/adapter/gorm.go

@@ -2,9 +2,12 @@ package gorm
 
 import (
 	"fmt"
+	"os"
+	"strconv"
 
 	"github.com/porter-dev/porter/internal/config"
 	"gorm.io/driver/postgres"
+	"gorm.io/driver/sqlite"
 	"gorm.io/gorm"
 )
 
@@ -18,5 +21,10 @@ func New(conf *config.DBConf) (*gorm.DB, error) {
 		conf.Host,
 	)
 
-	return gorm.Open(postgres.Open(dsn), &gorm.Config{})
+	if auth, _ := strconv.ParseBool(os.Getenv("ENABLE_AUTH")); auth {
+		return gorm.Open(postgres.Open(dsn), &gorm.Config{})
+	} else {
+		return gorm.Open(sqlite.Open("./internal/porter.db"), &gorm.Config{})
+	}
+
 }

+ 9 - 0
internal/auth/sessionstore.go

@@ -127,6 +127,15 @@ func NewStore(repo *repository.Repository, conf config.ServerConf) (*PGStore, er
 	return dbStore, nil
 }
 
+// NewFilesystemStore takes session key pairs to create a session-store in the local fs without using a db.
+func NewFilesystemStore(conf config.ServerConf) *sessions.FilesystemStore {
+
+	// Defaults to os.TempDir() when first argument (path) isn't specified.
+	store := sessions.NewFilesystemStore("", conf.CookieSecret)
+
+	return store
+}
+
 // Get Fetches a session for a given name after it has been added to the
 // registry.
 func (store *PGStore) Get(r *http.Request, name string) (*sessions.Session, error) {

+ 2 - 0
internal/kubernetes/kubeconfig.go

@@ -176,3 +176,5 @@ func (k *KubeConfig) createUserMap() map[string]KubeConfigUser {
 
 	return userMap
 }
+
+// func ReadLocalKubeConfig()

+ 4 - 3
server/api/api.go

@@ -4,7 +4,8 @@ import (
 	"github.com/go-playground/locales/en"
 	ut "github.com/go-playground/universal-translator"
 	"github.com/go-playground/validator/v10"
-	sessionstore "github.com/porter-dev/porter/internal/auth"
+
+	"github.com/gorilla/sessions"
 	lr "github.com/porter-dev/porter/internal/logger"
 	"github.com/porter-dev/porter/internal/repository"
 )
@@ -15,7 +16,7 @@ type App struct {
 	logger     *lr.Logger
 	repo       *repository.Repository
 	validator  *validator.Validate
-	store      *sessionstore.PGStore
+	store      sessions.Store
 	translator *ut.Translator
 	cookieName string
 }
@@ -25,7 +26,7 @@ func New(
 	logger *lr.Logger,
 	repo *repository.Repository,
 	validator *validator.Validate,
-	store *sessionstore.PGStore,
+	store sessions.Store,
 	cookieName string,
 ) *App {
 	// for now, will just support the english translator from the

+ 1 - 3
server/api/user_handler.go

@@ -3,7 +3,6 @@ package api
 import (
 	"encoding/json"
 	"errors"
-	"fmt"
 	"net/http"
 	"strconv"
 	"strings"
@@ -57,8 +56,7 @@ func (app *App) HandleCreateUser(w http.ResponseWriter, r *http.Request) {
 // HandleAuthCheck checks whether current session is authenticated.
 func (app *App) HandleAuthCheck(w http.ResponseWriter, r *http.Request) {
 	session, err := app.store.Get(r, app.cookieName)
-	cook, _ := r.Cookie("porter")
-	fmt.Println("cooki", cook)
+
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 	}

+ 3 - 3
server/router/middleware/auth.go

@@ -5,16 +5,16 @@ import (
 	"strconv"
 
 	"github.com/go-chi/chi"
-	sessionstore "github.com/porter-dev/porter/internal/auth"
+	"github.com/gorilla/sessions"
 )
 
 type Auth struct {
-	store      *sessionstore.PGStore
+	store      sessions.Store
 	cookieName string
 }
 
 func NewAuth(
-	store *sessionstore.PGStore,
+	store sessions.Store,
 	cookieName string,
 ) *Auth {
 	return &Auth{store, cookieName}

+ 2 - 3
server/router/router.go

@@ -2,15 +2,14 @@ package router
 
 import (
 	"github.com/go-chi/chi"
+	"github.com/gorilla/sessions"
 	"github.com/porter-dev/porter/server/api"
 	"github.com/porter-dev/porter/server/requestlog"
 	"github.com/porter-dev/porter/server/router/middleware"
-
-	sessionstore "github.com/porter-dev/porter/internal/auth"
 )
 
 // New creates a new Chi router instance
-func New(a *api.App, store *sessionstore.PGStore, cookieName string) *chi.Mux {
+func New(a *api.App, store sessions.Store, cookieName string) *chi.Mux {
 	l := a.Logger()
 	r := chi.NewRouter()
 	auth := middleware.NewAuth(store, cookieName)