Metadata-Version: 2.2
Name: wg-api-client
Version: 0.1.2
Summary: WireGuard Configuration API Client
Home-page: https://github.com/tiiuae/wg-api-client-lib
Author: TII UAE
Author-email: info@tii.ae
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: System :: Networking
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.25.0
Requires-Dist: configparser>=5.0.0
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# WireGuard Configuration API Client

A comprehensive client library and CLI tool for interacting with the WireGuard Configuration Distribution API.

[![CI](https://github.com/tiiuae/wg-api-client-lib/actions/workflows/ci.yml/badge.svg)](https://github.com/tiiuae/wg-api-client-lib/actions/workflows/ci.yml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![PyPI version](https://badge.fury.io/py/wg-api-client.svg)](https://badge.fury.io/py/wg-api-client)

## Features

- Complete API client for the WireGuard Configuration Distribution API
- Command-line interface for all API operations
- Automatic token authentication and renewal
- Configuration file support
- WireGuard keypair generation
- WireGuard configuration file creation

## Installation

### From PyPI (Recommended)

```bash
pip install wg-api-client
```

### From Source

```bash
git clone https://github.com/tiiuae/wg-api-client-lib.git
cd wg-api-client-lib
pip install -e .
```

### Prerequisites for Ubuntu

```bash
sudo apt update
sudo apt install -y python3 python3-pip wireguard-tools
```

## Usage

### As a Command-Line Tool

The package installs a `wg-api-client` command that can be used to interact with the API:

```bash
# Show help
wg-api-client --help

# Authenticate with the API
wg-api-client auth

# Get a WireGuard configuration for a device
wg-api-client get-config --device-id drone-01 --output drone01.conf
```

### As a Library

```python
from wg_api_client import WireGuardAPI, WireGuardHelper

# Initialize the API client
api = WireGuardAPI(
    api_url="http://20.46.55.161:8080/api/v1",
    hashed_credential="your-hashed-credential"
)

# Authenticate
success, _ = api.authenticate()
if success:
    # Generate a keypair
    private_key, public_key = WireGuardHelper.generate_keypair()
    
    # Request a configuration
    success, config_data = api.request_wireguard_config(
        device_id="drone-01",
        role="drone",
        public_key=public_key
    )
    
    if success:
        # Create a configuration file
        WireGuardHelper.create_client_config(config_data, "drone01.conf")
```

## Configuration

The tool stores configuration in `~/.wg_api_config` by default. You can specify a different location with the `--config-file` parameter.

## Available Commands

### Global Parameters

These parameters can be used with any command:

- `--api-url`: Base URL for the API (default: http://20.46.55.161:8080/api/v1)
- `--hashed-credential`: Hashed credential for authentication
- `--config-file`: Path to configuration file (default: ~/.wg_api_config)

### Authentication

```bash
wg-api-client auth
```

This will:
- Authenticate with the API using the hashed credential
- Store the JWT token in the configuration file
- Store the refresh token for automatic token renewal

### Device Configuration Management

#### Get WireGuard Configuration

```bash
wg-api-client get-config --device-id DEVICE_ID [--role {drone|fmo}] [--public-key KEY] [--output FILE]
```

Parameters:
- `--device-id`: Unique identifier for the device (required)
- `--role`: Device role - either "drone" or "fmo" (default: "drone")
- `--public-key`: WireGuard public key (if not provided, a new keypair will be generated)
- `--output`: Output configuration file (default: "wg.conf")

Examples:

```bash
# Generate a new keypair and configuration for a drone device
wg-api-client get-config --device-id drone-01

# Use an existing public key and set role to FMO
wg-api-client get-config --device-id fmo-main --role fmo --public-key "AbCdEf123..." --output fmo.conf
```

#### List All Devices (Admin only)

```bash
wg-api-client list-devices
```

This will display detailed information about all devices, including:
- Device ID
- Role
- IP address
- Public key
- Creation and update timestamps

#### Get Device Information (Admin only)

```bash
wg-api-client get-device DEVICE_ID
```

#### Delete a Device (Admin only)

```bash
wg-api-client delete-device DEVICE_ID
```

#### Delete All Devices (Admin only)

```bash
wg-api-client delete-all-devices [--confirm]
```

Use the `--confirm` flag to bypass the confirmation prompt.

### FMO-specific Operations

#### Get FMO Device Information (Admin only)

```bash
wg-api-client get-fmo
```

#### Remove FMO Role (Admin only)

```bash
wg-api-client delete-fmo
```

### Credential Management (Admin only)

#### Add a New Credential

```bash
wg-api-client add-credential --hashed-credential HASH [--role {user|admin}]
```

Parameters:
- `--hashed-credential`: Hashed credential to add (required)
- `--role`: Role for the credential - either "user" or "admin" (default: "user")

## Examples of Common Workflows

### Setting Up a New Drone Device

```bash
# Authenticate with the API
wg-api-client auth

# Generate a WireGuard configuration for the device
wg-api-client get-config --device-id drone-42 --output drone42.conf

# Transfer the generated configuration file to the device and apply it using the WireGuard tools
```

### Setting Up an FMO Device

```bash
# Authenticate with the API
wg-api-client auth

# Check if there's already an FMO device
wg-api-client get-fmo

# If needed, remove the current FMO role
wg-api-client delete-fmo

# Generate a WireGuard configuration for the new FMO device
wg-api-client get-config --device-id fmo-new --role fmo --output fmo.conf
```

### Administrator Tasks

```bash
# Check all registered devices
wg-api-client list-devices

# Add a new admin credential
wg-api-client add-credential --hashed-credential "your-hashed-credential" --role admin

# Clean up old devices
wg-api-client delete-device old-device-id
```

## Development

### Setup Development Environment

```bash
# Clone repository
git clone https://github.com/tiiuae/wg-api-client-lib.git
cd wg-api-client-lib

# Install development dependencies
pip install -r requirements-dev.txt

# Install in development mode
pip install -e .
```

### Run Tests

```bash
pytest
```

### Run Linters

```bash
# Format code with Black
black .

# Sort imports
isort .

# Check with pylint
pylint wg_api_client

# Check with mypy
mypy wg_api_client

# Security check with bandit
bandit -r wg_api_client
```

## Publishing

This package is available on PyPI and can be automatically published through GitHub releases.

### Automatic Publishing

1. Update version numbers in:
   - `wg_api_client/__init__.py` (`__version__` variable)
   - `setup.py` (`version` parameter)

2. Create a new GitHub release:
   - Go to the GitHub repository
   - Click "Releases" → "Create a new release"
   - Tag version should be in format `v{version}` (e.g., `v0.1.2`)
   - The GitHub Action will automatically build and publish to PyPI

### Manual Publishing

For detailed instructions on manual publishing, see [PUBLISHING.md](PUBLISHING.md).

## License

This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
