Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update sapi section #593

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crowdsec-docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = {
},
{
label: "Service API",
to: "/u/service_api/intro",
to: "/u/service_api/getting_started",
},
],
},
Expand Down
51 changes: 43 additions & 8 deletions crowdsec-docs/sidebarsUnversioned.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ module.exports = {
items: ["console/decisions/decisions_management"],
},
{
type: "doc",
type: "link",
label: "Service API",
id: "console/api/intro",
href: "/u/service_api/getting_started",
},
],
remediationSideBar: [
Expand Down Expand Up @@ -352,12 +352,47 @@ module.exports = {
},
],
serviceApiSideBar: [
"service_api/intro",
"service_api/quickstart",
"service_api/blocklists",
"service_api/integrations",
"service_api/sdks",
"service_api/faq",
{
type: "doc",
id: "service_api/getting_started",
label: "Getting Started"
},
{
type: "category",
label: "Quickstart",
items: [
"service_api/quickstart/authentication",
"service_api/quickstart/blocklists",
"service_api/quickstart/integrations",
]

},
{
type: "category",
label: "SDKs",
items: [
{
type: "doc",
label: "Python",
id: "service_api/sdks/python"
}
]
},
{
type: "link",
label: "Swagger",
href: "https://admin.api.crowdsec.net/v1/docs#/"
},
{
type: "link",
label: "Redoc",
href: "https://admin.api.crowdsec.net/v1/redoc"
},
{
type: "doc",
id: "service_api/faq",
label: "FAQ"
},
],
guidesSideBar: [
"user_guides/intro",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion crowdsec-docs/unversioned/console/api/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ We provide SDKs for the CrowdSec Service API to make it easier to interact with

## Next Steps

Now that you have an API key and know how to authenticate requests, you can start using the CrowdSec Service API to manage blocklists and integrations. Check out the API reference and SDKs to get started. You can also check out the [CrowdSec Service API Examples](
Now that you have an API key and know how to authenticate requests, you can start using the CrowdSec Service API to manage blocklists and integrations. Check out the API reference and SDKs to get started.
58 changes: 58 additions & 0 deletions crowdsec-docs/unversioned/service_api/getting_started.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
id: getting_started
title: Getting Started
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import useBaseUrl from '@docusaurus/useBaseUrl';

:::info
This is an [enterprise plan](https://www.crowdsec.net/pricing) feature
:::

## What is the Service API ?

The **Service API**, **SAPI** for short, provides access to selected **CrowdSec SaaS features**.
New SaaS features will usually appear on **SAPI** first before getting their UI counterpart.
The current capabilities of this API are:

- **Blocklist** creation & management
- Allowing you to create private blocklists and share them
- As well as subscribing to any of the blocklists available to your organization
- **Integrations** endpoints creation & management
- An Essential part of the **Blocklist as a Service** feature.
- Manage endpoints for your [**Firewalls**](/u/integrations/intro) or [**Remediation Components**](/u/bouncers/intro) to connect directly to.

## Getting your API keys

The CrowdSec Service API uses API keys to authenticate requests. You can create an API key in the CrowdSec Console by:

1. **Logging in** to the [CrowdSec Console](https://app.crowdsec.net/) and go to the **Settings** page.

![Settings_page](/img/console/settings/main.png)

2. Click on the **Service API Keys** section.

![Service_API_Keys](/img/console/settings/sapi_keys.png)

3. Click on the **Create API Key** button, fill in the form, set the permissions you want to grant.

![Create_API_Key](/img/console/settings/sapi_create_key.png)

6. Click on the **Create key** button.

![API_Key_Created](/img/console/settings/sapi_key_created.png)


## API Specifications

You can find a detailed description of the API in multiple formats here:

- [Swagger](https://admin.api.crowdsec.net/v1/docs#/)
- [Redoc](https://admin.api.crowdsec.net/v1/redoc)
- [Openapi specs](https://admin.api.crowdsec.net/v1/openapi.json)




41 changes: 0 additions & 41 deletions crowdsec-docs/unversioned/service_api/introduction.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
id: authentication
title: Authentication
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import useBaseUrl from '@docusaurus/useBaseUrl';


Once you have created an API key, you can use it to authenticate requests to the CrowdSec Service API by including it in the `x-api-key` header of your HTTP requests.

:::info
- We're assuming your API key is set in the environment variable `$KEY`
:::

<Tabs
defaultValue="curl"
groupId="service-api-selection"
values={[
{ label: 'cURL', value: 'curl' ,},
{ label: 'Python', value: 'python', },
]
}>
<TabItem value="curl">

```bash
curl -i -H "x-api-key: ${KEY}" -X GET -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/info
```

</TabItem>
<TabItem value="python">

```python
import os
from crowdsec_service_api import (
Server,
ApiKeyAuth,
Info,
)

KEY = os.getenv('KEY')

auth = ApiKeyAuth(api_key=KEY)
client = Info(base_url=Server.production_server.value, auth=auth)
# Get info about the user
response = client.get_info()
print(response)
```

</TabItem>
</Tabs>

<details>
<summary>answer on success</summary>

```json
{
"organization_id": "MY-ORG-ID-abcdef1234",
"subscription_type": "ENTERPRISE",
"api_key_name": "my test api key"
}
```
</details>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: quickstart
title: Quickstart
id: blocklists
title: Blocklists
---

import Tabs from '@theme/Tabs';
Expand All @@ -11,8 +11,6 @@ import useBaseUrl from '@docusaurus/useBaseUrl';
- We're assuming your API key is set in the environment variable `$KEY`
:::

## Blocklist(s) manipulation

### Create a blocklist

> Create a new private blocklist named `my_test_blocklist`
Expand Down Expand Up @@ -154,10 +152,10 @@ https://admin.api.crowdsec.net/v1/blocklists/1234MYBLOCKLISTID/ips \

```python
import os
import datetime
from datetime import datetime, UTC, timedelta

KEY = os.getenv('KEY')
EXPIRATION = (datetime.datetime.now() + datetime.timedelta(days=1)).isoformat()
EXPIRATION = datetime.now(UTC) + timedelta(days=1)

from crowdsec_service_api import (
Blocklists,
Expand Down Expand Up @@ -548,96 +546,3 @@ print(response)
5.6.7.8
```
</details>

## Integrations manipulation

### Creating integration

<Tabs
defaultValue="curl"
groupId="service-api-selection"
values={[
{ label: 'cURL', value: 'curl' ,},
{ label: 'Python', value: 'python', },
]
}>
<TabItem value="curl">

```bash
curl -i -H "x-api-key: ${KEY}" -X POST -H "Content-Type: application/json" \
https://admin.api.crowdsec.net/v1/integrations \
-d '{ "name": "test_integration_1", "description": "my test integration", "entity_type": "firewall_integration", "output_format": "plain_text" }'
```

</TabItem>
<TabItem value="python">

```python
import os

KEY = os.getenv('KEY')

from crowdsec_service_api import (
Integrations,
Server,
ApiKeyAuth,
)
from crowdsec_service_api.models import IntegrationCreateRequest

auth = ApiKeyAuth(api_key=KEY)
client = Integrations(base_url=Server.production_server.value, auth=auth)
request = IntegrationCreateRequest(
name='test_integration_1',
description='my test integration',
entity_type='firewall_integration',
output_format='plain_text',
)
response = client.create_integration(
request=request,
)
print(response)
```

</TabItem>
</Tabs>

:::warning
The `username` and `password` will only be displayed at creation time, be sure to write them down. If you lose them, you can always regenerate them with the [`update`](https://admin.api.crowdsec.net/v1/docs#/Integrations/updateIntegration) method.
:::

- [Swagger method link](https://admin.api.crowdsec.net/v1/docs#/Integrations/createIntegration)

<details>
<summary>answer on success</summary>
```json
{
"id": "INTEGRATIONID12345",
"name": "test_integration_1",
"organization_id": "MY-ORG-ID-abcdef1234",
"description": "my test integration",
"created_at": "2024-06-07T14:00:31.645929Z",
"updated_at": "2024-06-07T14:00:31.645943Z",
"entity_type": "firewall_integration",
"output_format": "plain_text",
"last_pull": null,
"blocklists": [],
"endpoint": "https://admin.api.crowdsec.net/v1/integrations/INTEGRATIONID12345/content",
"stats": {
"count": 0
},
"credentials": {
"username": "<login>",
"password": "<password>"
}
}

```
</details>

### View integration content

View integration content allows you to preview the list of IPs that are returned to your firewall (or whatever is going to consume the integration).

```bash
curl -i -u '<login>:<password>' https://admin.api.crowdsec.net/v1/integrations/INTEGRATIONID12345/content
```
Loading
Loading