Conditions in a OPA policy function (Rego)

85 views Asked by At

I have the following helper function in a test of my OPA policy. I would like to make it more generic and return the resource with the commonName field only if cn is not empty. Any ideas on how I can conditionally add the commonName field?

new_certificate(issuerName, uid, organization, organizationalUnit, cn) = {
    "apiVersion": "cert-manager.io/v1",
    "kind": "Certificate",
    "metadata": {
        "name": "test-certificate",
        "namespace": "tenant-ns"
    },
    "spec": {
        "isCA": true,
        "issuerRef": {
            "group": "rhcs-issuer.it-platform.redhat.com",
            "kind": "ClusterIssuer",
            "name": issuerName
        },
        "privateKey": {
            "algorithm": "ECDSA",
            "size": 256
        },
        "secretName": "test-tls",
        "commonName": cn,
        "subject": {
            "organizations": [
                organization
            ],
            "organizationalUnits": [
                organizationalUnit
            ]
        }
    }
}

Thanks, Erkan

0

There are 0 answers