Skip to content

Commit

Permalink
Refactor: refactored authenticate field to infer from username and pa…
Browse files Browse the repository at this point in the history
…ssword insted of specifying it inside config.
  • Loading branch information
ImTheCurse committed Jan 9, 2025
1 parent 2112a18 commit f4fe3e5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,6 @@ endpoints:
ssh:
username: "username"
password: "password"
authenticate: true
body: |
{
"command": "uptime"
Expand All @@ -2057,7 +2056,22 @@ endpoints:
- "[STATUS] == 0"
```

you can also use no authentication to monitor the endpoint by setting the `authenticate` field to `false`, but will it only check for connection status.
you can also use no authentication to monitor the endpoint by not specifying the username
and password fields.

```yaml
endpoints:
- name: ssh-example
url: "ssh://example.com:22" # port is optional. Default is 22.
ssh:
username: ""
password: ""
interval: 1m
conditions:
- "[CONNECTED] == true"
- "[STATUS] == 0"
```

The following placeholders are supported for endpoints of type SSH:
- `[CONNECTED]` resolves to `true` if the SSH connection was successful, `false` otherwise
Expand Down
2 changes: 1 addition & 1 deletion config/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (e *Endpoint) call(result *Result) {
}
result.Duration = time.Since(startTime)
} else if endpointType == TypeSSH {
if !e.SSHConfig.Authenticate {
if len(e.SSHConfig.Username) == 0 && len(e.SSHConfig.Password) == 0 {
result.Connected, result.HTTPStatus, err =
client.CheckSSHBanner(strings.TrimPrefix(e.URL, "ssh://"), e.ClientConfig)

Expand Down
5 changes: 2 additions & 3 deletions config/endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,8 @@ func TestEndpoint_ValidateAndSetDefaultsWithSSH(t *testing.T) {
Name: "ssh-test",
URL: "https://example.com",
SSHConfig: &ssh.Config{
Username: scenario.username,
Password: scenario.password,
Authenticate: true,
Username: scenario.username,
Password: scenario.password,
},
Conditions: []Condition{Condition("[STATUS] == 0")},
}
Expand Down
3 changes: 2 additions & 1 deletion config/endpoint/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ type Config struct {

// Validate the SSH configuration
func (cfg *Config) Validate() error {
if cfg.Authenticate == false {
if len(cfg.Username) == 0 && len(cfg.Password) == 0 {
return nil
}

if len(cfg.Username) == 0 {
return ErrEndpointWithoutSSHUsername
}
Expand Down
9 changes: 4 additions & 5 deletions config/endpoint/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
)

func TestSSH_validate(t *testing.T) {
cfg := &Config{Authenticate: true}
if err := cfg.Validate(); err == nil {
t.Error("expected an error")
} else if !errors.Is(err, ErrEndpointWithoutSSHUsername) {
t.Errorf("expected error to be '%v', got '%v'", ErrEndpointWithoutSSHUsername, err)
cfg := &Config{}
if err := cfg.Validate(); err != nil {
t.Error("didn't expect an error")
}

cfg.Username = "username"
if err := cfg.Validate(); err == nil {
t.Error("expected an error")
Expand Down

0 comments on commit f4fe3e5

Please sign in to comment.