wazuh / wazuh

Wazuh - The Open Source Security Platform. Unified XDR and SIEM protection for endpoints and cloud workloads.
https://wazuh.com/
Other
10.92k stars 1.66k forks source link

Comms API - `POST /authentication` endpoint implementation #24618

Closed GGP1 closed 2 months ago

GGP1 commented 3 months ago
Epic
#24305

Description

The API will use the JSON Web Tokens (JWTs) open standard (RFC 7519) to authenticate client requests. Any agent communicating with this API will be required to authenticate using a POST /authentication endpoint, which will be implemented in this issue.

The credentials provided by the agent will be compare against the ones obtained from the agent list index stored in the Wazuh indexer.

Authentication flow

Flow ```mermaid flowchart TD subgraph Agents Endpoints Clouds Other end subgraph Server["Server cluster"] subgraph Wazuh1["Server node n"] api1["Agent comms API"] end end subgraph Indexer subgraph Data_states["Data states"] agents_list["Agents list"] end end subgraph lb["Load Balancer"] lb_node["Per request"] end Agents -- 1. /login --> lb lb -- 1. /login --> Wazuh1 Wazuh1 -- 2. Read credentials --> agents_list Indexer -- 3. Credentials response --> Wazuh1 Wazuh1 -- 4. Authentication response --> Agents style Wazuh1 fill:#abc2eb style Data_states fill:#abc2eb ```

JWT token

Algorithm

The algorithm used to sign the tokens will be the Elliptic Curve Digital Signature Algorithm with the P-256 curve and the SHA-256 cryptographic hash function (ES256).

Payload

The payload will contain public claims such as the issuer, audience, subject and timestamps indicating when the token was issued and when it expires.

Additionally, it will contain a uuid field that will store the agent's UUID. This way, the API will be able to identify which agent is doing the request and respond according to it.

{
  "iss": "wazuh", > issuer
  "aud": "Wazuh Communications API", > audience
  "iat": 1717524220, > issued at
  "exp": 1717525120, > expiration
  "uuid": "018fe477-31c8-7580-ae4a-e0b36713eb05", > agent UUID v7
}

Timestamp and expiration

The iat field will be populated with the timestamp at which the token is issued and the exp field will be set to the timestamp at which the token is considered expired (iat + expiration_time).

The expiration time we will use is 900 seconds (15m), which is what we are using currently in the Server management API.

Request

The request body will contain a UUID and a key.

{
    "uuid": "<UUID>",
    "key": "<KEY>"
}

Responses

Success

A successful response will contain a 200 HTTP status and a JSON struct with a token field and the token itself in the value.

{
    "token": "<TOKEN>"
}

Failure

A failed response will contain a JSON struct following format defined in https://github.com/wazuh/wazuh/issues/23395#issuecomment-2178737901 and an HTTP status between 300 and 505.

{
    "error": {
        "message": "Invalid JWT token",
        "code": 403
    }
}

Dependencies

GGP1 commented 3 months ago

Update

GGP1 commented 3 months ago

Update

GGP1 commented 3 months ago

Update

GGP1 commented 2 months ago

Update

Applied the changes suggested.

GGP1 commented 2 months ago

Update

Applied the changes suggested.

GGP1 commented 2 months ago

Update

Added a dot to the end of the docstrings.