You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
5.9 KiB
222 lines
5.9 KiB
4 years ago
|
/*
|
||
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package dns
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"crypto/tls"
|
||
|
"crypto/x509"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
4 years ago
|
"strconv"
|
||
4 years ago
|
"time"
|
||
|
|
||
|
"github.com/dgrijalva/jwt-go"
|
||
|
"github.com/minio/minio/cmd/config"
|
||
4 years ago
|
xhttp "github.com/minio/minio/cmd/http"
|
||
4 years ago
|
)
|
||
|
|
||
|
var (
|
||
|
defaultOperatorContextTimeout = 10 * time.Second
|
||
4 years ago
|
// ErrNotImplemented - Indicates the functionality which is not implemented
|
||
4 years ago
|
ErrNotImplemented = errors.New("The method is not implemented")
|
||
|
)
|
||
|
|
||
4 years ago
|
func (c *OperatorDNS) addAuthHeader(r *http.Request) error {
|
||
|
if c.username == "" || c.password == "" {
|
||
|
return nil
|
||
|
}
|
||
4 years ago
|
|
||
|
claims := &jwt.StandardClaims{
|
||
|
ExpiresAt: int64(15 * time.Minute),
|
||
4 years ago
|
Issuer: c.username,
|
||
4 years ago
|
Subject: config.EnvDNSWebhook,
|
||
|
}
|
||
|
|
||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
|
||
4 years ago
|
ss, err := token.SignedString([]byte(c.password))
|
||
4 years ago
|
if err != nil {
|
||
4 years ago
|
return err
|
||
4 years ago
|
}
|
||
|
|
||
|
r.Header.Set("Authorization", "Bearer "+ss)
|
||
4 years ago
|
return nil
|
||
4 years ago
|
}
|
||
|
|
||
|
func (c *OperatorDNS) endpoint(bucket string, delete bool) (string, error) {
|
||
|
u, err := url.Parse(c.Endpoint)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
q := u.Query()
|
||
|
q.Add("bucket", bucket)
|
||
4 years ago
|
q.Add("delete", strconv.FormatBool(delete))
|
||
4 years ago
|
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 {
|
||
|
return err
|
||
|
}
|
||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, e, nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
4 years ago
|
if err = c.addAuthHeader(req); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
resp, err := c.httpClient.Do(req)
|
||
|
if err != nil {
|
||
4 years ago
|
if derr := c.Delete(bucket); derr != nil {
|
||
|
return derr
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
xhttp.DrainBody(resp.Body)
|
||
4 years ago
|
if resp.StatusCode != http.StatusOK {
|
||
|
return fmt.Errorf("request to create the service for bucket %s, failed with status %s", bucket, resp.Status)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
4 years ago
|
if err = c.addAuthHeader(req); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
resp, err := c.httpClient.Do(req)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
4 years ago
|
xhttp.DrainBody(resp.Body)
|
||
4 years ago
|
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 {
|
||
|
c.httpClient.CloseIdleConnections()
|
||
|
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
|
||
|
}
|
||
|
|
||
4 years ago
|
// String stringer name for this implementation of dns.Store
|
||
|
func (c *OperatorDNS) String() string {
|
||
|
return "webhookDNS"
|
||
|
}
|
||
|
|
||
4 years ago
|
// OperatorDNS - represents dns config for MinIO k8s operator.
|
||
|
type OperatorDNS struct {
|
||
|
httpClient *http.Client
|
||
|
Endpoint string
|
||
4 years ago
|
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
|
||
|
func RootCAs(CAs *x509.CertPool) OperatorOption {
|
||
|
return func(args *OperatorDNS) {
|
||
|
args.rootCAs = CAs
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
// NewOperatorDNS - initialize a new K8S Operator DNS set/unset values.
|
||
4 years ago
|
func NewOperatorDNS(endpoint string, setters ...OperatorOption) (Store, error) {
|
||
|
if endpoint == "" {
|
||
4 years ago
|
return nil, errors.New("invalid argument")
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
args := &OperatorDNS{
|
||
|
Endpoint: endpoint,
|
||
4 years ago
|
}
|
||
|
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,
|
||
4 years ago
|
},
|
||
4 years ago
|
// Go net/http automatically unzip if content-type is
|
||
|
// gzip disable this feature, as we are always interested
|
||
|
// in raw stream.
|
||
|
DisableCompression: true,
|
||
4 years ago
|
},
|
||
|
}
|
||
|
return args, nil
|
||
|
}
|