Skip to content

Commit

Permalink
Merge pull request #3785 from uselagoon/fix-backup-handler
Browse files Browse the repository at this point in the history
fix: backup handler to ignore namespaces lagoon does not care about
  • Loading branch information
tobybellwood authored Aug 5, 2024
2 parents d61b7d4 + 34a1df8 commit d31f3ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
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

0 comments on commit d31f3ce

Please sign in to comment.