Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 AuthenticationSettings validation #761

Merged
merged 8 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions api/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (

const (
S3MinAccessKeyLen = 16
S3MaxAccessKeyLen = 40
S3MaxAccessKeyLen = 128
S3SecretKeyLen = 40
)

var (
Expand Down Expand Up @@ -137,11 +138,15 @@ func (rs RedundancySettings) Validate() error {
// Validate returns an error if the authentication settings are not considered
// valid.
func (s3as S3AuthenticationSettings) Validate() error {
for id, key := range s3as.V4Keypairs {
if len(id) == 0 {
for accessKeyID, secretAccessKey := range s3as.V4Keypairs {
if len(accessKeyID) == 0 {
return fmt.Errorf("AccessKeyID cannot be empty")
} else if len(key) < S3MinAccessKeyLen || len(key) > S3MaxAccessKeyLen {
return fmt.Errorf("AccessKeyID must be between %d and %d characters long but was %d", S3MinAccessKeyLen, S3MaxAccessKeyLen, len(key))
} else if len(accessKeyID) < S3MinAccessKeyLen || len(accessKeyID) > S3MaxAccessKeyLen {
return fmt.Errorf("AccessKeyID must be between %d and %d characters long but was %d", S3MinAccessKeyLen, S3MaxAccessKeyLen, len(accessKeyID))
} else if len(secretAccessKey) == 0 {
return fmt.Errorf("SecretAccessKey cannot be empty")
} else if len(secretAccessKey) != S3SecretKeyLen {
return fmt.Errorf("SecretAccessKey must be %d characters long but was %d", S3SecretKeyLen, len(secretAccessKey))
}
}
return nil
Expand Down
11 changes: 11 additions & 0 deletions cmd/renterd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,17 @@ func main() {
} else if as.V4Keypairs == nil {
as.V4Keypairs = make(map[string]string)
}

// S3 key pair validation was broken at one point, we need to remove the
// invalid key pairs here to ensure we don't fail when we update the
// setting below.
for k, v := range as.V4Keypairs {
if err := (api.S3AuthenticationSettings{V4Keypairs: map[string]string{k: v}}).Validate(); err != nil {
logger.Sugar().Infof("removing invalid S3 keypair for AccessKeyID %s, reason: %v", k, err)
delete(as.V4Keypairs, k)
}
}

// merge keys
for k, v := range cfg.S3.KeypairsV4 {
as.V4Keypairs[k] = v
Expand Down
7 changes: 4 additions & 3 deletions internal/testing/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ var (
TotalShards: 3,
}

testS3Credentials = credentials.NewStaticV4("myaccesskeyidentifier", "mysupersecretkey", "")
testS3AuthPairs = map[string]string{"myaccesskeyidentifier": "mysupersecretkey"}
testS3AccessKeyID = "TESTINGYNHUWCPKOPSYQ"
testS3SecretAccessKey = "Rh30BNyj+qNI4ftYRteoZbHJ3X4Ln71QtZkRXzJ9"
testS3Credentials = credentials.NewStaticV4(testS3AccessKeyID, testS3SecretAccessKey, "")
)

type TT struct {
Expand Down Expand Up @@ -521,7 +522,7 @@ func newTestCluster(t *testing.T, opts testClusterOptions) *TestCluster {
tt.OK(busClient.UpdateSetting(context.Background(), api.SettingRedundancy, testRedundancySettings))
tt.OK(busClient.UpdateSetting(context.Background(), api.SettingContractSet, testContractSetSettings))
tt.OK(busClient.UpdateSetting(context.Background(), api.SettingS3Authentication, api.S3AuthenticationSettings{
V4Keypairs: testS3AuthPairs,
V4Keypairs: map[string]string{testS3AccessKeyID: testS3SecretAccessKey},
}))
tt.OK(busClient.UpdateSetting(context.Background(), api.SettingUploadPacking, api.UploadPackingSettings{Enabled: enableUploadPacking}))

Expand Down
42 changes: 28 additions & 14 deletions internal/testing/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ func TestS3SettingsValidate(t *testing.T) {
t.SkipNow()
}

charset := "doesntreallymatter"
stringOfLength := func(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = charset[frand.Intn(len(charset))]
}
return string(b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the charset doesn't matter then why not do strings.Repeat("a", length) ?

}

cluster := newTestCluster(t, clusterOptsDefault)
defer cluster.Shutdown()

Expand All @@ -612,33 +621,38 @@ func TestS3SettingsValidate(t *testing.T) {
shouldFail bool
}{
{
// Min length
id: "id",
key: "aaaaaaaaaaaaaaaa",
id: stringOfLength(api.S3MinAccessKeyLen),
key: stringOfLength(api.S3SecretKeyLen),
shouldFail: false,
},
{
// Max length
id: "id",
key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
id: stringOfLength(api.S3MaxAccessKeyLen),
key: stringOfLength(api.S3SecretKeyLen),
shouldFail: false,
},
{
// Min length - 1
id: "id",
key: "aaaaaaaaaaaaaaa",
id: stringOfLength(api.S3MinAccessKeyLen - 1),
key: stringOfLength(api.S3SecretKeyLen),
shouldFail: true,
},
{
// Max length + 1
id: "id",
key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
id: stringOfLength(api.S3MaxAccessKeyLen + 1),
key: stringOfLength(api.S3SecretKeyLen),
shouldFail: true,
},
{
// No ID
id: "",
key: "aaaaaaaaaaaaaaaa",
key: stringOfLength(api.S3SecretKeyLen),
shouldFail: true,
},
{
id: stringOfLength(api.S3MinAccessKeyLen),
key: "",
shouldFail: true,
},
{
id: stringOfLength(api.S3MinAccessKeyLen),
key: stringOfLength(api.S3SecretKeyLen + 1),
shouldFail: true,
},
}
Expand Down
4 changes: 2 additions & 2 deletions stores/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3769,11 +3769,11 @@
}

// assert it's validity is within expected bounds
min := now.Add(refreshHealthMinHealthValidity)
max := now.Add(refreshHealthMaxHealthValidity)
min := now.Add(refreshHealthMinHealthValidity).Add(-time.Second) // avoid NDF
max := now.Add(refreshHealthMaxHealthValidity).Add(time.Second) // avoid NDF
validUntil := time.Unix(slab.HealthValidUntil, 0)
if !(min.Before(validUntil) && max.After(validUntil)) {
t.Fatal("valid until not in boundaries", min, max, validUntil, now)

Check failure on line 3776 in stores/metadata_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

Test go.sia.tech/renterd/stores/TestSlabHealthInvalidation failed in 0.24s

metadata_test.go:3776: valid until not in boundaries 2023-11-28 04:44:00 +0000 UTC 2023-11-30 16:44:02 +0000 UTC 2023-11-28 04:44:00 +0000 UTC 2023-11-27 16:44:01 +0000 UTC
}
}
}
Expand Down
Loading