본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

deck 파일 린트

원문 보기 위키 갱신

deck 파일 린트 (deck file lint)

deck file lint는 JSON이나 YAML 파일의 내용을 검증할 수 있는 유연한 린터예요. 규칙(Rule), 셀렉터(Selector), 함수(Function), 룰셋(Ruleset)이라는 핵심 개념을 조합해서 파일 검증 규칙을 직접 만들 수 있어요. 예를 들어 Kong Gateway 서비스가 https 프로토콜만 사용하도록 강제하는 규칙을 만들고 decK 선언형 구성 파일에 적용해 볼 수 있어요. 이 문서에서는 기본 개념과 실제 검증 예제, 자주 쓰이는 패턴과 명령어 사용법까지 안내해요

출처: 문서

본문

decK로 린팅을 하려면 몇 가지 핵심 개념을 이해해야 해요:

  • Rules는 제공된 파일에 적용할 셀렉터, 함수, 실패 심각도를 정의해요.
  • Selectors는 검증할 객체를 선택하기 위해 입력 파일에 적용할 필터를 정의해요. 셀렉터는 Rule의 given 키워드에 지정돼요. 셀렉터는 JSONPath 문법으로 표현돼요.
  • Functions는 필터링된 값을 받아 검증을 수행하고, 위반이 있을 때 정보를 반환해요.
  • Rulesets는 Rule들의 모음이에요.

사용 가능한 규칙의 전체 목록은 vacuum 문서를 참고하세요.

deck file lint 예제

Kong Gateway 서비스는 decK 파일의 services 블록에 정의돼요. 서비스는 게이트웨이와 업스트림 Service 사이에서 사용되는 통신 프로토콜을 지정하는 protocol 필드를 포함해 여러 설정 값을 지원해요. 이 트래픽이 안전하도록 하려면 https 프로토콜만 사용되는지 검증하고 싶을 수 있어요.

다음은 이를 수행하는 단일 Rule을 담고 있는 Ruleset 파일 예제예요:

rules:
  service-https-check:
    description: "Ensure https usage in Kong GW Services"
    given: $.services[*].protocol
    severity: error
    then:
      function: pattern
      functionOptions:
        match: "^https$"

given에 지정된 JSONPath 셀렉터는 입력 파일에서 services 키 아래의 모든 Service에 있는 protocol 필드를 읽어요. 각 값마다 pattern 함수가 적용되는데, 이 함수는 match 필드에 지정된 정규 표현식 패턴을 기준으로 값을 평가해요. 이 예제에서는 protocol 필드의 문자열 값이 문자열 https와 정확히 일치해야 한다고 단언해요.

간단한 작업 추적 시스템을 위한 Service와 Route를 정의하는 다음과 같은 decK 선언형 구성 파일(kong.yaml)이 있다고 가정해 보세요:

_format_version: "3.0"
services:
  - host: tasks.example.com
    name: task-api
    path: /
    protocol: http
    routes:
      - methods:
          - GET
        name: task-api_gettasks
        paths:
          - ~/tasks$

이 구성을 ruleset.yaml에 저장된 예제 ruleset으로 검증하면 다음과 같은 위반이 발생해요:

deck file lint -s kong.yaml ruleset.yaml
Linting Violations: 1
Failures: 1

[error][7:15] Ensure https usage in Kong GW Services: `http` does not match the expression `^https$`

{:.no-copy-code}

선언형 구성을 다음과 같이 수정하면 이 위반이 해결돼요:

_format_version: "3.0"
services:
  - host: tasks.example.com
    name: task-api
    path: /
    protocol: https
    routes:
      - methods:
          - GET
        name: task-api_gettasks
        paths:
          - ~/tasks$
deck file lint -s kong.yaml ruleset.yaml; echo $?

결과:

0

{:.no-copy-code}

명령은 0(성공) 반환 코드를 내요. 위반이 감지되는 경우에는 0이 아닌 반환 코드가 반환되기 때문에 자동화 프로세스를 중단할 수 있고, 문제가 있는 구성이 프로덕션 코드베이스와 시스템에 유출되는 것을 막는 데 도움이 돼요.

일반적인 패턴

다음 섹션에서는 몇 가지 일반적인 린팅 패턴을 정의해요.

구성 파일에 select_tags가 포함되어 있는지 확인하기

select_tags를 사용하면 구성을 분할해서 여러 개의 독립적인 구성으로 관리할 수 있어요.

이 린팅 규칙은 모든 구성 파일에 select_tags가 정의되어 있는지 확인해요:

rules:
  select_tags:
    description: "Select Tags should be present."
    given: $._info
    severity: error
    then:
      field: select_tags
      function: defined
  select_tags_length:
    description: "Select Tags should be present."
    given: $._info
    severity: error
    then:
      field: select_tags
      function: schema
      functionOptions:
        schema:
          type: "array"
          minItems: 1
          items:
            type: "string"

Route에 HTTPS만 강제하기

Kong Gateway가 HTTPS만 리스닝하도록 강제하려면 모든 route에 protocols가 설정되어 있고 그 안에 단일 https 항목이 포함되어 있는지 확인하세요:

rules:
  protocols_set:
    description: "Ensure route protocols are set"
    given: $.routes[*]
    severity: error
    then:
      field: protocols
      function: "schema"
      functionOptions:
        schema:
          type: "array"
          minItems: 1
          maxItems: 1
          items:
            type: "string"
  protocols_https_only:
    description: "Ensure https usage in Kong GW Routes"
    given: $.routes[*].protocols[0]
    severity: error
    then:
      function: pattern
      functionOptions:
        match: "^https$"

명령 사용법

Usage:
  deck file lint [flags] ruleset-file

Flags:
  -D, --display-only-failures   only output results equal to or greater than --fail-severity
  -F, --fail-severity string    results of this level or above will trigger a failure exit code
                                [choices: "error", "warn", "info", "hint"] (default "error")
      --format string           output format [choices: "plain", "json", "yaml"] (default "plain")
  -h, --help                    help for lint
  -o, --output-file string      Output file to write to. Use - to write to stdout. (default "-")
  -s, --state string            decK file to process. Use - to read from stdin. (default "-")

관련 리소스

더 알아보기 (Learn more)

  • decK 구성 파일에서 select_tags를 활용하는 방법을 알아보세요 — select_tags