-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcleanup.go
More file actions
120 lines (98 loc) · 2.96 KB
/
Copy pathcleanup.go
File metadata and controls
120 lines (98 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bytes"
"fmt"
"net/url"
"strings"
"time"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/ssh"
)
var sshConfig *ssh.ClientConfig
var sshHost string
func createSSHConfig(unifiHost, sshUser, sshPassword string) (*ssh.ClientConfig, error) {
host, err := extractHost(unifiHost)
if err != nil {
return nil, fmt.Errorf("failed to extract host from URL: %w", err)
}
sshHost = host
return &ssh.ClientConfig{
User: sshUser,
Auth: []ssh.AuthMethod{
ssh.Password(sshPassword),
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
answers := make([]string, len(questions))
for i := range questions {
answers[i] = sshPassword
}
return answers, nil
}),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 10 * time.Second,
}, nil
}
func testSSHConnection() error {
client, err := ssh.Dial("tcp", sshHost+":22", sshConfig)
if err != nil {
return fmt.Errorf("failed to establish SSH connection to %s: %w", sshHost, err)
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("failed to create SSH session: %w", err)
}
defer session.Close()
return nil
}
func cleanupBouncerAuditEntries(lookbackMinutes int) error {
lookbackTime := time.Now().Add(-time.Duration(lookbackMinutes) * time.Minute).UnixMilli()
mongoCmd := fmt.Sprintf(
`mongo ace --port 27117 --quiet --eval 'db.admin_activity_log.updateMany({ "meta.display_property_value": { $regex: "^cs-unifi-bouncer-" }, time: { $gt: %d } }, { $set: { updates: [ { property_path: "description", new_value: "Updated from bouncer" } ] } })'`,
lookbackTime,
)
log.Debug().Msgf("Executing cleanup command on host %s", sshHost)
client, err := ssh.Dial("tcp", sshHost+":22", sshConfig)
if err != nil {
return fmt.Errorf("failed to establish SSH connection to %s: %w", sshHost, err)
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("failed to create SSH session: %w", err)
}
defer session.Close()
var stdout, stderr bytes.Buffer
session.Stdout = &stdout
session.Stderr = &stderr
if err := session.Run(mongoCmd); err != nil {
log.Warn().
Str("stdout", stdout.String()).
Str("stderr", stderr.String()).
Msg("Mongo cleanup command output")
return fmt.Errorf("mongo cleanup failed: %w", err)
}
output := strings.TrimSpace(stdout.String())
if output != "" {
log.Info().Msgf("Audit log cleanup completed: %s", output)
} else {
log.Debug().Msg("Audit log cleanup completed (no output)")
}
return nil
}
func extractHost(rawURL string) (string, error) {
if !strings.Contains(rawURL, "://") {
rawURL = "https://" + rawURL
}
u, err := url.Parse(rawURL)
if err != nil {
return "", err
}
host := u.Host
if colonIdx := strings.LastIndex(host, ":"); colonIdx != -1 {
if !strings.Contains(host, "]") || strings.LastIndex(host, "]") < colonIdx {
host = host[:colonIdx]
}
}
return host, nil
}