|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package resources |
| 24 | + |
| 25 | +import ( |
| 26 | + "crypto/sha256" |
| 27 | + "encoding/hex" |
| 28 | + "fmt" |
| 29 | + "sort" |
| 30 | + "strings" |
| 31 | + |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + |
| 34 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" |
| 35 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 36 | +) |
| 37 | + |
| 38 | +// ValidateSecretHashes checks the hash of used secrets |
| 39 | +// against the stored ones. |
| 40 | +// If a hash is different, the deployment is marked |
| 41 | +// with a SecretChangedCondition and the operator will no |
| 42 | +// touch it until this is resolved. |
| 43 | +func (r *Resources) ValidateSecretHashes() error { |
| 44 | + // validate performs a secret hash comparison for a single secret. |
| 45 | + // Return true if all is good, false when the SecretChanged condition |
| 46 | + // must be set. |
| 47 | + validate := func(secretName string, expectedHashRef *string, status *api.DeploymentStatus) (bool, error) { |
| 48 | + log := r.log.With().Str("secret-name", secretName).Logger() |
| 49 | + expectedHash := *expectedHashRef |
| 50 | + hash, err := r.getSecretHash(secretName) |
| 51 | + if expectedHash == "" { |
| 52 | + // No hash set yet, try to fill it |
| 53 | + if k8sutil.IsNotFound(err) { |
| 54 | + // Secret does not (yet) exists, do nothing |
| 55 | + return true, nil |
| 56 | + } |
| 57 | + if err != nil { |
| 58 | + log.Warn().Err(err).Msg("Failed to get secret") |
| 59 | + return true, nil // Since we do not yet have a hash, we let this go with only a warning. |
| 60 | + } |
| 61 | + // Hash fetched succesfully, store it |
| 62 | + *expectedHashRef = hash |
| 63 | + if r.context.UpdateStatus(*status); err != nil { |
| 64 | + log.Debug().Msg("Failed to save secret hash") |
| 65 | + return true, maskAny(err) |
| 66 | + } |
| 67 | + return true, nil |
| 68 | + } |
| 69 | + // Hash is set, it must match the current hash |
| 70 | + if err != nil { |
| 71 | + // Fetching error failed for other reason. |
| 72 | + log.Debug().Err(err).Msg("Failed to fetch secret hash") |
| 73 | + // This is not good, return false so SecretsChanged condition will be set. |
| 74 | + return false, nil |
| 75 | + } |
| 76 | + if hash != expectedHash { |
| 77 | + // Oops, hash has changed |
| 78 | + log.Debug(). |
| 79 | + Str("expected-hash", expectedHash). |
| 80 | + Str("new-hash", hash). |
| 81 | + Msg("Secret has changed.") |
| 82 | + // This is not good, return false so SecretsChanged condition will be set. |
| 83 | + return false, nil |
| 84 | + } |
| 85 | + // All good |
| 86 | + return true, nil |
| 87 | + } |
| 88 | + |
| 89 | + spec := r.context.GetSpec() |
| 90 | + log := r.log |
| 91 | + var badSecretNames []string |
| 92 | + status := r.context.GetStatus() |
| 93 | + if status.SecretHashes == nil { |
| 94 | + status.SecretHashes = &api.SecretHashes{} |
| 95 | + } |
| 96 | + hashes := status.SecretHashes |
| 97 | + if spec.IsAuthenticated() { |
| 98 | + secretName := spec.Authentication.GetJWTSecretName() |
| 99 | + if hashOK, err := validate(secretName, &hashes.AuthJWT, &status); err != nil { |
| 100 | + return maskAny(err) |
| 101 | + } else if !hashOK { |
| 102 | + badSecretNames = append(badSecretNames, secretName) |
| 103 | + } |
| 104 | + } |
| 105 | + if spec.RocksDB.IsEncrypted() { |
| 106 | + secretName := spec.RocksDB.Encryption.GetKeySecretName() |
| 107 | + if hashOK, err := validate(secretName, &hashes.RocksDBEncryptionKey, &status); err != nil { |
| 108 | + return maskAny(err) |
| 109 | + } else if !hashOK { |
| 110 | + badSecretNames = append(badSecretNames, secretName) |
| 111 | + } |
| 112 | + } |
| 113 | + if spec.IsSecure() { |
| 114 | + secretName := spec.TLS.GetCASecretName() |
| 115 | + if hashOK, err := validate(secretName, &hashes.TLSCA, &status); err != nil { |
| 116 | + return maskAny(err) |
| 117 | + } else if !hashOK { |
| 118 | + badSecretNames = append(badSecretNames, secretName) |
| 119 | + } |
| 120 | + } |
| 121 | + if spec.Sync.IsEnabled() { |
| 122 | + secretName := spec.Sync.TLS.GetCASecretName() |
| 123 | + if hashOK, err := validate(secretName, &hashes.SyncTLSCA, &status); err != nil { |
| 124 | + return maskAny(err) |
| 125 | + } else if !hashOK { |
| 126 | + badSecretNames = append(badSecretNames, secretName) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if len(badSecretNames) > 0 { |
| 131 | + // We have invalid hashes, set the SecretsChanged condition |
| 132 | + if status.Conditions.Update(api.ConditionTypeSecretsChanged, true, |
| 133 | + "Secrets have changed", fmt.Sprintf("Found %d changed secrets", len(badSecretNames))) { |
| 134 | + log.Warn().Msgf("Found %d changed secrets. Settings SecretsChanged condition", len(badSecretNames)) |
| 135 | + if err := r.context.UpdateStatus(status); err != nil { |
| 136 | + log.Error().Err(err).Msg("Failed to save SecretsChanged condition") |
| 137 | + return maskAny(err) |
| 138 | + } |
| 139 | + // Add an event about this |
| 140 | + r.context.CreateEvent(k8sutil.NewSecretsChangedEvent(badSecretNames, r.context.GetAPIObject())) |
| 141 | + } |
| 142 | + } else { |
| 143 | + // All good, we van remove the SecretsChanged condition |
| 144 | + if status.Conditions.Remove(api.ConditionTypeSecretsChanged) { |
| 145 | + log.Info().Msg("Resetting SecretsChanged condition") |
| 146 | + if err := r.context.UpdateStatus(status); err != nil { |
| 147 | + log.Error().Err(err).Msg("Failed to save SecretsChanged condition") |
| 148 | + return maskAny(err) |
| 149 | + } |
| 150 | + // Add an event about this |
| 151 | + r.context.CreateEvent(k8sutil.NewSecretsRestoredEvent(r.context.GetAPIObject())) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +// getSecretHash fetches a secret with given name and returns a hash over its value. |
| 159 | +func (r *Resources) getSecretHash(secretName string) (string, error) { |
| 160 | + kubecli := r.context.GetKubeCli() |
| 161 | + ns := r.context.GetNamespace() |
| 162 | + s, err := kubecli.CoreV1().Secrets(ns).Get(secretName, metav1.GetOptions{}) |
| 163 | + if err != nil { |
| 164 | + return "", maskAny(err) |
| 165 | + } |
| 166 | + // Create hash of value |
| 167 | + rows := make([]string, 0, len(s.Data)) |
| 168 | + for k, v := range s.Data { |
| 169 | + rows = append(rows, k+"="+hex.EncodeToString(v)) |
| 170 | + } |
| 171 | + // Sort so we're not detecting order differences |
| 172 | + sort.Strings(rows) |
| 173 | + data := strings.Join(rows, "\n") |
| 174 | + rawHash := sha256.Sum256([]byte(data)) |
| 175 | + hash := fmt.Sprintf("%0x", rawHash) |
| 176 | + return hash, nil |
| 177 | +} |
0 commit comments