id: azure-openai-managed-identity-not-used
info:
  name: Azure OpenAI Service Instance Managed Identity Not Used
  author: princechaddha
  severity: medium
  description: |
    Ensure that your Azure OpenAI service instances are using system-assigned and/or user-assigned managed identities to allow secure access to other cloud protected resources such as Azure key vaults. Managed identities minimizes risks, simplifies management, and maintains compliance with evolving cloud services.
  impact: |
    Not using managed identities can increase risks related to security and management, and hinder compliance with security best practices.
  remediation: |
    Configure your Azure OpenAI service instances to use either system-assigned or user-assigned managed identities to enhance security and simplify resource access management.
  reference:
    - https://docs.microsoft.com/en-us/azure/cognitive-services/authentication
  tags: cloud,devops,azure,microsoft,openai,azure-cloud-config,ai

flow: |
  code(1);
  for (let ServiceInstance of iterate(template.instanceList)) {
    ServiceInstance = JSON.parse(ServiceInstance);
    set("name", ServiceInstance.Name);
    set("resourceGroup", ServiceInstance.ResourceGroup);
    code(2);
  }

self-contained: true
code:
  - engine:
      - sh
      - bash
    source: |
      az cognitiveservices account list --output json --query '[?(kind==`OpenAI`)].{"Name":name,"ResourceGroup":resourceGroup}'

    extractors:
      - type: json
        name: instanceList
        internal: true
        json:
          - '.[]'

  - engine:
      - sh
      - bash
    source: |
      az cognitiveservices account identity show --name "$name" --resource-group "$resourceGroup" --query '{"IdentityType":type}'

    matchers-condition: and
    matchers:
      - type: word
        words:
          - '"IdentityType": null'
          - '"IdentityType": "None"'

    extractors:
      - type: dsl
        dsl:
          - 'name + " in " + resourceGroup + " does not use a managed identity"'
# digest: 4a0a00473045022100945bab0dc27ba091ec91d4985711f5036fe43509c43c46e3469fbbf687335748022045e04dc407597434705d69a87dd9eac2eb5d112f8f1ea2da2978fec0af63c9f6:922c64590222798bb761d5b6d8e72950