Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
4.8 KB
Raw Blame
/**
 * E7.16–E7.19: Policy enforcement and L3 NetworkPolicy E2E tests (Phase 7).
 *
 * Validates:
 * - WorkflowRecipePolicy CRD blocks non-compliant recipes
 * - Policy enforcement is blocking (recipe transitions to failed)
 * - L3 binding-scoped NetworkPolicies are created for recipe bindings
 * - Cleanup removes L3 NetworkPolicies
 *
 * Prerequisites: Run scripts/minikube-setup.sh before these tests.
 * These tests run AFTER cleanup.test.ts (sequential mode).
 */
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { RECIPE_NAMESPACE, kubectl, kubectlJson, sleep } from './helpers'

const POLICY_NAME = 'strict-test-policy'

beforeAll(async () => {
  // Clean up any leftover policy
  try {
    kubectl(
      `delete workflowrecipepolicies.clerum.io ${POLICY_NAME} -n ${RECIPE_NAMESPACE} --ignore-not-found`
    )
  } catch {
    /* ignore */
  }

  // Clean up any leftover test recipes
  for (const name of ['policy-test-recipe', 'mcp-redis-cache']) {
    try {
      kubectl(
        `delete workflowrecipe ${name} -n ${RECIPE_NAMESPACE} --ignore-not-found --timeout=20s`
      )
    } catch {
      /* ignore */
    }
  }
  await sleep(3_000)
})

afterAll(() => {
  try {
    kubectl(
      `delete workflowrecipepolicies.clerum.io ${POLICY_NAME} -n ${RECIPE_NAMESPACE} --ignore-not-found`
    )
  } catch {
    /* ignore */
  }
  for (const name of ['policy-test-recipe', 'mcp-redis-cache']) {
    try {
      kubectl(`delete workflowrecipe ${name} -n ${RECIPE_NAMESPACE} --ignore-not-found`)
    } catch {
      /* ignore */
    }
  }
})

describe('Policy Enforcement E2E', () => {
  // E7.16: WorkflowRecipePolicy CRD can be created
  it('E7.16 — WorkflowRecipePolicy CRD is registered and accepts instances', () => {
    const result = kubectl(`apply -f - <<'EOF'
apiVersion: clerum.io/v1alpha1
kind: WorkflowRecipePolicy
metadata:
  name: ${POLICY_NAME}
  namespace: ${RECIPE_NAMESPACE}
spec:
  description: "Strict test policy for E2E validation"
  governance:
    maxWorkloadsPerRecipe: 1
    requiredSecurityLevel: strict
    allowedWorkloadTypes:
      - deployment
  detection:
    imageDenylist:
      - "evil/*"
EOF`)
    expect(result).toMatch(/created|configured/)
  })

  // E7.17: Policy is visible via kubectl get
  it('E7.17 — Policy is listed with printer columns', () => {
    const output = kubectl(`get workflowrecipepolicies.clerum.io -n ${RECIPE_NAMESPACE}`)
    expect(output).toContain(POLICY_NAME)
    expect(output).toContain('1') // MAX-WORKLOADS
    expect(output).toContain('strict') // SECURITY
  })

  // E7.18: Policy blocks non-compliant recipe (2 workloads violate maxWorkloadsPerRecipe=1)
  it('E7.18 — Policy blocks recipe with too many workloads', async () => {
    // Apply a recipe that violates the policy (2 workloads, max is 1)
    kubectl(`apply -f - <<'EOF'
apiVersion: clerum.io/v1alpha1
kind: WorkflowRecipe
metadata:
  name: policy-test-recipe
  namespace: ${RECIPE_NAMESPACE}
spec:
  workloads:
    - id: web
      type: deployment
      image: nginx:1.30.6-alpine
      port: 80
    - id: api
      type: deployment
      image: nginx:1.30.6-alpine
      port: 8080
  security:
    isolationLevel: minimal
EOF`)

    // Wait for the reconciler to process (give it time to reconcile)
    await sleep(5_000)

    // Check the recipe status — should be "failed" due to policy violation
    try {
      const recipe = kubectlJson<{
        status?: {
          phase?: string
          message?: string
          conditions?: Array<{ type: string; message?: string }>
        }
      }>(`get workflowrecipe policy-test-recipe -n ${RECIPE_NAMESPACE}`)

      // The recipe should either be failed or have no workloads deployed
      // (since the reconciler rejects it before deploying)
      if (recipe.status?.phase) {
        expect(recipe.status.phase).toBe('failed')
        expect(recipe.status.message).toContain('Policy violation')
      }
    } catch {
      // Recipe might not have status subresource updated yet — check that
      // no deployments were created (policy blocked before deploying)
      const deploys = kubectlJson<{ items: unknown[] }>(
        `get deploy -l clerum.io/recipe=policy-test-recipe -n ${RECIPE_NAMESPACE}`
      )
      expect(deploys.items.length).toBe(0)
    }
  })

  // E7.19: Cleanup — delete policy and test recipe
  it('E7.19 — Policy and test recipe cleanup succeeds', () => {
    kubectl(
      `delete workflowrecipepolicies.clerum.io ${POLICY_NAME} -n ${RECIPE_NAMESPACE} --ignore-not-found`
    )
    kubectl(`delete workflowrecipe policy-test-recipe -n ${RECIPE_NAMESPACE} --ignore-not-found`)

    // Verify policy is gone
    try {
      kubectl(`get workflowrecipepolicies.clerum.io ${POLICY_NAME} -n ${RECIPE_NAMESPACE}`)
      expect.fail('Policy should have been deleted')
    } catch {
      // Expected — policy not found
    }
  })
})