Files
seaweedfs/weed/filer/s3iam_conf.go
T
a2a7a589bb filer: keep the content out of S3 config parse errors (#13202)
* filer: keep the content out of S3 config parse errors

protojson's errors quote the token they reject, and in these files that
can be a secret key or KMS key material, so a config that didn't parse
could log part of a key. The error now gives the line and column, the
field's type and name for a value of the wrong type, or says the file
ends early. Syntax errors come from encoding/json, which gives an offset
instead of a quote, so protojson's text is only read for valid JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep codespell from flagging a parse error test case

Codespell reads `tru` in `{"é": tru#` as a misspelling. `nul#` fails at
the same column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* credential: run the store interface tests on the memory store

The tests took the first registered store, which depends on package
init order: once filer_etc registered first they ran against a filer
that is not there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* credential: keep the content out of static S3 config file parse errors

LoadS3ConfigFile parsed the -s3.config file with protojson directly, so
the filer logged a rejected token such as a secret key. It now uses
ParseS3ConfigurationFromBytes like the other readers of that file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-01 10:56:25 -08:00

106 lines
3.6 KiB
Go

package filer
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strings"
"unicode/utf8"
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
jsonpb "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
var protojsonError = regexp.MustCompile(`\(line (\d+):(\d+)\): (invalid value for \w+ field \w+)?`)
func ParseS3ConfigurationFromBytes[T proto.Message](content []byte, config T) error {
options := &jsonpb.UnmarshalOptions{
DiscardUnknown: true,
AllowPartial: true,
}
err := options.Unmarshal(content, config)
if err == nil {
return nil
}
// protojson's errors quote the token they reject, which in these files can be a secret
// key or KMS key material, so an error only says where it is and, for a value of the
// wrong type, which field it's for. encoding/json finds syntax errors and gives an offset
// instead of a quote, so protojson's text is only read for a file that is valid JSON.
decoder := json.NewDecoder(bytes.NewReader(content))
var syntaxErr *json.SyntaxError
switch jsonErr := decoder.Decode(new(json.RawMessage)); {
case jsonErr == io.EOF || jsonErr == io.ErrUnexpectedEOF:
return errors.New("invalid configuration: unexpected end of input")
case errors.As(jsonErr, &syntaxErr):
return fmt.Errorf("invalid configuration at %s", position(content, int(syntaxErr.Offset)-1))
case jsonErr == nil:
if rest := bytes.TrimLeft(content[decoder.InputOffset():], " \t\r\n"); len(rest) > 0 {
return fmt.Errorf("invalid configuration at %s", position(content, len(content)-len(rest)))
}
}
if strings.HasSuffix(err.Error(), io.ErrUnexpectedEOF.Error()) {
// protojson reads six bytes past a \u escape of a surrogate, so on one near the end of
// the file it runs out of input. With room after it, it says where the escape is.
if padded := options.Unmarshal(append(bytes.Clone(content), " "...), config.ProtoReflect().New().Interface()); padded != nil {
err = padded
}
}
found := protojsonError.FindStringSubmatch(err.Error())
switch {
case found != nil && found[3] != "":
return fmt.Errorf("invalid configuration at line %s, column %s: %s", found[1], found[2], found[3])
case found != nil:
return fmt.Errorf("invalid configuration at line %s, column %s", found[1], found[2])
}
return errors.New("invalid configuration")
}
// position gives offset as protojson's errors do, with the column in runes.
func position(content []byte, offset int) string {
before := content[:offset]
line := bytes.Count(before, []byte("\n")) + 1
if i := bytes.LastIndexByte(before, '\n'); i >= 0 {
before = before[i+1:]
}
return fmt.Sprintf("line %d, column %d", line, utf8.RuneCount(before)+1)
}
func ProtoToText(writer io.Writer, config proto.Message) error {
m := jsonpb.MarshalOptions{
EmitUnpopulated: true,
Indent: " ",
}
text, marshalErr := m.Marshal(config)
if marshalErr != nil {
return fmt.Errorf("marshal proto message: %w", marshalErr)
}
_, writeErr := writer.Write(text)
if writeErr != nil {
return fmt.Errorf("fail to write proto message: %w", writeErr)
}
return writeErr
}
// CheckDuplicateAccessKey returns an error message when s3cfg has duplicate access keys
func CheckDuplicateAccessKey(s3cfg *iam_pb.S3ApiConfiguration) error {
accessKeySet := make(map[string]string)
for _, ident := range s3cfg.Identities {
for _, cred := range ident.Credentials {
if userName, found := accessKeySet[cred.AccessKey]; !found {
accessKeySet[cred.AccessKey] = ident.Name
} else if userName != ident.Name {
return fmt.Errorf("duplicate accessKey[%s], already configured in user[%s]", cred.AccessKey, userName)
}
}
}
return nil
}