บทที่ 13b ไคลเอนต์, กรุ๊ปแวร์ และ CLI Admin: #pConfigFile

การกำหนดค่าเป็นโค้ด: การส่งออก, วางแผน, นำไปใช้ และไปป์ไลน์ Git

สถานะเซิร์ฟเวอร์ทั้งหมดในรูปแบบไฟล์ YAML ใน Git: ผู้เช่า, โดเมน (DKIM selector, MTA-STS, การกำหนดเส้นทาง), กล่องเมลพร้อมนามแฝง, การส่งต่อและข้อความไม่อยู่ที่สำนักงาน, รายชื่อผู้รับจดหมาย, กล่องเมลร่วม, ห้อง, GAL และกฎทรัพยากร, การแชร์และการมอบหมาย, SMTP, smart host, เส้นทาง, กฎการขนส่ง, การป้องกันสแปมและไวรัส, ไฟร์วอลล์และระบบแบนอัตโนมัติ, TLS, แหล่งตัวตน, คีย์ API (เฉพาะเมตาดาต้า), เว็บฮุก, การแจ้งเตือน, การแจ้งเตือนฝ่ายปฏิบัติการ, POP3, ไฟล์, การจอง, ที่เก็บถาวรและการสลับเส้นทาง DNS อัตโนมัติ nextxch-cli config plan แสดงผลลัพธ์เหมือนกับ terraform plan ว่าจะมีอะไรถูกสร้าง เปลี่ยนแปลง หรือลบ; apply จะตรวจสอบทุกอย่างก่อนและดำเนินการตามลำดับที่กำหนด ผู้ดูแลระบบ: ระบบ > การกำหนดค่าเป็นไฟล์

เคล็ดลับการดำเนินงานและหมายเหตุสถาปัตยกรรม

ข้อมูลลับจะไม่ถูกใส่ลงในไฟล์และจะไม่ถูกส่งออก: รหัสผ่าน โทเค็น และคีย์จะถูกเขียนในรูปแบบ secretRef: {env: NAME} หรือ secretRef: {file: path} และถูกแก้ไข ณ เวลาที่นำไปใช้ (โดย nextxch-cli บนตัวรัน CI; บนเซิร์ฟเวอร์จะใช้เฉพาะตัวแปร NEXTXCH_SECRET_* และไฟล์ใต้ <data>/secrets/) คีย์ DKIM ใบรับรอง และแฮชรหัสผ่านจะยังคงอยู่บนเซิร์ฟเวอร์ ฟีเจอร์ระดับธุรกิจเช่น API v1; JSON Schema เปิดให้ใช้งานฟรี

1. รูปแบบไฟล์: apiVersion, kind, metadata.name, spec

หนึ่งไฟล์ที่มีเอกสารหลายฉบับ (คั่นด้วย ---) หรือโฟลเดอร์ที่มีหนึ่งไฟล์ต่อหนึ่งพื้นที่ (config export --split) ฟิลด์ที่ไม่มีอยู่ในไฟล์จะคงเดิมบนเซิร์ฟเวอร์ รายการ (นามแฝด สมาชิก) นับเป็นภาพรวมทั้งหมด

Reference / Configuration
# yaml-language-server: $schema=https://nextxch.com/schemas/nextxch-config-v1.schema.json
apiVersion: nextxch/v1
kind: Manifest            # optional: areas in which --prune may delete
metadata: {name: nextxch}
spec: {areas: [domains, accounts, sharing, transport]}
---
apiVersion: nextxch/v1
kind: Domain
metadata: {name: company.com}
spec:
  tenant: default
  catchAll: info@company.com
  dkim: {mode: dual, rsaSelector: s2026, ed25519Selector: ed2026}   # keys stay on the server
  mtaSts: {mode: enforce, maxAgeSeconds: 604800, mxHosts: [mail.company.com]}
---
apiVersion: nextxch/v1
kind: Account
metadata: {name: anna.smith@company.com}
spec:
  displayName: Anna Smith
  role: User                      # User, TenantAdmin, ServerAdmin, ComplianceAuditor
  quotaMb: 10240
  aliases: [anna@company.com]
  forwarding: {address: anna@private.example, keepCopy: true}
  outOfOffice: {enabled: false}
  password: {secretRef: {env: ANNA_PASSWORD}}   # never the password itself
---
apiVersion: nextxch/v1
kind: Account
metadata: {name: info@company.com}
spec: {type: shared, displayName: Info mailbox}   # user, shared, room, equipment
---
apiVersion: nextxch/v1
kind: Share
metadata: {name: "info@company.com/mailbox -> anna.smith@company.com"}
spec: {permissions: [read, write, sendAs]}
---
apiVersion: nextxch/v1
kind: SmtpSettings
metadata: {name: server}             # server-wide settings are always named "server"
spec:
  outboundSmartHost: smtp.provider.example
  outboundSmartPort: 587
  smartHostPassword: {secretRef: {file: secrets/smarthost.txt}}

2. เวิร์กฟลายด้วย nextxch-cli

ส่งออกและคอมมิตก่อน จากนั้นจึงทำการเปลี่ยนแปลงผ่านพูลรีเควสต์ ตัวอย่างผลลัพธ์ของแผน:

Reference / Configuration
export NEXTXCH_URL=https://mail.company.com
export NEXTXCH_API_KEY=smk_...            # key with config.read or config.write
nextxch-cli config export --split nextxch/
nextxch-cli config validate -f nextxch/    # offline: kinds and fields
nextxch-cli config plan -f nextxch/

  ~ Domain company.com
      mtaSts.mode: "testing" -> "enforce"
  ~ Account anna.smith@company.com
      quotaMb: 10240 -> 20480
      aliases: ["anna@company.com"] -> ["a.smith@company.com","anna@company.com"]
      ~ password: (secret changed)
  + Account info@company.com
      type: "shared"
      displayName: "Info mailbox"

Not in the file (1), deleted only with --prune:
    Account old@company.com

Plan: 1 to create, 2 to update, 0 to delete, 41 unchanged.
# exit code 2 = changes

nextxch-cli config apply -f nextxch/ --prune --yes
nextxch-cli config history

3. GitHub Actions: วางแผนเมื่อดึงคำขอ (pull request), ดำเนินการเมื่อผสาน (merge)

ไฟล์ฉบับสมบูรณ์พร้อมขั้นตอนคอมเมนต์: deploy/gitops/github-actions.yml ในที่เก็บข้อมูล คีย์ API มาจากความลับของที่เก็บข้อมูล ไม่ใช่จากไฟล์

Reference / Configuration
name: nextxch config
on:
  pull_request: {paths: ["nextxch/**"]}
  push: {branches: [main], paths: ["nextxch/**"]}
permissions: {contents: read, pull-requests: write}
concurrency: {group: nextxch-config, cancel-in-progress: false}
env: {NEXTXCH_IMAGE: "nextxch/server:latest"}
jobs:
  plan:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Plan
        env:
          NEXTXCH_URL: ${{ vars.NEXTXCH_URL }}
          NEXTXCH_API_KEY: ${{ secrets.NEXTXCH_CONFIG_READ_KEY }}
        run: |
          set +e
          docker run --rm -e NEXTXCH_URL -e NEXTXCH_API_KEY -e NO_COLOR=1 -v "$PWD:/work" -w /work \
            --entrypoint nextxch-cli "$NEXTXCH_IMAGE" config plan -f nextxch/ --prune > plan.txt 2>&1
          code=$?; cat plan.txt; [ "$code" -ne 1 ]
      # danach: actions/github-script postet plan.txt als Kommentar (siehe deploy/gitops)
  apply:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - env:
          NEXTXCH_URL: ${{ vars.NEXTXCH_URL }}
          NEXTXCH_API_KEY: ${{ secrets.NEXTXCH_CONFIG_WRITE_KEY }}
          SMARTHOST_PASSWORD: ${{ secrets.SMARTHOST_PASSWORD }}
        run: |
          docker run --rm -e NEXTXCH_URL -e NEXTXCH_API_KEY -e SMARTHOST_PASSWORD -v "$PWD:/work" -w /work \
            --entrypoint nextxch-cli "$NEXTXCH_IMAGE" config apply -f nextxch/ --prune --yes

4. GitLab CI: plan as merge request note, apply on the default branch

ไฟล์ฉบับสมบูรณ์: deploy/gitops/gitlab-ci.yml คีย์เป็นตัวแปร CI/CD ที่มีการปิดบังและป้องกัน; resource_group ป้องกันการประยุกต์ใช้พร้อมกันสองรายการ

Reference / Configuration
stages: [validate, plan, apply]
variables: {CONFIG_DIR: nextxch, NO_COLOR: "1"}
.nextxch-cli:
  image: {name: "nextxch/server:latest", entrypoint: [""]}
validate:
  extends: .nextxch-cli
  stage: validate
  script: [nextxch-cli config validate -f "$CONFIG_DIR"]
plan:
  extends: .nextxch-cli
  stage: plan
  rules: [{if: '$CI_PIPELINE_SOURCE == "merge_request_event"'}]
  variables: {NEXTXCH_API_KEY: $NEXTXCH_CONFIG_READ_KEY}
  script:
    - set +e
    - nextxch-cli config plan -f "$CONFIG_DIR" --prune > plan.txt 2>&1; code=$?
    - cat plan.txt; test "$code" -ne 1
  artifacts: {when: always, paths: [plan.txt]}
# plan-comment: curl auf /merge_requests/:iid/notes mit plan.txt (siehe deploy/gitops)
apply:
  extends: .nextxch-cli
  stage: apply
  resource_group: nextxch-config
  environment: production
  rules: [{if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'}]
  variables: {NEXTXCH_API_KEY: $NEXTXCH_CONFIG_WRITE_KEY}
  script: [nextxch-cli config apply -f "$CONFIG_DIR" --prune --yes]

5. JSON Schema in the editor and REST call

VS Code (ส่วนขยาย "YAML" ของ Red Hat) และ IDE ของ JetBrains ตรวจสอบไฟล์ขณะพิมพ์ ทุกเซิร์ฟเวอร์ยังให้บริการสกีมาที่ /api/v1/config/schema ด้วย

Reference / Configuration
# .vscode/settings.json
{
  "yaml.schemas": {
    "https://nextxch.com/schemas/nextxch-config-v1.schema.json": ["nextxch/*.yaml"]
  }
}

# Plan ohne CLI, direkt per REST (YAML als Body):
curl -s -X POST "$NEXTXCH_URL/api/v1/config/plan?prune=true" \
  -H "Authorization: Bearer $NEXTXCH_API_KEY" \
  -H "Content-Type: application/yaml" --data-binary @nextxch.yaml

6. Secrets in lists and ACME without a restart

MTA-Hooks mit Auth-Header und HMAC-Secret aus der Pipeline, ein Alarmziel mit einem Webhook-Secret und der Schalter zu Let's Encrypt mit DNS-01: Einmal anwenden, kein Neustart.

Reference / Configuration
apiVersion: nextxch/v1
kind: MtaFilters
metadata: {name: server}
spec:
  hooks:
    - name: spamcheck
      url: https://hooks.company.com/check
      stages: [data]
      authHeaderName: Authorization
      authHeaderValue: {secretRef: {env: HOOK_TOKEN}}   # in the plan: hooks[spamcheck].authHeaderValue
      hmacSecret: {secretRef: {file: secrets/hook-hmac.txt}}
---
apiVersion: nextxch/v1
kind: OpsAlerts
metadata: {name: server}
spec:
  targets:
    - name: pager
      type: webhook
      address: https://pager.company.com/hook
      secret: {secretRef: {env: PAGER_SECRET}}
---
apiVersion: nextxch/v1
kind: TlsSettings
metadata: {name: server}
spec:
  acme:
    enabled: true
    directoryUrl: https://acme-v02.api.letsencrypt.org/directory
    contactEmail: postmaster@company.com
    challenge: Dns01
    renewBeforeExpiryDays: 30

การตั้งค่า ค่าเริ่มต้น และแนวปฏิบัติที่ดีที่สุด

การตั้งค่า / ตำแหน่ง คำอธิบาย ค่าเริ่มต้น แนวปฏิบัติที่ดีที่สุดในการผลิต
config export [--area a,b] [--split ordner/] [-o datei] ส่งออกสถานะเซิร์ฟเวอร์แบบกำหนดแน่นอน: ลำดับเอกสารคงที่ รายการที่เรียงลำดับ ไม่มีประทับเวลา การส่งออกสองครั้งจะได้ไบต์ที่เหมือนกัน ความแตกต่างใน Git จะแสดงเฉพาะการเปลี่ยนแปลงจริงเท่านั้น alle Bereiche ใช้ --split สำหรับที่เก็บ (หนึ่งไฟล์ต่อพื้นที่: domains.yaml, accounts.yaml ...); พูลรีเควสต์ขนาดเล็กทำให้อ่านง่ายอยู่เสมอ
config plan -f datei|ordner [--prune] แสดงต่อวัตถุตลอดจนการสร้าง, ~ อัปเดต (ฟิลด์: เก่า -> ใหม่) และ - ลบ รหัสออก 0 = ไม่มีเปลี่ยนแปลง, 2 = มีการเปลี่ยนแปลง, 1 = เกิดข้อผิดพลาด ข้อมูลลับจะปรากฏเป็น "(เปลี่ยนข้อมูลลับแล้ว)" เท่านั้น ohne Löschen รันบนพูลรีเควสต์และโพสต์เป็นความคิดเห็น (ตัวอย่างด้านล่าง)
config apply -f ... [--prune] [--yes] [--force] [--dry-run] ตรวจสอบทุกอย่างก่อน (ฟิลด์, ข้อมูลอ้างอิง, รหัสลับ, กฎความปลอดภัย) จากนั้นสร้างและอัปเดตตามลำดับที่กำหนด (ผู้เช่า, โดเมน, บัญชี, รายการ, กฎ, การแชร์, การขนส่ง ...) และลบในลำดับย้อนกลับ หากขั้นตอนใดล้มเหลว ขั้นตอนที่ทำไปแล้วจะถูกยกเลิก ทุกการเปลี่ยนแปลงจะถูกบันทึกในบันทึกการตรวจสอบ (audit log) fragt nach ส่งผ่าน --yes ในไปพไลน์เสมอ หากไม่มีเทอร์มินัลคำสั่ง apply จะปฏิเสธ --dry-run เท่ากับ plan
--prune und Manifest Deletes happen only with --prune and only in the areas the Manifest document names (without a Manifest: only the kinds present in the file). Server settings are never deleted. aus การส่งออกจะเขียนไฟล์กำกับ; หากคุณจัดการเพียงบางส่วน ให้จำกัดพื้นที่ให้แคบลง
Schutz: letzter Admin, Domain mit Postfächern แผนที่จะทำให้ไม่มีผู้ดูแลระบบเซิร์ฟเวอร์ที่ใช้งานอยู่ และการลบโดเมนที่มีกล่องจดหมายอยู่จะถูกปฏิเสธ --force จะลบล้างเงื่อนไขนี้ (จากนั้นโดเมนจะถูกปิดการใช้งานแทนการลบ) คีย์ API ของตนเองไม่สามารถเพิกถอนตัวเองได้ aktiv ห้ามใส่ --force ตายตัวในไปป์ไลน์
secretRef: {env: NAME} | {file: pfad} Reference to a secret instead of its value. nextxch-cli resolves it on the runner (any variable, file relative to the configuration). The server itself only reads NEXTXCH_SECRET_* and files below <data>/secrets/. Re-applying the same value is no change. - ส่งความลับจากที่เก็บความลับของ GitHub/GitLab ไปยังงานในฐานะตัวแปรสภาพแวดล้อม
Scopes config.read / config.write REST: GET /api/v1/config/export, POST /api/v1/config/plan, POST /api/v1/config/apply, GET /api/v1/config/history, GET /api/v1/config/schema (ฟรี). คีย์: คีย์การรวมเทแนนท์ ใช้งานได้เฉพาะในขณะที่ผู้สร้างยังคงเป็นผู้ดูแลระบบอยู่ คีย์ของผู้ดูแลระบบเซิร์ฟเวอร์จะมองเห็นทั้งเซิร์ฟเวอร์ ส่วนคีย์ของผู้ดูแลระบบเทแนนท์จะมองเห็นเฉพาะเทแนนท์นั้น config.read เพียงพอสำหรับการส่งออกและการวางแผน ส่วน apply ต้องใช้ config.write - สองคีย์: config.read สำหรับแผนในพูลรีเควสต์, config.write สำหรับงาน apply บน main เท่านั้น, จำกัดไว้ที่ IP ของรันเนอร์หากเป็นไปได้
Mandanten-Admins ผู้ดูแลระบบผู้เช่า (เซสชันหรือคีย์ใดคีย์หนึ่ง) จะส่งออก วางแผน และใช้เฉพาะออบเจกต์ของผู้เช่าตนเอง: Domain (เปลี่ยนเท่านั้น, ห้ามสร้างหรือลบ), Account, DistributionList, Share, GalVisibilityRule, ResourceAccessRule และ Webhook การตั้งค่าเซิร์ฟเวอร์, ผู้เช่า, แหล่งที่มาของข้อมูลประจำตัว และคีย์ API จะอยู่กับผู้ดูแลระบบเซิร์ฟเวอร์; บัญชีที่มีบทบาท ServerAdmin หรือ ComplianceAuditor จะอยู่นอกเหนือขอบเขต --prune จะลบเฉพาะภายในผู้เช่าของตนเอง ประวัติจะแสดงเฉพาะการใช้งานของตนเองเท่านั้น - สำหรับตัวแทนจำหน่ายและแผนกต่างๆ: ผู้เช่าแต่ละรายจะเก็บบันทึกข้อมูล (repository) ของตนเองไว้พร้อมกับคีย์ของตนเอง ช่อง "tenant" อาจถูกเว้นว่างไว้ โดยจะถูกเติมด้วยผู้เช่าของตนเอง
secretRef in Listen รายการรายการนำรหัสผ่านผ่าน secretRef เช่นกัน: MtaFilters hooks[].authHeaderValue และ hooks[].hmacSecret, OpsAlerts targets[].secret (HMAC ของเป้าหมายเว็บฮุก) แผนจะตั้งชื่อรายการด้วย id หรือ name เช่น hooks[spamcheck].authHeaderValue ค่าธรรมดาในรายการจะถูกปฏิเสธ - รายการที่ไม่มี id จะได้รับเมื่อใช้งานครั้งแรก; หลังจากส่งออกแล้ว ให้คง id ไว้ในไฟล์ จากนั้นความลับจะยังคงอยู่กับรายการที่ถูกต้องเมื่อมีการจัดเรียงรายการใหม่
ACME zur Laufzeit TlsSettings.acme (เปิดใช้งาน, directoryUrl, contactEmail, challenge Http01/TlsAlpn01/Dns01, renewBeforeExpiryDays, reusePrivateKeyForDane) มีผลโดยไม่ต้องรีสตาร์ท: appsettings.json ยังคงเป็นฐาน ความแตกต่างจะอยู่ใน <data>/acme-settings.json และมีผลกับทั้งคลัสเตอร์ การเปลี่ยน URL ของไดเรกทอรีจะสร้างบัญชี ACME ใหม่ในการสั่งซื้อครั้งถัดไป appsettings.json ทดสอบกับไดเรกทอรี staging ของ Let's Encrypt ก่อน จากนั้นจึงสลับไปที่การผลิต
Arten (kinds) Tenant, Domain, Account, DistributionList, GalVisibilityRule, ResourceAccessRule, Share, SmtpSettings, Route, TransportRules, SpamFilter, MtaFilters, FirewallRule, TlsSettings, IdentitySource, ApiKey, Webhook, Notifications, OpsAlerts, DnsFailover, Pop3, FileStorage, Booking, ArchivePolicy ทุกฟิลด์: JSON Schema - คีย์ API ไม่ได้สร้างขึ้นจากไฟล์ (ค่าความลับมีอยู่เพียงครั้งเดียว); การเปลี่ยนแปลงไฟล์จะเปลี่ยนป้ายกำกับและสโคป และเพิกถอนด้วย --prune การตั้งค่าห้องประชุมและการจองจะเหมือนกันบนทุกโหนดคลัสเตอร์