forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"github.com/aws/smithy-go"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/platform/tlsconfig"
|
||||
)
|
||||
|
||||
// Config is the S3 connection subset. Limits belong to the attachment service.
|
||||
@@ -34,10 +36,11 @@ type Config struct {
|
||||
// S3 is an object store backed by an S3-compatible server (SeaweedFS in the
|
||||
// lab stand, MinIO/AWS in other deployments).
|
||||
type S3 struct {
|
||||
bucket string
|
||||
internal *s3.Client
|
||||
public *s3.Client
|
||||
timeout time.Duration
|
||||
bucket string
|
||||
internal *s3.Client
|
||||
public *s3.Client
|
||||
timeout time.Duration
|
||||
transport *http.Transport
|
||||
}
|
||||
|
||||
var _ Store = (*S3)(nil)
|
||||
@@ -55,20 +58,38 @@ func New(cfg Config) (*S3, error) {
|
||||
cfg.PublicEndpoint = cfg.Endpoint
|
||||
}
|
||||
|
||||
for _, endpoint := range []string{cfg.Endpoint, cfg.PublicEndpoint} {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u.Scheme != "https" || u.Hostname() == "" || u.User != nil {
|
||||
return nil, errors.New("object store: endpoints must be https urls without credentials")
|
||||
}
|
||||
if u.RawQuery != "" || u.Fragment != "" {
|
||||
return nil, errors.New("object store: endpoints must not contain a query or fragment")
|
||||
}
|
||||
}
|
||||
clientTLS, err := tlsconfig.NewClientFromEnv("")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("configure object store tls: %w", err)
|
||||
}
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
TLSClientConfig: clientTLS,
|
||||
TLSHandshakeTimeout: 3 * time.Second,
|
||||
ResponseHeaderTimeout: 15 * time.Second,
|
||||
MaxIdleConnsPerHost: 16,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
TLSHandshakeTimeout: 3 * time.Second,
|
||||
ResponseHeaderTimeout: 15 * time.Second,
|
||||
MaxIdleConnsPerHost: 16,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
Transport: transport,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return errors.New("object store: redirects are not permitted")
|
||||
},
|
||||
// Downloads stream through the response body; an overall client
|
||||
// timeout would abort them.
|
||||
// Downloads stream through the response body; an overall timeout
|
||||
// would abort them.
|
||||
}
|
||||
|
||||
newClient := func(endpoint string) *s3.Client {
|
||||
@@ -82,15 +103,19 @@ func New(cfg Config) (*S3, error) {
|
||||
}
|
||||
|
||||
st := &S3{
|
||||
bucket: cfg.Bucket,
|
||||
internal: newClient(cfg.Endpoint),
|
||||
public: newClient(cfg.PublicEndpoint),
|
||||
timeout: cfg.Timeout,
|
||||
bucket: cfg.Bucket,
|
||||
internal: newClient(cfg.Endpoint),
|
||||
public: newClient(cfg.PublicEndpoint),
|
||||
timeout: cfg.Timeout,
|
||||
transport: transport,
|
||||
}
|
||||
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// Close releases idle connections owned by the S3 clients.
|
||||
func (s *S3) Close() { s.transport.CloseIdleConnections() }
|
||||
|
||||
func (s *S3) Ping(ctx context.Context) error {
|
||||
// HeadBucket needs bucket-level permissions that the application identity
|
||||
// intentionally does not have. A HeadObject on a sentinel staging key is
|
||||
|
||||
Reference in New Issue
Block a user