Skip to content
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

feat(CSI-253): support custom CA certificate in API secret #324

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/common/csi-wekafs-api-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data:
# It is recommended to configure at least 2 management endpoints (cluster backend nodes), or a load-balancer if used
# e.g. 172.31.15.113:14000,172.31.12.91:14000
endpoints: MTcyLjMxLjQxLjU0OjE0MDAwLDE3Mi4zMS40Ny4xNTI6MTQwMDAsMTcyLjMxLjM4LjI1MDoxNDAwMCwxNzIuMzEuNDcuMTU1OjE0MDAwLDE3Mi4zMS4zMy45MToxNDAwMCwxNzIuMzEuMzguMTU1OjE0MDAwCg==
# protocol to use for API connection (may be either http or https, base64-encoded)
# protocol to use for API connection (may be either http or https, base64-encoded. NOTE: since Weka 4.3.0, HTTPS is mandatory)
scheme: aHR0cA==
# for multiple clusters setup, set specific container name rather than attempt to identify it automatically
localContainerName: ""
Expand All @@ -24,3 +24,7 @@ data:
# maybe either (true/false), base64-encoded
# NOTE: if a load balancer is used to access the cluster API, leave this setting as "false"
autoUpdateEndpoints: ZmFsc2U=
# When using HTTPS connection and self-signed or untrusted certificates, provide a CA certificate in PEM format, base64-encoded
# caCertificate: <base64-encoded-PEM>
caCertificate: ""

17 changes: 16 additions & 1 deletion pkg/wekafs/apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -89,9 +90,22 @@ func (e *ApiEndPoint) String() string {
}

func NewApiClient(ctx context.Context, credentials Credentials, allowInsecureHttps bool, hostname string) (*ApiClient, error) {
logger := log.Ctx(ctx)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: allowInsecureHttps},
}
useCustomCACert := credentials.CaCertificate != ""
if useCustomCACert {
var caCertPool *x509.CertPool
if pool, err := x509.SystemCertPool(); err != nil {
caCertPool = x509.NewCertPool()
} else {
caCertPool = pool
}
caCertPool.AppendCertsFromPEM([]byte(credentials.CaCertificate))
tr.TLSClientConfig.RootCAs = caCertPool
}

a := &ApiClient{
Mutex: sync.Mutex{},
client: &http.Client{
Expand All @@ -108,7 +122,7 @@ func NewApiClient(ctx context.Context, credentials Credentials, allowInsecureHtt
}
a.resetDefaultEndpoints(ctx)

log.Ctx(ctx).Trace().Bool("insecure_skip_verify", allowInsecureHttps).Msg("Creating new API client")
logger.Trace().Bool("insecure_skip_verify", allowInsecureHttps).Bool("custom_ca_cert", useCustomCACert).Msg("Creating new API client")
a.clientHash = a.generateHash()
return a, nil
}
Expand Down Expand Up @@ -756,6 +770,7 @@ type Credentials struct {
Endpoints []string
LocalContainerName string
AutoUpdateEndpoints bool
CaCertificate string
}

func (c *Credentials) String() string {
Expand Down
5 changes: 5 additions & 0 deletions pkg/wekafs/wekafs.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (api *ApiStore) fromSecrets(ctx context.Context, secrets map[string]string,
if ok {
autoUpdateEndpoints = strings.TrimSpace(strings.TrimSuffix(autoUpdateEndpointsStr, "\n")) == "true"
}
caCertificate, ok := secrets["caCertificate"]
if !ok {
caCertificate = ""
}

credentials := apiclient.Credentials{
Username: strings.TrimSpace(strings.TrimSuffix(secrets["username"], "\n")),
Expand All @@ -119,6 +123,7 @@ func (api *ApiStore) fromSecrets(ctx context.Context, secrets map[string]string,
HttpScheme: strings.TrimSpace(strings.TrimSuffix(secrets["scheme"], "\n")),
LocalContainerName: localContainerName,
AutoUpdateEndpoints: autoUpdateEndpoints,
CaCertificate: caCertificate,
}
return api.fromCredentials(ctx, credentials, hostname)
}
Expand Down
Loading