aboutsummaryrefslogtreecommitdiff
path: root/example/client-auth/build-pki.go
diff options
context:
space:
mode:
authorJeremy Clerc <jclerc@google.com>2017-02-15 14:29:43 +0100
committerJeremy Clerc <jclerc@google.com>2017-02-15 14:29:43 +0100
commit51e2e81502cdab076a3c0e4958fbb58884418fc8 (patch)
tree76d94f65b7b3814d0c5e335ec7e6952adf259647 /example/client-auth/build-pki.go
parent1d7c7f7b897ad203f8d873d6338edd7ec735f936 (diff)
downloadeasypki-51e2e81502cdab076a3c0e4958fbb58884418fc8.tar.xz
Add client authentication example.
Diffstat (limited to 'example/client-auth/build-pki.go')
-rw-r--r--example/client-auth/build-pki.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/example/client-auth/build-pki.go b/example/client-auth/build-pki.go
new file mode 100644
index 0000000..63dfefc
--- /dev/null
+++ b/example/client-auth/build-pki.go
@@ -0,0 +1,82 @@
+package main
+
+import (
+ "crypto/x509/pkix"
+ "flag"
+ "io/ioutil"
+ "log"
+
+ "crypto/x509"
+ "time"
+
+ "github.com/boltdb/bolt"
+ "github.com/go-yaml/yaml"
+ "github.com/google/easypki/pkg/certificate"
+ "github.com/google/easypki/pkg/easypki"
+ "github.com/google/easypki/pkg/store"
+)
+
+type configCerts struct {
+ Name string `yaml:"name"`
+ CommonName string `yaml:"commonName"`
+ DNSNames []string `yaml:"dnsNames"`
+ EmailAddresses []string `yaml:"emailAddresses"`
+ IsCA bool `yaml:"isCA"`
+ IsClient bool `yaml:"isClient"`
+ Signer string `yaml:"signer"`
+ Expire time.Duration `yaml:"expire"`
+}
+
+type config struct {
+ Subject pkix.Name `yaml:"subject"`
+ Certs []configCerts `yaml:"certs"`
+}
+
+func main() {
+ var (
+ configPath = flag.String("config_path", "chain.yaml", "Configuration path to generate PKI.")
+ dbPath = flag.String("db_path", "", "Bolt database path.")
+ )
+ flag.Parse()
+ b, err := ioutil.ReadFile(*configPath)
+ if err != nil {
+ log.Fatalf("Failed reading configuration file %v: %v", *configPath, err)
+ }
+ conf := &config{}
+ if err := yaml.Unmarshal(b, conf); err != nil {
+ log.Fatalf("Failed umarshaling yaml config (%v) %v: %v", *configPath, string(b), err)
+ }
+ db, err := bolt.Open(*dbPath, 0600, nil)
+ if err != nil {
+ log.Fatalf("Failed opening bolt database %v: %v", *dbPath, err)
+ }
+ defer db.Close()
+ pki := &easypki.EasyPKI{Store: &store.Bolt{DB: db}}
+ for _, cert := range conf.Certs {
+ req := &easypki.Request{
+ Name: cert.Name,
+ Template: &x509.Certificate{
+ Subject: conf.Subject,
+ NotAfter: time.Now().Add(cert.Expire),
+ IsCA: cert.IsCA,
+ DNSNames: cert.DNSNames,
+ EmailAddresses: cert.EmailAddresses,
+ },
+ IsClientCertificate: cert.IsClient,
+ }
+ if cert.IsCA {
+ req.Template.MaxPathLen = -1
+ }
+ req.Template.Subject.CommonName = cert.CommonName
+ var signer *certificate.Bundle
+ if cert.Signer != "" {
+ signer, err = pki.GetCA(cert.Signer)
+ if err != nil {
+ log.Fatalf("Cannot sign %v because cannot get CA %v: %v", cert.Name, cert.Signer, err)
+ }
+ }
+ if err := pki.Sign(signer, req); err != nil {
+ log.Fatalf("Cannot create bundle for %v: %v", cert.Name, err)
+ }
+ }
+}