Skip to content

Commit 1c9da35

Browse files
authored
Merge pull request #653 from alanprot/expose/FileSecret
Expose secret as FileSecret from config package
2 parents 3183099 + 2c133cf commit 1c9da35

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

config/http_config.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -718,23 +718,27 @@ func (s *InlineSecret) Immutable() bool {
718718
return true
719719
}
720720

721-
type fileSecret struct {
721+
type FileSecret struct {
722722
file string
723723
}
724724

725-
func (s *fileSecret) Fetch(ctx context.Context) (string, error) {
725+
func NewFileSecret(file string) *FileSecret {
726+
return &FileSecret{file: file}
727+
}
728+
729+
func (s *FileSecret) Fetch(ctx context.Context) (string, error) {
726730
fileBytes, err := os.ReadFile(s.file)
727731
if err != nil {
728732
return "", fmt.Errorf("unable to read file %s: %w", s.file, err)
729733
}
730734
return strings.TrimSpace(string(fileBytes)), nil
731735
}
732736

733-
func (s *fileSecret) Description() string {
737+
func (s *FileSecret) Description() string {
734738
return fmt.Sprintf("file %s", s.file)
735739
}
736740

737-
func (s *fileSecret) Immutable() bool {
741+
func (s *FileSecret) Immutable() bool {
738742
return false
739743
}
740744

@@ -763,9 +767,7 @@ func toSecret(secretManager SecretManager, text Secret, file, ref string) (Secre
763767
return NewInlineSecret(string(text)), nil
764768
}
765769
if file != "" {
766-
return &fileSecret{
767-
file: file,
768-
}, nil
770+
return NewFileSecret(file), nil
769771
}
770772
if ref != "" {
771773
if secretManager == nil {

config/http_config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ func TestBearerAuthFileRoundTripper(t *testing.T) {
756756
}, nil, nil)
757757

758758
// Normal flow.
759-
bearerAuthRoundTripper := NewAuthorizationCredentialsRoundTripper("Bearer", &fileSecret{file: BearerTokenFile}, fakeRoundTripper)
759+
bearerAuthRoundTripper := NewAuthorizationCredentialsRoundTripper("Bearer", &FileSecret{file: BearerTokenFile}, fakeRoundTripper)
760760
request, _ := http.NewRequest("GET", "/hitchhiker", nil)
761761
request.Header.Set("User-Agent", "Douglas Adams mind")
762762
_, err := bearerAuthRoundTripper.RoundTrip(request)
@@ -765,7 +765,7 @@ func TestBearerAuthFileRoundTripper(t *testing.T) {
765765
}
766766

767767
// Should honor already Authorization header set.
768-
bearerAuthRoundTripperShouldNotModifyExistingAuthorization := NewAuthorizationCredentialsRoundTripper("Bearer", &fileSecret{file: MissingBearerTokenFile}, fakeRoundTripper)
768+
bearerAuthRoundTripperShouldNotModifyExistingAuthorization := NewAuthorizationCredentialsRoundTripper("Bearer", &FileSecret{file: MissingBearerTokenFile}, fakeRoundTripper)
769769
request, _ = http.NewRequest("GET", "/hitchhiker", nil)
770770
request.Header.Set("Authorization", ExpectedBearer)
771771
_, err = bearerAuthRoundTripperShouldNotModifyExistingAuthorization.RoundTrip(request)

0 commit comments

Comments
 (0)