Skip to content

Commit 785741a

Browse files
committed
fix(backups): refuse to probe an S3 endpoint behind a path prefix
An endpoint such as https://s3.example.org/prefix cannot be addressed by minio-go, which has no path-prefix option, and the prefix cannot be added below the client either, because the SigV4 signature covers the request path. The CNPG probe was worse than failing: it kept only the host of endpointURL and listed the bucket at a location barman never writes to. Reject such endpoints with an explicit error, in the CNPG probe and in the shared client, so the size is left unset instead of measured somewhere else. Assisted-by: LLM Signed-off-by: Andrey Kolkov <[email protected]>
1 parent ada6afc commit 785741a

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎internal/backupcontroller/artifact_size.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/x509"
77
"errors"
88
"fmt"
9+
"strings"
910
"time"
1011

1112
"github.com/minio/minio-go/v7"
@@ -36,7 +37,7 @@ type s3ObjectLister interface {
3637
// s3Connection is what a driver resolves from its rendered strategy to
3738
// read the bucket with the same identity the backup was written with.
3839
type s3Connection struct {
39-
// Endpoint is host[:port], without a scheme.
40+
// Endpoint is host[:port], without a scheme or a path.
4041
Endpoint string
4142
Secure bool
4243
Region string
@@ -59,6 +60,12 @@ type minioObjectLister struct {
5960
}
6061

6162
func newMinioObjectLister(conn s3Connection) (s3ObjectLister, error) {
63+
// An endpoint behind a path prefix cannot be addressed: minio-go has no
64+
// such option, and the SigV4 signature covers the request path, so the
65+
// prefix cannot be added below the client either.
66+
if strings.Contains(conn.Endpoint, "/") {
67+
return nil, fmt.Errorf("endpoint %q has a path, which the S3 client cannot address", conn.Endpoint)
68+
}
6269
opts := &minio.Options{
6370
Creds: credentials.NewStaticV4(conn.AccessKey, conn.SecretKey, conn.SessionToken),
6471
Region: conn.Region,

‎internal/backupcontroller/artifact_size_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,12 @@ func TestCollectObjects(t *testing.T) {
8080
t.Error("listing error was not returned")
8181
}
8282
}
83+
84+
func TestNewMinioObjectLister_RejectsEndpointPath(t *testing.T) {
85+
if _, err := newMinioObjectLister(s3Connection{Endpoint: "s3.example.org/prefix", Secure: true}); err == nil {
86+
t.Error("an endpoint with a path was accepted")
87+
}
88+
if _, err := newMinioObjectLister(s3Connection{Endpoint: "s3.example.org:9000", Secure: true}); err != nil {
89+
t.Errorf("host:port endpoint rejected: %v", err)
90+
}
91+
}

‎internal/backupcontroller/cnpg_artifact_size.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func (r *BackupJobReconciler) cnpgS3Connection(ctx context.Context, namespace st
7373
if err != nil || u.Host == "" {
7474
return conn, fmt.Errorf("invalid endpointURL %q", store.EndpointURL)
7575
}
76+
// Dropping the path would probe the bucket at another location than
77+
// the one barman writes to.
78+
if strings.Trim(u.Path, "/") != "" {
79+
return conn, fmt.Errorf("endpointURL %q has a path, which the S3 client cannot address", store.EndpointURL)
80+
}
7681
conn.Endpoint, conn.Secure = u.Host, u.Scheme == "https"
7782
}
7883

‎internal/backupcontroller/cnpg_artifact_size_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ func TestCNPGArtifactSizeBytes(t *testing.T) {
141141
},
142142
backupID: "id", lister: &fakeS3Lister{objects: []s3ObjectInfo{{Key: "x", Size: 1}}},
143143
},
144+
"endpointURL with a path": {
145+
store: func() *strategyv1alpha1.BarmanObjectStoreTemplate {
146+
s := newSizedBarmanObjectStore()
147+
s.EndpointURL = "https://s3.example:8443/prefix"
148+
return s
149+
},
150+
backupID: "20261021T100000", lister: &fakeS3Lister{objects: []s3ObjectInfo{{Key: "x", Size: 1}}},
151+
},
144152
}
145153
for name, tc := range unset {
146154
t.Run(name+" leaves size unset", func(t *testing.T) {

0 commit comments

Comments
 (0)