{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paulkov.github.io/dpone/schemas/gitops/connection-registry.schema.json",
  "title": "dpone GitOps connection registry",
  "type": "object",
  "required": [
    "schema",
    "connections"
  ],
  "additionalProperties": true,
  "properties": {
    "schema": {
      "const": "dpone.connection-registry.v1"
    },
    "environment": {
      "type": "string"
    },
    "connections": {
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/connectionRef"
      },
      "additionalProperties": {
        "type": "object",
        "required": [
          "type",
          "credentials"
        ],
        "additionalProperties": true,
        "properties": {
          "type": {
            "type": "string",
            "minLength": 1
          },
          "connection": {
            "type": "object",
            "additionalProperties": true
          },
          "credentials": {
            "oneOf": [
              {
                "$ref": "#/$defs/vaultKv"
              },
              {
                "$ref": "#/$defs/kubernetesSecretVolume"
              },
              {
                "$ref": "#/$defs/kubernetesSecretApi"
              },
              {
                "$ref": "#/$defs/airflowConnection"
              },
              {
                "$ref": "#/$defs/envVar"
              }
            ]
          }
        }
      }
    },
    "fingerprint": {
      "$ref": "#/$defs/sha256"
    }
  },
  "$defs": {
    "sha256": {
      "type": "string",
      "pattern": "^sha256:[A-Fa-f0-9]{64}$"
    },
    "connectionRef": {
      "type": "string",
      "pattern": "^[A-Za-z0-9](?:[A-Za-z0-9_.-]{0,126}[A-Za-z0-9])?$",
      "maxLength": 128
    },
    "fields": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "type": "string",
        "minLength": 1,
        "pattern": "\\S"
      },
      "additionalProperties": {
        "type": "string",
        "minLength": 1,
        "pattern": "\\S"
      }
    },
    "envVarFields": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "type": "string",
        "minLength": 1,
        "pattern": "\\S"
      },
      "additionalProperties": {
        "type": "string",
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
      }
    },
    "airflowConnectionId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_.-]+$"
    },
    "safeVolumeMountPath": {
      "type": "string",
      "pattern": "^/run/secrets/dpone/\\S+$",
      "not": {
        "pattern": "(^|/)\\.\\.(/|$)"
      }
    },
    "safeVolumeFields": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "type": "string",
        "minLength": 1,
        "pattern": "\\S"
      },
      "additionalProperties": {
        "type": "string",
        "minLength": 1,
        "pattern": "\\S",
        "not": {
          "anyOf": [
            {
              "pattern": "^/"
            },
            {
              "pattern": "(^|/)\\.\\.(/|$)"
            }
          ]
        }
      }
    },
    "vaultKv": {
      "type": "object",
      "required": [
        "resolver",
        "mount",
        "path",
        "fields",
        "version_policy",
        "resolution_scope"
      ],
      "additionalProperties": true,
      "properties": {
        "resolver": {
          "const": "vault_kv"
        },
        "mount": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$",
          "maxLength": 128
        },
        "kv_version": {
          "enum": [
            1,
            2
          ]
        },
        "path": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9_.-]*(/[A-Za-z0-9][A-Za-z0-9_.-]*)*$",
          "maxLength": 512,
          "not": {
            "pattern": "(^|/)(v1|data)(/|$)"
          }
        },
        "fields": {
          "$ref": "#/$defs/fields"
        },
        "version_policy": {
          "enum": [
            "latest",
            "pinned"
          ]
        },
        "resolution_scope": {
          "enum": [
            "workload_start",
            "dag_run_start"
          ]
        }
      }
    },
    "kubernetesSecretVolume": {
      "type": "object",
      "required": [
        "resolver",
        "secret_name",
        "mount_path",
        "fields"
      ],
      "additionalProperties": true,
      "properties": {
        "resolver": {
          "const": "kubernetes_secret_volume"
        },
        "secret_name": {
          "type": "string",
          "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
          "maxLength": 63
        },
        "mount_path": {
          "$ref": "#/$defs/safeVolumeMountPath"
        },
        "payload_format": {
          "enum": [
            "field_mapping",
            "airflow_connection_uri"
          ]
        },
        "fields": {
          "$ref": "#/$defs/safeVolumeFields"
        }
      }
    },
    "kubernetesSecretApi": {
      "type": "object",
      "required": [
        "resolver",
        "namespace",
        "name",
        "fields"
      ],
      "additionalProperties": true,
      "properties": {
        "resolver": {
          "const": "kubernetes_secret_api"
        },
        "namespace": {
          "type": "string",
          "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
          "maxLength": 63
        },
        "name": {
          "type": "string",
          "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
          "maxLength": 63
        },
        "fields": {
          "$ref": "#/$defs/fields"
        }
      }
    },
    "airflowConnection": {
      "type": "object",
      "required": [
        "resolver",
        "connection_id",
        "execution_mode"
      ],
      "additionalProperties": true,
      "properties": {
        "resolver": {
          "const": "airflow_connection"
        },
        "connection_id": {
          "$ref": "#/$defs/airflowConnectionId"
        },
        "execution_mode": {
          "const": "operator_bridge"
        }
      }
    },
    "envVar": {
      "type": "object",
      "required": [
        "resolver",
        "support",
        "fields"
      ],
      "additionalProperties": true,
      "properties": {
        "resolver": {
          "const": "env_var"
        },
        "support": {
          "const": "development_only"
        },
        "fields": {
          "$ref": "#/$defs/envVarFields"
        }
      }
    }
  }
}
