2024-05-09 22:56:16 -04:00
package ssh
2024-04-08 21:00:40 -04:00
import (
"errors"
)
var (
// ErrEndpointWithoutSSHUsername is the error with which Gatus will panic if an endpoint with SSH monitoring is configured without a user.
ErrEndpointWithoutSSHUsername = errors . New ( "you must specify a username for each SSH endpoint" )
2025-11-20 03:06:36 +05:30
// ErrEndpointWithoutSSHAuth is the error with which Gatus will panic if an endpoint with SSH monitoring is configured without a password or private key.
ErrEndpointWithoutSSHAuth = errors . New ( "you must specify a password or private-key for each SSH endpoint" )
2024-04-08 21:00:40 -04:00
)
2024-05-09 22:56:16 -04:00
type Config struct {
2025-11-20 03:06:36 +05:30
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
PrivateKey string `yaml:"private-key,omitempty"`
2024-04-08 21:00:40 -04:00
}
2024-05-09 22:56:16 -04:00
// Validate the SSH configuration
func ( cfg * Config ) Validate () error {
2025-11-20 03:06:36 +05:30
// If there's no username, password, or private key, this endpoint can still check the SSH banner, so the endpoint is still valid
if len ( cfg . Username ) == 0 && len ( cfg . Password ) == 0 && len ( cfg . PrivateKey ) == 0 {
2025-01-19 23:22:41 +02:00
return nil
}
2025-11-20 03:06:36 +05:30
// If any authentication method is provided (password or private key), a username is required
2024-05-09 22:56:16 -04:00
if len ( cfg . Username ) == 0 {
2024-04-08 21:00:40 -04:00
return ErrEndpointWithoutSSHUsername
}
2025-11-20 03:06:36 +05:30
// If a username is provided, require at least a password or a private key
if len ( cfg . Password ) == 0 && len ( cfg . PrivateKey ) == 0 {
return ErrEndpointWithoutSSHAuth
2024-04-08 21:00:40 -04:00
}
return nil
}