2023-01-11 21:40:48 +01:00
|
|
|
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-02-02 21:07:25 +01:00
|
|
|
"errors"
|
2023-01-11 21:40:48 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
|
2024-02-16 15:35:42 +01:00
|
|
|
"github.com/offen/docker-volume-backup/internal/errwrap"
|
2023-01-11 21:40:48 +01:00
|
|
|
"github.com/offen/docker-volume-backup/internal/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type azureBlobStorage struct {
|
|
|
|
*storage.StorageBackend
|
|
|
|
client *azblob.Client
|
|
|
|
containerName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains values that define the configuration of an Azure Blob Storage.
|
|
|
|
type Config struct {
|
|
|
|
AccountName string
|
|
|
|
ContainerName string
|
|
|
|
PrimaryAccountKey string
|
2024-03-08 20:23:30 +01:00
|
|
|
ConnectionString string
|
2023-01-11 21:40:48 +01:00
|
|
|
Endpoint string
|
|
|
|
RemotePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStorageBackend creates and initializes a new Azure Blob Storage backend.
|
|
|
|
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
|
2024-03-08 20:23:30 +01:00
|
|
|
if opts.PrimaryAccountKey != "" && opts.ConnectionString != "" {
|
|
|
|
return nil, errwrap.Wrap(nil, "using primary account key and connection string are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:40:48 +01:00
|
|
|
endpointTemplate, err := template.New("endpoint").Parse(opts.Endpoint)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error parsing endpoint template")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
var ep bytes.Buffer
|
|
|
|
if err := endpointTemplate.Execute(&ep, opts); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error executing endpoint template")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
normalizedEndpoint := fmt.Sprintf("%s/", strings.TrimSuffix(ep.String(), "/"))
|
|
|
|
|
|
|
|
var client *azblob.Client
|
|
|
|
if opts.PrimaryAccountKey != "" {
|
|
|
|
cred, err := azblob.NewSharedKeyCredential(opts.AccountName, opts.PrimaryAccountKey)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error creating shared key Azure credential")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err = azblob.NewClientWithSharedKeyCredential(normalizedEndpoint, cred, nil)
|
|
|
|
if err != nil {
|
2024-03-08 20:23:30 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error creating azure client from primary account key")
|
|
|
|
}
|
|
|
|
} else if opts.ConnectionString != "" {
|
|
|
|
client, err = azblob.NewClientFromConnectionString(opts.ConnectionString, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrap(err, "error creating azure client from connection string")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cred, err := azidentity.NewManagedIdentityCredential(nil)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error creating managed identity credential")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
client, err = azblob.NewClient(normalizedEndpoint, cred, nil)
|
|
|
|
if err != nil {
|
2024-03-08 20:23:30 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error creating azure client from managed identity")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := azureBlobStorage{
|
|
|
|
client: client,
|
|
|
|
containerName: opts.ContainerName,
|
|
|
|
StorageBackend: &storage.StorageBackend{
|
|
|
|
DestinationPath: opts.RemotePath,
|
|
|
|
Log: logFunc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &storage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the storage backend
|
|
|
|
func (b *azureBlobStorage) Name() string {
|
|
|
|
return "Azure"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies the given file to the storage backend.
|
|
|
|
func (b *azureBlobStorage) Copy(file string) error {
|
|
|
|
fileReader, err := os.Open(file)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error opening file %s", file))
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
_, err = b.client.UploadStream(
|
|
|
|
context.Background(),
|
|
|
|
b.containerName,
|
|
|
|
filepath.Join(b.DestinationPath, filepath.Base(file)),
|
|
|
|
fileReader,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error uploading file %s", file))
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune rotates away backups according to the configuration and provided
|
|
|
|
// deadline for the Azure Blob storage backend.
|
|
|
|
func (b *azureBlobStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
|
|
|
lookupPrefix := filepath.Join(b.DestinationPath, pruningPrefix)
|
|
|
|
pager := b.client.NewListBlobsFlatPager(b.containerName, &container.ListBlobsFlatOptions{
|
|
|
|
Prefix: &lookupPrefix,
|
|
|
|
})
|
|
|
|
var matches []string
|
|
|
|
var totalCount uint
|
|
|
|
for pager.More() {
|
|
|
|
resp, err := pager.NextPage(context.Background())
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error paging over blobs")
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
for _, v := range resp.Segment.BlobItems {
|
|
|
|
totalCount++
|
|
|
|
if v.Properties.LastModified.Before(deadline) {
|
|
|
|
matches = append(matches, *v.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:19:44 +01:00
|
|
|
stats := &storage.PruneStats{
|
2023-01-11 21:40:48 +01:00
|
|
|
Total: totalCount,
|
|
|
|
Pruned: uint(len(matches)),
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:19:44 +01:00
|
|
|
pruneErr := b.DoPrune(b.Name(), len(matches), int(totalCount), deadline, func() error {
|
2023-01-11 21:40:48 +01:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(matches))
|
2023-02-02 21:07:25 +01:00
|
|
|
var errs []error
|
2023-01-11 21:40:48 +01:00
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
name := match
|
|
|
|
go func() {
|
|
|
|
_, err := b.client.DeleteBlob(context.Background(), b.containerName, name, nil)
|
|
|
|
if err != nil {
|
2023-02-02 21:07:25 +01:00
|
|
|
errs = append(errs, err)
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2023-02-02 21:07:25 +01:00
|
|
|
if len(errs) != 0 {
|
|
|
|
return errors.Join(errs...)
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|
|
|
|
return nil
|
2023-11-04 12:19:44 +01:00
|
|
|
})
|
2023-01-11 21:40:48 +01:00
|
|
|
|
2023-11-04 12:19:44 +01:00
|
|
|
return stats, pruneErr
|
2023-01-11 21:40:48 +01:00
|
|
|
}
|