Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 734fcb8cf4 | |||
| f933660d3b | |||
| 3370aff38f | |||
| 1a907467e9 | |||
| cf25a20a92 |
@@ -77,6 +77,7 @@ profiles::yum::global::repos:
|
||||
baseurl: https://artifactapi.k8s.syd1.au.unkin.net/api/v1/local/rpm-internal/
|
||||
gpgcheck: false
|
||||
mirrorlist: absent
|
||||
metadata_expire: '60'
|
||||
rpm-vendor:
|
||||
name: rpm-vendor
|
||||
descr: rpm-vendor repository
|
||||
@@ -84,6 +85,7 @@ profiles::yum::global::repos:
|
||||
baseurl: https://artifactapi.k8s.syd1.au.unkin.net/api/v1/local/rpm-vendor/
|
||||
gpgcheck: false
|
||||
mirrorlist: absent
|
||||
metadata_expire: '60'
|
||||
# Per-release variants, resolved from the host's EL major version so el8
|
||||
# hosts pull rpm-internal-el8/rpm-vendor-el8, el9 hosts el9, etc.
|
||||
rpm-internal-release:
|
||||
@@ -93,6 +95,7 @@ profiles::yum::global::repos:
|
||||
baseurl: https://artifactapi.k8s.syd1.au.unkin.net/api/v1/local/rpm-internal-el%{facts.os.release.major}/
|
||||
gpgcheck: false
|
||||
mirrorlist: absent
|
||||
metadata_expire: '60'
|
||||
rpm-vendor-release:
|
||||
name: rpm-vendor-el%{facts.os.release.major}
|
||||
descr: rpm-vendor-el%{facts.os.release.major} repository
|
||||
@@ -100,6 +103,7 @@ profiles::yum::global::repos:
|
||||
baseurl: https://artifactapi.k8s.syd1.au.unkin.net/api/v1/local/rpm-vendor-el%{facts.os.release.major}/
|
||||
gpgcheck: false
|
||||
mirrorlist: absent
|
||||
metadata_expire: '60'
|
||||
|
||||
# Additional repositories - default to absent, roles can override with ensure: present
|
||||
# FRRouting repositories
|
||||
|
||||
@@ -60,6 +60,7 @@ profiles::yum::global::repos:
|
||||
baseurl: https://artifactapi.k8s.syd1.au.unkin.net/api/v1/local/rpm-internal-f%{facts.os.release.major}/
|
||||
gpgcheck: false
|
||||
mirrorlist: absent
|
||||
metadata_expire: '60'
|
||||
rpm-vendor:
|
||||
name: rpm-vendor-f%{facts.os.release.major}
|
||||
descr: rpm-vendor-f%{facts.os.release.major} repository
|
||||
@@ -67,3 +68,4 @@ profiles::yum::global::repos:
|
||||
baseurl: https://artifactapi.k8s.syd1.au.unkin.net/api/v1/local/rpm-vendor-f%{facts.os.release.major}/
|
||||
gpgcheck: false
|
||||
mirrorlist: absent
|
||||
metadata_expire: '60'
|
||||
|
||||
@@ -26,6 +26,15 @@ profiles::puppet::cobbler_enc::packages:
|
||||
- 'requests'
|
||||
- 'PyYAML'
|
||||
profiles::puppet::enc::repo: https://git.service.au-syd1.consul/unkinben/puppet-enc.git
|
||||
|
||||
# Deep-merged with the entries in roles/infra/puppet.yaml.
|
||||
profiles::packages::include:
|
||||
encapic:
|
||||
ensure: '0.2.0'
|
||||
|
||||
profiles::puppet::encapic::encapi_url: https://encapi.k8s.syd1.au.unkin.net
|
||||
profiles::puppet::server::external_nodes: '/usr/bin/encapic-enc'
|
||||
|
||||
profiles::puppet::r10k::r10k_repo: https://git.unkin.net/unkin/puppet-r10k.git
|
||||
profiles::puppet::g10k::bin_path: '/usr/bin/g10k'
|
||||
profiles::puppet::g10k::cfg_path: '/etc/puppetlabs/r10k/r10k.yaml'
|
||||
@@ -47,18 +56,12 @@ profiles::helpers::certmanager::vault_config:
|
||||
|
||||
profiles::helpers::sshsignhost::vault_config:
|
||||
addr: 'https://vault.service.consul:8200'
|
||||
mount_point: 'sshca'
|
||||
mount_point: 'ssh-host-signer'
|
||||
approle_path: 'approle'
|
||||
role_name: 'signhost'
|
||||
role_name: 'hostrole'
|
||||
output_path: '/tmp/sshsignhost'
|
||||
role_id: "%{lookup('sshsignhost::role_id')}"
|
||||
|
||||
# Vault signing helpers, delivered as RPMs from the rpm-internal repo.
|
||||
profiles::packages::include:
|
||||
certmanager:
|
||||
ensure: '0.2.0'
|
||||
sshsignhost:
|
||||
ensure: '0.1.0'
|
||||
|
||||
profiles::puppet::server::agent_server: 'puppet.query.consul'
|
||||
profiles::puppet::server::report_server: 'puppet.query.consul'
|
||||
profiles::puppet::server::ca_server: 'puppetca.query.consul'
|
||||
|
||||
@@ -3,13 +3,21 @@
|
||||
require 'facter'
|
||||
require 'yaml'
|
||||
require 'net/http'
|
||||
require 'openssl'
|
||||
require 'uri'
|
||||
require 'fileutils'
|
||||
|
||||
# CobblerENC module: Fetches ENC data from Cobbler, caches it, and provides structured facts.
|
||||
module CobblerENC
|
||||
# EncapiENC module: Fetches ENC data from encapi, caches it, and provides structured facts.
|
||||
module EncapiENC
|
||||
CACHE_FILE = '/var/cache/puppet_enc.yaml'
|
||||
CACHE_TTL = 7 * 24 * 60 * 60 # 7 days in seconds
|
||||
# Facter runs under Puppet's vendored ruby, whose OpenSSL trusts only
|
||||
# /opt/puppetlabs/puppet/ssl/cert.pem and never the system trust store, so the
|
||||
# estate CA anchor profiles::pki::vaultca installs has to be named explicitly.
|
||||
CA_BUNDLE_PATHS = [
|
||||
'/etc/pki/ca-trust/source/anchors/vaultcaroot.pem',
|
||||
'/usr/local/share/ca-certificates/vaultcaroot.pem'
|
||||
].freeze
|
||||
@enc_data = nil # In-memory cache for the ENC response
|
||||
|
||||
def self.read_cache
|
||||
@@ -29,9 +37,22 @@ module CobblerENC
|
||||
File.write(CACHE_FILE, cache_data.to_yaml)
|
||||
end
|
||||
|
||||
def self.fetch_from_cobbler
|
||||
uri = URI("http://cobbler.main.unkin.net/cblr/svc/op/puppet/hostname/#{Facter.value(:fqdn) || Facter.value(:hostname)}")
|
||||
response = Net::HTTP.get_response(uri)
|
||||
def self.ca_bundle
|
||||
CA_BUNDLE_PATHS.find { |path| File.exist?(path) }
|
||||
end
|
||||
|
||||
def self.http_client(uri)
|
||||
client = Net::HTTP.new(uri.host, uri.port)
|
||||
client.use_ssl = true
|
||||
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
||||
bundle = ca_bundle
|
||||
client.ca_file = bundle if bundle
|
||||
client
|
||||
end
|
||||
|
||||
def self.fetch_from_encapi
|
||||
uri = URI("https://encapi.k8s.syd1.au.unkin.net/cblr/svc/op/puppet/hostname/#{Facter.value(:fqdn) || Facter.value(:hostname)}")
|
||||
response = http_client(uri).request(Net::HTTP::Get.new(uri))
|
||||
|
||||
raise "Failed to fetch ENC data. HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
||||
|
||||
@@ -41,7 +62,7 @@ module CobblerENC
|
||||
def self.retrieve_enc_data
|
||||
return @enc_data if @enc_data
|
||||
|
||||
@enc_data = fetch_from_cobbler
|
||||
@enc_data = fetch_from_encapi
|
||||
write_cache(@enc_data)
|
||||
@enc_data
|
||||
end
|
||||
@@ -49,26 +70,26 @@ module CobblerENC
|
||||
def self.fetch_enc_data
|
||||
retrieve_enc_data
|
||||
rescue StandardError => e
|
||||
Facter.warn("Error retrieving Cobbler ENC data: #{e.message}")
|
||||
Facter.warn("Error retrieving encapi ENC data: #{e.message}")
|
||||
@enc_data = read_cache
|
||||
return @enc_data unless @enc_data.empty?
|
||||
|
||||
raise 'No cached ENC data available and Cobbler is down.'
|
||||
raise 'No cached ENC data available and encapi is unreachable.'
|
||||
end
|
||||
|
||||
def self.enc_role
|
||||
fetch_enc_data.fetch('classes', {}).keys.first || raise('ENC Role not found in Cobbler ENC response')
|
||||
fetch_enc_data.fetch('classes', {}).keys.first || raise('ENC Role not found in encapi ENC response')
|
||||
end
|
||||
|
||||
def self.enc_env
|
||||
fetch_enc_data.fetch('environment', nil) || raise('ENC Environment not found in Cobbler ENC response')
|
||||
fetch_enc_data.fetch('environment', nil) || raise('ENC Environment not found in encapi ENC response')
|
||||
end
|
||||
end
|
||||
|
||||
Facter.add('enc_role') do
|
||||
setcode { CobblerENC.enc_role }
|
||||
setcode { EncapiENC.enc_role }
|
||||
end
|
||||
|
||||
Facter.add('enc_env') do
|
||||
setcode { CobblerENC.enc_env }
|
||||
setcode { EncapiENC.enc_env }
|
||||
end
|
||||
|
||||
@@ -24,8 +24,7 @@ class profiles::dns::updater (
|
||||
Stdlib::AbsolutePath $config_dir = '/etc/dns-updater',
|
||||
Stdlib::AbsolutePath $master_basedir = lookup('profiles::dns::master::basedir'),
|
||||
# dns-updater daemon (replaces the dns-update shell script). 'latest' so hosts
|
||||
# pick up new releases (e.g. the record filter); rpm-internal metadata_expire
|
||||
# is 1h so this does not thrash.
|
||||
# pick up new releases (e.g. the record filter).
|
||||
String $package_ensure = 'latest',
|
||||
Stdlib::AbsolutePath $api_socket = '/run/dns-updater/api.sock',
|
||||
String $resync = '10m',
|
||||
|
||||
@@ -1,28 +1,77 @@
|
||||
# profiles::helpers::certmanager
|
||||
#
|
||||
# renders the config.yaml read by the certmanager binary (RPM-installed)
|
||||
# wrapper class for python, pip and venv
|
||||
class profiles::helpers::certmanager (
|
||||
String $script_name = 'certmanager',
|
||||
Stdlib::AbsolutePath $base_path = "/opt/${script_name}",
|
||||
Stdlib::AbsolutePath $venv_path = "${base_path}/venv",
|
||||
Stdlib::AbsolutePath $config_path = "${base_path}/config.yaml",
|
||||
Hash $vault_config = {},
|
||||
String $owner = 'root',
|
||||
String $group = 'root',
|
||||
Boolean $systempkgs = false,
|
||||
String $version = 'system',
|
||||
Array[String[1]] $packages = ['requests', 'pyyaml'],
|
||||
){
|
||||
|
||||
file { $base_path:
|
||||
ensure => directory,
|
||||
mode => '0755',
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
}
|
||||
if $::facts['python3_version'] {
|
||||
|
||||
file { $config_path:
|
||||
ensure => file,
|
||||
mode => '0660',
|
||||
owner => 'puppet',
|
||||
group => 'root',
|
||||
content => Sensitive(template("profiles/helpers/${script_name}_config.yaml.erb")),
|
||||
require => File[$base_path],
|
||||
$python_version = $version ? {
|
||||
'system' => $::facts['python3_version'],
|
||||
default => $version,
|
||||
}
|
||||
|
||||
# ensure the base_path exists
|
||||
file { $base_path:
|
||||
ensure => directory,
|
||||
mode => '0755',
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
}
|
||||
|
||||
# create a venv
|
||||
python::pyvenv { $venv_path :
|
||||
ensure => present,
|
||||
version => $python_version,
|
||||
systempkgs => $systempkgs,
|
||||
venv_dir => $venv_path,
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
require => File[$base_path],
|
||||
}
|
||||
|
||||
# install the required pip packages
|
||||
$packages.each |String $package| {
|
||||
python::pip { "${venv_path}_${package}":
|
||||
ensure => present,
|
||||
pkgname => $package,
|
||||
virtualenv => $venv_path,
|
||||
}
|
||||
}
|
||||
|
||||
# create the script from a template
|
||||
file { "${base_path}/${script_name}":
|
||||
ensure => file,
|
||||
mode => '0755',
|
||||
content => template("profiles/helpers/${script_name}.erb"),
|
||||
require => Python::Pyvenv[$venv_path],
|
||||
}
|
||||
|
||||
# create the config from a template
|
||||
file { $config_path:
|
||||
ensure => file,
|
||||
mode => '0660',
|
||||
owner => 'puppet',
|
||||
group => 'root',
|
||||
content => Sensitive(template("profiles/helpers/${script_name}_config.yaml.erb")),
|
||||
require => Python::Pyvenv[$venv_path],
|
||||
}
|
||||
|
||||
# create symbolic link in $PATH
|
||||
file { "/usr/local/bin/${script_name}":
|
||||
ensure => 'link',
|
||||
target => "${base_path}/${script_name}",
|
||||
require => File["${base_path}/${script_name}"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,77 @@
|
||||
# profiles::helpers::sshsignhost
|
||||
#
|
||||
# renders the config.yaml read by the sshsignhost binary (RPM-installed)
|
||||
# wrapper class for python, pip and venv
|
||||
class profiles::helpers::sshsignhost (
|
||||
String $script_name = 'sshsignhost',
|
||||
Stdlib::AbsolutePath $base_path = "/opt/${script_name}",
|
||||
Stdlib::AbsolutePath $venv_path = "${base_path}/venv",
|
||||
Stdlib::AbsolutePath $config_path = "${base_path}/config.yaml",
|
||||
Hash $vault_config = {},
|
||||
String $owner = 'root',
|
||||
String $group = 'root',
|
||||
Boolean $systempkgs = false,
|
||||
String $version = 'system',
|
||||
Array[String[1]] $packages = ['requests', 'pyyaml'],
|
||||
){
|
||||
|
||||
file { $base_path:
|
||||
ensure => directory,
|
||||
mode => '0755',
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
}
|
||||
if $::facts['python3_version'] {
|
||||
|
||||
file { $config_path:
|
||||
ensure => file,
|
||||
mode => '0660',
|
||||
owner => 'puppet',
|
||||
group => 'root',
|
||||
content => Sensitive(template("profiles/helpers/${script_name}_config.yaml.erb")),
|
||||
require => File[$base_path],
|
||||
$python_version = $version ? {
|
||||
'system' => $::facts['python3_version'],
|
||||
default => $version,
|
||||
}
|
||||
|
||||
# ensure the base_path exists
|
||||
file { $base_path:
|
||||
ensure => directory,
|
||||
mode => '0755',
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
}
|
||||
|
||||
# create a venv
|
||||
python::pyvenv { $venv_path :
|
||||
ensure => present,
|
||||
version => $python_version,
|
||||
systempkgs => $systempkgs,
|
||||
venv_dir => $venv_path,
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
require => File[$base_path],
|
||||
}
|
||||
|
||||
# install the required pip packages
|
||||
$packages.each |String $package| {
|
||||
python::pip { "${venv_path}_${package}":
|
||||
ensure => present,
|
||||
pkgname => $package,
|
||||
virtualenv => $venv_path,
|
||||
}
|
||||
}
|
||||
|
||||
# create the script from a template
|
||||
file { "${base_path}/${script_name}":
|
||||
ensure => file,
|
||||
mode => '0755',
|
||||
content => template("profiles/helpers/${script_name}.erb"),
|
||||
require => Python::Pyvenv[$venv_path],
|
||||
}
|
||||
|
||||
# create the config from a template
|
||||
file { $config_path:
|
||||
ensure => file,
|
||||
mode => '0660',
|
||||
owner => 'puppet',
|
||||
group => 'root',
|
||||
content => Sensitive(template("profiles/helpers/${script_name}_config.yaml.erb")),
|
||||
require => Python::Pyvenv[$venv_path],
|
||||
}
|
||||
|
||||
# create symbolic link in $PATH
|
||||
file { "/usr/local/bin/${script_name}":
|
||||
ensure => 'link',
|
||||
target => "${base_path}/${script_name}",
|
||||
require => File["${base_path}/${script_name}"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
# Class: profiles::puppet::encapic
|
||||
#
|
||||
# Manages the configuration for the encapic ENC client. The package itself is
|
||||
# installed through profiles::packages (pinned in hiera); this class owns the
|
||||
# config so the encapi endpoint can change without repackaging.
|
||||
class profiles::puppet::encapic (
|
||||
Stdlib::HTTPUrl $encapi_url,
|
||||
Stdlib::AbsolutePath $config_dir = '/etc/encapic',
|
||||
String $config_name = 'encapic.conf',
|
||||
String $owner = 'root',
|
||||
String $group = 'root',
|
||||
) {
|
||||
|
||||
# The RPM ships this file as %config(noreplace), so puppet must write it only
|
||||
# once the package is present or the install overwrites it.
|
||||
file { $config_dir:
|
||||
ensure => directory,
|
||||
mode => '0755',
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
require => Package['encapic'],
|
||||
}
|
||||
|
||||
file { "${config_dir}/${config_name}":
|
||||
ensure => file,
|
||||
mode => '0644',
|
||||
owner => $owner,
|
||||
group => $group,
|
||||
content => "ENCAPI_URL=${encapi_url}\n",
|
||||
require => File[$config_dir],
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ class profiles::puppet::puppetmaster (
|
||||
include profiles::puppet::g10k
|
||||
include profiles::puppet::enc
|
||||
include profiles::puppet::cobbler_enc
|
||||
include profiles::puppet::encapic
|
||||
include profiles::puppet::autosign
|
||||
include profiles::puppet::gems
|
||||
include profiles::helpers::certmanager
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
#!<%= @venv_path %>/bin/python
|
||||
|
||||
import argparse
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
import yaml
|
||||
from zipfile import ZipFile
|
||||
|
||||
# remove this after certs are generated everywhere
|
||||
requests.packages.urllib3.disable_warnings()
|
||||
|
||||
def load_config(config_path):
|
||||
with open(config_path, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
return config['vault']
|
||||
|
||||
def authenticate_approle(vault_config):
|
||||
url = f"{vault_config['addr']}/v1/auth/{vault_config['approle_path']}/login"
|
||||
payload = {
|
||||
"role_id": vault_config['role_id'],
|
||||
}
|
||||
response = requests.post(url, json=payload, verify=False)
|
||||
if response.status_code == 200:
|
||||
auth_response = response.json()
|
||||
return auth_response['auth']['client_token']
|
||||
else:
|
||||
print(f"Error authenticating with AppRole: {response.text}")
|
||||
return None
|
||||
|
||||
def request_certificate(common_name, alt_names, ip_sans, expiry_days, vault_config):
|
||||
# Authenticate using AppRole and get a token
|
||||
client_token = authenticate_approle(vault_config)
|
||||
if not client_token:
|
||||
print("Failed to authenticate with Vault using AppRole.")
|
||||
return None
|
||||
|
||||
url = f"{vault_config['addr']}/v1/{vault_config['mount_point']}/issue/{vault_config['role_name']}"
|
||||
headers = {'X-Vault-Token': client_token}
|
||||
payload = {
|
||||
"common_name": common_name,
|
||||
"alt_names": ",".join(alt_names),
|
||||
"ip_sans": ",".join(ip_sans),
|
||||
"ttl": f"{expiry_days}d"
|
||||
}
|
||||
response = requests.post(url, headers=headers, json=payload, verify=False)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Error requesting certificate: {response.text}")
|
||||
return None
|
||||
|
||||
def save_cert_files(certificate_response, common_name, compress, config, json_output):
|
||||
base_path = config.get('output_path', '.')
|
||||
cert_dir = os.path.join(base_path, common_name)
|
||||
if json_output:
|
||||
import json
|
||||
output = {
|
||||
'certificate': certificate_response['data']['certificate'],
|
||||
'private_key': certificate_response['data']['private_key'],
|
||||
'full_chain': certificate_response['data']['issuing_ca'] + "\n" + certificate_response['data']['certificate'],
|
||||
}
|
||||
print(json.dumps(output))
|
||||
elif not compress:
|
||||
os.makedirs(cert_dir, exist_ok=True)
|
||||
with open(os.path.join(cert_dir, "certificate.crt"), "w") as cert_file:
|
||||
cert_file.write(certificate_response['data']['certificate'])
|
||||
with open(os.path.join(cert_dir, "private.key"), "w") as key_file:
|
||||
key_file.write(certificate_response['data']['private_key'])
|
||||
with open(os.path.join(cert_dir, "full_chain.crt"), "w") as full_chain_file:
|
||||
full_chain_file.write(certificate_response['data']['issuing_ca'] + "\n" + certificate_response['data']['certificate'])
|
||||
else:
|
||||
zip_name = f"{os.path.join(base_path, common_name)}.zip"
|
||||
with ZipFile(zip_name, 'w') as zipf:
|
||||
zipf.writestr("certificate.crt", certificate_response['data']['certificate'])
|
||||
zipf.writestr("private.key", certificate_response['data']['private_key'])
|
||||
zipf.writestr("full_chain.crt", certificate_response['data']['issuing_ca'] + "\n" + certificate_response['data']['certificate'])
|
||||
|
||||
def main(config_file):
|
||||
config = load_config(config_file)
|
||||
parser = argparse.ArgumentParser(description='Request and retrieve a certificate from Vault.')
|
||||
parser.add_argument('common_name', type=str, help='Common Name for the certificate')
|
||||
parser.add_argument('-a', '--alt-names', type=str, default='', help='Comma-separated alternative names for the certificate')
|
||||
parser.add_argument('-i', '--ip-sans', type=str, default='', help='Comma-separated IP Subject Alternative Names for the certificate')
|
||||
parser.add_argument('-e', '--expiry-days', type=int, default=365, help='Validity of the certificate in days (default: 365)')
|
||||
parser.add_argument('-c', '--compress', action='store_true', help='Compress the certificate, key, and full chain into a zip file')
|
||||
parser.add_argument('--json', action='store_true', help='Output results in JSON format')
|
||||
args = parser.parse_args()
|
||||
alt_names = [name.strip() for name in args.alt_names.split(',') if name]
|
||||
ip_sans = [ip.strip() for ip in args.ip_sans.split(',') if ip]
|
||||
certificate_response = request_certificate(args.common_name, alt_names, ip_sans, args.expiry_days, config)
|
||||
if certificate_response:
|
||||
if args.json:
|
||||
save_cert_files(certificate_response, args.common_name, args.compress, config, True)
|
||||
else:
|
||||
save_cert_files(certificate_response, args.common_name, args.compress, config, False)
|
||||
else:
|
||||
print("Failed to obtain certificate.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
config_file = '<%= @config_path %>'
|
||||
main(config_file)
|
||||
@@ -4,4 +4,4 @@ vault:
|
||||
approle_path: '<%= @vault_config['approle_path'] %>'
|
||||
mount_point: '<%= @vault_config['mount_point'] %>'
|
||||
role_name: '<%= @vault_config['role_name'] %>'
|
||||
output_path: '<%= @vault_config['output_path'] %>'
|
||||
output_path: '<%= @vault_config['output_path'] %>'
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
#!<%= @venv_path %>/bin/python
|
||||
import argparse
|
||||
import requests
|
||||
import json
|
||||
import yaml
|
||||
|
||||
# remove this after certs are generated everywhere
|
||||
requests.packages.urllib3.disable_warnings()
|
||||
|
||||
def load_config(config_path):
|
||||
with open(config_path, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
return config['vault']
|
||||
|
||||
def authenticate_approle(vault_config):
|
||||
url = f"{vault_config['addr']}/v1/auth/{vault_config['approle_path']}/login"
|
||||
payload = {
|
||||
"role_id": vault_config['role_id'],
|
||||
}
|
||||
response = requests.post(url, json=payload, verify=False)
|
||||
if response.status_code == 200:
|
||||
auth_response = response.json()
|
||||
return auth_response['auth']['client_token']
|
||||
else:
|
||||
print(f"Error authenticating with AppRole: {response.text}")
|
||||
return None
|
||||
|
||||
def sign_ssh_certificate(vault_config, public_key, valid_principals, ttl):
|
||||
# Authenticate using AppRole and get a token
|
||||
client_token = authenticate_approle(vault_config)
|
||||
if not client_token:
|
||||
print("Failed to authenticate with Vault using AppRole.")
|
||||
return None
|
||||
|
||||
# Prepare the SSH certificate signing request
|
||||
url = f"{vault_config['addr']}/v1/{vault_config['mount_point']}/sign/{vault_config['role_name']}"
|
||||
headers = {'X-Vault-Token': client_token}
|
||||
payload = {
|
||||
"cert_type": "host",
|
||||
"public_key": public_key,
|
||||
"valid_principals": valid_principals,
|
||||
"ttl": ttl
|
||||
}
|
||||
|
||||
# Request the SSH certificate signing
|
||||
response = requests.post(url, headers=headers, json=payload, verify=False)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Error requesting certificate: {response.text}")
|
||||
return None
|
||||
|
||||
def main(config_file):
|
||||
config = load_config(config_file)
|
||||
parser = argparse.ArgumentParser(description='Sign SSH host certificate using Vault.')
|
||||
parser.add_argument('--public_key', required=True, help='SSH public key as a string')
|
||||
parser.add_argument('--valid_principals', required=True, help='Comma-separated list of valid principals')
|
||||
parser.add_argument('--ttl', default='87600h', help='Time-to-live for the certificate (default: 87600h)')
|
||||
parser.add_argument('--json', action='store_true', help='Output the resulting certificate as JSON')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Load configuration
|
||||
config = load_config(config_file)
|
||||
|
||||
# Sign SSH certificate
|
||||
response = sign_ssh_certificate(config, args.public_key, args.valid_principals, args.ttl)
|
||||
|
||||
if response and 'data' in response and 'signed_key' in response['data']:
|
||||
if args.json:
|
||||
output = {
|
||||
'signed_key': response['data']['signed_key'],
|
||||
}
|
||||
print(json.dumps(output))
|
||||
else:
|
||||
print(response['data']['signed_key'])
|
||||
else:
|
||||
print("Error: The response does not contain the expected data.")
|
||||
exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
config_file = '<%= @config_path %>'
|
||||
main(config_file)
|
||||
@@ -4,3 +4,4 @@ vault:
|
||||
approle_path: '<%= @vault_config['approle_path'] %>'
|
||||
mount_point: '<%= @vault_config['mount_point'] %>'
|
||||
role_name: '<%= @vault_config['role_name'] %>'
|
||||
output_path: '<%= @vault_config['output_path'] %>'
|
||||
|
||||
Reference in New Issue
Block a user