-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
180 lines (162 loc) · 4.84 KB
/
utils.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"log"
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var (
providedKeys = make(map[string]int64)
)
func resetBotState() {
resetUnsealState()
resetRekeyState()
rekeyActiveMutex.Lock()
defer rekeyActiveMutex.Unlock()
rekeyActive = false
vaultIsUnsealedLock.Lock()
defer vaultIsUnsealedLock.Unlock()
vaultIsUnsealed = false
}
func sendMessage(bot *tgbotapi.BotAPI, chatId int64, message string) {
msg := tgbotapi.NewMessage(chatId, message)
if _, err := bot.Send(msg); err != nil {
log.Printf("Error sending message to chat ID %d: %v", chatId, err)
}
}
func broadcastMessage(bot *tgbotapi.BotAPI, message string) {
for userId, userDets := range allowedUserIDs {
userName := ""
if userDets != nil {
userName = userDets.UserName
} else {
userName = strconv.Itoa(int(userId))
}
msg := tgbotapi.NewMessage(userId, message)
if _, err := bot.Send(msg); err != nil {
log.Printf("Failed to send message to user %s: %v", userName, err)
}
}
}
func getVaultStatusMessage() (string, error) {
res, err := checkVaultStatus()
if err != nil {
return fmt.Sprintf("Unable to get the status of the vault. Please try again later. Error: %+v", err), err
}
return fmt.Sprintf("Current status of the vault: Initialized is %t and Sealed is %t", res.Initialized, res.Sealed), nil
}
func verifyVaultUnseal(bot *tgbotapi.BotAPI, chatId int64) {
for i := 0; i < 5; i++ {
time.Sleep(10 * time.Second)
res, err := checkVaultStatus()
if err != nil {
log.Printf("Error checking Vault status: %v", err)
continue
}
if !res.Sealed {
vaultIsUnsealedLock.Lock()
vaultIsUnsealed = true
vaultIsUnsealedLock.Unlock()
sendMessage(bot, chatId, "Vault unsealed successfully verified.")
broadcastMessage(bot, "Vault unsealed successfully verified.")
return
}
}
sendMessage(bot, chatId, "Vault is still sealed. The required keys setting might be incorrect.")
broadcastMessage(bot, "Vault is still sealed. The required keys setting might be incorrect.")
}
func startRekeyTimer(bot *tgbotapi.BotAPI, chatId int64) {
time.Sleep(10 * time.Minute)
rekeyActiveMutex.Lock()
if rekeyActive {
rekeyActive = false
rekeyKeys = make(map[int64]struct{})
providedKeys = make(map[string]int64)
broadcastMessage(bot, "Rekey process timed out. Please start the process again if needed.")
setAllCommands(bot)
}
rekeyActiveMutex.Unlock()
}
func sendVaultStatusUpdate(bot *tgbotapi.BotAPI, statusChan <-chan string) {
for {
select {
case message := <-statusChan:
for id, t := range allowedUserIDs {
if t != nil && time.Since(t.LastUpdated) > 5*time.Minute {
t.LastUpdated = time.Now()
msg := tgbotapi.NewMessage(id, message)
if _, err := bot.Send(msg); err != nil {
log.Printf("Failed to send message to user ID %d: %v", t.UserName, err)
}
}
}
}
}
}
func pollVaultEverySec(statusChan chan string, bot *tgbotapi.BotAPI) {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
res, err := checkVaultStatus()
if err != nil {
statusChan <- fmt.Sprintf("Vault is down and will restart soon. Here is the error: %+v", err)
}
if res.Sealed == true {
if autoUnsealEnabled {
keys, err := loadUnsealKeys(bot) // Pass the bot parameter
if err != nil {
log.Printf("Error loading unseal keys: %v", err)
continue
}
err = unsealVault(keys)
if err != nil {
log.Printf("Error auto-unsealing Vault: %v", err)
} else {
log.Println("Vault auto-unsealed successfully.")
}
}
statusChan <- fmt.Sprintf("Vault Restarted. Initialised is %t and Sealed is %t", res.Initialized, res.Sealed)
}
}
}
}
func discardUnsealOperation() {
resetUnsealState()
log.Println("Discarded unseal operation.")
}
func discardRekeyOperation() error {
inProgress, err := isRekeyInProgress()
if err != nil {
return fmt.Errorf("Error checking rekey status: %v", err)
}
if inProgress {
err := cancelRekeyProcess()
if err != nil {
return fmt.Errorf("Error discarding rekey operation: %v", err)
}
}
resetRekeyState()
log.Println("Discarded rekey operation.")
return nil
}
func resetUnsealState() {
unsealKeys = make(map[int64]struct{})
providedKeys = make(map[string]int64)
if unsealTimer != nil {
unsealTimer.Stop()
unsealTimer = nil
}
log.Println("Unseal state reset.")
}
func resetRekeyState() {
rekeyKeys = make(map[int64]struct{})
providedKeys = make(map[string]int64)
if rekeyTimer != nil {
rekeyTimer.Stop()
rekeyTimer = nil
}
log.Println("Rekey state reset.")
}