Skip to content

Instantly share code, notes, and snippets.

@iasonliu
Forked from tam7t/certdump.go
Created December 14, 2018 04:51
Show Gist options
  • Save iasonliu/dd4ea1b59d8cc9b75b5aa70b7cacc9e9 to your computer and use it in GitHub Desktop.
Save iasonliu/dd4ea1b59d8cc9b75b5aa70b7cacc9e9 to your computer and use it in GitHub Desktop.
certdump consul-template plugin for writing vault-generated certificates to separate files
package main
import (
"io/ioutil"
"log"
"os"
"os/user"
"strconv"
)
func main() {
err := realMain()
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
func realMain() error {
if len(os.Args) != 4 {
// Ensure the empty input case is handled correctly
return nil
}
// certdump <filepath> <owner> <data>
path := os.Args[1]
owner := os.Args[2]
data := os.Args[3]
err := ioutil.WriteFile(path, []byte(data), 0700)
if err != nil {
return err
}
u, err := user.Lookup(owner)
if err != nil {
return err
}
uid, err := strconv.Atoi(u.Uid)
if err != nil {
return err
}
gid := os.Getgid()
err = os.Chmod(path, 0660)
if err != nil {
return err
}
err = os.Chown(path, uid, gid)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment