Skip to content

Commit 5fa55e6

Browse files
authored
add sudo validation (#72)
1 parent b6718eb commit 5fa55e6

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

v1/image.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ func ValidateInstanceImage(ctx context.Context, instance Instance, privateKey st
6363
return err
6464
}
6565

66-
fmt.Printf("Instance image validation passed for %s: architecture=%s, os=%s, home=%s, systemd=%s\n",
67-
instance.CloudID, arch, osVersion, homeDir, systemdStatus)
66+
sudoStatus, err := validateSudoAccess(ctx, sshClient)
67+
if err != nil {
68+
return err
69+
}
70+
71+
fmt.Printf("Instance image validation passed for %s: architecture=%s, os=%s, home=%s, systemd=%s, sudo=%s\n",
72+
instance.CloudID, arch, osVersion, homeDir, systemdStatus, sudoStatus)
6873

6974
return nil
7075
}
@@ -155,3 +160,15 @@ func validateSystemd(ctx context.Context, sshClient *ssh.Client) (string, error)
155160

156161
return "", fmt.Errorf("expected systemd to be running or degraded, got: %s", systemdStatus)
157162
}
163+
164+
func validateSudoAccess(ctx context.Context, sshClient *ssh.Client) (string, error) {
165+
// Test if sudo is available and can run without a password
166+
// -n flag means non-interactive (no password prompt)
167+
stdout, stderr, err := sshClient.RunCommand(ctx, "sudo -n true")
168+
if err != nil {
169+
return "", fmt.Errorf("failed to verify sudo access: %w, stdout: %s, stderr: %s", err, stdout, stderr)
170+
}
171+
172+
// If the command succeeded, we have passwordless sudo access
173+
return "available", nil
174+
}

0 commit comments

Comments
 (0)