From 8eb0d3d4a986f76d0da7c4d9775a0d1c6adc26ec Mon Sep 17 00:00:00 2001 From: Cesar Celis Hernandez Date: Fri, 27 Oct 2023 12:16:02 -0400 Subject: [PATCH] Proposal for controlling minio via CRD in a declarative way --- .gitignore | 4 +- go.mod | 1 + go.sum | 5 ++ manifests/minio.min.io_tenants.yaml | 9 ++ pkg/apis/minio.min.io/v2/types.go | 12 ++- pkg/controller/main-controller.go | 10 ++- pkg/controller/operator.go | 133 ++++++++++++++++++++++++++++ pkg/controller/status.go | 27 ++++++ 8 files changed, 196 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 1e1c59cdfa8..17c4dbd5fb9 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ nancy examples/.DS_Store testing/openshift/bundle/* examples/**/obj/ -.idea \ No newline at end of file +.idea +operator.iml +go_build_github_com_minio_operator_cmd_operator diff --git a/go.mod b/go.mod index df4839cf46e..9b6efef8a27 100644 --- a/go.mod +++ b/go.mod @@ -133,6 +133,7 @@ require ( github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/sha256-simd v1.0.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/moby/spdystream v0.2.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a // indirect diff --git a/go.sum b/go.sum index 7f641d04d38..85417df5f86 100644 --- a/go.sum +++ b/go.sum @@ -191,6 +191,8 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= @@ -474,6 +476,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR github.com/gopherjs/gopherjs v0.0.0-20220104163920-15ed2e8cf2bd/go.mod h1:cz9oNYuRUWGdHmLF2IodMLkAhcPtXeULvcBNagUrxTI= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= @@ -658,6 +661,8 @@ github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= +github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/manifests/minio.min.io_tenants.yaml b/manifests/minio.min.io_tenants.yaml index d18f067d261..ed545d9ab21 100644 --- a/manifests/minio.min.io_tenants.yaml +++ b/manifests/minio.min.io_tenants.yaml @@ -804,6 +804,13 @@ spec: type: string type: object type: array + ldapPolicyAttachToSingleUser: + type: object + properties: + user: + type: string + policy: + type: string certConfig: properties: commonName: @@ -4938,6 +4945,8 @@ spec: type: array provisionedBuckets: type: boolean + ldapPolicyAttachToSingleUser: + type: boolean provisionedUsers: type: boolean revision: diff --git a/pkg/apis/minio.min.io/v2/types.go b/pkg/apis/minio.min.io/v2/types.go index 8c6ed8c6ec2..5666cae1ef8 100644 --- a/pkg/apis/minio.min.io/v2/types.go +++ b/pkg/apis/minio.min.io/v2/types.go @@ -72,6 +72,12 @@ type TenantDomains struct { Console string `json:"console,omitempty"` } +type LDAPPolicyAttach struct { + User string `json:"user,omitempty"` + Group string `json:"group,omitempty"` + Policy string `json:"policy,omitempty"` +} + // Features (`features`) - Object describing which MinIO features to enable/disable in the MinIO Tenant. + type Features struct { // *Optional* + @@ -360,7 +366,8 @@ type TenantSpec struct { // // If provided, statefulset will add these volumes. You should set the rules for the corresponding volumes and volume mounts. We will not test this rule, k8s will show the result. // +optional - AdditionalVolumeMounts []corev1.VolumeMount `json:"additionalVolumeMounts,omitempty"` + AdditionalVolumeMounts []corev1.VolumeMount `json:"additionalVolumeMounts,omitempty"` + LDAPPolicyAttachToSingleUser *LDAPPolicyAttach `json:"ldapPolicyAttachToSingleUser,omitempty"` } // Logging describes Logging for MinIO tenants. @@ -545,7 +552,8 @@ type TenantStatus struct { // // Health Message regarding the State of the tenant // ProvisionedBuckets keeps track for telling if operator already created initial buckets for the tenant - ProvisionedBuckets bool `json:"provisionedBuckets,omitempty"` + ProvisionedBuckets bool `json:"provisionedBuckets,omitempty"` + LDAPPolicyAttachToSingleUser bool `json:"ldapPolicyAttachToSingleUser,omitempty"` } // CertificateConfig (`certConfig`) defines controlling attributes associated to any TLS certificate automatically generated by the Operator as part of tenant creation. These fields have no effect if `spec.autoCert: false`. diff --git a/pkg/controller/main-controller.go b/pkg/controller/main-controller.go index 6ca10b6d7ac..5d2f6292ef8 100644 --- a/pkg/controller/main-controller.go +++ b/pkg/controller/main-controller.go @@ -19,6 +19,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/minio/operator/pkg/utils" "net/http" "os" "os/signal" @@ -26,8 +27,6 @@ import ( "syscall" "time" - "github.com/minio/operator/pkg/utils" - "github.com/minio/madmin-go/v2" "github.com/minio/operator/pkg/common" xcerts "github.com/minio/pkg/certs" @@ -1381,6 +1380,13 @@ func (c *Controller) syncHandler(key string) (Result, error) { c.RegisterEvent(ctx, tenant, corev1.EventTypeNormal, "BucketsCreated", "Buckets created") } + // Section to attach LDAP Policy to a user. + if tenant.Status.LDAPPolicyAttachToSingleUser { + klog.Info("A user already got the LDAP policy attached") + } else if len(tenant.Spec.LDAPPolicyAttachToSingleUser.User) > 0 { + c.ldapPolicyAttachToSingleUser(ctx, tenant) + } + // Finally, we update the status block of the Tenant resource to reflect the // current state of the world tenant, err = c.updateTenantStatus(ctx, tenant, StatusInitialized, totalAvailableReplicas) diff --git a/pkg/controller/operator.go b/pkg/controller/operator.go index 2c6c09673d4..815df7668f4 100644 --- a/pkg/controller/operator.go +++ b/pkg/controller/operator.go @@ -22,8 +22,13 @@ import ( "crypto/tls" "crypto/x509" "fmt" + corev1 "k8s.io/api/core/v1" + runetimetime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/clientcmd" + "k8s.io/client-go/tools/remotecommand" "net" "net/http" + "strings" "time" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -314,6 +319,134 @@ func (c *Controller) createUsers(ctx context.Context, tenant *miniov2.Tenant, te return err } +func (c *Controller) ldapPolicyAttachToSingleUser(ctx context.Context, tenant *miniov2.Tenant) (err error) { + // This commands has not been executed yet and they were requested + klog.Info("mc_command: There is a mc command to execute") + // For now, we are using the mc binary on the first pod of the first pool. + // If proposal is approved, we can proceed to improve it further. + tenantName := tenant.Name + tenantNamespace := tenant.Namespace + pool0Name := tenant.Spec.Pools[0].Name + podName := tenantName + "-" + pool0Name + "-0" + request := c.kubeClientSet.CoreV1().RESTClient().Post().Resource("pods").Name(podName).Namespace( + tenantNamespace).SubResource("exec") + scheme := runetimetime.NewScheme() + if err := corev1.AddToScheme(scheme); err != nil { + panic("some issue") + } + parameterCodec := runetimetime.NewParameterCodec(scheme) + LDAPPolicyAttachToSingleUser := tenant.Spec.LDAPPolicyAttachToSingleUser + klog.Info(LDAPPolicyAttachToSingleUser) + User := LDAPPolicyAttachToSingleUser.User + klog.Info(User) + Policy := LDAPPolicyAttachToSingleUser.Policy + klog.Info(Policy) + TenantVars := tenant.GetEnvVars() + klog.Info(TenantVars) + valye := tenant.MinIOServerHostAddress() + MinIOServerEndpoint := tenant.MinIOServerEndpoint() + tenant.ConfigurationSecretName() + userCredentials := c.fetchUserCredentials(ctx, tenant) + tenant.HasConfigurationSecret() + tenantSpecConfigName := tenant.Spec.Configuration.Name + klog.Info(tenantSpecConfigName) + usersss := tenant.Spec.Users + klog.Info(usersss) + secret, _ := c.kubeClientSet.CoreV1().Secrets(tenant.Namespace).Get(ctx, tenantSpecConfigName, metav1.GetOptions{}) + secretData := secret.Data + content := string(secretData["config.env"]) + contents := strings.Split(content, "\n") + klog.Info(contents) + root_access_key := "" + root_secret_key := "" + for i := range contents { + contenido := contents[i] + klog.Info(contenido) + if strings.Contains(contenido, "MINIO_ROOT_USER") { + valor := strings.Split(contenido, "=") + usuario := valor[1] + usuarioPelon := usuario[1 : len(usuario)-1] + klog.Info(usuarioPelon) + root_access_key = usuarioPelon + } + if strings.Contains(contenido, "MINIO_ROOT_PASSWORD") { + valor := strings.Split(contenido, "=") + password := valor[1] + passwordPelon := password[1 : len(password)-1] + klog.Info(passwordPelon) + root_secret_key = passwordPelon + } + } + klog.Info(root_access_key) + klog.Info(root_secret_key) + klog.Info(MinIOServerEndpoint) + klog.Info(valye) + console_access_key := string(userCredentials[0].Data["CONSOLE_ACCESS_KEY"]) + console_secret_key := string(userCredentials[0].Data["CONSOLE_SECRET_KEY"]) + klog.Info(console_access_key) + klog.Info(console_secret_key) + // export set HOME=/tmp is to allow mc to work when not using root user in security context to avoid + // mc: Unable to save new mc config. mkdir /.mc: permission denied. + alias := "export set HOME=/tmp; mc alias set myminio " + MinIOServerEndpoint + " " + root_access_key + " " + root_secret_key + ldpapolicyattachtosingleuser_command := "mc idp ldap policy attach myminio " + Policy + " --user=" + "'" + User + "'" + klog.Info(ldpapolicyattachtosingleuser_command) + klog.Info(alias) + final_command := alias + "; " + ldpapolicyattachtosingleuser_command + cmds := []string{ + "sh", + "-c", + final_command, + } + request.VersionedParams(&corev1.PodExecOptions{ + Stdin: false, + Stdout: true, + Stderr: true, + TTY: false, + Container: "minio", + Command: cmds, + }, parameterCodec) + // Get a rest.Config from the kubeconfig file. This will be passed into all + // the client objects we create. + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + configOverrides := &clientcmd.ConfigOverrides{} + kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides) + restConfig, err := kubeconfig.ClientConfig() + if err != nil { + panic(err) + } + exec, err := remotecommand.NewSPDYExecutor(restConfig, "POST", request.URL()) + fmt.Println(exec) + if err != nil { + panic(err.Error()) + } + var stdout, stderr bytes.Buffer + fmt.Println(stdout) + fmt.Println(stderr) + err = exec.Stream(remotecommand.StreamOptions{ + Stdin: nil, + Stdout: &stdout, + Stderr: &stderr, + Tty: false, + }) + if err != nil { + klog.Info(err) + } + actualOutput := stdout.String() + actualError := stderr.String() + klog.Infof("Output from pod: %v", actualOutput) + klog.Infof("Error from pod: %v", actualError) + // If expected output is contained by the actual output, it will be + // considered as a success. + if strings.Contains(actualError, "The specified policy change is already in effect") { + klog.Info("Command(s) succeeded, will not be executed again") + // By saving the command in the status, we guarantee that will not be re-executed. + if _, err = c.updateLDAPPolicyAttachToSingleUserStatus(ctx, tenant, true); err != nil { + klog.V(2).Infof(err.Error()) + } + } + return nil +} + func (c *Controller) createBuckets(ctx context.Context, tenant *miniov2.Tenant, tenantConfiguration map[string][]byte) (err error) { defer func() { if err == nil { diff --git a/pkg/controller/status.go b/pkg/controller/status.go index a3666dcb96e..7343c5ce75b 100644 --- a/pkg/controller/status.go +++ b/pkg/controller/status.go @@ -29,6 +29,33 @@ func (c *Controller) updateTenantStatus(ctx context.Context, tenant *miniov2.Ten return c.updateTenantStatusWithRetry(ctx, tenant, currentState, availableReplicas, true) } +func (c *Controller) updateLDAPPolicyAttachToSingleUserStatus(ctx context.Context, tenant *miniov2.Tenant, succeeded bool) (*miniov2.Tenant, error) { + return c.updateLDAPPolicyAttachToSingleUserWithRetry(ctx, tenant, succeeded, true) +} + +func (c *Controller) updateLDAPPolicyAttachToSingleUserWithRetry(ctx context.Context, tenant *miniov2.Tenant, succeeded bool, retry bool) ( + *miniov2.Tenant, error) { + tenantCopy := tenant.DeepCopy() + tenantCopy.Spec = miniov2.TenantSpec{} + tenantCopy.Status = *tenant.Status.DeepCopy() + tenantCopy.Status.LDAPPolicyAttachToSingleUser = succeeded + opts := metav1.UpdateOptions{} + t, err := c.minioClientSet.MinioV2().Tenants(tenant.Namespace).UpdateStatus(ctx, tenantCopy, opts) + t.EnsureDefaults() + if err != nil { + if k8serrors.IsConflict(err) && retry { + klog.Info("Hit conflict issue, getting latest version of tenant") + tenant, err = c.minioClientSet.MinioV2().Tenants(tenant.Namespace).Get(ctx, tenant.Name, metav1.GetOptions{}) + if err != nil { + return tenant, err + } + return c.updateLDAPPolicyAttachToSingleUserWithRetry(ctx, tenant, succeeded, false) + } + return t, err + } + return t, nil +} + func (c *Controller) updateTenantStatusWithRetry(ctx context.Context, tenant *miniov2.Tenant, currentState string, availableReplicas int32, retry bool) (*miniov2.Tenant, error) { // If we are updating the tenant with the same status as before we are going to skip it as to avoid a resource number // change and have the operator loop re-processing the tenant endlessly