#!/bin/bash # Copyright (c) HashiCorp, Inc. # SPDX-License-Identifier: BUSL-1.1 set -e trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT readonly SCRIPT_NAME="$(basename ${BASH_SOURCE[0]})" readonly SCRIPT_DIR="$(dirname ${BASH_SOURCE[0]})" # Start a couple dev agents in the background echo "Starting Dev Agents" consul agent -dev -hcl 'acl { enabled = true default_policy="allow" tokens { initial_management = "root" } }' >/dev/null 2>&1 & consul agent -dev -dns-port=9600 -grpc-port=9502 -grpc-tls-port=9503 -http-port=9500 -serf-lan-port=9301 -serf-wan-port=9302 -server-port=9300 >/dev/null 2>&1 & # should be long enough for the dev agents to be available sleep 5 # This script expects a consul dev agent with acls enabled in default allow to be running on localhost # consul agent -dev -hcl 'acl { enabled = true default_policy="allow" tokens { initial_management = "root" } }' # It also requires another dev agent running on alternative ports to peer with # consul agent -dev -dns-port=9600 -grpc-port=9502 -grpc-tls-port=9503 -http-port=9500 -serf-lan-port=9301 -serf-wan-port=9302 -server-port=9300 # Just running Consul will cause the following data to be in the snapshot: # Register # ConnectCA # ConnectCAProviderState # ConnectCAConfig # Autopilot # Index # SystemMetadata # CoordinateBatchUpdate # FederationState # ChunkingState # FreeVirtualIP # Partition # Tombstone # Ensure a KV entry ends up in the snapshot echo "Creating KV Entry" consul kv put foo/bar 1 >/dev/null # Ensure a tombstone ends up in the snapshot echo "Forcing KV Tombstone Creation" consul kv put foo/baz 2 >/dev/null consul kv delete foo/baz > /dev/null # Ensure a session ends up in the snapshot echo "Creating Session" curl -s -X PUT localhost:8500/v1/session/create >/dev/null # Ensure a prepared query ends up in the snapshot echo "Creating Prepared Query" curl -s -X POST localhost:8500/v1/query -d '{"Name": "test", "Token": "root", "Service": {"Service": "test"}}' >/dev/null # Ensure an ACL token ends up in the snapshot echo "Creating ACL Token" consul acl token create -node-identity=localhost:dc1 >/dev/null # Ensure an ACL policy ends up in the snapshot echo "Creating ACL Policy" consul acl policy create -name=test -rules='node_prefix "" { policy = "write" }' >/dev/null # Ensure an ACL role ends up in the snapshot echo "Creating ACL Role" consul acl role create -name=test -policy-name=test >/dev/null # Ensure an ACL auth method ends up in the snapshot echo "Creating ACL Auth Method" consul acl auth-method create -type jwt -name test -config '{"JWTValidationPubKeys": ["-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENRw6ZwlBOx5XZKjcc1HhU00sDehc\n8nqeeSnRZLv89yT7M7qUOFDtR29FR/AFUSAEOFl1iIYLqNMElHs2VkgAZA==\n-----END PUBLIC KEY-----"]}' >/dev/null # Ensure an ACL binding rule ends up in the snapshot echo "Creating ACL Binding Rule" consul acl binding-rule create -bind-type="service" -bind-name="service" -method="test" >/dev/null # Ensure config entries end up in the snapshot echo "Creating Proxy Default Config Entry" consul config write - >/dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null </dev/null if [ $? -eq 0 ]; then set -e echo "Creating Sameness Group Config Entry" consul config write - >/dev/null </dev/null </dev/null </dev/null </dev/null echo "Saving Snapshot to all.snap" sleep 2 consul snapshot save "${SCRIPT_DIR}/all.snap" >/dev/null