Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Mise en place des PodSecurityAdmission sur les namespaces clients #34

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

Whisper40
Copy link
Contributor

@Whisper40 Whisper40 commented Jan 8, 2024

Dans l'objectif de remplacer les podSecurityPolicy en prévision du passage en Kubernetes 1.26, il est nécessaire de mettre en place les nouvelles règles de sécurité.

Nous mettons en place par défaut un enforce sur la policy "restricted", avec un warning emis dès lors que l'applicatif ne respecte pas la policy "restricted" et réalisons par défaut l'émission d'un event d'audit lorsque la policy ne respecte pas le niveau "restricted".

Le choix restricted a été fait dans l'objectif d'avoir une politique sécurisée par défaut.

En raison de projets clients devant être en privileged, nous rendons possible l'ajout d'une liste de namespaces devant être valorisés à privileged. Nous émettons un log de type Warn pour souligner à un administrateur la particularité de configuration d'un namespace en particulier dans les logs de l'opérateur.
Sans cette modification tous les namespaces clients sont considérés comme héritant de la politique par défaut.

Copy link
Contributor

@evrardjp evrardjp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the commit message, the following should not appear : "URL://pages/viewpage.action?pageId=228069521" -> This is an internal link and has no interest for the external reader. Instead you should mention the relevant content of the page on the commit message.

Your commit message also doesn't mention why you implemented the feature under a config map (and its addition of privileged_namespaces).

You also don't mention why you are not using a secure by default approach here.

Finally, you don't mention why you intend to warn in logs for privileged namespace, while it's been technically validated by the admin (by defining the config map) -- is that because it might be seen elsewhere/by different ppl?

Last but not least: Keep in mind I haven't tested it. Someone else must approve, as I don't know how this is intertwined with existing systems.

At first sight, it looks okay, and the update could work out of the box.

Readme.md Show resolved Hide resolved
internal/utils/constants.go Outdated Show resolved Hide resolved
internal/utils/constants.go Outdated Show resolved Hide resolved
internal/utils/constants.go Outdated Show resolved Hide resolved
internal/utils/helpers.go Outdated Show resolved Hide resolved
internal/services/provisionner.go Outdated Show resolved Hide resolved
internal/utils/helpers.go Outdated Show resolved Hide resolved
@Whisper40
Copy link
Contributor Author

Tests :

  • Sans valorisation du configmap -> Label restricted appliqué sur les namespaces clients
  • Avec valorisation incorrecte :
2024-01-10T13:22:17Z ERR PODSECURITYADMISSION_ENFORCEMENT is incorrect. must be one of privileged, baseline, restricted 
2024-01-10T13:22:17Z INF Generating resources from LDAP groups

Le label restricted est appliqué par défaut en raison de l'erreur de configuration du paramètre.

  • Avec valorisation baseline :
    Le label baseline est appliqué sur les namespaces clients

Le fonctionnement est donc validé sur cette partie d'application de labels.

Copy link
Contributor

@evrardjp evrardjp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the commit message, so we can merge.
Rest is good, I just proposed a few extra changes, which don't need to be included in this commit, but could be included in a refactor for clarity.

"environment": project.Spec.Environment,
"pod-security.kubernetes.io/enforce": GetPodSecurityStandardName(project.Name),
"pod-security.kubernetes.io/warn": string(utils.Config.PodSecurityAdmissionWarning),
"pod-security.kubernetes.io/audit": string(utils.Config.PodSecurityAdmissionAudit),
}

return utils.Union(defaultLabels, utils.Config.CustomLabels)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very familiar with the code base, so I might need to check what this does. That's why I said someone else should also review. AT FIRST SIGHT, it looks lik ethis would work.

@@ -99,6 +100,24 @@ func MakeConfig() (*types.Config, error) {
ldapUserFilter := getEnv("LDAP_USERFILTER", "(cn=%s)")
tenant := strings.ToLower(getEnv("TENANT", KubiTenantUndeterminable))

podSecurityAdmissionEnforcement, errPodSecurityAdmissionEnforcement := podSecurity.ParseLevel(strings.ToLower(getEnv("PODSECURITYADMISSION_ENFORCEMENT", string(podSecurity.LevelRestricted))))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment, for the lazy ppl like me, to say:

# No need to state a default or crash, because kubernetes defaults to restricted. 

internal/utils/config.go Show resolved Hide resolved
func GetPodSecurityStandardName(namespace string) string {
if utils.IsInPrivilegedNsList(namespace) {
utils.Log.Warn().Msgf("Namespace %v is labeled as privileged", namespace)
return utils.PodSecurityPrivileged
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: https://github.com/kubernetes/pod-security-admission/blob/c976ce3661b8678781853e7e84c6f8d0e42be500/api/constants.go#L22

Maybe we can use that constant instead, and remove the string utils.PodSecurityPrivileged from defaults.go (as this is a constant, not a default!) .

Yet, I don't think it's a deal breaker, so let's move on.

"creator": "kubi",
"environment": project.Spec.Environment,
"pod-security.kubernetes.io/enforce": GetPodSecurityStandardName(project.Name),
"pod-security.kubernetes.io/warn": string(utils.Config.PodSecurityAdmissionWarning),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I will propose a rename in a followup commit, for clarity.

utils.Log.Warn().Msgf("Namespace %v is labeled as privileged", namespace)
return utils.PodSecurityPrivileged
}
return string(utils.Config.PodSecurityAdmissionEnforcement)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: same for the returned constant here.

PodSecurityAdmissionWarning = "restricted"
PodSecurityAdmissionAudit = "restricted"

PodSecurityPrivileged = "privileged"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is a constant, not a default, unlike the first 3.
(The first 3 define what we want as default, picked for the ns)

Dans l'objectif de remplacer les podSecurityPolicy en prévision du passage en Kubernetes 1.26, il est nécessaire de mettre en place les nouvelles règles de sécurité.

Nous mettons en place par défaut un enforce sur la policy "restricted", avec un warning emis dès lors que l'applicatif ne respecte pas la policy "restricted" et réalisons par défaut l'émission d'un event d'audit lorsque la policy ne respecte pas le niveau "restricted".
Le choix restricted a été fait dans l'objectif d'avoir une politique sécurisée par défaut.

En raison de projets clients devant être en privileged, nous rendons possible l'ajout d'une liste de namespaces devant être valorisés à privileged. Nous émettons un log de type Warn pour souligner à un administrateur la particularité de configuration d'un namespace en particulier dans les logs de l'opérateur.
Sans cette modification tous les namespaces clients sont considérés comme héritant de la politique par défaut.

La configuration est appliquée à travers le configmap de Kubi car c'est là qu'est toute la configuration du composant. Par conséquent, une modification de la configuration nécessite forcément un redémarrage de l'opérateur. (Peut être réalisé via Stakater).
Copy link
Contributor

@evrardjp evrardjp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move on

@evrardjp evrardjp merged commit 8bc6831 into master Jan 10, 2024
6 of 7 checks passed
@pape07
Copy link

pape07 commented Jan 11, 2024

Tests :

* Sans valorisation du configmap -> Label restricted appliqué sur les namespaces clients

* Avec valorisation incorrecte :
2024-01-10T13:22:17Z ERR PODSECURITYADMISSION_ENFORCEMENT is incorrect. must be one of privileged, baseline, restricted 
2024-01-10T13:22:17Z INF Generating resources from LDAP groups

Le label restricted est appliqué par défaut en raison de l'erreur de configuration du paramètre.

* Avec valorisation baseline :
  Le label baseline est appliqué sur les namespaces clients

Le fonctionnement est donc validé sur cette partie d'application de labels.

je valide le fonctionnement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants