mcpField
C
custom-builtin-functions

custom-builtin-functions

github.com/chainloop-dev開発60738 回インストール今日更新
リポジトリ

custom-builtin-functions について

Go で OPA/Rego ポリシーエンジンのカスタム組み込み関数を追加するためのスキルです。名前空間、エラー処理、キャンセル対応、関数の文書化を案内します。

主な機能

  1. 01Go によるカスタム組み込み関数の実装
  2. 02chainloop.* 名前空間の使用
  3. 03サービス呼び出しの非制限指定
  4. 04コンテキストによるタイムアウトとキャンセル
  5. 05関数シグネチャと動作の文書化

活用シーン

  1. 01Rego ポリシーへの固有関数の追加
  2. 02ポリシー関数からのサードパーティサービス呼び出し

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/chainloop-dev/chainloop --skill custom-builtin-functions

インストール後、Claude Code で/custom-builtin-functionsを入力して読み込まれたか確認します。

詳細

ポリシーエンジンの拡張

OPA/Rego ポリシーエンジンは、Go で記述されたカスタム組み込み関数をサポートしています。

カスタム組み込み関数の追加:

  1. 組み込み関数の実装を作成する(例:pkg/policies/engine/rego/builtins/myfeature.go):
package builtins

import (
    "github.com/open-policy-agent/opa/ast"
    "github.com/open-policy-agent/opa/topdown"
    "github.com/open-policy-agent/opa/types"
)

const myFuncName = "chainloop.my_function"

func RegisterMyBuiltins() error {
    return Register(&ast.Builtin{
        Name: myFuncName,
        Description: "Description of what this function does",
        Decl: types.NewFunction(
            types.Args(types.Named("input", types.S).Description("this is the input")),
            types.Named("result", types.S).Description("this is the result"),
        ),
    }, myFunctionImpl)
}

func myFunctionImpl(bctx topdown.BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
    // Extract arguments
    input, ok := operands[0].Value.(ast.String)
    if !ok {
        return fmt.Errorf("input must be a string")
    }

    // Implement logic
    result := processInput(string(input))

    // Return result
    return iter(ast.StringTerm(result))
}

// Autoregisters on package load
func init() {
    if err := RegisterMyBuiltins(); err != nil {
        panic(fmt.Sprintf("failed to register built-ins: %v", err))
    }
}
  1. ポリシー内で使用する(*.rego):
package example
import rego.v1

result := {
    "violations": violations,
    "skipped": false
}

violations contains msg if {
    output := chainloop.my_function(input.value)
    output != "expected"
    msg := "Function returned unexpected value"
}

ガイドライン:

  • すべてのカスタム組み込み関数で chainloop.* 名前空間を使用してください
  • サードパーティのサービスを呼び出す関数は、組み込み関数の定義に NonRestrictiveBuiltin カテゴリを追加して、非制限的としてマークしてください
  • 適切なエラー処理を必ず実装し、意味のあるエラーメッセージを返してください
  • タイムアウト/キャンセルをサポートするため、BuiltinContext のコンテキストを使用してください
  • Description フィールドとパラメーター定義に、関数のシグネチャと動作を記述してください

コメント