2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-09-09 15:20:49 -04:00
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-30 17:09:41 -04:00
|
|
|
"io"
|
2020-09-09 15:20:49 -04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-09-10 17:19:32 -04:00
|
|
|
"strconv"
|
2020-09-30 17:09:41 -04:00
|
|
|
"strings"
|
2020-09-09 15:20:49 -04:00
|
|
|
"time"
|
|
|
|
|
2021-11-05 15:20:08 -04:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/config"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2020-09-09 15:20:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultOperatorContextTimeout = 10 * time.Second
|
2020-09-10 17:19:32 -04:00
|
|
|
// ErrNotImplemented - Indicates the functionality which is not implemented
|
2020-09-09 15:20:49 -04:00
|
|
|
ErrNotImplemented = errors.New("The method is not implemented")
|
|
|
|
)
|
|
|
|
|
2020-09-10 17:19:32 -04:00
|
|
|
func (c *OperatorDNS) addAuthHeader(r *http.Request) error {
|
|
|
|
if c.username == "" || c.password == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-09 15:20:49 -04:00
|
|
|
|
|
|
|
claims := &jwt.StandardClaims{
|
|
|
|
ExpiresAt: int64(15 * time.Minute),
|
2020-09-10 17:19:32 -04:00
|
|
|
Issuer: c.username,
|
2020-09-09 15:20:49 -04:00
|
|
|
Subject: config.EnvDNSWebhook,
|
|
|
|
}
|
|
|
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
|
2020-09-10 17:19:32 -04:00
|
|
|
ss, err := token.SignedString([]byte(c.password))
|
2020-09-09 15:20:49 -04:00
|
|
|
if err != nil {
|
2020-09-10 17:19:32 -04:00
|
|
|
return err
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Set("Authorization", "Bearer "+ss)
|
2020-09-10 17:19:32 -04:00
|
|
|
return nil
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
|
|
|
|
2024-11-11 09:51:43 -05:00
|
|
|
func (c *OperatorDNS) endpoint(bucket string, del bool) (string, error) {
|
2020-09-09 15:20:49 -04:00
|
|
|
u, err := url.Parse(c.Endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
q := u.Query()
|
|
|
|
q.Add("bucket", bucket)
|
2024-11-11 09:51:43 -05:00
|
|
|
q.Add("delete", strconv.FormatBool(del))
|
2020-09-09 15:20:49 -04:00
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put - Adds DNS entries into operator webhook server
|
|
|
|
func (c *OperatorDNS) Put(bucket string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultOperatorContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
e, err := c.endpoint(bucket, false)
|
|
|
|
if err != nil {
|
2020-09-30 17:09:41 -04:00
|
|
|
return newError(bucket, err)
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, e, nil)
|
|
|
|
if err != nil {
|
2020-09-30 17:09:41 -04:00
|
|
|
return newError(bucket, err)
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
2020-09-10 17:19:32 -04:00
|
|
|
if err = c.addAuthHeader(req); err != nil {
|
2020-09-30 17:09:41 -04:00
|
|
|
return newError(bucket, err)
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
2021-09-22 12:16:12 -04:00
|
|
|
|
2020-09-09 15:20:49 -04:00
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2020-09-10 17:19:32 -04:00
|
|
|
if derr := c.Delete(bucket); derr != nil {
|
2020-09-30 17:09:41 -04:00
|
|
|
return newError(bucket, derr)
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
2021-09-22 12:16:12 -04:00
|
|
|
return err
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
2021-09-22 12:16:12 -04:00
|
|
|
defer xhttp.DrainBody(resp.Body)
|
|
|
|
|
2020-09-09 15:20:49 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2021-09-22 12:16:12 -04:00
|
|
|
var errorStringBuilder strings.Builder
|
|
|
|
io.Copy(&errorStringBuilder, io.LimitReader(resp.Body, resp.ContentLength))
|
2020-09-30 17:09:41 -04:00
|
|
|
errorString := errorStringBuilder.String()
|
2023-03-06 11:56:10 -05:00
|
|
|
if resp.StatusCode == http.StatusConflict {
|
2021-04-08 11:24:55 -04:00
|
|
|
return ErrBucketConflict(Error{bucket, errors.New(errorString)})
|
|
|
|
}
|
2020-09-30 17:09:41 -04:00
|
|
|
return newError(bucket, fmt.Errorf("service create for bucket %s, failed with status %s, error %s", bucket, resp.Status, errorString))
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:09:41 -04:00
|
|
|
func newError(bucket string, err error) error {
|
|
|
|
e := Error{bucket, err}
|
|
|
|
if strings.Contains(err.Error(), "invalid bucket name") {
|
|
|
|
return ErrInvalidBucketName(e)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:20:49 -04:00
|
|
|
// Delete - Removes DNS entries added in Put().
|
|
|
|
func (c *OperatorDNS) Delete(bucket string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultOperatorContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
e, err := c.endpoint(bucket, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, e, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-10 17:19:32 -04:00
|
|
|
if err = c.addAuthHeader(req); err != nil {
|
2020-09-09 15:20:49 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-10 17:19:32 -04:00
|
|
|
xhttp.DrainBody(resp.Body)
|
2020-09-09 15:20:49 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("request to delete the service for bucket %s, failed with status %s", bucket, resp.Status)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord - Removes a specific DNS entry
|
|
|
|
// No Op for Operator because operator deals on with bucket entries
|
|
|
|
func (c *OperatorDNS) DeleteRecord(record SrvRecord) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the internal http client
|
|
|
|
func (c *OperatorDNS) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List - Retrieves list of DNS entries for the domain.
|
|
|
|
// This is a No Op for Operator because, there is no intent to enforce global
|
|
|
|
// namespace at MinIO level with this DNS entry. The global namespace in
|
|
|
|
// enforced by the Kubernetes Operator
|
|
|
|
func (c *OperatorDNS) List() (srvRecords map[string][]SrvRecord, err error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get - Retrieves DNS records for a bucket.
|
|
|
|
// This is a No Op for Operator because, there is no intent to enforce global
|
|
|
|
// namespace at MinIO level with this DNS entry. The global namespace in
|
|
|
|
// enforced by the Kubernetes Operator
|
|
|
|
func (c *OperatorDNS) Get(bucket string) (srvRecords []SrvRecord, err error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2020-09-10 17:19:32 -04:00
|
|
|
// String stringer name for this implementation of dns.Store
|
|
|
|
func (c *OperatorDNS) String() string {
|
|
|
|
return "webhookDNS"
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:20:49 -04:00
|
|
|
// OperatorDNS - represents dns config for MinIO k8s operator.
|
|
|
|
type OperatorDNS struct {
|
|
|
|
httpClient *http.Client
|
|
|
|
Endpoint string
|
2020-09-10 17:19:32 -04:00
|
|
|
rootCAs *x509.CertPool
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorOption - functional options pattern style for OperatorDNS
|
|
|
|
type OperatorOption func(*OperatorDNS)
|
|
|
|
|
|
|
|
// Authentication - custom username and password for authenticating at the endpoint
|
|
|
|
func Authentication(username, password string) OperatorOption {
|
|
|
|
return func(args *OperatorDNS) {
|
|
|
|
args.username = username
|
|
|
|
args.password = password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootCAs - add custom trust certs pool
|
2021-11-16 12:28:29 -05:00
|
|
|
func RootCAs(certPool *x509.CertPool) OperatorOption {
|
2020-09-10 17:19:32 -04:00
|
|
|
return func(args *OperatorDNS) {
|
2021-11-16 12:28:29 -05:00
|
|
|
args.rootCAs = certPool
|
2020-09-10 17:19:32 -04:00
|
|
|
}
|
2020-09-09 15:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOperatorDNS - initialize a new K8S Operator DNS set/unset values.
|
2020-09-10 17:19:32 -04:00
|
|
|
func NewOperatorDNS(endpoint string, setters ...OperatorOption) (Store, error) {
|
|
|
|
if endpoint == "" {
|
2020-09-09 15:20:49 -04:00
|
|
|
return nil, errors.New("invalid argument")
|
|
|
|
}
|
2020-09-10 17:19:32 -04:00
|
|
|
|
2020-09-09 15:20:49 -04:00
|
|
|
args := &OperatorDNS{
|
|
|
|
Endpoint: endpoint,
|
2020-09-10 17:19:32 -04:00
|
|
|
}
|
|
|
|
for _, setter := range setters {
|
|
|
|
setter(args)
|
|
|
|
}
|
|
|
|
args.httpClient = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 3 * time.Second,
|
|
|
|
KeepAlive: 5 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
ResponseHeaderTimeout: 3 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 3 * time.Second,
|
|
|
|
ExpectContinueTimeout: 3 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: args.rootCAs,
|
2020-09-09 15:20:49 -04:00
|
|
|
},
|
2020-09-10 17:19:32 -04:00
|
|
|
// Go net/http automatically unzip if content-type is
|
|
|
|
// gzip disable this feature, as we are always interested
|
|
|
|
// in raw stream.
|
|
|
|
DisableCompression: true,
|
2020-09-09 15:20:49 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|