-
Couldn't load subscription status.
- Fork 538
support/datastore: add read-only http datastore. #5809
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,220 @@ | ||||||
| package datastore | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "net/http" | ||||||
| "net/url" | ||||||
| "os" | ||||||
| "strconv" | ||||||
| "strings" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/stellar/go/support/log" | ||||||
| ) | ||||||
|
|
||||||
| // HTTPDataStore implements DataStore for HTTP(S) endpoints. | ||||||
| // This is designed for read-only access to publicly available files. | ||||||
| type HTTPDataStore struct { | ||||||
| client *http.Client | ||||||
| baseURL string | ||||||
| headers map[string]string | ||||||
| } | ||||||
|
|
||||||
| func NewHTTPDataStore(datastoreConfig DataStoreConfig) (DataStore, error) { | ||||||
| baseURL, ok := datastoreConfig.Params["base_url"] | ||||||
| if !ok { | ||||||
| return nil, errors.New("invalid HTTP config, no base_url") | ||||||
| } | ||||||
|
|
||||||
| parsedURL, err := url.Parse(baseURL) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("invalid base_url: %w", err) | ||||||
| } | ||||||
| if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" { | ||||||
| return nil, errors.New("base_url must use http or https scheme") | ||||||
| } | ||||||
|
|
||||||
| if !strings.HasSuffix(baseURL, "/") { | ||||||
| baseURL = baseURL + "/" | ||||||
| } | ||||||
|
|
||||||
| timeout := 30 * time.Second | ||||||
| if timeoutStr, ok := datastoreConfig.Params["timeout"]; ok { | ||||||
| parsedTimeout, err := time.ParseDuration(timeoutStr) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("invalid timeout: %w", err) | ||||||
| } | ||||||
| timeout = parsedTimeout | ||||||
| } | ||||||
|
|
||||||
| headers := make(map[string]string) | ||||||
| for key, value := range datastoreConfig.Params { | ||||||
| if strings.HasPrefix(key, "header_") { | ||||||
| headerName := strings.TrimPrefix(key, "header_") | ||||||
| headers[headerName] = value | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| client := &http.Client{ | ||||||
| Timeout: timeout, | ||||||
| } | ||||||
|
|
||||||
| return &HTTPDataStore{ | ||||||
| client: client, | ||||||
| baseURL: baseURL, | ||||||
| headers: headers, | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
| func (h *HTTPDataStore) buildURL(filePath string) string { | ||||||
| return h.baseURL + filePath | ||||||
| } | ||||||
|
|
||||||
| func (h *HTTPDataStore) addHeaders(req *http.Request) { | ||||||
| for key, value := range h.headers { | ||||||
| req.Header.Set(key, value) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (h *HTTPDataStore) checkHTTPStatus(resp *http.Response, filePath string) error { | ||||||
| switch resp.StatusCode { | ||||||
| case http.StatusOK: | ||||||
| return nil | ||||||
| case http.StatusNotFound: | ||||||
| return os.ErrNotExist | ||||||
| default: | ||||||
| return fmt.Errorf("HTTP error %d for file %s", resp.StatusCode, filePath) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (h *HTTPDataStore) doHeadRequest(ctx context.Context, filePath string) (*http.Response, error) { | ||||||
| requestURL := h.buildURL(filePath) | ||||||
| req, err := http.NewRequestWithContext(ctx, "HEAD", requestURL, nil) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to create HEAD request for %s: %w", filePath, err) | ||||||
| } | ||||||
| h.addHeaders(req) | ||||||
|
|
||||||
| resp, err := h.client.Do(req) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("HEAD request failed for %s: %w", filePath, err) | ||||||
| } | ||||||
|
|
||||||
| if err := h.checkHTTPStatus(resp, filePath); err != nil { | ||||||
| resp.Body.Close() | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| return resp, nil | ||||||
| } | ||||||
|
|
||||||
| // GetFileMetadata retrieves basic metadata for a file via HTTP HEAD request. | ||||||
| func (h *HTTPDataStore) GetFileMetadata(ctx context.Context, filePath string) (map[string]string, error) { | ||||||
| resp, err := h.doHeadRequest(ctx, filePath) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| defer resp.Body.Close() | ||||||
|
|
||||||
| metadata := make(map[string]string) | ||||||
| for key, values := range resp.Header { | ||||||
| if len(values) > 0 { | ||||||
| metadata[strings.ToLower(key)] = values[0] | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return metadata, nil | ||||||
| } | ||||||
|
|
||||||
| // GetFileLastModified retrieves the last modified time from HTTP headers. | ||||||
| func (h *HTTPDataStore) GetFileLastModified(ctx context.Context, filePath string) (time.Time, error) { | ||||||
| metadata, err := h.GetFileMetadata(ctx, filePath) | ||||||
| if err != nil { | ||||||
| return time.Time{}, err | ||||||
| } | ||||||
|
|
||||||
| if lastModified, ok := metadata["last-modified"]; ok { | ||||||
| return http.ParseTime(lastModified) | ||||||
| } | ||||||
|
|
||||||
| return time.Time{}, errors.New("last-modified header not found") | ||||||
| } | ||||||
|
|
||||||
| // GetFile downloads a file from the HTTP endpoint. | ||||||
| func (h *HTTPDataStore) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) { | ||||||
| requestURL := h.buildURL(filePath) | ||||||
| req, err := http.NewRequestWithContext(ctx, "GET", requestURL, nil) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("failed to create GET request for %s: %w", filePath, err) | ||||||
| } | ||||||
| h.addHeaders(req) | ||||||
|
|
||||||
| resp, err := h.client.Do(req) | ||||||
| if err != nil { | ||||||
| log.Debugf("Error retrieving file '%s': %v", filePath, err) | ||||||
|
||||||
| log.Debugf("Error retrieving file '%s': %v", filePath, err) | |
| log.Debugf("Error retrieving file '%s'", filePath) |
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 HTTP client lacks important security configurations. Consider adding MaxIdleConns, MaxIdleConnsPerHost, and IdleConnTimeout to prevent connection pool exhaustion, and disable automatic redirect following for better security control.