github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/pkg/keystore/keystoretest/keygen/keygen.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build ignore
    16  
    17  // Generate opengpg keys for Application Container Keystore. Outputs to keymap.go
    18  // and will overwrite existing files.
    19  
    20  package main
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"io/ioutil"
    26  	"log"
    27  	"os"
    28  	"strings"
    29  	"text/template"
    30  
    31  	"golang.org/x/crypto/openpgp"
    32  )
    33  
    34  type Key struct {
    35  	Name              string
    36  	Fingerprint       string
    37  	ArmoredPublicKey  string
    38  	ArmoredPrivateKey string
    39  }
    40  
    41  var output = "keymap.go"
    42  
    43  var keymapTemplate = `// Code generated by go generate.
    44  // Source file: keygen.go
    45  // DO NOT EDIT!
    46  
    47  package keystoretest
    48  
    49  var KeyMap = map[string]*KeyDetails{
    50  {{range .}}	"{{.Name}}": &KeyDetails{
    51  		Fingerprint: ` + "`" + `{{.Fingerprint}}` + "`" + `,
    52  		ArmoredPublicKey: ` + "`" + `{{.ArmoredPublicKey}}` + "`" + `,
    53  		ArmoredPrivateKey: ` + "`" + `{{.ArmoredPrivateKey}}` + "`" + `,
    54  	},
    55  {{end}}}
    56  `
    57  
    58  var names = []string{
    59  	"example.com",
    60  	"coreos.com",
    61  	"example.com/app",
    62  	"acme.com",
    63  	"acme.com/services",
    64  	"acme.com/services/web/nginx",
    65  }
    66  
    67  func main() {
    68  	ks := make([]Key, 0)
    69  	for _, name := range names {
    70  		entity, err := newEntity(name)
    71  		if err != nil {
    72  			log.Fatal(err)
    73  		}
    74  
    75  		privateKeyBuf := bytes.NewBuffer(nil)
    76  		w0, err := armor.Encode(privateKeyBuf, openpgp.PrivateKeyType, nil)
    77  		if err != nil {
    78  			log.Fatal(err)
    79  		}
    80  		if err := entity.SerializePrivate(w0, nil); err != nil {
    81  			log.Fatal(err)
    82  		}
    83  		w0.Close()
    84  
    85  		publicKeyBuf := bytes.NewBuffer(nil)
    86  		w1, err := armor.Encode(publicKeyBuf, openpgp.PublicKeyType, nil)
    87  		if err != nil {
    88  			log.Fatal(err)
    89  		}
    90  		if err := entity.Serialize(w1); err != nil {
    91  			log.Fatal(err)
    92  		}
    93  		w1.Close()
    94  
    95  		fingerprint := fmt.Sprintf("%x", entity.PrimaryKey.Fingerprint)
    96  		key := Key{
    97  			Name:              name,
    98  			Fingerprint:       fingerprint,
    99  			ArmoredPublicKey:  publicKeyBuf.String(),
   100  			ArmoredPrivateKey: privateKeyBuf.String(),
   101  		}
   102  		ks = append(ks, key)
   103  	}
   104  	tmpl, err := template.New("keymap").Parse(keymapTemplate)
   105  	if err != nil {
   106  		log.Fatal(err)
   107  	}
   108  	f, err := os.Create(output)
   109  	if err != nil {
   110  		log.Fatal(err)
   111  	}
   112  	defer f.Close()
   113  	err = tmpl.Execute(f, ks)
   114  	if err != nil {
   115  		log.Fatal(err)
   116  	}
   117  }
   118  
   119  func newEntity(name string) (*openpgp.Entity, error) {
   120  	parts := strings.Split(name, "/")
   121  	comment := fmt.Sprintf("%s Signing Key", name)
   122  	email := fmt.Sprintf("signer@%s", parts[0])
   123  	entity, err := openpgp.NewEntity("signer", comment, email, nil)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	if err := entity.SerializePrivate(ioutil.Discard, nil); err != nil {
   128  		return nil, err
   129  	}
   130  	return entity, nil
   131  }