Skip to content

Commit

Permalink
Merge branch 'useGoBackupLib' into APPS-1237-backup-service-secret-agent
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/shared/backup_go.go
#	pkg/shared/restore_go.go
  • Loading branch information
filkeith committed Jul 30, 2024
2 parents 3fc622c + 51b35cd commit a9cf9b3
Show file tree
Hide file tree
Showing 28 changed files with 203 additions and 126 deletions.
15 changes: 14 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ run:
linters:
disable-all: true
enable:
- gci
- bodyclose
- unused
- dogsled
- dupl
- errcheck
- exportloopref
Expand All @@ -19,21 +23,30 @@ linters:
- gocyclo
- gofmt
- goimports
- gocyclo
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- prealloc
- promlinter
- nolintlint
- nakedret
- prealloc # pre-allocate slices with define size if the slice size is known in advance
- predeclared
- revive
- sloglint
- staticcheck
- stylecheck
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- whitespace
- lll

issues:
exclude-rules:
Expand Down
2 changes: 1 addition & 1 deletion cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (

// run parses the CLI parameters and executes backup.
//
//nolint:funlen

func run() int {
var (
configFile string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
require (
github.com/aerospike/aerospike-client-go/v7 v7.6.0
github.com/aerospike/aerospike-management-lib v1.4.0
github.com/aerospike/backup-go v0.0.0-20240728070104-6c6b315f52d8
github.com/aerospike/backup-go v0.0.0-20240730103930-5ef82c03f86f
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ github.com/aerospike/backup-go v0.0.0-20240728070104-6c6b315f52d8 h1:novGz78Gq6E
github.com/aerospike/backup-go v0.0.0-20240728070104-6c6b315f52d8/go.mod h1:WfFo03sY4w5qu3IALjqNQaPZVe+0p7GqYPG+qifkm+8=
github.com/aerospike/backup-go v0.0.0-20240729112203-dee1dc63b0c9 h1:fSXsqkD5XWCdDn8NfKnQVevYHTMgxtQ8c4twYAo+2Go=
github.com/aerospike/backup-go v0.0.0-20240729112203-dee1dc63b0c9/go.mod h1:WfFo03sY4w5qu3IALjqNQaPZVe+0p7GqYPG+qifkm+8=
github.com/aerospike/backup-go v0.0.0-20240730084743-64d81fa5d7a4 h1:YvY6Gyedc3h6qX4tKaFaERqK0CjoHTMwgTvnffWig14=
github.com/aerospike/backup-go v0.0.0-20240730084743-64d81fa5d7a4/go.mod h1:WfFo03sY4w5qu3IALjqNQaPZVe+0p7GqYPG+qifkm+8=
github.com/aerospike/backup-go v0.0.0-20240730103930-5ef82c03f86f h1:7gVhto19JTcDKBG2cMnEP3I+ktr+05ny32NjtaLvBH4=
github.com/aerospike/backup-go v0.0.0-20240730103930-5ef82c03f86f/go.mod h1:WfFo03sY4w5qu3IALjqNQaPZVe+0p7GqYPG+qifkm+8=
github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2 v1.27.0 h1:7bZWKoXhzI+mMR/HjdMx8ZCC5+6fY0lS5tr0bbgiLlo=
Expand Down
2 changes: 0 additions & 2 deletions internal/server/backup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func readBackupsLogic(routines map[string]*model.BackupRoutine,
backends service.BackendsHolder,
timeBounds *model.TimeBounds,
isFullBackup bool) (map[string][]model.BackupDetails, error) {

result := make(map[string][]model.BackupDetails)
for routine := range routines {
reader, _ := backends.GetReader(routine)
Expand All @@ -149,7 +148,6 @@ func readBackupsLogic(routines map[string]*model.BackupRoutine,

func backupsReadFunction(
backend service.BackupListReader, fullBackup bool) func(*model.TimeBounds) ([]model.BackupDetails, error) {

if fullBackup {
return backend.FullBackupList
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/config_cluster_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (ws *HTTPServer) readAerospikeCluster(w http.ResponseWriter, r *http.Reques
// @Success 200
// @Failure 400 {string} string
//
//nolint:dupl

func (ws *HTTPServer) updateAerospikeCluster(w http.ResponseWriter, r *http.Request) {
var updatedCluster model.AerospikeCluster
err := json.NewDecoder(r.Body).Decode(&updatedCluster)
Expand Down
4 changes: 2 additions & 2 deletions internal/server/config_handlers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:dupl
package server

import (
Expand Down Expand Up @@ -69,10 +68,11 @@ func (ws *HTTPServer) updateConfig(w http.ResponseWriter, r *http.Request) {
// @Success 200
// @Failure 400 {string} string
func (ws *HTTPServer) applyConfig(w http.ResponseWriter, _ *http.Request) {
err := service.ApplyNewConfig(ws.scheduler, ws.config, ws.backupBackends)
handlers, err := service.ApplyNewConfig(ws.scheduler, ws.config, ws.backupBackends)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ws.handlerHolder = handlers
w.WriteHeader(http.StatusOK)
}
4 changes: 3 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/netip"
"strings"
"time"

"github.com/aerospike/backup/pkg/model"
"github.com/aerospike/backup/pkg/service"
Expand Down Expand Up @@ -104,7 +105,8 @@ func NewHTTPServer(
return &HTTPServer{
config: config,
server: &http.Server{
Addr: addr,
Addr: addr,
ReadHeaderTimeout: 5 * time.Second,
},
rateLimiter: rateLimiter,
whiteList: newIPWhiteList(serverConfig.GetRateOrDefault().GetWhiteListOrDefault()),
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/aerospike_cluster.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// nolint:lll
//nolint:lll
package model

import (
Expand Down Expand Up @@ -174,7 +174,6 @@ func initTLS(t *TLS, clusterLabel *string) *tls.Config {
var clientPool []tls.Certificate
if (t.Certfile != nil && len(*t.Certfile) > 0) ||
t.Keyfile != nil && len(*t.Keyfile) > 0 {

// Read cert file
certFileBytes, err := readFromFile(*t.Certfile)
if err != nil {
Expand Down Expand Up @@ -231,6 +230,7 @@ func initTLS(t *TLS, clusterLabel *string) *tls.Config {
RootCAs: serverPool,
InsecureSkipVerify: false,
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
}

return tlsConfig
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Config) Validate() error {
return err
}

if err := c.ServiceConfig.Logger.Validate(); err != nil { //nolint:revive
if err := c.ServiceConfig.Logger.Validate(); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/restore_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *RestoreRequest) Validate() error {
if err := r.SourceStorage.Validate(); err != nil {
return err
}
if err := r.Policy.Validate(); err != nil { //nolint:revive
if err := r.Policy.Validate(); err != nil {
return err
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions pkg/service/backup_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (h *BackupHandler) startFullBackupForAllNamespaces(
backupFolder := getFullPath(h.backend.fullBackupsPath, h.backupFullPolicy, namespace, upperBound)
backupPath := h.backend.wrapWithPrefix(backupFolder)
handler, err := backupService.BackupRun(ctx, h.backupRoutine, h.backupFullPolicy, client,
h.storage, h.secretAgent, timebounds, &namespace, backupPath)
h.storage, h.secretAgent, timebounds, namespace, backupPath)
if err != nil {
backupFailureCounter.Inc()
return fmt.Errorf("could not start backup of namespace %s, routine %s: %w", namespace, h.routineName, err)
Expand Down Expand Up @@ -259,7 +259,6 @@ func (h *BackupHandler) runIncrementalBackup(ctx context.Context, now time.Time)

func (h *BackupHandler) startIncrementalBackupForAllNamespaces(
ctx context.Context, client *aerospike.Client, upperBound time.Time) {

timebounds := model.NewTimeBoundsFrom(h.state.LastRun())
if h.backupFullPolicy.IsSealed() {
timebounds.ToTime = &upperBound
Expand All @@ -270,7 +269,7 @@ func (h *BackupHandler) startIncrementalBackupForAllNamespaces(
backupFolder := getIncrementalPath(h.backend.incrementalBackupsPath, namespace, upperBound)
backupPath := h.backend.wrapWithPrefix(backupFolder)
handler, err := backupService.BackupRun(ctx,
h.backupRoutine, h.backupIncrPolicy, client, h.storage, h.secretAgent, *timebounds, &namespace, backupPath)
h.backupRoutine, h.backupIncrPolicy, client, h.storage, h.secretAgent, *timebounds, namespace, backupPath)
if err != nil {
incrBackupFailureCounter.Inc()
slog.Warn("could not start backup", "namespace", namespace, "routine", h.routineName, "err", err)
Expand Down
13 changes: 10 additions & 3 deletions pkg/service/backup_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ func NewAdHocFullBackupJobForRoutine(name string) *quartz.JobDetail {
return quartz.NewJobDetail(job.Job(), jobKey)
}

func ApplyNewConfig(scheduler quartz.Scheduler, config *model.Config, backends BackendsHolder) error {
func ApplyNewConfig(scheduler quartz.Scheduler, config *model.Config, backends BackendsHolder,
) (BackupHandlerHolder, error) {
err := scheduler.Clear()
if err != nil {
return err
return nil, err
}

backends.SetData(BuildBackupBackends(config))

return scheduleRoutines(scheduler, config, MakeHandlers(config, backends))
handlers := MakeHandlers(config, backends)
err = scheduleRoutines(scheduler, config, handlers)
if err != nil {
return nil, err
}

return handlers, nil
}

// ScheduleBackup creates a new quartz.Scheduler, schedules all the configured backup jobs,
Expand Down
1 change: 1 addition & 0 deletions pkg/service/configuration_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Reader interface {
type HTTPReader struct{}

func (h HTTPReader) read(url string) ([]byte, error) {
// #nosec G107
resp, err := http.Get(url)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/configuration_manager_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (cm *FileConfigurationManager) WriteConfiguration(config *model.Config) err
return fmt.Errorf("failed to marshal configuration data: %w", err)
}

err = os.WriteFile(filePath, data, 0644)
err = os.WriteFile(filePath, data, 0600)
if err != nil {
return fmt.Errorf("failed to write configuration to file %q: %w", filePath, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/configuration_service_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func TestPolicy_AddErrors(t *testing.T) {
Storage: map[string]*model.Storage{storage: {}},
}

for _, testPolicy := range fails {
err := AddPolicy(config, policy, &testPolicy.policy)
for i := range fails {
err := AddPolicy(config, policy, &fails[i].policy)
if err == nil {
t.Errorf("Expected an error on %s", testPolicy.name)
t.Errorf("Expected an error on %s", fails[i].name)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/configuration_service_routine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func TestRoutine_AddErrors(t *testing.T) {
Storage: map[string]*model.Storage{storage: {}},
}

for _, testRoutine := range fails {
err := AddRoutine(config, routine, &testRoutine.routine)
for i := range fails {
err := AddRoutine(config, routine, &fails[i].routine)
if err == nil {
t.Errorf("Expected an error on %s", testRoutine.name)
t.Errorf("Expected an error on %s", fails[i].name)
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/service/jobs_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewJobsHolder() *JobsHolder {
}

func (h *JobsHolder) newJob() int {
// #nosec G404
jobID := rand.Int()
h.Lock()
defer h.Unlock()
Expand All @@ -38,21 +39,21 @@ func (h *JobsHolder) getStatus(jobID int) (*model.RestoreJobStatus, error) {
return &copyJob, nil
}

func (h *JobsHolder) increaseStats(jobID int, new *model.RestoreResult) {
func (h *JobsHolder) increaseStats(jobID int, newStats *model.RestoreResult) {
h.Lock()
defer h.Unlock()
current, found := h.restoreJobs[jobID]
if found {
current.TotalBytes += new.TotalBytes
current.TotalRecords += new.TotalRecords
current.ExpiredRecords += new.ExpiredRecords
current.SkippedRecords += new.SkippedRecords
current.IgnoredRecords += new.IgnoredRecords
current.InsertedRecords += new.InsertedRecords
current.ExistedRecords += new.ExistedRecords
current.FresherRecords += new.FresherRecords
current.IndexCount += new.IndexCount
current.UDFCount += new.UDFCount
current.TotalBytes += newStats.TotalBytes
current.TotalRecords += newStats.TotalRecords
current.ExpiredRecords += newStats.ExpiredRecords
current.SkippedRecords += newStats.SkippedRecords
current.IgnoredRecords += newStats.IgnoredRecords
current.InsertedRecords += newStats.InsertedRecords
current.ExistedRecords += newStats.ExistedRecords
current.FresherRecords += newStats.FresherRecords
current.IndexCount += newStats.IndexCount
current.UDFCount += newStats.UDFCount
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/os_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (o *OSDiskAccessor) write(filePath string, data []byte) error {
return err
}

return os.WriteFile(filePath, data, 0644)
return os.WriteFile(filePath, data, 0600)
}

func (o *OSDiskAccessor) lsDir(path string) ([]string, error) {
Expand Down
7 changes: 3 additions & 4 deletions pkg/service/os_accessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestDeleteFolder(t *testing.T) {
parentFolder := tempFolder + "/parent"
folderToDelete := parentFolder + "/nested"
_ = os.MkdirAll(folderToDelete, 0744)
_ = os.WriteFile(folderToDelete+"/file.txt", []byte("hello world"), 0666)
_ = os.WriteFile(folderToDelete+"/file.txt", []byte("hello world"), 0600)

err := NewOSDiskAccessor().DeleteFolder(folderToDelete)

Expand Down Expand Up @@ -70,7 +70,7 @@ func TestLsDir(t *testing.T) {
setup: func() string {
dir := t.TempDir()
file := filepath.Join(dir, "file")
_ = os.WriteFile(file, []byte("test content"), 0644)
_ = os.WriteFile(file, []byte("test content"), 0600)
return dir
},
expected: nil,
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestLsFiles(t *testing.T) {
setup: func() string {
dir := t.TempDir()
file := filepath.Join(dir, "file")
_ = os.WriteFile(file, []byte("test content"), 0644)
_ = os.WriteFile(file, []byte("test content"), 0600)
return dir
},
expected: []string{"file"},
Expand Down Expand Up @@ -220,7 +220,6 @@ func TestReadBackupDetails(t *testing.T) {
}

func TestReadBackupDetailsNegative(t *testing.T) {

accessor := &OSDiskAccessor{}
tests := []struct {
name string
Expand Down
Loading

0 comments on commit a9cf9b3

Please sign in to comment.