Requires authenticated Proxmox session with username/password configured.

refactor(proxmox): support for PAM authentication

- Added support for username and password authentication alongside existing token-based authentication.
- Updated validation rules to require either token or username/password for authentication.
- Modified the Init function to handle session creation based on the selected authentication method.
- Increased timeout duration for context in the Init function.
This commit is contained in:
yusing
2026-01-24 21:25:52 +08:00
parent 8f7ef5a015
commit 71e5a507ba

View File

@@ -17,8 +17,12 @@ import (
type Config struct {
URL string `json:"url" validate:"required,url"`
TokenID string `json:"token_id" validate:"required"`
Secret strutils.Redacted `json:"secret" validate:"required"`
Username string `json:"username" validate:"required_without=TokenID Secret"`
Password strutils.Redacted `json:"password" validate:"required_without=TokenID Secret"`
Realm string `json:"realm" validate:"required_without=TokenID Secret"`
TokenID string `json:"token_id" validate:"required_without=Username Password"`
Secret strutils.Redacted `json:"secret" validate:"required_without=Username Password"`
NoTLSVerify bool `json:"no_tls_verify" yaml:"no_tls_verify,omitempty"`
@@ -49,16 +53,33 @@ func (c *Config) Init(ctx context.Context) gperr.Error {
}
opts := []proxmox.Option{
proxmox.WithAPIToken(c.TokenID, c.Secret.String()),
proxmox.WithHTTPClient(&http.Client{
Transport: tr,
}),
}
useCredentials := false
if c.Username != "" && c.Password != "" {
opts = append(opts, proxmox.WithCredentials(&proxmox.Credentials{
Username: c.Username,
Password: c.Password.String(),
Realm: c.Realm,
}))
useCredentials = true
} else {
opts = append(opts, proxmox.WithAPIToken(c.TokenID, c.Secret.String()))
}
c.client = NewClient(c.URL, opts...)
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if useCredentials {
err := c.client.CreateSession(ctx)
if err != nil {
return gperr.New("failed to create session").With(err)
}
}
if err := c.client.UpdateClusterInfo(ctx); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return gperr.New("timeout fetching proxmox cluster info")