From 6361a1d63bfee54fb9318f08a83c32a918e25d2e Mon Sep 17 00:00:00 2001 From: roybarda Date: Thu, 10 Nov 2022 19:12:59 +0200 Subject: [PATCH 1/4] env variable to identify playground environments --- .../templates/controller-statefulset.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml b/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml index af082a3..a8ccf93 100644 --- a/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml +++ b/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml @@ -109,7 +109,10 @@ spec: value: {{ .Values.appsec.storage.name }}-svc - name: LEARNING_HOST value: {{ .Values.appsec.learning.name }}-svc - {{- end }} + {{- else }} + - name: K8S_PLAYGROUND_ENVIRONMENT + value: {{ .Values.appsec.playground }} + {{- end -}} resources: {{ toYaml .Values.resources | nindent 12 }} volumeMounts: From 16dd617473ca19b813df382ec1889e24dd0425fb Mon Sep 17 00:00:00 2001 From: roybarda Date: Thu, 10 Nov 2022 19:49:18 +0200 Subject: [PATCH 2/4] fixing helm chart bug in playground variable --- .../templates/controller-statefulset.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml b/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml index a8ccf93..ab29e97 100644 --- a/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml +++ b/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml @@ -110,6 +110,7 @@ spec: - name: LEARNING_HOST value: {{ .Values.appsec.learning.name }}-svc {{- else }} + env: - name: K8S_PLAYGROUND_ENVIRONMENT value: {{ .Values.appsec.playground }} {{- end -}} From af5056511e13d35d50f13258b061b436d50dab98 Mon Sep 17 00:00:00 2001 From: roybarda Date: Thu, 10 Nov 2022 20:39:40 +0200 Subject: [PATCH 3/4] Readjusting playground in helm --- .../templates/controller-statefulset.yaml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml b/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml index ab29e97..5502372 100644 --- a/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml +++ b/build_system/charts/open-appsec-k8s-nginx-ingress/templates/controller-statefulset.yaml @@ -103,17 +103,16 @@ spec: - {{ .Values.appsec.proxy }} {{- end }} imagePullPolicy: {{ .Values.appsec.image.pullPolicy }} - {{- if eq .Values.appsec.playground false }} env: + {{- if eq .Values.appsec.playground false }} - name: SHARED_STORAGE_HOST value: {{ .Values.appsec.storage.name }}-svc - name: LEARNING_HOST value: {{ .Values.appsec.learning.name }}-svc {{- else }} - env: - - name: K8S_PLAYGROUND_ENVIRONMENT - value: {{ .Values.appsec.playground }} - {{- end -}} + - name: PLAYGROUND + value: "true" + {{- end }} resources: {{ toYaml .Values.resources | nindent 12 }} volumeMounts: From 2b00b9304657ec2dab914077a30b7de7773bc289 Mon Sep 17 00:00:00 2001 From: Roy Barda Date: Sun, 13 Nov 2022 12:32:29 +0000 Subject: [PATCH 4/4] identifying playground clusters --- .../k8s_policy_gen/k8s_policy_gen.cc | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/components/security_apps/orchestration/k8s_policy_gen/k8s_policy_gen.cc b/components/security_apps/orchestration/k8s_policy_gen/k8s_policy_gen.cc index 9eef08a..7cf3195 100644 --- a/components/security_apps/orchestration/k8s_policy_gen/k8s_policy_gen.cc +++ b/components/security_apps/orchestration/k8s_policy_gen/k8s_policy_gen.cc @@ -13,9 +13,12 @@ #include "k8s_policy_gen.h" +#include +#include #include #include #include +#include #include #include #include @@ -854,9 +857,28 @@ private: string token; map practice_name_to_id_map; + bool + isPlaygroundEnv() + { + string playground_variable = "PLAYGROUND"; + const char* env_string = getenv(playground_variable.c_str()); + + if (env_string) + { + string env_value = env_string; + std::transform(env_value.begin(), env_value.end(), env_value.begin(), + [](unsigned char c){ return std::tolower(c); }); + return env_value == "true"; + } + + return false; + } + bool getClusterId() { + string playground_uid = isPlaygroundEnv() ? "playground-" : ""; + dbgTrace(D_K8S_POLICY) << "Getting cluster UID"; NamespaceData namespaces_data; bool res = messaging->sendObject( @@ -885,7 +907,7 @@ private: uid, EnvKeyAttr::LogSection::SOURCE ); - Singleton::Consume::by()->setClusterId(uid); + Singleton::Consume::by()->setClusterId(playground_uid + uid); return true; } }