Ai knowledge and logicHelpers

sql_server_query

Run bounded SELECT queries using an organization connection secret.

sql_server_query(context, connection_secret, query, parameters=None, max_rows=500)

Reads an external SQL Server without returning its stored credentials to Python. Store a JSON connection object in your organization's secret vault, then pass its secret name. By default, the connection requires a dedicated SELECT-only login and rejects broader grants even for a SELECT query. An existing application credential can be used through the explicit supplied-credential mode described below.

{
  "server": "sql.example.com",
  "database": "orders",
  "user": "octocom_reader",
  "password": "<store the password only in the secret vault>",
  "port": 1433,
  "encrypt": true,
  "trustServerCertificate": false,
  "viaProxy": true,
  "permissionMode": "verified-read-only",
  "readOnlyFunctions": []
}

port, encrypt, trustServerCertificate, viaProxy, permissionMode and readOnlyFunctions are optional with the defaults shown. The transport always uses Octocom's outbound proxy. viaProxy: false, private IP addresses and internal hostnames are rejected. The proxy must also refuse DNS targets resolving to private ranges. Keep TLS encryption and certificate verification enabled whenever the database supports them.

def execute_action(context):
    result = sql_server_query(
        context, "ORDER_DATABASE",
        "SELECT TOP (10) OrderNumber, Status FROM dbo.Orders WHERE Email=@email ORDER BY CreatedAt DESC",
        parameters={"email": context["args"]["email"]},
        max_rows=10,
    )
    if result["truncated"]:
        raise ValueError("Narrow the lookup before using these results")
    return result["rows"]

Bind values with @name parameters. Do not build SQL by concatenating customer input. Parameter values may be strings, finite numbers, booleans or None. Pass one SELECT, optionally with CTEs. Statement batches, writes, stored procedures, linked servers, cross-database names and arbitrary functions are unsupported. Common read functions such as ISNULL, SUM, CAST, CONVERT, DATEADD, LEN and LOWER are supported.

The result is {"rows": [...], "truncated": False}. Column names and nested values are preserved. Results stop at max_rows (1 to 2,000, default 500) or 1 MB, whichever comes first, with truncated: True. A truncated result is incomplete. Narrow the query or implement pagination in the query rather than treating it as a complete set.

Database permissions and trusted functions

In the default verified-read-only mode, database grants are the security boundary. The SELECT syntax filter is an additional restriction, not a SQL Server READ ONLY transaction. Before each caller query, Octocom checks effective server, database, schema, object and column permissions. Missing or inconclusive checks fail closed. The login must not own the database or a schema, have write/alter/impersonation rights, or inherit them through roles.

Your DBA should create a separate reader login and database user, grant CONNECT and SELECT only on the required tables or safe views, and remove unrelated grants. For example, after creating the dedicated user:

GRANT CONNECT TO [octocom_reader];
GRANT SELECT ON OBJECT::[dbo].[Orders] TO [octocom_reader];

Prefer these direct grants over membership in db_datareader: that role can also confer control of its legacy role-owned schema, which the permission check rejects. Do not reuse a login with database-wide EXECUTE through a custom role such as db_executor. Keep an existing application's login unchanged and provision a separate reader for Python.

If a query needs a trusted T-SQL function, list its exact schema.name in the connection secret's readOnlyFunctions array. The DBA must review that function before granting access. Scalar functions require EXECUTE on that function only; table-valued functions require SELECT. Octocom verifies the object exists and is a T-SQL function (FN, IF or TF), rejecting CLR functions, stored procedures and type changes. Broad database/schema EXECUTE is never accepted.

GRANT EXECUTE ON OBJECT::[dbo].[read_pending_quantity] TO [octocom_reader];

The connection owner remains responsible for trusted view/function internals, ownership chains, signed code, computed columns and stable grants. A catalog type check does not inspect every transitive dependency or prove that privileged code has no external effects. Prefer a read-only database replica when that stronger guarantee is required. Permission changes are database administration work; Octocom does not create or modify logins automatically.

Using an existing credential

When migrating an existing integration whose SQL login has broader database permissions, the connection owner can explicitly add "permissionMode": "supplied-credential" to that connection secret. This permits database/schema/object grants such as broad EXECUTE or write permissions without changing the login. The setting is stored per organization connection, not passed by Python or in a query request. Omitting it retains the strict default; unknown values are rejected.

This mode trusts the supplied credential and the organization's query authors. It still accepts only the supported single-SELECT syntax, binds parameters separately, and enforces the same proxy restrictions, timeouts and result limits. Server administrators, database owners, system databases and system principals remain unsupported. Configured functions must still be accessible T-SQL functions. Accessible CLR functions, external tables, synonyms and assembly-typed columns are rejected in both modes.

This is SELECT-constrained access, not database-enforced read-only access. The syntax scanner rejects known writes, batches and unsupported constructs, but is not a full SQL parser or a proof that a query has no side effects. Views, functions and their dependencies may carry behavior the scanner cannot inspect. SQL Server retains the credential's actual privileges. Moving fixed backend queries into editable Python also moves control of those queries to the organization's Python authors. Use this mode only for a trusted connection and trusted query authors; prefer a dedicated reader where that trust is inappropriate.

Changing this setting does not execute a query, change database grants or activate an action. Verify representative queries and their results before switching an existing integration to Python.

HTTP 403 includes a fixed reason and an actionable error. In strict mode, examples include database-execute for broad EXECUTE and schema-permissions for schema privileges; an inconclusive rejection uses permission-check-failed. In supplied-credential mode, failed access or catalog checks use query-safety-check-failed. Diagnostics never return object names or provider error details and cannot authorize a rejected connection. Other HTTP and timeout errors propagate without returning partial success or retrying the query. Logs record the selected permission mode, but exclude upstream error bodies, SQL text, parameter values and credentials.

On this page