Authentification
Rapport de recherche Datadog : Bilan sur l'adoption de l'informatique sans serveur Rapport : Bilan sur l'adoption de l'informatique sans serveur

Authentification

Toutes les requêtes à destination de l’API Datadog doivent être authentifiées. Les requêtes qui écrivent des données nécessitent un accès au reporting ainsi qu’une API key. Les requêtes qui lisent des données nécessitent un accès complet ainsi qu’une application key.

Remarque : par défaut, tous les clients de l’API Datadog utilisent les API du site américain de Datadog. Si vous utilisez le site européen de Datadog, définissez la variable d’environnement DATADOG_HOST sur https://api.datadoghq.eu ou remplacez directement cette valeur lors de la création de votre client.

Gérer les clés d’application et d’API de votre compte.

Valider la clé d'API

GET https://api.datadoghq.eu/api/v1/validatehttps://api.datadoghq.com/api/v1/validate

Présentation

Vérifiez si la clé d’API (et non la clé d’application) est valide. Si elle n’est pas valide, un message d’erreur 403 est renvoyé.

Réponse

OK

Represent validation endpoint responses.

Expand All

Champ

Type

Description

valid

boolean

Return true if the authentication response is valid.

{
  "valid": true
}

Authentication error

Error response object.

Expand All

Champ

Type

Description

errors [required]

[string]

Array of errors returned by the API.

{
  "errors": [
    "Bad Request"
  ]
}

Exemple de code


                                        # Curl command
curl -X GET "https://api.datadoghq.eu"https://api.datadoghq.com/api/v1/validate" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_CLIENT_API_KEY}"
package main

import (
    "context"
    "fmt"
    "os"
    datadog "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
)

func main() {
    ctx := context.WithValue(
        context.Background(),
        datadog.ContextAPIKeys,
        map[string]datadog.APIKey{
            "apiKeyAuth": {
                Key: os.Getenv("DD_CLIENT_API_KEY"),
            },
        },
    )


    configuration := datadog.NewConfiguration()

    api_client := datadog.NewAPIClient(configuration)
    resp, r, err := api_client.AuthenticationApi.Validate(ctx).Execute()
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error when calling `AuthenticationApi.Validate``: %v\n", err)
        fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
    }
    // response from `Validate`: AuthenticationValidationResponse
    fmt.Fprintf(os.Stdout, "Response from `AuthenticationApi.Validate`: %v\n", resp)
}
// Import classes:
import java.util.*;
import com.datadog.api.v1.client.ApiClient;
import com.datadog.api.v1.client.ApiException;
import com.datadog.api.v1.client.Configuration;
import com.datadog.api.v1.client.auth.*;
import com.datadog.api.v1.client.model.*;
import com.datadog.api.v1.client.api.AuthenticationApi;

public class Example {
    public static void main(String[] args) {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        // Configure the Datadog site to send API calls to
        HashMap<String, String> serverVariables = new HashMap<String, String>();
        String site = System.getenv("DD_SITE");
        if (site != null) {
            serverVariables.put("site", site);
            defaultClient.setServerVariables(serverVariables);
        }
        // Configure API key authorization: 
        HashMap<String, String> secrets = new HashMap<String, String>();
        secrets.put("apiKeyAuth", System.getenv("DD_CLIENT_API_KEY"));
        defaultClient.configureApiKeys(secrets);

        AuthenticationApi apiInstance = new AuthenticationApi(defaultClient);
        try {
            AuthenticationValidationResponse result = apiInstance.validate()
                .execute();
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling AuthenticationApi#validate");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
    }
}
import os
from dateutil.parser import parse as dateutil_parser
import datadog_api_client.v1
from datadog_api_client.v1.api import authentication_api
from datadog_api_client.v1.models import *
from pprint import pprint
# Defining the host is optional and defaults to https://api.datadoghq.com
# See configuration.py for a list of all supported configuration parameters.
configuration = datadog_api_client.v1.Configuration(
    host = "https://api.datadoghq.com"
)

# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.

# Configure API key authorization: apiKeyAuth
configuration.api_key['apiKeyAuth'] = os.getenv('DD_CLIENT_API_KEY')

# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['apiKeyAuth'] = 'Bearer'

# Enter a context with an instance of the API client
with datadog_api_client.v1.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = authentication_api.AuthenticationApi(api_client)

    # example, this endpoint has no required or optional parameters
    try:
        # Validate API key
        api_response = api_instance.validate()
        pprint(api_response)
    except datadog_api_client.v1.ApiException as e:
        print("Exception when calling AuthenticationApi->validate: %s\n" % e)
# load the gem
require 'datadog_api_client/v1'
# setup authorization
DatadogAPIClient::V1.configure do |config|
  # Configure API key authorization: apiKeyAuth
  config.api_key['apiKeyAuth'] = ENV["DD_CLIENT_API_KEY"]
  # Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
  #config.api_key_prefix['apiKeyAuth'] = 'Bearer'
end

api_instance = DatadogAPIClient::V1::AuthenticationApi.new

begin
  #Validate API key
  result = api_instance.validate
  p result
rescue DatadogAPIClient::V1::ApiError => e
  puts "Exception when calling AuthenticationApi->validate: #{e}"
end