Configuration
The configuration of the NSPS connector plays a critical role in ensuring its functionality, security, and ability to integrate with external systems. The connector requires specific parameters both for its own operation and for interaction with the ES.
The simplest and most flexible way to manage these parameters is by using environment variables. This allows easy adaptation of the connector to different environments (development, testing, production) without the need for code recompilation.
Core Configuration Requirements
The connector must support the configuration of the following key parameters:
-
Bearer Token for NSPS (
API_TOKEN):- Purpose: This token is used to authenticate all incoming requests from NSPS to the connector. It is a static secret key that must be registered and configured within NSPS.
- Recommendation: Set as an environment variable (
API_TOKEN). This allows quick token updates in case of compromise without modifying the connector's code. - For more details, refer to the Authorization section.
-
External System Credentials:
- Purpose: Parameters required for the connector to authenticate when sending requests to the external network system.
- Types: Depend on the external system's implementation. These can include:
- Basic Auth (username/password).
- Bearer Auth with a static token.
- JWT with
access_tokenandrefresh_tokensupport, if required.
- Recommendation: Store as environment variables (e.g.,
EXTERNAL_API_KEY,EXTERNAL_API_USERNAME,EXTERNAL_API_PASSWORD).
-
External System Base URL:
- Purpose: The base URI to which the connector will send its requests.
- Format: Must be configurable with an optional base path (e.g.,
https://external-system.com/apiorhttps://external-system.com). - Recommendation: Store as an environment variable (e.g.,
EXTERNAL_API_BASE_URL).
-
Other Additional Parameters (if necessary):
- Timeouts: Time limits for HTTP requests to the ES.
- Retry limits: Number of retries for temporary ES errors.
- Logging modes: Level of log detail (DEBUG, INFO, WARNING, ERROR).
Example Configuration using Environment Variables
.env File (for Local Development)
During local development and testing, it is convenient to use a .env file to store environment variables.
# NSPS Connector Configuration
API_TOKEN="your_nsps_bearer_token_here"
# External System Configuration
EXTERNAL_API_BASE_URL="https://api.external-system.com/v1"
EXTERNAL_API_KEY="your_external_api_key_here"
EXTERNAL_API_SECRET="your_external_api_secret_here"
# EXTERNAL_API_USERNAME="user"
# EXTERNAL_API_PASSWORD="password"
# Other Settings
LOG_LEVEL="INFO"
HTTP_TIMEOUT_SECONDS=10
Loading Configuration into Code
Below there are examples of how the connector can load these environment variables into your application. It is recommended to use libraries that assist with the validation and typing of environment variables.
import { config as loadEnv } from 'dotenv';
import { z } from 'zod';
// Load variables from .env file
loadEnv();
// Define validation schema for environment variables
const EnvSchema = z.object({
API_TOKEN: z.string().min(1, 'API_TOKEN is required'),
EXTERNAL_API_BASE_URL: z.string().url('EXTERNAL_API_BASE_URL must be a valid URL'),
EXTERNAL_API_KEY: z.string().min(1, 'EXTERNAL_API_KEY is required'),
EXTERNAL_API_SECRET: z.string().optional(), // e.g., if secret is not always needed
LOG_LEVEL: z.enum(['DEBUG', 'INFO', 'WARN', 'ERROR']).default('INFO'),
HTTP_TIMEOUT_SECONDS: z.string().transform(Number).default('10'),
});
// Parse and validate environment variables
export const settings = EnvSchema.parse(process.env);
// Example usage in code:
// import { settings } from './config';
// console.log(settings.API_TOKEN);
// console.log(settings.EXTERNAL_API_BASE_URL);
from pydantic import BaseSettings, HttpUrl
from typing import Optional
class Settings(BaseSettings):
API_TOKEN: str
EXTERNAL_API_BASE_URL: HttpUrl # Automatic URL validation
EXTERNAL_API_KEY: str
EXTERNAL_API_SECRET: Optional[str] = None # Optional parameter
LOG_LEVEL: str = "INFO"
HTTP_TIMEOUT_SECONDS: int = 10
class Config:
env_file = ".env" # Load from .env file
case_sensitive = True # Case-sensitive for variable names
settings = Settings()
# Example usage in code:
# from .core.config import settings
# print(settings.API_TOKEN)
# print(settings.EXTERNAL_API_BASE_URL)