minor refactoring
tronnet/workflows: Static Analysis -- Golang / static-go (push) Successful in 13s

This commit is contained in:
alu
2026-08-31 20:32:45 +00:00
parent 8b9c8c14d5
commit 30a4264bb1
8 changed files with 80 additions and 63 deletions
+1 -6
View File
@@ -8,12 +8,7 @@ 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"`
Token PVEAPIToken `json:"token"`
PAASClientRole string `json:"paas-client-role"`
}
+5 -6
View File
@@ -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"`
}
+15
View File
@@ -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)
}
-35
View File
@@ -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
+39 -1
View File
@@ -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) {
+1 -1
View File
@@ -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)
}
+2 -12
View File
@@ -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
+15
View File
@@ -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
}