Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backup handler to ignore namespaces lagoon does not care about #3785

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/backup-handler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/uuid v1.3.0
github.com/isayme/go-amqp-reconnect v0.0.0-20210303120416-fc811b0bcda2
github.com/streadway/amqp v1.0.0
github.com/uselagoon/machinery v0.0.22
github.com/uselagoon/machinery v0.0.25
)

require (
Expand Down
6 changes: 4 additions & 2 deletions services/backup-handler/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/uselagoon/machinery v0.0.22 h1:4FVJRLS8he2NAZYMIJXQqVb25jQ03tF8dNhQbrBMX3g=
github.com/uselagoon/machinery v0.0.22/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY=
github.com/uselagoon/machinery v0.0.25-0.20240731103619-a4140d3a8941 h1:clRmB6HIdP9KQtviEQjRTJJYNEWqudBZE3diNFAadnU=
github.com/uselagoon/machinery v0.0.25-0.20240731103619-a4140d3a8941/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY=
github.com/uselagoon/machinery v0.0.25 h1:Xaf7f8c+U16HYQBqoChCv37dCBdH+aUgOkuHG5YXLCo=
github.com/uselagoon/machinery v0.0.25/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY=
29 changes: 13 additions & 16 deletions services/backup-handler/internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,6 @@ func (b *BackupHandler) WebhookHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("unable to decode json data from webhook, error is %s:", err.Error())
} else {
// get backups from the API

token, err := jwt.GenerateAdminToken(b.Endpoint.TokenSigningKey, b.Endpoint.JWTAudience, b.Endpoint.JWTSubject, b.Endpoint.JWTIssuer, time.Now().Unix(), 60)
if err != nil {
// the token wasn't generated
log.Printf("unable to generate token: %v", err)
return
}
l := lclient.New(b.Endpoint.Endpoint, b.Endpoint.JWTSubject, b.Endpoint.Version, &token, false)
ctx := context.Background()
apiEnv, err := lagoon.GetEnvironmentByNamespace(ctx, backupData.Name, l)
if err != nil {
log.Printf("unable to connect to the api, error is %s:", err.Error())
return
}

// handle restores
if backupData.RestoreLocation != "" {
singleBackup := Webhook{
Expand All @@ -169,11 +154,23 @@ func (b *BackupHandler) WebhookHandler(w http.ResponseWriter, r *http.Request) {
// else handle snapshots
} else if backupData.Snapshots != nil {
// use the name from the webhook to get the environment in the api
backupsEnv, err := lagoon.GetBackupsForEnvironmentByName(ctx, apiEnv.Name, apiEnv.ProjectID, l)
ctx := context.Background()
token, err := jwt.GenerateAdminToken(b.Endpoint.TokenSigningKey, b.Endpoint.JWTAudience, b.Endpoint.JWTSubject, b.Endpoint.JWTIssuer, time.Now().Unix(), 60)
if err != nil {
// the token wasn't generated
log.Printf("unable to generate token: %v", err)
return
}
l := lclient.New(b.Endpoint.Endpoint, b.Endpoint.JWTSubject, b.Endpoint.Version, &token, false)
backupsEnv, err := lagoon.GetBackupsByEnvironmentNamespace(ctx, backupData.Name, l)
if err != nil {
log.Printf("unable to connect to the api, error is %s:", err.Error())
return
}
if backupsEnv.OpenshiftProjectName != backupData.Name {
log.Printf("unable to handle backups, returned environment does not match namespace for backups %s", backupData.Name)
return
}
// remove backups that no longer exists from the api
for index, backup := range backupsEnv.Backups {
// check that the backup in the api is not in the webhook payload
Expand Down