Skip to content

Commit

Permalink
Using root ca when connect to storage (#193)
Browse files Browse the repository at this point in the history
* Support of storage tls connection has been added
  • Loading branch information
bma13 authored Apr 17, 2024
1 parent 9a362c8 commit b54e0b9
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 40 deletions.
4 changes: 2 additions & 2 deletions deploy/ydb-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.5.3
version: 0.5.4

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.5.3"
appVersion: "0.5.4"
19 changes: 19 additions & 0 deletions e2e/tests/data/ca.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIC/TCCAeWgAwIBAgIRAKt07W/6Gy+wIe5lk+0YyqwwDQYJKoZIhvcNAQELBQAw
FzEVMBMGA1UEAxMMdGVzdC1yb290LWNhMCAXDTI0MDQxMjA4MTM0NFoYDzIwNTQw
NDA1MDgxMzQ0WjAXMRUwEwYDVQQDEwx0ZXN0LXJvb3QtY2EwggEiMA0GCSqGSIb3
DQEBAQUAA4IBDwAwggEKAoIBAQCs4c67HN45wf9jokQLdxDsfLUO5I3FiPVE4uWZ
Ma2zNSL2pMJBk95Vmj6pP/3HA6llUm3flVotzVzHh3C0j/WBZf6YE31eWlyMokuE
uLAGfKw/qL+gqC6Phoa72f9kJwnGXsVMDZijAEyqNquLZwgkK+4jgQcVhpGi/3ws
fop0qYVcK5LKAT5lGSx0MEuW74jheLDlscMsmUqVl2SCWRC/UGY+nUOTpcKK8228
Corc+DEFstqOIXGH9n/k0ZmBxjh8eU4IRp+LiDcB6x/yI4edAYJK/mnejmSA2i6a
K2mSzCJfBSVnDxwiGWY6xm8eAh6MaDU6iuqqkFFPltGl90CRAgMBAAGjQjBAMA4G
A1UdDwEB/wQEAwICpDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTFXnzlk4tO
SosBlUEM7AiDOYuW7TANBgkqhkiG9w0BAQsFAAOCAQEAJHTeKc1ySltDwwINFVp1
z5kFlIMyp3l146xn6qT5VWzYP4dZWdJz3gjAML56HRCHNe6B3MijjQY8sRObD5YI
589xpEhLMr+JR/DmU3Yol0XGILUdZ6TeK6FK+U3gYJdy3U39rcV2usEGfN5SRV4b
rUZg8asLFWPY7cdWBNIkF2yuJcF6PIpnuhzbfiEtOZ9ucvgnc62XPDnIuMSBKojG
Bj7QfqadEddSOztZFL00FZULIwSVE/8o0+HQvTBGjbZMuvuSBTJswujYUkD5Sy2O
wfc+dMJ6dbcDZR+5Q1kLEg+Oq7jjx/BTS35Axo1fPxO/WumJppTUmuEbFtEoX47o
fQ==
-----END CERTIFICATE-----
115 changes: 115 additions & 0 deletions e2e/tests/data/storage-block-4-2-config-tls.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
static_erasure: block-4-2
host_configs:
- drive:
- path: SectorMap:1:1
type: SSD
host_config_id: 1
domains_config:
domain:
- name: Root
storage_pool_types:
- kind: ssd
pool_config:
box_id: 1
erasure_species: block-4-2
kind: ssd
pdisk_filter:
- property:
- type: SSD
vdisk_kind: Default
state_storage:
- ring:
node: [1, 2, 3, 4, 5, 6, 7, 8]
nto_select: 5
ssid: 1
table_service_config:
sql_version: 1
actor_system_config:
executor:
- name: System
threads: 1
type: BASIC
- name: User
threads: 1
type: BASIC
- name: Batch
threads: 1
type: BASIC
- name: IO
threads: 1
time_per_mailbox_micro_secs: 100
type: IO
- name: IC
spin_threshold: 10
threads: 4
time_per_mailbox_micro_secs: 100
type: BASIC
scheduler:
progress_threshold: 10000
resolution: 256
spin_threshold: 0
blob_storage_config:
service_set:
groups:
- erasure_species: block-4-2
rings:
- fail_domains:
- vdisk_locations:
- node_id: storage-0
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-1
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-2
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-3
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-4
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-5
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-6
pdisk_category: SSD
path: SectorMap:1:1
- vdisk_locations:
- node_id: storage-7
pdisk_category: SSD
path: SectorMap:1:1
channel_profile_config:
profile:
- channel:
- erasure_species: block-4-2
pdisk_category: 1
storage_pool_kind: ssd
- erasure_species: block-4-2
pdisk_category: 1
storage_pool_kind: ssd
- erasure_species: block-4-2
pdisk_category: 1
storage_pool_kind: ssd
profile_id: 0
grpc_config:
start_grpc_proxy: true
ssl_port: 2135
ca: /tls/grpc/ca.crt
cert: /tls/grpc/tls.crt
key: /tls/grpc/tls.key
grpc_memory_quota_bytes: '1073741824'
host: '[::]'
keep_alive_enable: true
keep_alive_idle_timeout_trigger_sec: 90
keep_alive_max_probe_count: 3
keep_alive_probe_interval_sec: 10
services: [legacy, yql, scripting, cms, discovery, monitoring, import, export, locking, maintenance]
streaming_config: {enable_output_streams: true}
21 changes: 21 additions & 0 deletions e2e/tests/data/tls.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDYDCCAkigAwIBAgIRAKHQc3G24VDsWff5qQxyl3EwDQYJKoZIhvcNAQELBQAw
FzEVMBMGA1UEAxMMdGVzdC1yb290LWNhMB4XDTI0MDQxMjA4MTgxOVoXDTM5MDQw
OTA4MTgxOVowDzENMAsGA1UEChMEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEP
ADCCAQoCggEBAMqGcyqm3UIEZCL3Ge8Qh63OU0QykI3ymtj9gDJ57vtZdUOpHF18
QsX4Urf6vGNvI8gWXEtPbK8Dchqo+3Reuejjq8aRFs1RaPVZoWPD8i782p6L6/oX
4k5zAwvCdjC2y/YuUf4GTZqpfwDbhSfH+EdacrfwmYjLxYaehEn7z9M67R5wSekr
1bMuxBKZW1sclmip3JRf3uBuHfMLkoYpTa7KcEycn9YstZ1h2XuWFLk42GS4bMZs
Cq2E3sEqD8LOyCHs/qWWGM3Txz6edLTi5U6XtzPeWwxyM5W7fOpC0Q3iw58myGsm
DDmUngyneS/mzyLvKTywzsu2sRAqdjqsZCUCAwEAAaOBrjCBqzAdBgNVHSUEFjAU
BggrBgEFBQcDAQYIKwYBBQUHAwIwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBTF
Xnzlk4tOSosBlUEM7AiDOYuW7TBbBgNVHREEVDBSgiJzdG9yYWdlLWdycGMueWRi
LnN2Yy5jbHVzdGVyLmxvY2FsgiwqLnN0b3JhZ2UtaW50ZXJjb25uZWN0LnlkYi5z
dmMuY2x1c3Rlci5sb2NhbDANBgkqhkiG9w0BAQsFAAOCAQEAQYRvaGGFsnR4cp8Y
MJo948t7zI3Pgy20YonmYTriz1zeEYNj9+5t678p04FlCjIx0j4dad1tFC1bNtnI
FOJNMkhiyC1JPKSN7HR90L5P9JfsuunUVTNEHP6EuLj2/VnMXj+30qX9i0kVkcnH
OV9yXk8cpN0GuUUyfKUq5k/WS+/15JRoLJ9F8vS1lm77nHeQ9F7m1Yjqkc131N3k
czFLe3wexWhnKCnNw5OtdFgJsMgL09pYgnN+0xyE4iLY8f0o/b58J/tpbKkeJb9L
YogP1+ultfGvrlX8TE6iAEi8UA+rZCNpGwiqTI2Y9clZNnkYu7UgW7kn2ih5/vgU
xntdsA==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions e2e/tests/data/tls.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyoZzKqbdQgRkIvcZ7xCHrc5TRDKQjfKa2P2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H8d9bMzR6CbyljyXH/wvd0SKCo4gZQ
x1M6hdEVWm30JM8nJ8d/fyXqkeySzvlvDtSMbbFkDkvvZms/FNSioyMYOLiOTiLu
TAdsNxoNhEDRte2MMKDGfkECgYAGH1o8xr2gbVWSSYv8M5+4osUYpmqsNF0myxME
Vi2tckfTe5gH2fxeM+tKpyLGXkIqlgUh4f1Aiz9w0jU9eIhKR5bDy4Wa1h68nMuL
araw4RK8lS8GAa04VcKC7kgFy+/oZZJ8rTNPmZMvzoBik1x2oK0jAC29OzJM13gP
LuyXYQKBgQCvkar/nthVUlScVa3uPzqKdpNlqmeXQRMVwcmRQJcJoHYWkaF2RRAx
AcjWTkP6/ThUgPhI76O0neQqO2N0P+FnCkaE4qU6Wg+dZcH0q7HVnt2TRuCzsECQ
kxDqdbF+qbXmYfbJ4dKkJdF0dNad4/d2hL/wbvyHXDEEGpuIlZAZrg==
-----END RSA PRIVATE KEY-----
51 changes: 51 additions & 0 deletions e2e/tests/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,57 @@ var _ = Describe("Operator smoke test", func() {
}
})

It("using grpcs for storage connection", func() {
By("create secret...")
cert := testobjects.DefaultCertificate(
filepath.Join(".", "data", "tls.crt"),
filepath.Join(".", "data", "tls.key"),
filepath.Join(".", "data", "ca.crt"),
)
Expect(k8sClient.Create(ctx, cert)).Should(Succeed())
defer func() {
Expect(k8sClient.Delete(ctx, cert)).Should(Succeed())
}()

By("create storage...")
storageSample = testobjects.DefaultStorage(filepath.Join(".", "data", "storage-block-4-2-config-tls.yaml"))
storageSample.Spec.Service.GRPC.TLSConfiguration.Enabled = true
storageSample.Spec.Service.GRPC.TLSConfiguration.Certificate = corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: testobjects.CertificateSecretName},
Key: "tls.crt",
}
storageSample.Spec.Service.GRPC.TLSConfiguration.Key = corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: testobjects.CertificateSecretName},
Key: "tls.key",
}
storageSample.Spec.Service.GRPC.TLSConfiguration.CertificateAuthority = corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: testobjects.CertificateSecretName},
Key: "ca.crt",
}

Expect(k8sClient.Create(ctx, storageSample)).Should(Succeed())
defer func() {
Expect(k8sClient.Delete(ctx, storageSample)).Should(Succeed())
}()
By("create database...")
Expect(k8sClient.Create(ctx, databaseSample)).Should(Succeed())
defer func() {
Expect(k8sClient.Delete(ctx, databaseSample)).Should(Succeed())
}()

By("waiting until Storage is ready...")
waitUntilStorageReady(ctx, storageSample.Name, testobjects.YdbNamespace)

By("checking that all the storage pods are running and ready...")
checkPodsRunningAndReady(ctx, "ydb-cluster", "kind-storage", storageSample.Spec.Nodes)

By("waiting until database is ready...")
waitUntilDatabaseReady(ctx, databaseSample.Name, testobjects.YdbNamespace)

By("checking that all the database pods are running and ready...")
checkPodsRunningAndReady(ctx, "ydb-cluster", "kind-database", databaseSample.Spec.Nodes)
})

AfterEach(func() {
Expect(uninstallOperatorWithHelm(testobjects.YdbNamespace)).Should(BeTrue())
Expect(k8sClient.Delete(ctx, &namespace)).Should(Succeed())
Expand Down
35 changes: 29 additions & 6 deletions e2e/tests/test-objects/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
)

const (
YdbImage = "cr.yandex/crptqonuodf51kdj7a7d/ydb:23.3.17"
YdbNamespace = "ydb"
StorageName = "storage"
DatabaseName = "database"
DefaultDomain = "Root"
ReadyStatus = "Ready"
YdbImage = "cr.yandex/crptqonuodf51kdj7a7d/ydb:23.3.17"
YdbNamespace = "ydb"
StorageName = "storage"
DatabaseName = "database"
CertificateSecretName = "storage-crt"
DefaultDomain = "Root"
ReadyStatus = "Ready"
)

func constructAntiAffinityFor(key, value string) *corev1.Affinity {
Expand Down Expand Up @@ -162,3 +163,25 @@ func DefaultDatabase() *v1alpha1.Database {
},
}
}

func DefaultCertificate(certPath, keyPath, caPath string) *corev1.Secret {
cert, err := os.ReadFile(certPath)
Expect(err).To(BeNil())
key, err := os.ReadFile(keyPath)
Expect(err).To(BeNil())
ca, err := os.ReadFile(caPath)
Expect(err).To(BeNil())

return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: CertificateSecretName,
Namespace: YdbNamespace,
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{
"ca.crt": ca,
"tls.crt": cert,
"tls.key": key,
},
}
}
2 changes: 2 additions & 0 deletions internal/cms/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (t *Tenant) Create(
ctx context.Context,
database *resources.DatabaseBuilder,
creds ydbCredentials.Credentials,
opts ...ydb.Option,
) error {
logger := log.FromContext(ctx)
createDatabaseURL := fmt.Sprintf(
Expand All @@ -42,6 +43,7 @@ func (t *Tenant) Create(
db, err := connection.Open(ctx,
createDatabaseURL,
ydb.WithCredentials(creds),
ydb.MergeOptions(opts...),
)
if err != nil {
logger.Error(err, "Error connecting to YDB storage")
Expand Down
29 changes: 21 additions & 8 deletions internal/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -41,14 +42,26 @@ func Close(ctx context.Context, db *ydb.Driver) {
}
}

func LoadTLSCredentials(secure bool) grpc.DialOption {
if secure {
certPool, _ := x509.SystemCertPool()
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
func LoadTLSCredentials(secure bool, caBundle []byte) (grpc.DialOption, error) {
if !secure {
return grpc.WithTransportCredentials(insecure.NewCredentials()), nil
}
var certPool *x509.CertPool
if len(caBundle) > 0 {
certPool = x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(caBundle); !ok {
return nil, errors.New("failed to parse CA bundle")
}
} else {
var err error
certPool, err = x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("failed to get system cert pool, error: %w", err)
}
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
}
return grpc.WithTransportCredentials(insecure.NewCredentials())
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
}
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil
}
12 changes: 11 additions & 1 deletion internal/controllers/database/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,18 @@ func (r *Reconciler) handleTenantCreation(
)
return Stop, ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
}
tlsOptions, err := resources.GetYDBTLSOption(ctx, database.Storage, r.Config)
if err != nil {
r.Recorder.Event(
database,
corev1.EventTypeWarning,
"ControllerError",
fmt.Sprintf("Failed to get YDB TLS options: %s", err),
)
return Stop, ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err
}

err = tenant.Create(ctx, database, creds)
err = tenant.Create(ctx, database, creds, tlsOptions)
if err != nil {
r.Recorder.Event(
database,
Expand Down
Loading

0 comments on commit b54e0b9

Please sign in to comment.