diff --git a/app/common/config.go b/app/common/config.go index 0ab1ac7..24626b1 100644 --- a/app/common/config.go +++ b/app/common/config.go @@ -7,14 +7,9 @@ import ( ) type PVEConfig struct { - URL string `json:"url"` - Token struct { - User string `json:"user"` - Realm string `json:"realm"` - ID string `json:"id"` - UUID string `json:"uuid"` - } `json:"token"` - PAASClientRole string `json:"paas-client-role"` + URL string `json:"url"` + Token PVEAPIToken `json:"token"` + PAASClientRole string `json:"paas-client-role"` } type LDAPConfig struct { diff --git a/app/common/types.go b/app/common/types.go index 9b87cd0..ae88a7a 100644 --- a/app/common/types.go +++ b/app/common/types.go @@ -32,10 +32,9 @@ type Group = paas.Group type Username = paas.Username type User = paas.User -func ParseGroupname(groupname string) (Groupname, error) { - return paas.ParseGroupname(groupname) -} - -func ParseUsername(username string) (Username, error) { - return paas.ParseUsername(username) +type PVEAPIToken struct { + User string `json:"user"` + Realm string `json:"realm"` + ID string `json:"id"` + UUID string `json:"uuid"` } diff --git a/app/common/utils.go b/app/common/utils.go index 97802ad..b4f906c 100644 --- a/app/common/utils.go +++ b/app/common/utils.go @@ -1,9 +1,20 @@ package app import ( + "fmt" "reflect" + + paas "proxmoxaas-common-lib" ) +func ParseGroupname(groupname string) (Groupname, error) { + return paas.ParseGroupname(groupname) +} + +func ParseUsername(username string) (Username, error) { + return paas.ParseUsername(username) +} + // RequireAll ensures that EVERY non-excluded exported field in the struct is non-zero. func RequireAll(v any, excludes ...string) bool { val := reflect.ValueOf(v) @@ -53,3 +64,7 @@ func AtLeastOne(v any, excludes ...string) bool { } return false } + +func (token PVEAPIToken) ToString() string { + return fmt.Sprintf(`%s@%s!%s`, token.User, token.Realm, token.ID) +} diff --git a/app/localdb/localdb.go b/app/localdb/localdb.go index 3627484..00b1c46 100644 --- a/app/localdb/localdb.go +++ b/app/localdb/localdb.go @@ -2,10 +2,8 @@ package localdb import ( common "access-manager-api/app/common" - "encoding/json" "fmt" "net/http" - "os" ) type DB struct { @@ -15,39 +13,6 @@ type DB struct { var db *DB -func (db *DB) load(localDBPath string) error { - db.data = make(map[string]common.Pool) - db.path = localDBPath - - root, err := os.OpenRoot(".") - if err != nil { - return err - } - defer root.Close() - - content, err := root.ReadFile(localDBPath) - if err != nil { - return err - } - - err = json.Unmarshal(content, &db.data) - if err != nil { - return err - } - return nil -} - -func (db *DB) save() error { - localDBPath := db.path - // write to file with pretty print for readability reasons - json, err := json.MarshalIndent(db.data, "", "\t") - if err != nil { - return err - } - err = os.WriteFile(localDBPath, []byte(json), 0600) - return err -} - func NewClientFromCredentials(config common.LocalDBConfig, username common.Username, password string) (common.Backend, int, error) { if db != nil { return *db, http.StatusOK, nil diff --git a/app/localdb/utils.go b/app/localdb/utils.go index 1742a3f..6551022 100644 --- a/app/localdb/utils.go +++ b/app/localdb/utils.go @@ -1,6 +1,44 @@ package localdb -import "reflect" +import ( + common "access-manager-api/app/common" + "encoding/json" + "os" + "reflect" +) + +func (db *DB) load(localDBPath string) error { + db.data = make(map[string]common.Pool) + db.path = localDBPath + + root, err := os.OpenRoot(".") + if err != nil { + return err + } + defer root.Close() + + content, err := root.ReadFile(localDBPath) + if err != nil { + return err + } + + err = json.Unmarshal(content, &db.data) + if err != nil { + return err + } + return nil +} + +func (db *DB) save() error { + localDBPath := db.path + // write to file with pretty print for readability reasons + json, err := json.MarshalIndent(db.data, "", "\t") + if err != nil { + return err + } + err = os.WriteFile(localDBPath, []byte(json), 0600) + return err +} // MergeNonZero overwrites fields in dst with fields in src iff src is not a zero value. func MergeNonZero[T any](dst *T, src *T) { diff --git a/app/main.go b/app/main.go index 9bb6749..3f32728 100644 --- a/app/main.go +++ b/app/main.go @@ -48,7 +48,7 @@ func Run() { SetupAPISessionStore(router, &Config) // setup root api token - client, code, err := pve.NewClientFromAPIToken(Config.PVE) + client, code, err := pve.NewClientFromAPIToken(Config.PVE, Config.PVE.Token) if err != nil { log.Fatalf("error initializing pve root client: %d %s", code, err) } diff --git a/app/pve/pve.go b/app/pve/pve.go index 2b446ba..01e2b88 100644 --- a/app/pve/pve.go +++ b/app/pve/pve.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "slices" - "strings" common "access-manager-api/app/common" @@ -19,14 +18,6 @@ type ProxmoxClient struct { client *proxmox.Client } -func IsProxmoxNotFound(err error) bool { - if err != nil { - // for whatever reason proxmox returns 500 for user/group/pool not found - return proxmox.IsNotFound(err) || strings.Contains(err.Error(), "no such") || strings.Contains(err.Error(), "does not exist") - } - return false -} - // creates a new client binding with associated permissions func NewClientFromCredentials(config common.PVEConfig, username common.Username, password string) (common.Backend, int, error) { HTTPClient := http.Client{ @@ -52,16 +43,15 @@ func NewClientFromCredentials(config common.PVEConfig, username common.Username, } // creates a new client binding with associated permissions -func NewClientFromAPIToken(config common.PVEConfig) (common.Backend, int, error) { +func NewClientFromAPIToken(config common.PVEConfig, token common.PVEAPIToken) (common.Backend, int, error) { HTTPClient := http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{}, }, } - token := fmt.Sprintf(`%s@%s!%s`, config.Token.User, config.Token.Realm, config.Token.ID) client := proxmox.NewClient(config.URL, proxmox.WithHTTPClient(&HTTPClient), - proxmox.WithAPIToken(token, config.Token.UUID), + proxmox.WithAPIToken(token.ToString(), config.Token.UUID), ) // check that the user is authenticated because proxmox.NewClient does not return an error diff --git a/app/pve/utils.go b/app/pve/utils.go new file mode 100644 index 0000000..6004f97 --- /dev/null +++ b/app/pve/utils.go @@ -0,0 +1,15 @@ +package pve + +import ( + "strings" + + "github.com/luthermonson/go-proxmox" +) + +func IsProxmoxNotFound(err error) bool { + if err != nil { + // for whatever reason proxmox returns 500 for user/group/pool not found + return proxmox.IsNotFound(err) || strings.Contains(err.Error(), "no such") || strings.Contains(err.Error(), "does not exist") + } + return false +}