vaultcerthelper

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2020 License: MIT Imports: 4 Imported by: 1

README

vault-cert-helpers

A helper class to iterate through certs from Vault

Mostly an excuse to help me learn go-vcr for testing.

Usage

package main

import (
	"fmt"
	"net/http"

	"github.com/hashicorp/vault/api"

	. "github.com/petems/vault-cert-helpers"
)

func createVaultClient() *api.Client {

	// Create an HTTP client and inject our transport
	client := &http.Client{}

	// Create Vault client with vcr'd http.Client
	vaultClient, err := api.NewClient(&api.Config{Address: "http://127.0.0.1:8200", HttpClient: client})
	if err != nil {
		panic("Failed to get new Vault client")
	}

	// We're using VAULT_DEV_ROOT_TOKEN_ID=ROOT with a vault server -dev
	vaultClient.SetToken("ROOT")

	return vaultClient
}

func main() {

	vaultClient := createVaultClient()

	// Get list of certs from /pki endpoint
	listOfCertsSecret, err := GetListOfCerts(vaultClient, "pki")

	if err != nil {
		panic(err)
	}

	arrayOfCerts, err := GetArrayOfCertsFromVault(vaultClient, listOfCertsSecret, "pki")

	fmt.Printf("First cert CN is: %s\n", arrayOfCerts[0].Subject.CommonName)
	fmt.Printf("Second cert CN is: %s\n", arrayOfCerts[1].Subject.CommonName)

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetArrayOfCertsFromVault

func GetArrayOfCertsFromVault(client *api.Client, secret *api.Secret, pkiPath string) (arrayOfCerts []*x509.Certificate, err error)

GetArrayOfCertsFromVault iterates through a given list of keys from a vault secret and returns a slice of *x509.Certificate's from the PEM data

arrayOfCerts, err := GetArrayOfCertsFromVault(client, secret, "pki")

func GetListOfCerts

func GetListOfCerts(client *api.Client, pkiPath string) (*api.Secret, error)

GetListOfCerts fetches the list of certs from a given pki backend

listOfCerts, err := GetListOfCerts(client, "pki")

func ParseCertFromVaultSecret

func ParseCertFromVaultSecret(secret *api.Secret) (*x509.Certificate, error)

ParseCertFromVaultSecret parses the value from the "certificate" field in cert data from vault and returns a *x509.Certificate

cert, err := ParseCertFromVaultSecret(secret)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL