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

supporting cws multi-policy in terraform #2681

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

homoeconomics
Copy link

@homoeconomics homoeconomics commented Nov 15, 2024

Adding new resources and data sources to support the CWS multi-policy feature.

  1. Updated the data source datadog_csm_threats_agent_rules such that users could pass in the policy_id as an optional attribute to fetch rules in a specific policy.
  2. Added a new data source datadog_csm_threats_policies for users to list their policies.
  3. Added a new resource datadog_csm_threats_policy for users to manage their CWS policies.
  4. Added a new resource datadog_csm_threats_multi_policy_agent_rule for users to manage their rules in a policy.

How to deploy policy resources:

resource "datadog_csm_threats_policies_list" "all" {
    entries {
        policy_id = datadog_csm_threats_policy.policy1.id
        name = "TERRAFORM_POLICY1"
        priority = 1
    }
    entries {
        policy_id = datadog_csm_threats_policy.policy2.id
        name = "TERRAFORM_POLICY2"
        priority = 2
    }
}

resource "datadog_csm_threats_policy" "policy1" {
    description = "created with terraform"
    enabled     = false
    tags        = []
}

resource "datadog_csm_threats_policy" "policy2" {
    description = "created with terraform 2"
    enabled     = true
    tags        = ["env:staging"]
}

TODO: record the tests after the DD SDK is updated.

@homoeconomics homoeconomics force-pushed the daniel.zhou/CWS-3394-tf-provider branch from 6322019 to 5da3fb7 Compare November 15, 2024 20:48
import (
"context"
"fmt"
mathrand "math/rand"

Choose a reason for hiding this comment

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

Code Vulnerability

Suggested change
mathrand "math/rand"
mathrand "crypto/rand"
The math/rand library does not generate cryptographically secure random numbers. (...read more)

Using the math/rand package in Go for generating random numbers may lead to vulnerabilities in certain security-critical contexts. Here's why it is recommended to exercise caution when using this package:

  1. Pseudorandomness: The math/rand package generates pseudorandom numbers, which are generated from a deterministic algorithm and a seed value. These numbers are not truly random and may exhibit patterns or predictable sequences. In security-critical applications, such as cryptography or secure password generation, true randomness is essential to prevent guessing or predicting the random values.
  2. Predictable seed value: By default, the math/rand package uses a predictable seed value based on the current time. This means that if multiple processes or instances of the software start at the same time or use the same seed, they will generate the exact same sequence of random numbers. This predictability can be exploited by an attacker to reproduce the random values and potentially compromise the security of the system.
  3. Insufficient entropy: In security-sensitive contexts, it is crucial to have a good source of entropy, which is a measure of unpredictability. The math/rand package does not provide a direct way to access system-level entropy sources. It relies on a fixed seed or a manually set seed value, which may not have sufficient entropy to generate adequately random numbers for cryptographic operations or other security-critical tasks.
  4. SecureRandom: In contrast to math/rand, the crypto/rand package in Go provides a secure random number generator that uses a system-level entropy source. It generates cryptographically secure random numbers suitable for security-sensitive applications. It is recommended to use crypto/rand for generating random numbers in scenarios that require strong randomness and security.

To mitigate vulnerabilities and ensure the secure generation of random values, it is recommended to use the crypto/rand package instead of math/rand for security-critical applications. The crypto/rand package provides a more reliable source of random numbers, leveraging the underlying operating system's entropy source for improved security.

Always consider the specific requirements of your application and the context in which random numbers are used. Following best practices and using appropriate cryptographic libraries can help mitigate vulnerabilities and ensure the security of your Go applications.

View in Datadog  Leave us feedback  Documentation

func (r *csmThreatsPoliciesListResource) applyBatchPolicies(ctx context.Context, entries []csmThreatsPoliciesListEntryModel, diags *diag.Diagnostics) ([]csmThreatsPoliciesListEntryModel, error) {
listResp, httpResp, err := r.api.ListCSMThreatsAgentPolicies(r.auth)
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
Copy link
Author

Choose a reason for hiding this comment

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

Do you mean httpResp.StatusCode != 404?

@@ -97,3 +97,4 @@ require (
)

go 1.23
replace github.com/DataDog/datadog-api-client-go/v2 v2.34.1-0.20241226155556-e60f30b0e84e => ../datadog-api-spec/generated/datadog-api-client-go
Copy link
Author

Choose a reason for hiding this comment

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

I guess this is only for local testing and you don't plan to check this in?


### Optional

- `entries` (Block Set) A set of policies that belong to this list/batch. All non-listed policies get deleted. (see [below for nested schema](#nestedblock--entries))
Copy link
Author

Choose a reason for hiding this comment

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

Maybe we should also mention that customers are supposed to create exactly one policy list.

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.

2 participants