-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(s3toss): reject invalid config entries #35404
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
Open
VectorPeak
wants to merge
6
commits into
taosdata:main
Choose a base branch
from
VectorPeak:fix-s3toss-config-parse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−5
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51e849e
fix(s3toss): reject invalid config entries
VectorPeak 37afe2f
test(s3toss): reset config fields directly
VectorPeak e388c64
fix(s3toss): validate access string protocol
VectorPeak 3de45f2
fix(s3toss): honor s3 uri style
VectorPeak b166b31
Revert "fix(s3toss): honor s3 uri style"
VectorPeak 71621ec
Revert "fix(s3toss): validate access string protocol"
VectorPeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseAccessStringAcceptsKnownAndUnknownOptions(t *testing.T) { | ||
| resetConfig() | ||
|
|
||
| if err := parseAccessString("endpoint=s3.amazonaws.com;bucket=mybucket;uriStyle=path;protocol=http;accessKeyId=AK;secretAccessKey=SK;region=us-east-2"); err != nil { | ||
| t.Fatalf("parseAccessString returned error: %v", err) | ||
| } | ||
|
|
||
| if config.Endpoint != "s3.amazonaws.com" { | ||
| t.Fatalf("Endpoint = %q, want s3.amazonaws.com", config.Endpoint) | ||
| } | ||
| if config.Bucket != "mybucket" { | ||
| t.Fatalf("Bucket = %q, want mybucket", config.Bucket) | ||
| } | ||
| if config.Secure { | ||
| t.Fatal("Secure = true, want false for protocol=http") | ||
| } | ||
| if config.AccessKey != "AK" || config.SecretKey != "SK" || config.Region != "us-east-2" { | ||
| t.Fatalf("unexpected credentials or region: access=%q secret=%q region=%q", config.AccessKey, config.SecretKey, config.Region) | ||
| } | ||
| } | ||
|
|
||
| func TestParseAccessStringRejectsMalformedOption(t *testing.T) { | ||
| resetConfig() | ||
|
|
||
| err := parseAccessString("endpoint") | ||
| if err == nil { | ||
| t.Fatal("parseAccessString succeeded for malformed option") | ||
| } | ||
| if !strings.Contains(err.Error(), "expected key=value") { | ||
| t.Fatalf("error = %q, want expected key=value", err) | ||
| } | ||
| } | ||
|
|
||
| func TestParseTaosCfgParsesSsAccessString(t *testing.T) { | ||
| resetConfig() | ||
|
|
||
| cfgPath := writeConfig(t, "dataDir /tmp/taos 0\nssAccessString s3:endpoint=s3.amazonaws.com;bucket=mybucket;uriStyle=path;protocol=http;accessKeyId=AK;secretAccessKey=SK;region=us-east-2\n") | ||
|
|
||
| if err := parseTaosCfg(cfgPath); err != nil { | ||
| t.Fatalf("parseTaosCfg returned error: %v", err) | ||
| } | ||
|
|
||
| if config.Endpoint != "s3.amazonaws.com" { | ||
| t.Fatalf("Endpoint = %q, want s3.amazonaws.com", config.Endpoint) | ||
| } | ||
| if config.Bucket != "mybucket" || config.AccessKey != "AK" || config.SecretKey != "SK" || config.Region != "us-east-2" { | ||
| t.Fatalf("unexpected S3 config: bucket=%q access=%q secret=%q region=%q", config.Bucket, config.AccessKey, config.SecretKey, config.Region) | ||
| } | ||
| if config.Secure { | ||
| t.Fatal("Secure = true, want false for protocol=http") | ||
| } | ||
| } | ||
|
|
||
| func TestParseTaosCfgRejectsMalformedSsAccessString(t *testing.T) { | ||
| resetConfig() | ||
|
|
||
| cfgPath := writeConfig(t, "dataDir /tmp/taos 0\nssAccessString s3:endpoint\n") | ||
|
|
||
| err := parseTaosCfg(cfgPath) | ||
| if err == nil { | ||
| t.Fatal("parseTaosCfg succeeded for malformed ssAccessString") | ||
| } | ||
| if !strings.Contains(err.Error(), "expected key=value") { | ||
| t.Fatalf("error = %q, want expected key=value", err) | ||
| } | ||
| } | ||
|
|
||
| func TestParseTaosCfgRejectsInvalidDataDirLevel(t *testing.T) { | ||
| resetConfig() | ||
|
|
||
| cfgPath := writeConfig(t, "dataDir /tmp/taos 3\n") | ||
|
|
||
| err := parseTaosCfg(cfgPath) | ||
| if err == nil { | ||
| t.Fatal("parseTaosCfg succeeded for invalid dataDir level") | ||
| } | ||
| if !strings.Contains(err.Error(), "invalid dataDir level 3") { | ||
| t.Fatalf("error = %q, want invalid dataDir level 3", err) | ||
| } | ||
| } | ||
|
|
||
| func TestParseTaosCfgRejectsInvalidS3Endpoint(t *testing.T) { | ||
| resetConfig() | ||
|
|
||
| cfgPath := writeConfig(t, "dataDir /tmp/taos 0\ns3endpoint localhost:9000\n") | ||
|
|
||
| err := parseTaosCfg(cfgPath) | ||
| if err == nil { | ||
| t.Fatal("parseTaosCfg succeeded for invalid s3endpoint") | ||
| } | ||
| if !strings.Contains(err.Error(), "expected http:// or https:// prefix") { | ||
| t.Fatalf("error = %q, want endpoint prefix error", err) | ||
| } | ||
| } | ||
|
|
||
| func writeConfig(t *testing.T, contents string) string { | ||
| t.Helper() | ||
|
|
||
| cfgPath := filepath.Join(t.TempDir(), "taos.cfg") | ||
| if err := os.WriteFile(cfgPath, []byte(contents), 0o644); err != nil { | ||
| t.Fatalf("WriteFile failed: %v", err) | ||
| } | ||
| return cfgPath | ||
| } | ||
|
|
||
| func resetConfig() { | ||
| config.BlockSize = 0 | ||
| config.DNode = 0 | ||
| config.DataDirs = [3][]string{} | ||
| config.Endpoint = "" | ||
| config.Secure = false | ||
| config.AccessKey = "" | ||
| config.SecretKey = "" | ||
| config.Bucket = "" | ||
| config.Region = "" | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
resetConfig()function duplicates the entire anonymous struct definition ofconfigfrommain.go. This duplication is a maintainability hazard: if theconfigstruct inmain.gois modified in the future (e.g., adding or removing a field), this test file will fail to compile because the two anonymous struct types will no longer be identical and assignable.To improve maintainability, we should reset the fields of the global
configvariable individually. This avoids duplicating the struct definition and ensures the tests remain compatible even if the struct fields change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 37afe2f by resetting each config field directly instead of assigning a duplicated anonymous struct literal. Re-ran
cd tools/s3toss && go test ./...successfully (ok github.com/taosdata/s3toss 0.003s).