github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/pkg/keystore/keystoretest/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 "github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/crypto/openpgp" 32 "github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/crypto/openpgp/armor" 33 ) 34 35 type Key struct { 36 Name string 37 Fingerprint string 38 ArmoredPublicKey string 39 ArmoredPrivateKey string 40 } 41 42 var output = "keymap.go" 43 44 var keymapTemplate = `// Code generated by go generate. 45 // Source file: keygen.go 46 // DO NOT EDIT! 47 48 package keystoretest 49 50 var KeyMap = map[string]*KeyDetails{ 51 {{range .}} "{{.Name}}": &KeyDetails{ 52 Fingerprint: ` + "`" + `{{.Fingerprint}}` + "`" + `, 53 ArmoredPublicKey: ` + "`" + `{{.ArmoredPublicKey}}` + "`" + `, 54 ArmoredPrivateKey: ` + "`" + `{{.ArmoredPrivateKey}}` + "`" + `, 55 }, 56 {{end}}} 57 ` 58 59 var names = []string{ 60 "example.com", 61 "coreos.com", 62 "example.com/app", 63 "acme.com", 64 "acme.com/services", 65 "acme.com/services/web/nginx", 66 } 67 68 func main() { 69 ks := make([]Key, 0) 70 for _, name := range names { 71 entity, err := newEntity(name) 72 if err != nil { 73 log.Fatal(err) 74 } 75 76 privateKeyBuf := bytes.NewBuffer(nil) 77 w0, err := armor.Encode(privateKeyBuf, openpgp.PrivateKeyType, nil) 78 if err != nil { 79 log.Fatal(err) 80 } 81 if err := entity.SerializePrivate(w0, nil); err != nil { 82 log.Fatal(err) 83 } 84 w0.Close() 85 86 publicKeyBuf := bytes.NewBuffer(nil) 87 w1, err := armor.Encode(publicKeyBuf, openpgp.PublicKeyType, nil) 88 if err != nil { 89 log.Fatal(err) 90 } 91 if err := entity.Serialize(w1); err != nil { 92 log.Fatal(err) 93 } 94 w1.Close() 95 96 fingerprint := fmt.Sprintf("%x", entity.PrimaryKey.Fingerprint) 97 key := Key{ 98 Name: name, 99 Fingerprint: fingerprint, 100 ArmoredPublicKey: publicKeyBuf.String(), 101 ArmoredPrivateKey: privateKeyBuf.String(), 102 } 103 ks = append(ks, key) 104 } 105 tmpl, err := template.New("keymap").Parse(keymapTemplate) 106 if err != nil { 107 log.Fatal(err) 108 } 109 f, err := os.Create(output) 110 if err != nil { 111 log.Fatal(err) 112 } 113 defer f.Close() 114 err = tmpl.Execute(f, ks) 115 if err != nil { 116 log.Fatal(err) 117 } 118 } 119 120 func newEntity(name string) (*openpgp.Entity, error) { 121 parts := strings.Split(name, "/") 122 comment := fmt.Sprintf("%s Signing Key", name) 123 email := fmt.Sprintf("signer@%s", parts[0]) 124 entity, err := openpgp.NewEntity("signer", comment, email, nil) 125 if err != nil { 126 return nil, err 127 } 128 if err := entity.SerializePrivate(ioutil.Discard, nil); err != nil { 129 return nil, err 130 } 131 return entity, nil 132 }