Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
moelsayed committed Feb 21, 2021
1 parent 007e478 commit 5785e8d
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 141 deletions.
2 changes: 1 addition & 1 deletion docs/proposals/20210112-encryption-roviders.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kind: KubeOneCluster
features:
encryptionProviders:
enabled: true
customProvidersFile: |
customEncryptionConfiguration: |
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/kubeone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ type Features struct {
// OpenIDConnect
OpenIDConnect *OpenIDConnect `json:"openidConnect,omitempty"`
// Encryption Providers
// +k8s:conversion-gen=false
EncryptionProviders *EncryptionProviders `json:"encryptionProviders,omitempty"`
}

Expand Down Expand Up @@ -575,6 +576,6 @@ type Addons struct {
type EncryptionProviders struct {
// Enable
Enable bool `json:"enable"`
// CustomProvidersFile
CustomProvidersFile string `json:"customProvidersFile"`
// CustomEncryptionConfiguration
CustomEncryptionConfiguration string `json:"customEncryptionConfiguration"`
}
21 changes: 6 additions & 15 deletions pkg/apis/kubeone/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,12 @@ type MachineControllerConfig struct {
type Features struct {
PodNodeSelector *PodNodeSelector `json:"podNodeSelector"`
// Deprecated: will be removed once Kubernetes 1.19 reaches EOL
PodPresets *PodPresets `json:"podPresets"`
PodSecurityPolicy *PodSecurityPolicy `json:"podSecurityPolicy"`
StaticAuditLog *StaticAuditLog `json:"staticAuditLog"`
DynamicAuditLog *DynamicAuditLog `json:"dynamicAuditLog"`
MetricsServer *MetricsServer `json:"metricsServer"`
OpenIDConnect *OpenIDConnect `json:"openidConnect"`
EncryptionProviders *EncryptionProviders `json:"encryptionProviders,omitempty"`
PodPresets *PodPresets `json:"podPresets"`
PodSecurityPolicy *PodSecurityPolicy `json:"podSecurityPolicy"`
StaticAuditLog *StaticAuditLog `json:"staticAuditLog"`
DynamicAuditLog *DynamicAuditLog `json:"dynamicAuditLog"`
MetricsServer *MetricsServer `json:"metricsServer"`
OpenIDConnect *OpenIDConnect `json:"openidConnect"`
}

// SystemPackages controls configurations of APT/YUM
Expand Down Expand Up @@ -323,11 +322,3 @@ type Addons struct {
// Path on the local file system to the directory with addons manifests.
Path string `json:"path"`
}

// Encryption Providers feature flag
type EncryptionProviders struct {
// Enable
Enable bool `json:"enable"`
// CustomProvidersFile
CustomProvidersFile string `json:"customProvidersFile"`
}
35 changes: 1 addition & 34 deletions pkg/apis/kubeone/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions pkg/apis/kubeone/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/apis/kubeone/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,6 @@ type Addons struct {
type EncryptionProviders struct {
// Enable
Enable bool `json:"enable"`
// CustomProvidersFile
CustomProvidersFile string `json:"customProvidersFile"`
// CustomEncryptionConfiguration
CustomEncryptionConfiguration string `json:"customEncryptionConfiguration"`
}
6 changes: 3 additions & 3 deletions pkg/apis/kubeone/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions pkg/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ func runApply(opts *applyOpts) error {
}

if opts.RotateEncryptionKey {
if s.Cluster.Features.EncryptionProviders.CustomProvidersFile != "" {
if !s.EncryptionEnabled() {
return errors.New("Encryption Providers support is not enabled for this cluster")
}

if s.Cluster.Features.EncryptionProviders != nil &&
s.Cluster.Features.EncryptionProviders.CustomEncryptionConfiguration != "" {
return errors.New("key rotation of custom providers file is not supported")
}
return runApplyRotateKey(s, opts)
Expand Down Expand Up @@ -332,7 +337,7 @@ func runApplyUpgradeIfNeeded(s *state.State, opts *applyOpts) error {
// disable case, we do this as early as possible.
if s.ShouldDisableEncryption() {
// something should happen here
tasksToRun = tasks.WithDisableEncryptionProviders(nil, s.Cluster.Features.EncryptionProviders.CustomProvidersFile != "")
tasksToRun = tasks.WithDisableEncryptionProviders(nil, s.Cluster.Features.EncryptionProviders.CustomEncryptionConfiguration != "")
}

tasksToRun = tasks.WithUpgrade(tasksToRun)
Expand Down Expand Up @@ -400,11 +405,11 @@ func runApplyUpgradeIfNeeded(s *state.State, opts *applyOpts) error {
func runApplyRotateKey(s *state.State, opts *applyOpts) error {
if !opts.ForceUpgrade {
s.Logger.Error("rotating encryption keys requires the --force-upgrade flag")
return nil
return errors.New("rotating encryption keys requires the --force-upgrade flag")
}
if !s.EncryptionEnabled() {
s.Logger.Error("rotating encryption keys failed: Encryption Providers support is not enabled")
return nil
return errors.New("rotating encryption keys failed: Encryption Providers support is not enabled")
}

fmt.Println("The following actions will be taken: ")
Expand Down
7 changes: 5 additions & 2 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ func createAndPrintManifest(printOptions *printOpts) error {
cfg.Set(yamled.Path{"features", "openidConnect", "config", "requiredClaim"}, "")
cfg.Set(yamled.Path{"features", "openidConnect", "config", "caFile"}, "")
}
if printOptions.EnableEncryptionProviders {
cfg.Set(yamled.Path{"features", "encryptionProviders", "enable"}, printOptions.EnableEncryptionProviders)
}

// machine-controller
if !printOptions.DeployMachineController {
Expand Down Expand Up @@ -685,8 +688,8 @@ features:
# For more information: https://kubernetes.io/docs/tasks/administer-cluster/encrypt-data/
encryptionProviders:
# disabled by default
enable: {{ .EnableEncryptionProviders }}
customProvidersConfig: # inline string
enable: {{ .EnableEncryptionProviders }}
customProvidersConfig: # inline string
systemPackages:
# will add Docker and Kubernetes repositories to OS package manager
configureRepositories: true # it's true by default
Expand Down
2 changes: 2 additions & 0 deletions pkg/state/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync"

"github.com/Masterminds/semver/v3"

apiserverconfigv1 "k8s.io/apiserver/pkg/apis/config/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -41,6 +42,7 @@ type EncryptionConfiguration struct {
Config *apiserverconfigv1.EncryptionConfiguration
Custom bool
}

type Host struct {
Config *kubeone.HostConfig

Expand Down
2 changes: 1 addition & 1 deletion pkg/state/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *State) EncryptionEnabled() bool {
}

func (s *State) GetEncryptionProviderConfigName() string {
if (s.EncryptionEnabled() && s.Cluster.Features.EncryptionProviders.CustomProvidersFile != "") ||
if (s.EncryptionEnabled() && s.Cluster.Features.EncryptionProviders.CustomEncryptionConfiguration != "") ||
s.LiveCluster.EncryptionConfiguration.Custom {
return customEncryptionProvidersFile
}
Expand Down
58 changes: 21 additions & 37 deletions pkg/tasks/encryption_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ package tasks

import (
"context"
"errors"
"fmt"
"time"

"github.com/pkg/errors"

kubeoneapi "k8c.io/kubeone/pkg/apis/kubeone"
"k8c.io/kubeone/pkg/scripts"
"k8c.io/kubeone/pkg/ssh"
"k8c.io/kubeone/pkg/state"
"k8c.io/kubeone/pkg/templates"
encryptionproviders "k8c.io/kubeone/pkg/templates/encryptionproviders"

encryptionproviders "k8c.io/kubeone/pkg/templates/encryption-providers"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
apiserverconfigv1 "k8s.io/apiserver/pkg/apis/config/v1"
"k8s.io/client-go/util/retry"
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
kyaml "sigs.k8s.io/yaml"
)
Expand All @@ -55,9 +55,10 @@ func FetchEncryptionProvidersFile(s *state.State) error {
if err != nil {
return err
}

s.LiveCluster.Lock.Lock()
s.LiveCluster.EncryptionConfiguration.Config = &apiserverconfigv1.EncryptionConfiguration{}
err = kyaml.UnmarshalStrict([]byte(config), s.LiveCluster.EncryptionConfiguration.Config)
s.LiveCluster.Lock.Unlock()
return err
}

Expand All @@ -71,7 +72,9 @@ func UploadIdentityFirstEncryptionConficguration(s *state.State) error {

oldConfig := s.LiveCluster.EncryptionConfiguration.Config.DeepCopy()

encryptionproviders.UpdateEncryptionConfigDecryptOnly(oldConfig)
if err := encryptionproviders.UpdateEncryptionConfigDecryptOnly(oldConfig); err != nil {
return err
}

config, err := templates.KubernetesToYAML([]runtime.Object{oldConfig})
if err != nil {
Expand All @@ -89,7 +92,6 @@ func UploadEncryptionConfigurationWithNewKey(s *state.State) error {
return errors.New("failed to read live cluster encryption providers configuration")
}

// oldConfig := s.LiveCluster.EncryptionConfiguration.Config.DeepCopy()
if err := encryptionproviders.UpdateEncryptionConfigWithNewKey(s.LiveCluster.EncryptionConfiguration.Config); err != nil {
return err
}
Expand All @@ -111,8 +113,6 @@ func UploadEncryptionConfigurationWithoutOldKey(s *state.State) error {
return errors.New("failed to read live cluster encryption providers configuration")
}

// oldConfig := s.LiveCluster.EncryptionConfiguration.Config.DeepCopy()

encryptionproviders.UpdateEncryptionConfigRemoveOldKey(s.LiveCluster.EncryptionConfiguration.Config)

config, err := templates.KubernetesToYAML([]runtime.Object{s.LiveCluster.EncryptionConfiguration.Config})
Expand Down Expand Up @@ -144,40 +144,24 @@ func RewriteClusterSecrets(s *state.State) error {
if err != nil {
return err
}
for i := range secrets.Items {
secret := secrets.Items[i]
if err = s.DynamicClient.Update(context.Background(), &secret, &dynclient.UpdateOptions{}); err != nil {
if kerrors.IsConflict(err) {
err = s.DynamicClient.Get(context.Background(), types.NamespacedName{Name: secret.Name, Namespace: secret.Namespace}, &secret)
if err != nil {
return err
}
if err = s.DynamicClient.Update(context.Background(), &secret, &dynclient.UpdateOptions{}); err != nil {
return err
}
for _, secret := range secrets.Items {
updateErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
toRewrite := corev1.Secret{}
name := secret.Name
namespace := secret.Namespace
if err := s.DynamicClient.Get(s.Context, types.NamespacedName{Name: name, Namespace: namespace}, &toRewrite); err != nil {
return err
}
return err

return s.DynamicClient.Update(s.Context, &toRewrite)
})
if updateErr != nil {
return errors.WithStack(updateErr)
}
}
return nil
}

// FIXME: Static pods are not managed by the API, so we can't simply delete them to restart.
// We should use a cleaner method to do this.
func RestartKubeAPI(s *state.State) error {
s.Logger.Infof("Restarting KubeAPI...")
return s.RunTaskOnControlPlane(func(s *state.State, _ *kubeoneapi.HostConfig, _ ssh.Connection) error {
_, _, err := s.Runner.RunRaw(`docker restart $(docker ps --filter="label=io.kubernetes.container.name=kube-apiserver" -q)`)
return err
}, state.RunParallel)
}

func WaitForAPI(s *state.State) error {
s.Logger.Infof("Waiting %v to ensure all components are up...", 2*timeoutNodeUpgrade)
time.Sleep(2 * timeoutNodeUpgrade)
return nil
}

func RemoveEncryptionProviderFile(s *state.State) error {
s.Logger.Infof("Removing EncryptionProviders configuration file...")
return s.RunTaskOnControlPlane(func(s *state.State, _ *kubeoneapi.HostConfig, _ ssh.Connection) error {
Expand Down
Loading

0 comments on commit 5785e8d

Please sign in to comment.