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

Add integration test for Batchx509SVID RPC #5402

Closed
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
51 changes: 51 additions & 0 deletions test/integration/suites/batchx509SVID/00-gen-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

# Exit on error
set -e

# Define directories
BASE_DIR="/opt/spire/conf"
SERVER_DIR="${BASE_DIR}/server"
AGENT_DIR="${BASE_DIR}/agent"

# Create necessary directories
mkdir -p "${SERVER_DIR}"
mkdir -p "${AGENT_DIR}"

# Generate Root CA Certificate and Key
echo "Generating Root CA certificate and key..."
openssl genrsa -out root-ca.key 2048
valverdethiago marked this conversation as resolved.
Show resolved Hide resolved
openssl req -new -x509 -key root-ca.key -out root-ca.crt -days 3650 -subj "/CN=SPIRE Root CA"

# Generate Server Certificate and Key
echo "Generating Server certificate and key..."
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr -subj "/CN=SPIRE Server"
openssl x509 -req -in server.csr -CA root-ca.crt -CAkey root-ca.key -CAcreateserial -out server.crt -days 365

# Generate Agent Certificate and Key
echo "Generating Agent certificate and key..."
openssl genrsa -out agent.key 2048
openssl req -new -key agent.key -out agent.csr -subj "/CN=SPIRE Agent"
openssl x509 -req -in agent.csr -CA root-ca.crt -CAkey root-ca.key -CAcreateserial -out agent.crt -days 365

# Create Trust Bundles
echo "Creating trust bundles..."
cat root-ca.crt server.crt > "${SERVER_DIR}/agent-cacert.pem"
cat root-ca.crt agent.crt > "${AGENT_DIR}/bootstrap.crt"

# Combine Certificates and Keys
echo "Creating combined certificate files..."
cat agent.crt agent.key > "${AGENT_DIR}/agent.crt.pem"
cat server.crt server.key > "${SERVER_DIR}/server.crt.pem"

# Create Combined Key and Certificate Files
echo "Creating combined key and certificate files..."
cat agent.key agent.crt > "${AGENT_DIR}/agent.key.pem"
cat server.key server.crt > "${SERVER_DIR}/server.key.pem"

# Clean up intermediate files
echo "Cleaning up..."
rm server.key server.csr server.crt agent.key agent.csr agent.crt

echo "Certificate files generated and placed in ${BASE_DIR}."
19 changes: 19 additions & 0 deletions test/integration/suites/batchx509SVID/01-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

"${ROOTDIR}/setup/x509pop/setup.sh" conf/server conf/agent

"${ROOTDIR}/setup/debugserver/build.sh" "${RUNDIR}/conf/server/debugclient"
"${ROOTDIR}/setup/debugagent/build.sh" "${RUNDIR}/conf/agent/debugclient"


echo "Starting SPIRE server..."
spire-server run -config conf/server/server.conf > ${RUNDIR}/spire-server.log 2>&1 &
valverdethiago marked this conversation as resolved.
Show resolved Hide resolved
SERVER_PID=$!
sleep 5

echo "Starting SPIRE agent..."
spire-agent run -config conf/agent/agent.conf &
AGENT_PID=$!
sleep 5
14 changes: 14 additions & 0 deletions test/integration/suites/batchx509SVID/02-create-entries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -e

ENTRIES_FILE="${RUNDIR}/data.json"

# Start the entry creation process in the background
spire-server entry create -data "${ENTRIES_FILE}" &
ENTRY_PID=$!

# Wait for the entry creation process to finish
wait $ENTRY_PID
valverdethiago marked this conversation as resolved.
Show resolved Hide resolved

echo "Entries uploaded successfully."

26 changes: 26 additions & 0 deletions test/integration/suites/batchx509SVID/03-test-batchx509svid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -e

ENTRIES_FILE="${RUNDIR}/data.json"

# Parse the JSON file and extract SPIFFE IDs
echo "Checking entries from ${ENTRIES_FILE}..."

# Extract SPIFFE IDs using jq
SPIFFE_IDS=$(jq -r '.entries[].spiffe_id' "$ENTRIES_FILE")

# Check each entry's existence
for SPIFFE_ID in $SPIFFE_IDS; do
echo "Checking if entry with SPIFFE ID ${SPIFFE_ID} exists..."
OUTPUT=$(spire-server entry show -spiffeID "${SPIFFE_ID}" 2>&1)

if echo "$OUTPUT" | grep -q "Error"; then
echo "Error: Entry with SPIFFE ID ${SPIFFE_ID} not found."
exit 1
else
echo "Entry with SPIFFE ID ${SPIFFE_ID} exists."
fi
done
Copy link
Member

@amoore877 amoore877 Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this step as-is verifies that the entries exist, as created in the prior step.

however, neither this suite nor this step test the BatchNewX509SVID API

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I talked with @zmt the batch API is called internally when we add a single entry or create a batch operation as the test is already doing. The check for the existence of the entry fullfils the purpose of the task case if they were created in a batch operation and actually exist the operation is working as expected, right?

Copy link
Member

@amoore877 amoore877 Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I believe you're detailing is Batch Entry Create, not Batch X509 SVID.

if the former, then the test needs to be re-named and the README updated

if the intention is to cover the latter, then comment still stands- this test as-is is not covering either explicit usage of that API or implicit usage by the running agent (which https://github.com/spiffe/spire/blob/main/test/integration/suites/fetch-x509-svids/05-fetch-x509-svids may already be doing)


echo "All entries checked successfully."
exit 0
26 changes: 26 additions & 0 deletions test/integration/suites/batchx509SVID/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Batchx509SVID RPC Integration Test

## Overview

This test ensures the continued operation of the `Batchx509SVID` RPC in Open Source SPIRE.

## Test Steps

1. **Setup (`01-setup.sh`)**
- Generates required certificates and keys.
- Starts the SPIRE server and agent.

2. **Create Registration (`02-create_entries.sh`)**
- Creates necessary registration entries for testing.

2. **Check entries creation (`03-test-batchx509svid.sh`)**
- Verifies necessary registration entries for testing.

3. **Teardown (`teardown.sh`)**
- Stops the SPIRE server and agent.
- Cleans up any remaining artifacts.

Run the setup script:

```bash
./test/integration/test-one.sh ./test/integration/suites/batchx509SVID
31 changes: 31 additions & 0 deletions test/integration/suites/batchx509SVID/conf/agent/agent.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
agent {
data_dir = "/opt/spire/data/agent"
log_level = "DEBUG"
server_address = "spire-server"
server_port = "8081"
socket_path = "/tmp/spire-agent/public/api.sock"
trust_bundle_path = "/opt/spire/conf/agent/bootstrap.crt"
trust_domain = "example.org"
admin_socket_path = "/opt/debug.sock"
experimental {
x509_svid_cache_max_size = 8
}
}

plugins {
NodeAttestor "x509pop" {
plugin_data {
private_key_path = "/opt/spire/conf/agent/agent.key.pem"
certificate_path = "/opt/spire/conf/agent/agent.crt.pem"
}
}
KeyManager "disk" {
plugin_data {
directory = "/opt/spire/data/agent"
}
}
WorkloadAttestor "unix" {
plugin_data {
}
}
}
26 changes: 26 additions & 0 deletions test/integration/suites/batchx509SVID/conf/server/server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
server {
bind_address = "0.0.0.0"
bind_port = "8081"
trust_domain = "example.org"
data_dir = "/opt/spire/data/server"
log_level = "DEBUG"
ca_ttl = "1h"
default_x509_svid_ttl = "10m"
}

plugins {
DataStore "sql" {
plugin_data {
database_type = "sqlite3"
connection_string = "/opt/spire/data/server/datastore.sqlite3"
}
}
NodeAttestor "x509pop" {
plugin_data {
ca_bundle_path = "/opt/spire/conf/server/agent-cacert.pem"
}
}
KeyManager "memory" {
plugin_data = {}
}
}
82 changes: 82 additions & 0 deletions test/integration/suites/batchx509SVID/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"entries": [
{
"spiffe_id": "spiffe://example.org/workload1",
"parent_id": "spiffe://example.org/parent1",
"selectors": [
{
"type": "unix",
"value": "user:root"
}
],
"x509_svid_ttl": 3600,
"jwt_svid_ttl": 7200,
"federates_with": [
"spiffe://example.org/another-trust-domain"
],
"entry_id": "entry-1",
"admin": false,
"downstream": false,
"entryExpiry": 1710000000,
"dns_names": [
"service1.example.org"
],
"revision_number": 1,
"store_svid": true,
"hint": "primary entry",
"created_at": 1710000000
},
{
"spiffe_id": "spiffe://example.org/workload2",
"parent_id": "spiffe://example.org/parent2",
"selectors": [
{
"type": "unix",
"value": "user:admin"
}
],
"x509_svid_ttl": 1800,
"jwt_svid_ttl": 3600,
"federates_with": [
"spiffe://example.org/another-trust-domain"
],
"entry_id": "entry-2",
"admin": true,
"downstream": true,
"entryExpiry": 1710003600,
"dns_names": [
"service2.example.org"
],
"revision_number": 2,
"store_svid": false,
"hint": "admin entry",
"created_at": 1710003600
},
{
"spiffe_id": "spiffe://example.org/workload3",
"parent_id": "spiffe://example.org/parent3",
"selectors": [
{
"type": "unix",
"value": "user:service"
}
],
"x509_svid_ttl": 7200,
"jwt_svid_ttl": 14400,
"federates_with": [
"spiffe://example.org/another-trust-domain"
],
"entry_id": "entry-3",
"admin": false,
"downstream": false,
"entryExpiry": 1710007200,
"dns_names": [
"service3.example.org"
],
"revision_number": 3,
"store_svid": true,
"hint": "secondary entry",
"created_at": 1710007200
}
]
}
24 changes: 24 additions & 0 deletions test/integration/suites/batchx509SVID/teardown
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

echo "Bringing down services..."

# Stop the SPIRE server
if pgrep spire-server > /dev/null; then
echo "Stopping SPIRE server..."
sudo kill -9 $(pgrep spire-server) || true
fi

# Stop the SPIRE agent
if pgrep spire-agent > /dev/null; then
echo "Stopping SPIRE agent..."
sudo kill -9 $(pgrep spire-agent) || true
fi

# Remove temporary directories if needed
echo "Cleaning up..."
rm -rf /opt/spire/data
rm -rf /opt/spire/conf

echo "Teardown complete."