This commit is contained in:
@@ -7,14 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PVEConfig struct {
|
type PVEConfig struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Token struct {
|
Token PVEAPIToken `json:"token"`
|
||||||
User string `json:"user"`
|
PAASClientRole string `json:"paas-client-role"`
|
||||||
Realm string `json:"realm"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
UUID string `json:"uuid"`
|
|
||||||
} `json:"token"`
|
|
||||||
PAASClientRole string `json:"paas-client-role"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type LDAPConfig struct {
|
type LDAPConfig struct {
|
||||||
|
|||||||
+5
-6
@@ -32,10 +32,9 @@ type Group = paas.Group
|
|||||||
type Username = paas.Username
|
type Username = paas.Username
|
||||||
type User = paas.User
|
type User = paas.User
|
||||||
|
|
||||||
func ParseGroupname(groupname string) (Groupname, error) {
|
type PVEAPIToken struct {
|
||||||
return paas.ParseGroupname(groupname)
|
User string `json:"user"`
|
||||||
}
|
Realm string `json:"realm"`
|
||||||
|
ID string `json:"id"`
|
||||||
func ParseUsername(username string) (Username, error) {
|
UUID string `json:"uuid"`
|
||||||
return paas.ParseUsername(username)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,20 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"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.
|
// RequireAll ensures that EVERY non-excluded exported field in the struct is non-zero.
|
||||||
func RequireAll(v any, excludes ...string) bool {
|
func RequireAll(v any, excludes ...string) bool {
|
||||||
val := reflect.ValueOf(v)
|
val := reflect.ValueOf(v)
|
||||||
@@ -53,3 +64,7 @@ func AtLeastOne(v any, excludes ...string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (token PVEAPIToken) ToString() string {
|
||||||
|
return fmt.Sprintf(`%s@%s!%s`, token.User, token.Realm, token.ID)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,10 +2,8 @@ package localdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
common "access-manager-api/app/common"
|
common "access-manager-api/app/common"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
@@ -15,39 +13,6 @@ type DB struct {
|
|||||||
|
|
||||||
var db *DB
|
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) {
|
func NewClientFromCredentials(config common.LocalDBConfig, username common.Username, password string) (common.Backend, int, error) {
|
||||||
if db != nil {
|
if db != nil {
|
||||||
return *db, http.StatusOK, nil
|
return *db, http.StatusOK, nil
|
||||||
|
|||||||
+39
-1
@@ -1,6 +1,44 @@
|
|||||||
package localdb
|
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.
|
// MergeNonZero overwrites fields in dst with fields in src iff src is not a zero value.
|
||||||
func MergeNonZero[T any](dst *T, src *T) {
|
func MergeNonZero[T any](dst *T, src *T) {
|
||||||
|
|||||||
+1
-1
@@ -48,7 +48,7 @@ func Run() {
|
|||||||
SetupAPISessionStore(router, &Config)
|
SetupAPISessionStore(router, &Config)
|
||||||
|
|
||||||
// setup root api token
|
// setup root api token
|
||||||
client, code, err := pve.NewClientFromAPIToken(Config.PVE)
|
client, code, err := pve.NewClientFromAPIToken(Config.PVE, Config.PVE.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error initializing pve root client: %d %s", code, err)
|
log.Fatalf("error initializing pve root client: %d %s", code, err)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-12
@@ -7,7 +7,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
|
||||||
|
|
||||||
common "access-manager-api/app/common"
|
common "access-manager-api/app/common"
|
||||||
|
|
||||||
@@ -19,14 +18,6 @@ type ProxmoxClient struct {
|
|||||||
client *proxmox.Client
|
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
|
// creates a new client binding with associated permissions
|
||||||
func NewClientFromCredentials(config common.PVEConfig, username common.Username, password string) (common.Backend, int, error) {
|
func NewClientFromCredentials(config common.PVEConfig, username common.Username, password string) (common.Backend, int, error) {
|
||||||
HTTPClient := http.Client{
|
HTTPClient := http.Client{
|
||||||
@@ -52,16 +43,15 @@ func NewClientFromCredentials(config common.PVEConfig, username common.Username,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// creates a new client binding with associated permissions
|
// 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{
|
HTTPClient := http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{},
|
TLSClientConfig: &tls.Config{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
token := fmt.Sprintf(`%s@%s!%s`, config.Token.User, config.Token.Realm, config.Token.ID)
|
|
||||||
client := proxmox.NewClient(config.URL,
|
client := proxmox.NewClient(config.URL,
|
||||||
proxmox.WithHTTPClient(&HTTPClient),
|
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
|
// check that the user is authenticated because proxmox.NewClient does not return an error
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user