Skip to content

Commit

Permalink
certs now can be saved to a secret
Browse files Browse the repository at this point in the history
  • Loading branch information
groundnuty committed Jul 31, 2017
1 parent 12a5196 commit af53625
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
serviceNames string
subdomain string
labels string
secretName string
createSecret bool
)

func main() {
Expand All @@ -60,6 +62,8 @@ func main() {
flag.StringVar(&serviceIPs, "service-ips", "", "service IP addresses that resolve to this Pod; comma separated")
flag.StringVar(&subdomain, "subdomain", "", "subdomain as defined by pod.spec.subdomain")
flag.StringVar(&labels, "labels", "", "labels to include in CertificateSigningRequest object; comma seprated list of key=value")
flag.StringVar(&secretName, "secret-name", "", "secret name to store generated files")
flag.BoolVar(&createSecret, "create-secret", false, "create a new secret instead of waiting for one to update")
flag.Parse()

certificateSigningRequestName := fmt.Sprintf("%s-%s", podName, namespace)
Expand Down Expand Up @@ -175,7 +179,7 @@ func main() {
if err := ioutil.WriteFile(csrFile, certificateRequestBytes, 0644); err != nil {
log.Fatal("unable to %s, error: %s", csrFile, err)
}

log.Printf("wrote %s", csrFile)

// Submit a certificate signing request, wait for it to be approved, then save
Expand Down Expand Up @@ -241,6 +245,33 @@ func main() {
client.CertificatesV1Beta1().DeleteCertificateSigningRequest(context.Background(), certificateSigningRequestName)
log.Printf("Removed approved request %s", certificateSigningRequestName)

if secretName != "" {
for {
ks, err := client.CoreV1().GetSecret(context.Background(), secretName, namespace)
if err != nil {
if createSecret {
log.Fatalf("TODO: cannot create secrets")
} else {
log.Printf("Secret to store credentials (%s) not found; trying again in 5 seconds", secretName)
time.Sleep(5 * time.Second)
continue
}
}

k8sCrt, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")

stringData := make(map[string]string)
stringData["tls.key"] = string(pemKeyBytes)
stringData["tls.crt"] = string(certificate)
stringData["k8s.crt"] = string(k8sCrt) // ok

ks.StringData = stringData
_, err = client.CoreV1().UpdateSecret(context.TODO(), ks)
log.Printf("Stored credentials in secret: (%s)", secretName)

break
}
}

os.Exit(0)
}
Expand Down

0 comments on commit af53625

Please sign in to comment.