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

Read and propagate helm config for security products #23575

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,39 @@
endpoint string
resources []string
operations []admiv1.OperationType
filter *containers.Filter
filters map[string]*containers.Filter
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a good idea.

This leads to coupling within the same webhook multiple features that are not actually related.

We should probably have one webhook per feature (or a webhook for a set of features only if it makes sense to, which I assume is a very rare situation)

containerRegistry string
pinnedLibraries []libInfo
}

// NewWebhook returns a new Webhook
func NewWebhook() (*Webhook, error) {
filter, err := apmSSINamespaceFilter()

var filters map[string]*containers.Filter = make(map[string]*containers.Filter);

ssiFilter, err := apmSSINamespaceFilter();
if err != nil {
return nil, err
}
filters["apm_config.instrumentation.enabled"] = ssiFilter;

asmFilter, err := apmProductNamespaceFilter("asm_config.enabled_namespaces", "asm_config.disabled_namespaces");
Copy link
Contributor

Choose a reason for hiding this comment

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

ASM is not related to APM afaik (correct me if I am wrong).

Given this, I don't think the naming is good here (i.e. apmProductNamespaceFilter(...) is returning an asm filter)

Same applies to the following lines.

if err != nil {
return nil, err
}
filters["asm_config.enabled"] = asmFilter;

iastFilter, err := apmProductNamespaceFilter("iast_config.enabled_namespaces", "iast_config.disabled_namespaces");
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above

if err != nil {
return nil, err
}
filters["iast_config.enabled"] = iastFilter;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it is good to have the absolute config name as a key in the filters map. (same applies to the rest).

Using filters["iast"] should be enough, and would be better to create some constant like const iast = "iast" and use it wherever needed; this helps avoid runtime errors in case of typo in the key when used as a plain string.

But despite this:

  • It is not good to have all the features in the same webhook in the first place


scaFilter, err := apmProductNamespaceFilter("sca_config.enabled_namespaces", "sca_config.disabled_namespaces");
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above

if err != nil {
return nil, err
}
filters["sca_config.enabled"] = scaFilter;
Comment on lines +154 to +176
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ssiFilter, err := apmSSINamespaceFilter();
if err != nil {
return nil, err
}
filters["apm_config.instrumentation.enabled"] = ssiFilter;
asmFilter, err := apmProductNamespaceFilter("asm_config.enabled_namespaces", "asm_config.disabled_namespaces");
if err != nil {
return nil, err
}
filters["asm_config.enabled"] = asmFilter;
iastFilter, err := apmProductNamespaceFilter("iast_config.enabled_namespaces", "iast_config.disabled_namespaces");
if err != nil {
return nil, err
}
filters["iast_config.enabled"] = iastFilter;
scaFilter, err := apmProductNamespaceFilter("sca_config.enabled_namespaces", "sca_config.disabled_namespaces");
if err != nil {
return nil, err
}
filters["sca_config.enabled"] = scaFilter;
var err error
filters["apm_config.instrumentation.enabled"], err = apmSSINamespaceFilter()
if err != nil {
return nil, err
}
filters["asm_config.enabled"], err = apmProductNamespaceFilter("asm_config.enabled_namespaces", "asm_config.disabled_namespaces")
if err != nil {
return nil, err
}
filters["iast_config.enabled"], err = apmProductNamespaceFilter("iast_config.enabled_namespaces", "iast_config.disabled_namespaces")
if err != nil {
return nil, err
}
filters["sca_config.enabled"], err = apmProductNamespaceFilter("sca_config.enabled_namespaces", "sca_config.disabled_namespaces")
if err != nil {
return nil, err
}


containerRegistry := config.Datadog.GetString("admission_controller.auto_instrumentation.container_registry")

Expand All @@ -161,7 +183,7 @@
endpoint: config.Datadog.GetString("admission_controller.auto_instrumentation.endpoint"),
resources: []string{"pods"},
operations: []admiv1.OperationType{admiv1.Create},
filter: filter,
filters: filters,
containerRegistry: containerRegistry,
pinnedLibraries: getPinnedLibraries(containerRegistry),
}, nil
Expand All @@ -178,7 +200,8 @@
return apmInstrumentationWebhook, errInitAPMInstrumentation
}

// apmSSINamespaceFilter returns the filter used by APM SSI to filter namespaces.
// apmProductNamespaceFilter returns a filter than can be test if a
// functionality should be inject based on the pods namespace.
// The filter excludes two namespaces by default: "kube-system" and the
// namespace where datadog is installed.
// Cases:
Expand All @@ -191,12 +214,12 @@
// namespaces that are not included in the list of disabled namespaces and that
// are not one of the ones disabled by default.
// - Enabled and disabled namespaces: return error.
func apmSSINamespaceFilter() (*containers.Filter, error) {
apmEnabledNamespaces := config.Datadog.GetStringSlice("apm_config.instrumentation.enabled_namespaces")
apmDisabledNamespaces := config.Datadog.GetStringSlice("apm_config.instrumentation.disabled_namespaces")
func apmProductNamespaceFilter(enabledNamespace string, disabledNamespace string) (*containers.Filter, error) {
apmEnabledNamespaces := config.Datadog.GetStringSlice(enabledNamespace)
apmDisabledNamespaces := config.Datadog.GetStringSlice(disabledNamespace)

if len(apmEnabledNamespaces) > 0 && len(apmDisabledNamespaces) > 0 {
return nil, fmt.Errorf("apm.instrumentation.enabled_namespaces and apm.instrumentation.disabled_namespaces configuration cannot be set together")
return nil, fmt.Errorf("%s and %s configuration cannot be set together", enabledNamespace, disabledNamespace)
}

// Prefix the namespaces as needed by the containers.Filter.
Expand Down Expand Up @@ -229,6 +252,11 @@
return containers.NewFilter(containers.GlobalFilter, apmEnabledNamespacesWithPrefix, filterExcludeList)
}

// apmSSINamespaceFilter returns the filter used by APM SSI to filter namespaces.
func apmSSINamespaceFilter() (*containers.Filter, error) {
return apmProductNamespaceFilter("apm_config.instrumentation.enabled_namespaces", "apm_config.instrumentation.disabled_namespaces");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return apmProductNamespaceFilter("apm_config.instrumentation.enabled_namespaces", "apm_config.instrumentation.disabled_namespaces");
return apmProductNamespaceFilter("apm_config.instrumentation.enabled_namespaces", "apm_config.instrumentation.disabled_namespaces")

}

// Name returns the name of the webhook
func (w *Webhook) Name() string {
return w.name
Expand Down Expand Up @@ -287,7 +315,11 @@
}
injectApmTelemetryConfig(pod)

if w.isEnabledInNamespace(pod.Namespace) {
w.injectProductActivation(pod, "asm_config.enabled", "DD_APPSEC_ENABLED")

Check failure on line 318 in pkg/clusteragent/admission/mutate/autoinstrumentation/auto_instrumentation.go

View workflow job for this annotation

GitHub Actions / windows-lint

Error return value of `w.injectProductActivation` is not checked (errcheck)

Check failure on line 318 in pkg/clusteragent/admission/mutate/autoinstrumentation/auto_instrumentation.go

View workflow job for this annotation

GitHub Actions / windows-lint

Error return value of `w.injectProductActivation` is not checked (errcheck)
w.injectProductActivation(pod, "iast_config.enabled", "DD_IAST_ENABLED")

Check failure on line 319 in pkg/clusteragent/admission/mutate/autoinstrumentation/auto_instrumentation.go

View workflow job for this annotation

GitHub Actions / windows-lint

Error return value of `w.injectProductActivation` is not checked (errcheck)

Check failure on line 319 in pkg/clusteragent/admission/mutate/autoinstrumentation/auto_instrumentation.go

View workflow job for this annotation

GitHub Actions / windows-lint

Error return value of `w.injectProductActivation` is not checked (errcheck)
w.injectProductActivation(pod, "sca_config.enabled", "DD_SCA_ENABLED")

Check failure on line 320 in pkg/clusteragent/admission/mutate/autoinstrumentation/auto_instrumentation.go

View workflow job for this annotation

GitHub Actions / windows-lint

Error return value of `w.injectProductActivation` is not checked (errcheck)

Check failure on line 320 in pkg/clusteragent/admission/mutate/autoinstrumentation/auto_instrumentation.go

View workflow job for this annotation

GitHub Actions / windows-lint

Error return value of `w.injectProductActivation` is not checked (errcheck)

if w.isSsiEnabledInNamespace(pod.Namespace) {
// if Single Step Instrumentation is enabled, pods can still opt out using the label
if pod.GetLabels()[common.EnabledLabelKey] == "false" {
log.Debugf("Skipping single step instrumentation of pod %q due to label", mutatecommon.PodString(pod))
Expand All @@ -312,7 +344,7 @@
}
// Inject env variables used for Onboarding KPIs propagation
var injectionType string
if w.isEnabledInNamespace(pod.Namespace) {
if w.isSsiEnabledInNamespace(pod.Namespace) {
// if Single Step Instrumentation is enabled, inject DD_INSTRUMENTATION_INSTALL_TYPE:k8s_single_step
_ = mutatecommon.InjectEnv(pod, singleStepInstrumentationInstallTypeEnvVar)
injectionType = singleStepInstrumentationInstallType
Expand Down Expand Up @@ -435,7 +467,7 @@
}

// Get libraries to inject for APM Instrumentation
if w.isEnabledInNamespace(pod.Namespace) {
if w.isSsiEnabledInNamespace(pod.Namespace) {
libInfoList, autoDetected = w.getLibrariesToInjectForApmInstrumentation(pod)
if len(libInfoList) > 0 {
return libInfoList, autoDetected
Expand Down Expand Up @@ -552,20 +584,43 @@
return config.Datadog.GetBool("admission_controller.mutate_unlabelled")
}

return apmWebhook.isEnabledInNamespace(pod.Namespace) || config.Datadog.GetBool("admission_controller.mutate_unlabelled")
return apmWebhook.isSsiEnabledInNamespace(pod.Namespace) || config.Datadog.GetBool("admission_controller.mutate_unlabelled")
}

// isEnabledInNamespace indicates if Single Step Instrumentation is enabled for
// isEnabledInNamespace indicates if a product is enabled for the namespace in the cluster
func (w *Webhook) isEnabledInNamespace(enabledConfig string, namespace string) bool {
enabledValue := config.Datadog.GetString(enabledConfig)

if len(enabledValue) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if len(enabledValue) == 0 {
if enabledValue == "" {

log.Debugf("%s has no value", enabledConfig)
return false
}

return !w.filters[enabledConfig].IsExcluded(nil, "", "", namespace)
}

// isSsiEnabledInNamespace indicates if Single Step Instrumentation is enabled for
// the namespace in the cluster
func (w *Webhook) isEnabledInNamespace(namespace string) bool {
func (w *Webhook) isSsiEnabledInNamespace(namespace string) bool {
apmInstrumentationEnabled := config.Datadog.GetBool("apm_config.instrumentation.enabled")

if !apmInstrumentationEnabled {
log.Debugf("APM Instrumentation is disabled")
return false
}

return !w.filter.IsExcluded(nil, "", "", namespace)
return !w.filters["apm_config.instrumentation.enabled"].IsExcluded(nil, "", "", namespace)
}

func (w *Webhook) injectProductActivation(pod *corev1.Pod, enableKey string, envVarKey string) error {
if (w.isEnabledInNamespace(enableKey, pod.Namespace)) {
enabledValue := config.Datadog.GetBool(enableKey)
_ = mutatecommon.InjectEnv(pod, corev1.EnvVar{
Name: envVarKey,
Value: strconv.FormatBool(enabledValue),
});
}
return nil;
Comment on lines +616 to +623
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (w.isEnabledInNamespace(enableKey, pod.Namespace)) {
enabledValue := config.Datadog.GetBool(enableKey)
_ = mutatecommon.InjectEnv(pod, corev1.EnvVar{
Name: envVarKey,
Value: strconv.FormatBool(enabledValue),
});
}
return nil;
if w.isEnabledInNamespace(enableKey, pod.Namespace) {
enabledValue := config.Datadog.GetBool(enableKey)
mutatecommon.InjectEnv(pod, corev1.EnvVar{
Name: envVarKey,
Value: strconv.FormatBool(enabledValue),
})
}
return nil

}

func (w *Webhook) injectAutoInstruConfig(pod *corev1.Pod, libsToInject []libInfo, autoDetected bool, injectionType string) error {
Expand Down Expand Up @@ -682,7 +737,7 @@

injectLibVolume(pod)

if w.isEnabledInNamespace(pod.Namespace) {
if w.isSsiEnabledInNamespace(pod.Namespace) {
libConfig := basicConfig()
if name, err := getServiceNameFromPod(pod); err == nil {
// Set service name if it can be derived from a pod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,93 @@ func TestInjectAutoInstrumentation(t *testing.T) {
mockConfig.SetWithoutSource("apm_config.instrumentation.lib_versions", map[string]string{"ruby": "v1.2.3"})
},
},
{
name: "Single Step Instrumentation: enable ASM",
pod: common.FakePodWithParent(
"ns",
map[string]string{},
map[string]string{},
[]corev1.EnvVar{},
"replicaset", "test-app-123",
),
expectedEnvs: []corev1.EnvVar{
{
Name: "DD_INSTRUMENTATION_INSTALL_TIME",
Value: installTime,
},
{
Name: "DD_INSTRUMENTATION_INSTALL_ID",
Value: uuid,
},
{
Name: "DD_APPSEC_ENABLED",
Value: "true",
},
},
expectedInjectedLibraries: map[string]string{},
wantErr: false,
setupConfig: func() {
mockConfig.SetWithoutSource("asm_config.enabled", true)
},
},
{
name: "Single Step Instrumentation: enable iast",
pod: common.FakePodWithParent(
"ns",
map[string]string{},
map[string]string{},
[]corev1.EnvVar{},
"replicaset", "test-app-123",
),
expectedEnvs: []corev1.EnvVar{
{
Name: "DD_INSTRUMENTATION_INSTALL_TIME",
Value: installTime,
},
{
Name: "DD_INSTRUMENTATION_INSTALL_ID",
Value: uuid,
},
{
Name: "DD_IAST_ENABLED",
Value: "true",
},
},
expectedInjectedLibraries: map[string]string{},
wantErr: false,
setupConfig: func() {
mockConfig.SetWithoutSource("iast_config.enabled", true)
},
},
{
name: "Single Step Instrumentation: disable sca",
pod: common.FakePodWithParent(
"ns",
map[string]string{},
map[string]string{},
[]corev1.EnvVar{},
"replicaset", "test-app-123",
),
expectedEnvs: []corev1.EnvVar{
{
Name: "DD_INSTRUMENTATION_INSTALL_TIME",
Value: installTime,
},
{
Name: "DD_INSTRUMENTATION_INSTALL_ID",
Value: uuid,
},
{
Name: "DD_SCA_ENABLED",
Value: "false",
},
},
expectedInjectedLibraries: map[string]string{},
wantErr: false,
setupConfig: func() {
mockConfig.SetWithoutSource("sca_config.enabled", false)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/setup/apm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func setupAPM(config pkgconfigmodel.Config) {
config.BindEnvAndSetDefault("apm_config.instrumentation.disabled_namespaces", []string{}, "DD_APM_INSTRUMENTATION_DISABLED_NAMESPACES")
config.BindEnvAndSetDefault("apm_config.instrumentation.lib_versions", map[string]string{}, "DD_APM_INSTRUMENTATION_LIB_VERSIONS")

config.BindEnv("asm_config.enabled", "DD_APPSEC_ENABLED_PROPAGATE")
config.BindEnvAndSetDefault("asm_config.enabled_namespaces", []string{}, "DD_APPSEC_ENABLED_PROPAGATE_NAMESPACES")
config.BindEnvAndSetDefault("asm_config.disabled_namespaces", []string{}, "DD_APPSEC_DISABLED_PROPAGATE_NAMESPACES")
config.BindEnv("iast_config.enabled", "DD_IAST_ENABLED_PROPAGATE")
config.BindEnvAndSetDefault("iast_config.enabled_namespaces", []string{}, "DD_IAST_ENABLED_PROPAGATE_NAMESPACES")
config.BindEnvAndSetDefault("iast_config.disabled_namespaces", []string{}, "DD_IAST_DISABLED_PROPAGATE_NAMESPACES")
config.BindEnv("sca_config.enabled", "DD_SCA_ENABLED_PROPAGATE")
config.BindEnvAndSetDefault("sca_config.enabled_namespaces", []string{}, "DD_SCA_ENABLED_PROPAGATE_NAMESPACES")
config.BindEnvAndSetDefault("sca_config.disabled_namespaces", []string{}, "DD_SCA_DISABLED_PROPAGATE_NAMESPACES")

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why you placed these configurations here undeapm.go
AFAIK, these three features are not related to APM, should these configs be places under pkg/config/setup/config.go ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need to use asm_config.*, asm.* should be enough. (same for all the others)

config.BindEnv("apm_config.max_catalog_services", "DD_APM_MAX_CATALOG_SERVICES")
config.BindEnv("apm_config.receiver_timeout", "DD_APM_RECEIVER_TIMEOUT")
config.BindEnv("apm_config.max_payload_size", "DD_APM_MAX_PAYLOAD_SIZE")
Expand Down
Loading