diff --git a/pkg/drivers/kopiabackup/kopiabackup.go b/pkg/drivers/kopiabackup/kopiabackup.go index 66fe825c..d6b2388b 100644 --- a/pkg/drivers/kopiabackup/kopiabackup.go +++ b/pkg/drivers/kopiabackup/kopiabackup.go @@ -396,7 +396,7 @@ func jobFor( } // Add security Context only if the PSA is enabled. if jobOption.PodUserId != "" || jobOption.PodGroupId != "" { - job, err = utils.AddSecurityContextToJob(job, jobOption.PodUserId, jobOption.PodGroupId) + job, err = utils.AddSecurityContextToJob(job, jobOption.PodUserId, jobOption.PodGroupId, jobOption.SourcePVCName, jobOption.SourcePVCNamespace) if err != nil { return nil, err } @@ -542,6 +542,12 @@ func buildJob(jobName string, jobOptions drivers.JobOpts) (*batchv1.Job, error) logrus.Errorf("%s: %v", fn, errMsg) return nil, fmt.Errorf(errMsg) } + + if err := utils.SetupRoleBindingForSCC(jobName, resourceNamespace, jobOptions.SourcePVCName); err != nil { + errMsg := fmt.Sprintf("error creating role binding %s/%s: %v", resourceNamespace, jobName, err) + logrus.Errorf("%s: %v", fn, errMsg) + return nil, fmt.Errorf(errMsg) + } return jobFor( jobOptions, jobName, diff --git a/pkg/drivers/kopiarestore/kopiarestore.go b/pkg/drivers/kopiarestore/kopiarestore.go index 3544c632..ef02d776 100644 --- a/pkg/drivers/kopiarestore/kopiarestore.go +++ b/pkg/drivers/kopiarestore/kopiarestore.go @@ -191,6 +191,12 @@ func jobFor( return nil, err } + if err := utils.SetupRoleBindingForSCC(jobName, jobOption.Namespace, jobOption.DestinationPVCName); err != nil { + errMsg := fmt.Sprintf("error creating role binding %s/%s: %v", jobOption.Namespace, jobName, err) + logrus.Errorf(errMsg) + return nil, fmt.Errorf(errMsg) + } + cmd := strings.Join([]string{ "/kopiaexecutor", "restore", @@ -297,7 +303,7 @@ func jobFor( } // Add security Context only if the PSA is enabled. if jobOption.PodUserId != "" || jobOption.PodGroupId != "" { - job, err = utils.AddSecurityContextToJob(job, jobOption.PodUserId, jobOption.PodGroupId) + job, err = utils.AddSecurityContextToJob(job, jobOption.PodUserId, jobOption.PodGroupId, jobOption.DestinationPVCName, jobOption.Namespace) if err != nil { return nil, err } diff --git a/pkg/drivers/nfsbackup/nfsbackup.go b/pkg/drivers/nfsbackup/nfsbackup.go index 0fb6d767..9ca1068c 100644 --- a/pkg/drivers/nfsbackup/nfsbackup.go +++ b/pkg/drivers/nfsbackup/nfsbackup.go @@ -146,6 +146,12 @@ func buildJob( return nil, fmt.Errorf(errMsg) } + if err := utils.SetupRoleBindingForSCC(jobOptions.RestoreExportName, jobOptions.Namespace, jobOptions.SourcePVCName); err != nil { + errMsg := fmt.Sprintf("error creating role binding %s/%s: %v", jobOptions.Namespace, jobOptions.RestoreExportName, err) + logrus.Errorf(errMsg) + return nil, fmt.Errorf(errMsg) + } + resources, err := utils.NFSResourceRequirements(jobOptions.JobConfigMap, jobOptions.JobConfigMapNs) if err != nil { return nil, err @@ -300,7 +306,7 @@ func jobForBackupResource( // Not passing the groupId as we do not want to set the RunAsGroup field in the securityContext // This helps us in setting the primaryGroup ID to root for the user ID. if uid != "" { - job, err = utils.AddSecurityContextToJob(job, uid, "") + job, err = utils.AddSecurityContextToJob(job, uid, "", jobOption.SourcePVCName, jobOption.SourcePVCNamespace) if err != nil { return nil, err } diff --git a/pkg/drivers/nfscsirestore/nfscsirestore.go b/pkg/drivers/nfscsirestore/nfscsirestore.go index 60d84d62..915d3a49 100644 --- a/pkg/drivers/nfscsirestore/nfscsirestore.go +++ b/pkg/drivers/nfscsirestore/nfscsirestore.go @@ -147,6 +147,12 @@ func buildJob( return nil, fmt.Errorf(errMsg) } + if err := utils.SetupRoleBindingForSCC(jobName, jobOptions.Namespace, jobOptions.DestinationPVCName); err != nil { + errMsg := fmt.Sprintf("error creating role binding %s/%s: %v", jobOptions.Namespace, jobName, err) + logrus.Errorf(errMsg) + return nil, fmt.Errorf(errMsg) + } + resources, err := utils.NFSResourceRequirements(jobOptions.JobConfigMap, jobOptions.JobConfigMapNs) if err != nil { return nil, err @@ -277,7 +283,7 @@ func jobForRestoreCSISnapshot( } // Add security Context only if the PSA is enabled. if jobOption.PodUserId != "" || jobOption.PodGroupId != "" { - job, err = utils.AddSecurityContextToJob(job, jobOption.PodUserId, jobOption.PodGroupId) + job, err = utils.AddSecurityContextToJob(job, jobOption.PodUserId, jobOption.PodGroupId, jobOption.DestinationPVCName, jobOption.Namespace) if err != nil { return nil, err } diff --git a/pkg/drivers/nfsrestore/nfsrestore.go b/pkg/drivers/nfsrestore/nfsrestore.go index efc45ac5..6b5b4b78 100644 --- a/pkg/drivers/nfsrestore/nfsrestore.go +++ b/pkg/drivers/nfsrestore/nfsrestore.go @@ -147,6 +147,12 @@ func buildJob( return nil, fmt.Errorf(errMsg) } + if err := utils.SetupRoleBindingForSCC(jobOptions.RestoreExportName, jobOptions.Namespace, jobOptions.DestinationPVCName); err != nil { + errMsg := fmt.Sprintf("error creating role binding %s/%s: %v", jobOptions.Namespace, jobOptions.RestoreExportName, err) + logrus.Errorf(errMsg) + return nil, fmt.Errorf(errMsg) + } + resources, err := utils.NFSResourceRequirements(jobOptions.JobConfigMap, jobOptions.JobConfigMapNs) if err != nil { return nil, err @@ -323,7 +329,7 @@ func jobForRestoreResource( } // Not passing the groupId as we do not want to set the RunAsGroup field in the securityContext // This helps us in setting the primaryGroup ID to root for the user ID. - job, err = utils.AddSecurityContextToJob(job, utils.KdmpJobUid, "") + job, err = utils.AddSecurityContextToJob(job, utils.KdmpJobUid, "", jobOption.DestinationPVCName, jobOption.Namespace) if err != nil { return nil, err } diff --git a/pkg/drivers/utils/common.go b/pkg/drivers/utils/common.go index 44036c01..4897b251 100644 --- a/pkg/drivers/utils/common.go +++ b/pkg/drivers/utils/common.go @@ -51,6 +51,8 @@ const ( // BurstKey - configmap burst key name BurstKey = "K8S_BURST" k8sMinVersionSASecretTokenNotSupport = "1.24" + SccRoleBindingNameSuffix = "-scc" + AnyUidClusterRoleName = "system:openshift:scc:anyuid" ) var ( @@ -136,6 +138,49 @@ func SetupServiceAccount(name, namespace string, role *rbacv1.Role) error { return nil } +func SetupRoleBindingForSCC(name, namespace, pvcName string) error { + _, _, isOCP, err := GetOcpNsUidGid(namespace, "", "") + if err != nil { + return fmt.Errorf("failed to check if cluster is OCP: %v", err) + } + + provisionerName, err := GetProvisionerNameFromPvc(pvcName, namespace) + if err != nil { + return fmt.Errorf("failed to get provisioner name from pvc: %v", err) + } + + provisionersListToUseAnyUid, err := GetArrayConfigValue(KdmpConfigmapName, KdmpConfigmapNamespace, provisionersToUseAnyUid) + if err != nil { + logrus.Errorf("failed to extract provisioners list from configmap: %v", err) + return err + } + if len(provisionersListToUseAnyUid) > 0 { + if isOCP && contains(provisionersListToUseAnyUid, provisionerName) { + failed, err := addRoleBindingForScc(name, namespace, AnyUidClusterRoleName) + if failed { + return err + } + } + } + return nil +} + +// Check if corresponding SCC cluster role exists, then only create rolebinding for it. +// This way we will avoid creating rolebinding in non-ocp cluster. +func addRoleBindingForScc(name string, namespace string, sccClusterRoleName string) (bool, error) { + // Check if the cluster role exists for the given SCC + if _, err := rbacops.Instance().GetClusterRole(sccClusterRoleName); err == nil { + if _, err := rbacops.Instance().CreateRoleBinding(roleBindingForScc(name, namespace, sccClusterRoleName)); err != nil && !errors.IsAlreadyExists(err) { + return true, fmt.Errorf("create %s/%s rolebinding: %s", namespace, name+SccRoleBindingNameSuffix, err) + } + } else { + if !errors.IsNotFound(err) { + return true, fmt.Errorf("get anyuid clusterrole %s failed: %s", AnyUidClusterRoleName, err) + } + } + return false, nil +} + // CleanServiceAccount removes a service account with a corresponding role and rolebinding. func CleanServiceAccount(name, namespace string) error { if err := rbacops.Instance().DeleteRole(name, namespace); err != nil && !errors.IsNotFound(err) { @@ -144,6 +189,9 @@ func CleanServiceAccount(name, namespace string) error { if err := rbacops.Instance().DeleteRoleBinding(name, namespace); err != nil && !errors.IsNotFound(err) { return fmt.Errorf("delete %s/%s rolebinding: %s", namespace, name, err) } + if err := rbacops.Instance().DeleteRoleBinding(name+SccRoleBindingNameSuffix, namespace); err != nil && !errors.IsNotFound(err) { + return fmt.Errorf("delete %s/%s rolebinding: %s", namespace, name, err) + } if err := coreops.Instance().DeleteServiceAccount(name, namespace); err != nil && !errors.IsNotFound(err) { return fmt.Errorf("delete %s/%s serviceaccount: %s", namespace, name, err) } @@ -218,6 +266,31 @@ func SetupNFSServiceAccount(name, namespace string, role *rbacv1.ClusterRole) er return nil } +// In OCP standard scc cluster role name are predefined and one pod can adhere to one SCC at a time. +func roleBindingForScc(name, namespace string, sccClusterRoleName string) *rbacv1.RoleBinding { + return &rbacv1.RoleBinding{ + ObjectMeta: metav1.ObjectMeta{ + Name: name + SccRoleBindingNameSuffix, + Namespace: namespace, + Annotations: map[string]string{ + SkipResourceAnnotation: "true", + }, + }, + Subjects: []rbacv1.Subject{ + { + Kind: rbacv1.ServiceAccountKind, + Name: name, + Namespace: namespace, + }, + }, + RoleRef: rbacv1.RoleRef{ + Name: sccClusterRoleName, + Kind: "ClusterRole", + APIGroup: rbacv1.GroupName, + }, + } +} + func roleBindingFor(name, namespace string) *rbacv1.RoleBinding { return &rbacv1.RoleBinding{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/drivers/utils/utils.go b/pkg/drivers/utils/utils.go index 410334db..0bbba793 100644 --- a/pkg/drivers/utils/utils.go +++ b/pkg/drivers/utils/utils.go @@ -78,7 +78,9 @@ const ( OcpGidRangeAnnotationKey = "openshift.io/sa.scc.supplemental-groups" kopiaBackupString = "kopiaexecutor backup" // if providerType in node spec has this string then it is GCP hosted cluster - GCPBasedClusterString = "gce://" + GCPBasedClusterString = "gce://" + provisionersToUseAnyUid = "PROVISIONERS_TO_USE_ANYUID" + pvcStorageProvisionerKey = "volume.kubernetes.io/storage-provisioner" ) var ( @@ -1020,7 +1022,7 @@ func GetShortUID(uid string) string { // If static uids like kdmpJobUid or kdmpJobGid is used that means // these are dummy UIDs used for backing up resources to backuplocation // which doesn't need specific UID specific permission. -func AddSecurityContextToJob(job *batchv1.Job, podUserId, podGroupId string) (*batchv1.Job, error) { +func AddSecurityContextToJob(job *batchv1.Job, podUserId, podGroupId, pvcName, pvcNamespace string) (*batchv1.Job, error) { if job == nil { return job, fmt.Errorf("recieved a nil job object to add security context") } @@ -1034,6 +1036,36 @@ func AddSecurityContextToJob(job *batchv1.Job, podUserId, podGroupId string) (*b if err != nil { return nil, err } + + // If PROVISIONERS_TO_USE_ANYUID is set in kdmp-config, then add rolebinding for anyuid SCC + provisionersListToUseAnyUid, err := GetArrayConfigValue(KdmpConfigmapName, KdmpConfigmapNamespace, provisionersToUseAnyUid) + if err != nil { + errMsg := fmt.Sprintf("failed to extract provisioners list from configmap: %v", err) + logrus.Errorf(errMsg) + return nil, fmt.Errorf(errMsg) + } + + // Get provisioner name from the pvcName, pvcNamespace + provisionerName, err := GetProvisionerNameFromPvc(pvcName, pvcNamespace) + if err != nil { + errMsg := fmt.Sprintf("failed to get provisionerName name for pvc [%s/%s]: %v", pvcNamespace, pvcName, err) + logrus.Errorf(errMsg) + return nil, fmt.Errorf(errMsg) + } + + if len(provisionersListToUseAnyUid) > 0 { + if isOcp && contains(provisionersListToUseAnyUid, provisionerName) { + logrus.Infof("PROVISIONERS_TO_USE_ANYUID is set to use, running the job %v with anyuid SCC", job.Name) + // Add the annotation to force the pod to adopt anyuid scc in OCP + // It may not work if the pod's SA doesn't have permission to use anyuid SCC + if job.Spec.Template.Annotations == nil { + job.Spec.Template.Annotations = make(map[string]string) + } + job.Spec.Template.Annotations["openshift.io/required-scc"] = "anyuid" + return job, nil + } + } + // if the namespace is OCP, then overwrite the UID and GID from the namespace annotation if isOcp { podUserId = ocpUid @@ -1178,3 +1210,35 @@ func GetAccessModeFromPvc(srcPvcName, srcPvcNameSpace string) ([]corev1.Persiste accessModes := srcPvc.Status.AccessModes return accessModes, nil } + +func GetProvisionerNameFromPvc(pvcName, pvcNamespace string) (string, error) { + pvc, err := core.Instance().GetPersistentVolumeClaim(pvcName, pvcNamespace) + if err != nil { + return "", err + } + provisionerName := pvc.Annotations[pvcStorageProvisionerKey] + return provisionerName, nil +} + +// this function extracts an array from the configMap +// Ex: provisionersToUseAnyUid: provisioner1,provisioner2 will return ["provisioner1", "provisioner2"] +func GetArrayConfigValue(cm, ns, key string) ([]string, error) { + arrayDataAsString := GetConfigValue(cm, ns, key) + + arrayData := strings.Split(arrayDataAsString, ",") + for k, str := range arrayData { + arrayData[k] = strings.TrimSpace(str) + } + + return arrayData, nil +} + +// Helper function to check if a slice contains a string +func contains(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +}