Article
· Jul 18 8m read

Python tool for exporting/importing InterSystems API Manager configurations

🛠️ Managing InterSystems InterSystems API Manager (IAM = Kong Gateway) configurations in CI/CD

🔍 Context: InterSystems IAM configurations 

As part of integrating InterSystems IRIS into a secure and controlled environment, InterSystems IAM relies on Kong Gateway to manage exposed APIs.
Kong acts as a modern API Gateway, capable of handling authentication, security, traffic management, plugins, and more.

However, maintaining consistent Kong configurations (routes, services, plugins, etc.) across different environments (development, testing, production) is a major challenge. This is where tools like deck and this Python script become highly valuable.


⚙️ Overview of kong_config_tool.py

This tool allows you to:

  • Export the current configuration of a Kong Gateway into a versionable YAML file.
  • Import a YAML configuration into a Kong Gateway (via deck sync).
  • Automate full logging (logs, stdout/stderr) for accurate tracking.
  • Easily integrate into a CI/CD pipeline.

🎯 Goals and Benefits

🔄 Consistent Synchronization Across Environments

The tool simplifies propagating Kong configuration between environments. By exporting from dev and importing into staging or prod, you ensure functional parity.

🔐 Traceability and Audits via Logs

With the --log option, all operations (including internal deck commands) are logged:

  • Who executed what
  • What configuration was applied
  • What was Kong’s response (number of resources created, modified, etc.)

🧪 CI/CD Pipeline Integration

In GitLab CI, GitHub Actions, or Jenkins:

  • The export step can be triggered automatically after API changes.
  • The import step can deploy the Kong config on every merge or release.
  • The generated YAML files can be version-controlled in Git.

🧰 Example GitLab Pipeline

stages:
  - export
  - deploy

export_kong:
  stage: export
  script:
    - python3 kong_config_tool.py --export --log
  artifacts:
    paths:
      - kong.yaml
      - kong_config_tool.log

deploy_kong:
  stage: deploy
  script:
    - python3 kong_config_tool.py --import --log

🛡️ Security and Reproducibility

Since InterSystems IAM is often used in sensitive environments (healthcare, finance...), it’s essential to:

  • Avoid manual errors using deck sync
  • Ensure each deployment applies the exact same configuration
  • Maintain a clear audit trail via .log files

💡 Tool Highlights

Feature Description
--export Saves the current config to a file like kong-<timestamp>.yaml
--import Applies the contents of kong.yaml to the Gateway
--log Enables full logging (stdout, stderr, logs)
Automatic Symlink kong.yaml is always a symlink to the latest exported version
Easy Integration No heavy dependencies — relies on standard Python and deck

📦 Conclusion

The kong_config_tool.py script is a key component for industrializing Kong configuration management in the context of InterSystems IAM. It enables:

  • Better configuration control
  • Enhanced traceability
  • Smooth integration into CI/CD pipelines
  • Compliance with security requirements

🚀 Potential Future Enhancements

  • Native GitOps integration (ArgoCD, FluxCD)
  • Configuration validation with deck diff
  • Error notifications (Slack, Teams)

🧬 Python Code Overview

The kong_config_tool.py script is a Python CLI tool designed to automate configuration exports and imports for Kong Gateway using deck, while ensuring robust logging.


📁 General Structure

#!/usr/bin/env python3

import argparse
import subprocess
from datetime import datetime
from pathlib import Path
import sys
import logging
  • Uses only standard Python modules.
  • argparse: to handle command-line options.
  • subprocess: to run deck commands.
  • logging: for structured output (console + file).

🧱 Logger Initialization

logger = logging.getLogger("kong_config_tool")
  • Initializes a named logger, configurable based on whether a log file is requested.

📝 setup_logging(log_file=None)

This function:

  • Creates handlers for both console and/or file.
  • Redirects sys.stdout and sys.stderr to the log file if --log is provided.

🔎 This captures everything: Python logs, print(), errors, and also output from deck.


📤 export_kong_config()

deck_dir = Path.cwd()
output_file = deck_dir / f"kong-{timestamp}.yaml"
  • Executes deck gateway dump -o ... to export the current configuration.
  • Captures stdout and stderr and sends them to logger.debug(...).
  • Creates or updates a kong.yaml symlink pointing to the exported file — simplifying future imports.
  • Logs and exits on failure.

📥 import_kong_config()

  • Checks for the presence of the kong.yaml file (symlink or actual file).
  • Runs deck gateway sync kong.yaml.
  • Captures and logs full output.
  • Handles errors via CalledProcessError.

🔁 This logic mirrors the export process.


🚀 main()

The main entry point that:

  • Handles --export, --import, and --log arguments.
  • Calls the appropriate functions.

Example usage:

python kong_config_tool.py --export --log

python kong_config_tool.py --import --log

💡 If --log is omitted, output goes to console only.


🧪 Typical CI/CD Execution

Export

python kong_config_tool.py --export --log 

Results:

  • kong-2025-07-18_12-34-56.yaml (versionable content)
  • kong.yaml (useful symlink for import)
  • kong_config_tool.log (audit log)

Import

python kong_config_tool.py --import --log 

Results:

  • Applies the configuration to a new gateway (staging, prod, etc.)
  • kong_config_tool.log to prove what was done

✅ Code Summary Table

Feature Implementation
Intuitive CLI Interface argparse with help descriptions
Clean Export deck gateway dump + timestamp
Controlled Import deck gateway sync kong.yaml
Full Logging logging + stdout/stderr redirection
Resilience Error handling via try/except
CI/CD Ready Simple interface, no external dependencies

Let me know if you'd like the English version of the actual code too!

 
kong_config_tool.py

 

Discussion (0)1
Log in or sign up to continue