> For the complete documentation index, see [llms.txt](https://docs.octoxlabs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.octoxlabs.com/adapters/s/sql-adapter.md).

# Sql Adapter

This adapter reads rows from a Microsoft Sql Adapter view or from a custom query and turns each row into a device. Columns named after the OctoXLabs global fields, such as Hostname, IpAddresses and MacAddresses, are mapped automatically.

## Use cases

* Bring in an internal CMDB or asset table that only exists as a Sql Adapter view.

```
Adapters = sql-adapter
```

* Find devices in the Sql Adapter source that no endpoint tool reports.

```
Adapters = sql-adapter AND NOT Adapters = falcon-crowdstrike
```

## Before you begin

### Network

| Source              | Destination      | Port           | Protocol  |
| ------------------- | ---------------- | -------------- | --------- |
| OctoXLabs connector | Sql Adapter host | 1433 (default) | TCP (TDS) |

Port 1433 is the default of the Port parameter. With Kerberos, the connector also needs to reach the Key Distribution Center (usually a domain controller). The port is not set in the connector code.

### Required permissions

| Scope or role | Permission            | Needed for                                      |
| ------------- | --------------------- | ----------------------------------------------- |
| Database user | Read, Select and List | The view or the tables used by the custom query |

The source instructions state that the user must have Read, Select and List privileges. The connection test with a custom query also runs the query, so the user must be allowed to run it.

## Create credentials in Sql Adapter

{% stepper %}
{% step %}

### Open Sql Adapter Management Studio

Open Sql Adapter Management Studio.
{% endstep %}

{% step %}

### Choose the authentication method

Select the authentication method used by the server.
{% endstep %}

{% step %}

### Enter the user details

Fill in the details and select your database in **Default Database**.
{% endstep %}

{% step %}

### Save the user

Save the user.
{% endstep %}

{% step %}

### Sign in once

Log in to the database with this user once to avoid an error.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
To check Kerberos after connecting, query `sys.dm_exec_connections` for the current session and confirm that `auth_scheme` is `KERBEROS`. If it is not, recheck the SPN registration in AD.
{% endhint %}

## Connection parameters

| Parameter                | Required    | Type     | Default  | Description                                                                                                                            |
| ------------------------ | ----------- | -------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| IP                       | Yes         | Text     |          | IP address of the Sql Adapter host.                                                                                                    |
| Port                     | No          | Integer  | 1433     | Sql Adapter port.                                                                                                                      |
| Domain                   | No          | Text     |          | Active Directory domain. When set, the login is sent as domain\username.                                                               |
| Database                 | Yes         | Text     |          | Database to connect to.                                                                                                                |
| Username                 | Yes         | Text     |          | Login name.                                                                                                                            |
| Password                 | Conditional | Password |          | Required for SQL authentication and for Kerberos mode password. Can be empty for Kerberos mode keytab.                                 |
| View Name                | Conditional | Text     |          | View to query. Required unless Custom Query is set.                                                                                    |
| Custom Query             | No          | Text     |          | A single read-only query that replaces the view.                                                                                       |
| Query Timeout            | No          | Integer  | 60       | Seconds before a query is stopped.                                                                                                     |
| Use PyODBC Driver        | No          | Boolean  | False    | Use PyODBC instead of pymssql. Needs ODBC Driver 17 or 18 for Sql Adapter on the connector machine. Driver 17 is tried first, then 18. |
| Batch Size               | No          | Integer  | 300      | Rows fetched per batch.                                                                                                                |
| Auth Type                | No          | Text     | SQL      | Set to Kerberos to sign in with Active Directory credentials.                                                                          |
| Kerberos Mode            | No          | Text     | password | password uses the username and password to get a ticket. keytab uses an uploaded keytab file.                                          |
| Kerberos Realm           | No          | Text     |          | AD realm in uppercase, for example CORP.LOCAL. Can be empty when the username is in user\@REALM form.                                  |
| KDC IP                   | No          | Text     |          | Host name or IP of the Key Distribution Center.                                                                                        |
| Kerberos SPN             | No          | Text     |          | Service principal name override, for example MSSQLSvc/sqlserver.corp.local:1433.                                                       |
| Keytab                   | Conditional | File     |          | Required when Kerberos Mode is keytab. Generate it with ktpass on a domain controller.                                                 |
| Encrypt                  | No          | Boolean  | True     | TLS encryption. Applied only with Kerberos.                                                                                            |
| Trust Server Certificate | No          | Boolean  | True     | Skip certificate chain validation. Applied only with Kerberos.                                                                         |

## Troubleshooting

| Error message                                                                       | Cause                                                                                 | Fix                                                       |
| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | --------------------------------------------------------- |
| It is mandatory to enter either custom query or view                                | Neither View Name nor Custom Query is set.                                            | Fill in one of them.                                      |
| No ODBC Driver found. Install 'ODBC Driver 17' or 'ODBC Driver 18' for Sql Adapter. | PyODBC or Kerberos is used and neither driver is installed on the connector machine.  | Install ODBC Driver 17 or 18 for Sql Adapter.             |
| Kerberos config failed: Sql Adapter reports auth\_scheme='...'                      | The connection did not use Kerberos, often because of a wrong SPN or Encrypt setting. | Check the SPN registration in AD and the Encrypt setting. |
| Keytab is required when kerberos\_mode is 'keytab'.                                 | Kerberos Mode is keytab and no keytab was uploaded.                                   | Upload the keytab file.                                   |
| Password is required for Sql Adapter Authentication.                                | Auth Type is SQL and the password is empty.                                           | Enter the password.                                       |

## Change history
