-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (162 loc) · 4.62 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/json"
"net/http"
"sync"
"os"
"github.com/gin-gonic/gin"
"github.com/glueops/vault-backup-validator/logger"
"github.com/glueops/vault-backup-validator/vault"
"github.com/hashicorp/vault/api"
)
func InitiateVaultSetup(c *gin.Context, restore_params vault.RestoreParams) (*api.Client, error){
// Install, Start the vault process with config
_, err := vault.InstallVault(restore_params.VaultVersion)
if(err != nil) {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return nil, err
}
_, err = vault.SetupVault()
if(err != nil) {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return nil, err
}
// Store the vault unseal key and token to use later
initialSecret := make(map[string]string)
jsonData, err := os.ReadFile("secrets.json")
if err != nil {
logger.Logger.Error("Error reading secrets.json file: "+err.Error())
return nil, err
}
json.Unmarshal(jsonData, &initialSecret)
initialVaultSecret := vault.VaultSecrets{
Keys: []string{initialSecret["key"]},
Token: initialSecret["token"],
}
vault_addr := "http://localhost:8200"
client, err := vault.NewVault(vault_addr,initialVaultSecret.Token)
if(err != nil){
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return nil, err
}
vaultObj := vault.Vault{Client: client}
// Unseal Vault
_, err = vaultObj.Unseal(&initialVaultSecret)
if(err != nil){
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return client, err
}
return client, nil
}
func VerifyBackup(client *api.Client, requestBody vault.RestoreParams, c *gin.Context) (bool, error){
vaultObj := vault.Vault{Client: client}
// Restore snapshot in vault
err := vault.RestoreSnapshotFromS3(client,requestBody)
if(err!=nil) {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return false, err
}
logger.Logger.Info("Vault restore successful..Proceeding to unseal..")
secrets, err := vaultObj.ParseSecrets(requestBody.SourceKeysURL)
if(err!=nil) {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return false, err
}
// Unseal vault again after restore, with the backup keys
_ , err = vaultObj.Unseal(secrets)
if(err!=nil) {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return false, err
}
logger.Logger.Info("Vault is unsealed..proceeding to verify the restore")
// Verify restored values in vault
verify_success, err := vault.VerifyRestore(vaultObj.Client, secrets, requestBody)
if(err!=nil) {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return verify_success, err
}
return verify_success, nil
}
var validationMutex sync.Mutex
var isValidationInProgress bool
func validateHandler(c *gin.Context) {
validationMutex.Lock()
defer validationMutex.Unlock()
if isValidationInProgress {
c.JSON(http.StatusServiceUnavailable, "API is busy validating. Please try again later.")
return
}
isValidationInProgress = true
defer func() {
isValidationInProgress = false
}()
// Cleanup vault incase of execution errors or in general
defer vault.CleanupVault()
// Unmarshal the input to vault.RestoreParams
var requestBody vault.RestoreParams
if err := c.BindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid JSON data, "+err.Error(),
})
return
}
// Validate inuput params
if err := vault.ValidateResotreParams(requestBody); err != nil{
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
client, err := InitiateVaultSetup(c,requestBody)
if(err != nil){
return
}
verify_success, err := VerifyBackup(client, requestBody, c)
if(err != nil){
return
}
if(verify_success){
logger.Logger.Info("Backup is verified successfully and it is valid")
response := gin.H{
"status": "success",
"message": "Backup is valid",
}
c.JSON(http.StatusOK, response)
}else{
logger.Logger.Info("Backup is verified and it is Invalid")
response := gin.H{
"status": "error",
"message": "Backup is invalid",
}
c.JSON(http.StatusBadRequest, response)
}
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
r.POST("/api/v1/validate",validateHandler)
return r
}
func main() {
logger.InitLogger()
r := setupRouter()
r.Run(":8080")
}