From 6e398baca7f2e2e4f7214a1dde0bf35249ac5832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Jer=C3=A9z=20Gir=C3=B3n?= Date: Wed, 24 Jul 2024 09:22:02 -0600 Subject: [PATCH] Add GetExecutionDownloadLink function to Service --- .../executions/get_execution_download_link.go | 33 +++++++++++++++++++ .../get_execution_download_link.sql | 12 +++++++ 2 files changed, 45 insertions(+) create mode 100644 internal/service/executions/get_execution_download_link.go create mode 100644 internal/service/executions/get_execution_download_link.sql diff --git a/internal/service/executions/get_execution_download_link.go b/internal/service/executions/get_execution_download_link.go new file mode 100644 index 0000000..87333d2 --- /dev/null +++ b/internal/service/executions/get_execution_download_link.go @@ -0,0 +1,33 @@ +package executions + +import ( + "context" + "fmt" + "time" + + "github.com/eduardolat/pgbackweb/internal/database/dbgen" + "github.com/google/uuid" +) + +func (s *Service) GetExecutionDownloadLink( + ctx context.Context, executionID uuid.UUID, +) (string, error) { + data, err := s.dbgen.ExecutionsServiceGetDownloadLinkData( + ctx, dbgen.ExecutionsServiceGetDownloadLinkDataParams{ + ExecutionID: executionID, + DecryptionKey: *s.env.PBW_ENCRYPTION_KEY, + }, + ) + if err != nil { + return "", err + } + + if !data.Path.Valid { + return "", fmt.Errorf("execution has no file associated") + } + + return s.ints.S3Client.GetDownloadLink( + data.DecryptedAccessKey, data.DecryptedSecretKey, data.Region, + data.Endpoint, data.BucketName, data.Path.String, time.Hour*12, + ) +} diff --git a/internal/service/executions/get_execution_download_link.sql b/internal/service/executions/get_execution_download_link.sql new file mode 100644 index 0000000..360dfc1 --- /dev/null +++ b/internal/service/executions/get_execution_download_link.sql @@ -0,0 +1,12 @@ +-- name: ExecutionsServiceGetDownloadLinkData :one +SELECT + executions.path AS path, + destinations.bucket_name AS bucket_name, + destinations.region AS region, + destinations.endpoint AS endpoint, + pgp_sym_decrypt(destinations.access_key, sqlc.arg('decryption_key')::TEXT) AS decrypted_access_key, + pgp_sym_decrypt(destinations.secret_key, sqlc.arg('decryption_key')::TEXT) AS decrypted_secret_key +FROM executions +JOIN backups ON backups.id = executions.backup_id +JOIN destinations ON destinations.id = backups.destination_id +WHERE executions.id = @execution_id;