Skip to content

Commit 3249807

Browse files
committed
add support for banning standalone remotes and dont proceed with upload if any of them are banned
1 parent c878981 commit 3249807

File tree

6 files changed

+98
-14
lines changed

6 files changed

+98
-14
lines changed

cache/get.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"time"
55
)
66

7-
func Get(key string) bool {
7+
func Get(key string) (bool, time.Time) {
8+
var expires time.Time
89
changes := false
910
exists := false
1011

@@ -24,6 +25,7 @@ func Get(key string) bool {
2425
} else {
2526
// the item has not expired
2627
exists = true
28+
expires = expiry
2729
}
2830
}
2931

@@ -35,5 +37,5 @@ func Get(key string) bool {
3537
}
3638
}
3739

38-
return exists
40+
return exists, expires
3941
}

cmd/upload.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cmd
22

33
import (
4+
"github.com/dustin/go-humanize"
45
"github.com/l3uddz/crop/config"
56
"github.com/l3uddz/crop/uploader"
67
"github.com/pkg/errors"
8+
"github.com/sirupsen/logrus"
79
"github.com/spf13/cobra"
810
"github.com/yale8848/gorpool"
911
)
@@ -27,7 +29,6 @@ var uploadCmd = &cobra.Command{
2729
}
2830

2931
log := log.WithField("uploader", uploaderName)
30-
log.Info("Uploader commencing...")
3132

3233
// create uploader
3334
upload, err := uploader.New(config.Config, &uploaderConfig, uploaderName)
@@ -40,8 +41,32 @@ var uploadCmd = &cobra.Command{
4041
if upload.ServiceAccountCount > 0 {
4142
upload.Log.WithField("found_files", upload.ServiceAccountCount).
4243
Info("Loaded service accounts")
44+
} else {
45+
// no service accounts were loaded
46+
// check to see if any of the copy or move remote(s) are banned
47+
banned, expiry := upload.RemotesBanned(upload.Config.Remotes.Copy)
48+
if banned && !expiry.IsZero() {
49+
// one of the copy remotes is banned, abort
50+
upload.Log.WithFields(logrus.Fields{
51+
"expires_time": expiry,
52+
"expires_in": humanize.Time(expiry),
53+
}).Warn("Cannot proceed with upload as a copy remote is banned")
54+
continue
55+
}
56+
57+
banned, expiry = upload.RemotesBanned([]string{upload.Config.Remotes.Move})
58+
if banned && !expiry.IsZero() {
59+
// the move remote is banned, abort
60+
upload.Log.WithFields(logrus.Fields{
61+
"expires_time": expiry,
62+
"expires_in": humanize.Time(expiry),
63+
}).Warn("Cannot proceed with upload as the move remote is banned")
64+
continue
65+
}
4366
}
4467

68+
log.Info("Uploader commencing...")
69+
4570
// refresh details about files to upload
4671
if err := upload.RefreshLocalFiles(); err != nil {
4772
upload.Log.WithError(err).Error("Failed refreshing details of files to upload")

stringutils/until.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package stringutils
2+
3+
import "strings"
4+
5+
func FromLeftUntil(from string, sub string) string {
6+
pos := strings.Index(from, sub)
7+
if pos < 0 {
8+
return from
9+
}
10+
11+
return from[:pos]
12+
}

uploader/sa.go renamed to uploader/cache.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package uploader
33
import (
44
"github.com/l3uddz/crop/cache"
55
"github.com/l3uddz/crop/pathutils"
6+
"github.com/l3uddz/crop/stringutils"
67
"github.com/pkg/errors"
8+
"time"
79
)
810

911
/* Private */
@@ -13,7 +15,7 @@ func (u *Uploader) getAvailableServiceAccount() (*pathutils.Path, error) {
1315
// find an unbanned service account
1416
for _, sa := range u.ServiceAccountFiles {
1517
// does the cache already contain this service account?
16-
if cache.Get(sa.RealPath) {
18+
if exists, _ := cache.Get(sa.RealPath); exists {
1719
// service account is currently banned
1820
continue
1921
}
@@ -30,3 +32,30 @@ func (u *Uploader) getAvailableServiceAccount() (*pathutils.Path, error) {
3032

3133
return serviceAccountFile, nil
3234
}
35+
36+
/* Public */
37+
func (u *Uploader) RemotesBanned(remotes []string) (bool, time.Time) {
38+
var banned bool
39+
var expires time.Time
40+
41+
// ignore empty remotes slice
42+
if remotes == nil {
43+
return banned, expires
44+
}
45+
46+
// format remotes into remote names if possible
47+
var checkRemotes []string
48+
for _, remote := range remotes {
49+
checkRemotes = append(checkRemotes, stringutils.FromLeftUntil(remote, ":"))
50+
}
51+
52+
// iterate remotes
53+
for _, remote := range checkRemotes {
54+
banned, expires = cache.Get(remote)
55+
if banned {
56+
break
57+
}
58+
}
59+
60+
return banned, expires
61+
}

uploader/copy.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/l3uddz/crop/cache"
66
"github.com/l3uddz/crop/pathutils"
77
"github.com/l3uddz/crop/rclone"
8+
"github.com/l3uddz/crop/stringutils"
89
"github.com/pkg/errors"
910
"github.com/sirupsen/logrus"
1011
"time"
@@ -58,14 +59,20 @@ func (u *Uploader) Copy(additionalRcloneParams []string) error {
5859
break
5960
}
6061

61-
// are we using service accounts?
62-
if u.ServiceAccountCount == 0 {
63-
return fmt.Errorf("copy failed with exit code: %v", exitCode)
64-
}
65-
6662
// is this an exit code we can retry?
6763
switch exitCode {
6864
case rclone.EXIT_FATAL_ERROR:
65+
// are we using service accounts?
66+
if u.ServiceAccountCount == 0 {
67+
// we are not using service accounts, so mark this remote as banned
68+
if err := cache.Set(stringutils.FromLeftUntil(remotePath, ":"),
69+
time.Now().UTC().Add(25*time.Hour)); err != nil {
70+
rLog.WithError(err).Errorf("Failed banning remote")
71+
}
72+
73+
return fmt.Errorf("copy failed with exit code: %v", exitCode)
74+
}
75+
6976
// ban this service account
7077
if err := cache.Set(serviceAccount.RealPath, time.Now().UTC().Add(25*time.Hour)); err != nil {
7178
rLog.WithError(err).Error("Failed banning service account, cannot try again...")

uploader/move.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/l3uddz/crop/cache"
66
"github.com/l3uddz/crop/pathutils"
77
"github.com/l3uddz/crop/rclone"
8+
"github.com/l3uddz/crop/stringutils"
89
"github.com/pkg/errors"
910
"github.com/sirupsen/logrus"
1011
"time"
@@ -89,14 +90,22 @@ func (u *Uploader) Move(serverSide bool, additionalRcloneParams []string) error
8990
return fmt.Errorf("failed and cannot proceed with exit code: %v", exitCode)
9091
}
9192

92-
// are we using service accounts?
93-
if u.ServiceAccountCount == 0 {
94-
return fmt.Errorf("move failed with exit code: %v", exitCode)
95-
}
96-
9793
// is this an exit code we can retry?
9894
switch exitCode {
9995
case rclone.EXIT_FATAL_ERROR:
96+
// are we using service accounts?
97+
if u.ServiceAccountCount == 0 {
98+
// we are not using service accounts, so mark this remote as banned (if non server side move)
99+
if !serverSide {
100+
if err := cache.Set(stringutils.FromLeftUntil(move.To, ":"),
101+
time.Now().UTC().Add(25*time.Hour)); err != nil {
102+
rLog.WithError(err).Errorf("Failed banning remote")
103+
}
104+
}
105+
106+
return fmt.Errorf("move failed with exit code: %v", exitCode)
107+
}
108+
100109
// ban this service account
101110
if err := cache.Set(serviceAccount.RealPath, time.Now().UTC().Add(25*time.Hour)); err != nil {
102111
rLog.WithError(err).Error("Failed banning service account, cannot try again...")

0 commit comments

Comments
 (0)