// This file is part of MinIO Console Server
// Copyright (c) 2021 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package operatorapi

import (
	"context"
	"errors"
	"reflect"
	"testing"

	"github.com/minio/console/operatorapi/operations/operator_api"

	"github.com/minio/console/models"
	miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
	DeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
	DeleteSecretMock        func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
	CreateSecretMock        func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
	UpdateSecretMock        func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
)

func (c k8sClientMock) deletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
	return DeletePodCollectionMock(ctx, namespace, opts, listOpts)
}

func (c k8sClientMock) deleteSecret(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
	return DeleteSecretMock(ctx, namespace, name, opts)
}

func (c k8sClientMock) createSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
	return CreateSecretMock(ctx, namespace, secret, opts)
}

func (c k8sClientMock) updateSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) {
	return UpdateSecretMock(ctx, namespace, secret, opts)
}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PTFpYKy9pUjRDYkI2d0pzaExWbmZYSjJSQ096a0xwNVVYV3IzaURFVWFvMWJDMjJzcUJjRnZ2WllmL2l6ClhQbWJNSkNGS1BhSTZDT2ZJbGZXRWptYlFaZ0dSN21lZDNISkhFZDE3NTg5azBvN0RHeXB0bnl6MUs3akFvNmkKRFY5NFZ5NytCLzBuQWRkY1ZrVm5aTjJXU3RMam1xcTY2NGZtZmt0bTZRS0JnQzBsMk5OVlFWK2N0OFFRRFpINQpBRFIrSGM3Qk0wNDhURGhNTmhoR3JHc2tWNngwMCtMZTdISGdpT3h2NXJudkNlTlY2M001YUZHdFVWbllTN1VoCkE1NndaNVlZeFFnQ0xzNi9PRmZhK3NiTngrSjdnSjRjNXdMZVlJMXVPMTlzZHBHa2VHZ25vK3dXVmxDSzFCbW0KRGM0TXA2STRiUTVtdy93YVpLQnpjRTJLCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K"
	badCrt := "ASD"
	badKey := "ASD"
	type args struct {
		ctx                     context.Context
		opClient                OperatorClientI
		clientSet               K8sClientI
		namespace               string
		params                  operator_api.TenantUpdateCertificateParams
		mockTenantGet           func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error)
		mockDeleteSecret        func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
		mockCreateSecret        func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
		mockDeletePodCollection func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "error getting tenant information",
			args: args{
				ctx:       context.Background(),
				opClient:  opClient,
				clientSet: k8sClient,
				namespace: "",
				params:    operator_api.TenantUpdateCertificateParams{},
				mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) {
					return nil, errors.New("invalid tenant")
				},
			},
			wantErr: true,
		},
		{
			name: "error replacing external certs for tenant because of empty keypair values",
			args: args{
				ctx:       context.Background(),
				opClient:  opClient,
				clientSet: k8sClient,
				namespace: "",
				params: operator_api.TenantUpdateCertificateParams{
					Body: &models.TLSConfiguration{
						Minio: []*models.KeyPairConfiguration{
							{
								Crt: nil,
								Key: nil,
							},
						},
					},
				},
				mockDeletePodCollection: func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
					return nil
				},
				mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) {
					return &miniov2.Tenant{
						Spec: miniov2.TenantSpec{
							ExternalCertSecret: []*miniov2.LocalCertificateReference{
								{
									Name: "secret",
								},
							},
						},
					}, nil
				},
			},
			wantErr: true,
		},
		{
			name: "error replacing external certs for tenant because of malformed encoded certs",
			args: args{
				ctx:       context.Background(),
				opClient:  opClient,
				clientSet: k8sClient,
				namespace: "",
				params: operator_api.TenantUpdateCertificateParams{
					Body: &models.TLSConfiguration{
						Minio: []*models.KeyPairConfiguration{
							{
								Crt: &badCrt,
								Key: &badKey,
							},
						},
					},
				},
				mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) {
					return &miniov2.Tenant{
						Spec: miniov2.TenantSpec{
							ExternalCertSecret: []*miniov2.LocalCertificateReference{
								{
									Name: "secret",
								},
							},
						},
					}, nil
				},
				mockDeleteSecret: func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
					return nil
				},
			},
			wantErr: true,
		},
		{
			name: "error replacing external certs for tenant because of error during secret creation",
			args: args{
				ctx:       context.Background(),
				opClient:  opClient,
				clientSet: k8sClient,
				namespace: "",
				params: operator_api.TenantUpdateCertificateParams{
					Body: &models.TLSConfiguration{
						Minio: []*models.KeyPairConfiguration{
							{
								Crt: &crt,
								Key: &key,
							},
						},
					},
				},
				mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) {
					return &miniov2.Tenant{
						Spec: miniov2.TenantSpec{
							ExternalCertSecret: []*miniov2.LocalCertificateReference{
								{
									Name: "secret",
								},
							},
						},
					}, nil
				},
				mockDeleteSecret: func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
					return nil
				},
				mockCreateSecret: func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
					return nil, errors.New("error creating secret")
				},
			},
			wantErr: true,
		},
		{
			name: "certificates replaced but error during deleting existing tenant pods",
			args: args{
				ctx:       context.Background(),
				opClient:  opClient,
				clientSet: k8sClient,
				namespace: "",
				params: operator_api.TenantUpdateCertificateParams{
					Body: &models.TLSConfiguration{
						Minio: []*models.KeyPairConfiguration{
							{
								Crt: &crt,
								Key: &key,
							},
						},
					},
				},
				mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) {
					return &miniov2.Tenant{
						Spec: miniov2.TenantSpec{
							ExternalCertSecret: []*miniov2.LocalCertificateReference{
								{
									Name: "secret",
								},
							},
						},
					}, nil
				},
				mockDeleteSecret: func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
					return nil
				},
				mockCreateSecret: func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
					return &v1.Secret{}, nil
				},
				mockDeletePodCollection: func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
					return errors.New("error deleting minio pods")
				},
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		opClientTenantGetMock = tt.args.mockTenantGet
		DeleteSecretMock = tt.args.mockDeleteSecret
		CreateSecretMock = tt.args.mockCreateSecret
		DeletePodCollectionMock = tt.args.mockDeletePodCollection
		t.Run(tt.name, func(t *testing.T) {
			if err := tenantUpdateCertificates(tt.args.ctx, tt.args.opClient, tt.args.clientSet, tt.args.namespace, tt.args.params); (err != nil) != tt.wantErr {
				t.Errorf("tenantUpdateCertificates() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func Test_tenantUpdateEncryption(t *testing.T) {
	k8sClient := k8sClientMock{}
	opClient := opClientMock{}
	type args struct {
		ctx                     context.Context
		opClient                OperatorClientI
		clientSet               K8sClientI
		namespace               string
		params                  operator_api.TenantUpdateEncryptionParams
		mockTenantGet           func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error)
		mockDeleteSecret        func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
		mockCreateSecret        func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
		mockDeletePodCollection func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "error updating encryption configuration because of error getting tenant",
			args: args{
				ctx:       context.Background(),
				opClient:  opClient,
				clientSet: k8sClient,
				namespace: "",
				params:    operator_api.TenantUpdateEncryptionParams{},
				mockTenantGet: func(ctx context.Context, namespace string, tenantName string, options metav1.GetOptions) (*miniov2.Tenant, error) {
					return nil, errors.New("invalid tenant")
				},
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			opClientTenantGetMock = tt.args.mockTenantGet
			DeleteSecretMock = tt.args.mockDeleteSecret
			CreateSecretMock = tt.args.mockCreateSecret
			DeletePodCollectionMock = tt.args.mockDeletePodCollection
			if err := tenantUpdateEncryption(tt.args.ctx, tt.args.opClient, tt.args.clientSet, tt.args.namespace, tt.args.params); (err != nil) != tt.wantErr {
				t.Errorf("tenantUpdateEncryption() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func Test_createOrReplaceKesConfigurationSecrets(t *testing.T) {
	k8sClient := k8sClientMock{}
	crt := "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUUzRENDQTBTZ0F3SUJBZ0lRS0pDalNyK0xaMnJrYVo5ODAvZnV5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Us3akFvNmkKRFY5NFZ5NytCLzBuQWRkY1ZrVm5aTjJXU3RMam1xcTY2NGZtZmt0bTZRS0JnQzBsMk5OVlFWK2N0OFFRRFpINQpBRFIrSGM3Qk0wNDhURGhNTmhoR3JHc2tWNngwMCtMZTdISGdpT3h2NXJudkNlTlY2M001YUZHdFVWbllTN1VoCkE1NndaNVlZeFFnQ0xzNi9PRmZhK3NiTngrSjdnSjRjNXdMZVlJMXVPMTlzZHBHa2VHZ25vK3dXVmxDSzFCbW0KRGM0TXA2STRiUTVtdy93YVpLQnpjRTJLCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K"
	badCrt := "ASD"
	badKey := "ASD"
	appRole := "ASD"
	appSecret := "ASD"
	endpoint := "ASD"
	type args struct {
		ctx                        context.Context
		clientSet                  K8sClientI
		ns                         string
		encryptionCfg              *models.EncryptionConfiguration
		kesConfigurationSecretName string
		kesClientCertSecretName    string
		tenantName                 string
		mockDeleteSecret           func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
		mockCreateSecret           func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
	}
	tests := []struct {
		name    string
		args    args
		want    *v1.LocalObjectReference
		want1   *miniov2.LocalCertificateReference
		wantErr bool
	}{
		{
			name: "error decoding the client certificate",
			args: args{
				ctx:       context.Background(),
				clientSet: k8sClient,
				encryptionCfg: &models.EncryptionConfiguration{
					Client: &models.KeyPairConfiguration{
						Crt: &badCrt,
						Key: &badKey,
					},
				},
				ns:                         "default",
				kesConfigurationSecretName: "test-secret",
				kesClientCertSecretName:    "test-client-secret",
				tenantName:                 "test",
				mockDeleteSecret: func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
					return nil
				},
			},
			want:    nil,
			want1:   nil,
			wantErr: true,
		},
		{
			name: "error because of malformed decoded certificate",
			args: args{
				ctx:       context.Background(),
				clientSet: k8sClient,
				encryptionCfg: &models.EncryptionConfiguration{
					Client: &models.KeyPairConfiguration{
						Crt: &key, // will cause an error because we are passing a private key as the public key
						Key: &key,
					},
				},
				ns:                         "default",
				kesConfigurationSecretName: "test-secret",
				kesClientCertSecretName:    "test-client-secret",
				tenantName:                 "test",
				mockDeleteSecret: func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
					return nil
				},
			},
			want:    nil,
			want1:   nil,
			wantErr: true,
		},
		{
			name: "error because of malformed decoded certificate",
			args: args{
				ctx:       context.Background(),
				clientSet: k8sClient,
				encryptionCfg: &models.EncryptionConfiguration{
					Client: &models.KeyPairConfiguration{
						Crt: &crt,
						Key: &key,
					},
					Vault: &models.VaultConfiguration{
						Approle: &models.VaultConfigurationApprole{
							Engine: "",
							ID:     &appRole,
							Retry:  0,
							Secret: &appSecret,
						},
						Endpoint:  &endpoint,
						Engine:    "",
						Namespace: "",
						Prefix:    "",
						Status:    nil,
						TLS: &models.VaultConfigurationTLS{
							Ca:  crt,
							Crt: crt,
							Key: key,
						},
					},
				},
				ns:                         "default",
				kesConfigurationSecretName: "test-secret",
				kesClientCertSecretName:    "test-client-secret",
				tenantName:                 "test",
				mockDeleteSecret: func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
					return nil
				},
				mockCreateSecret: func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
					return &v1.Secret{}, nil
				},
			},
			want: &v1.LocalObjectReference{
				Name: "test-secret",
			},
			want1: &miniov2.LocalCertificateReference{
				Name: "test-client-secret",
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			DeleteSecretMock = tt.args.mockDeleteSecret
			CreateSecretMock = tt.args.mockCreateSecret
			got, got1, err := createOrReplaceKesConfigurationSecrets(tt.args.ctx, tt.args.clientSet, tt.args.ns, tt.args.encryptionCfg, tt.args.kesConfigurationSecretName, tt.args.kesClientCertSecretName, tt.args.tenantName)
			if (err != nil) != tt.wantErr {
				t.Errorf("createOrReplaceKesConfigurationSecrets() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("createOrReplaceKesConfigurationSecrets() got = %v, want %v", got, tt.want)
			}
			if !reflect.DeepEqual(got1, tt.want1) {
				t.Errorf("createOrReplaceKesConfigurationSecrets() got1 = %v, want %v", got1, tt.want1)
			}
		})
	}
}
