github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/vault/helper/pgpkeys/flag_test.go (about) 1 package pgpkeys 2 3 import ( 4 "bytes" 5 "encoding/base64" 6 "encoding/hex" 7 "flag" 8 "fmt" 9 "io/ioutil" 10 "os" 11 "reflect" 12 "strings" 13 "testing" 14 15 "github.com/keybase/go-crypto/openpgp" 16 "github.com/keybase/go-crypto/openpgp/packet" 17 ) 18 19 func TestPubKeyFilesFlag_implements(t *testing.T) { 20 var raw interface{} 21 raw = new(PubKeyFilesFlag) 22 if _, ok := raw.(flag.Value); !ok { 23 t.Fatalf("PubKeysFilesFlag should be a Value") 24 } 25 } 26 27 func TestPubKeyFilesFlagSetBinary(t *testing.T) { 28 tempDir, err := ioutil.TempDir("", "vault-test") 29 if err != nil { 30 t.Fatalf("Error creating temporary directory: %s", err) 31 } 32 defer os.RemoveAll(tempDir) 33 34 decoder := base64.StdEncoding 35 pub1Bytes, err := decoder.DecodeString(pubKey1) 36 if err != nil { 37 t.Fatalf("Error decoding bytes for public key 1: %s", err) 38 } 39 err = ioutil.WriteFile(tempDir+"/pubkey1", pub1Bytes, 0755) 40 if err != nil { 41 t.Fatalf("Error writing pub key 1 to temp file: %s", err) 42 } 43 pub2Bytes, err := decoder.DecodeString(pubKey2) 44 if err != nil { 45 t.Fatalf("Error decoding bytes for public key 2: %s", err) 46 } 47 err = ioutil.WriteFile(tempDir+"/pubkey2", pub2Bytes, 0755) 48 if err != nil { 49 t.Fatalf("Error writing pub key 2 to temp file: %s", err) 50 } 51 pub3Bytes, err := decoder.DecodeString(pubKey3) 52 if err != nil { 53 t.Fatalf("Error decoding bytes for public key 3: %s", err) 54 } 55 err = ioutil.WriteFile(tempDir+"/pubkey3", pub3Bytes, 0755) 56 if err != nil { 57 t.Fatalf("Error writing pub key 3 to temp file: %s", err) 58 } 59 60 pkf := new(PubKeyFilesFlag) 61 err = pkf.Set(tempDir + "/pubkey1,@" + tempDir + "/pubkey2") 62 if err != nil { 63 t.Fatalf("err: %s", err) 64 } 65 66 err = pkf.Set(tempDir + "/pubkey3") 67 if err == nil { 68 t.Fatalf("err: should not have been able to set a second value") 69 } 70 71 expected := []string{strings.Replace(pubKey1, "\n", "", -1), strings.Replace(pubKey2, "\n", "", -1)} 72 if !reflect.DeepEqual(pkf.String(), fmt.Sprint(expected)) { 73 t.Fatalf("Bad: %#v", pkf) 74 } 75 } 76 77 func TestPubKeyFilesFlagSetB64(t *testing.T) { 78 tempDir, err := ioutil.TempDir("", "vault-test") 79 if err != nil { 80 t.Fatalf("Error creating temporary directory: %s", err) 81 } 82 defer os.RemoveAll(tempDir) 83 84 err = ioutil.WriteFile(tempDir+"/pubkey1", []byte(pubKey1), 0755) 85 if err != nil { 86 t.Fatalf("Error writing pub key 1 to temp file: %s", err) 87 } 88 err = ioutil.WriteFile(tempDir+"/pubkey2", []byte(pubKey2), 0755) 89 if err != nil { 90 t.Fatalf("Error writing pub key 2 to temp file: %s", err) 91 } 92 err = ioutil.WriteFile(tempDir+"/pubkey3", []byte(pubKey3), 0755) 93 if err != nil { 94 t.Fatalf("Error writing pub key 3 to temp file: %s", err) 95 } 96 97 pkf := new(PubKeyFilesFlag) 98 err = pkf.Set(tempDir + "/pubkey1,@" + tempDir + "/pubkey2") 99 if err != nil { 100 t.Fatalf("err: %s", err) 101 } 102 103 err = pkf.Set(tempDir + "/pubkey3") 104 if err == nil { 105 t.Fatalf("err: should not have been able to set a second value") 106 } 107 108 expected := []string{pubKey1, pubKey2} 109 if !reflect.DeepEqual(pkf.String(), fmt.Sprint(expected)) { 110 t.Fatalf("bad: got %s, expected %s", pkf.String(), fmt.Sprint(expected)) 111 } 112 } 113 114 func TestPubKeyFilesFlagSetKeybase(t *testing.T) { 115 tempDir, err := ioutil.TempDir("", "vault-test") 116 if err != nil { 117 t.Fatalf("Error creating temporary directory: %s", err) 118 } 119 defer os.RemoveAll(tempDir) 120 121 err = ioutil.WriteFile(tempDir+"/pubkey2", []byte(pubKey2), 0755) 122 if err != nil { 123 t.Fatalf("Error writing pub key 2 to temp file: %s", err) 124 } 125 126 pkf := new(PubKeyFilesFlag) 127 err = pkf.Set("keybase:jefferai,@" + tempDir + "/pubkey2" + ",keybase:hashicorp") 128 if err != nil { 129 t.Fatalf("err: %s", err) 130 } 131 fingerprints := []string{} 132 for _, pubkey := range []string(*pkf) { 133 keyBytes, err := base64.StdEncoding.DecodeString(pubkey) 134 if err != nil { 135 t.Fatalf("bad: %v", err) 136 } 137 pubKeyBuf := bytes.NewBuffer(keyBytes) 138 reader := packet.NewReader(pubKeyBuf) 139 entity, err := openpgp.ReadEntity(reader) 140 if err != nil { 141 t.Fatalf("bad: %v", err) 142 } 143 if entity == nil { 144 t.Fatalf("nil entity encountered") 145 } 146 fingerprints = append(fingerprints, hex.EncodeToString(entity.PrimaryKey.Fingerprint[:])) 147 } 148 149 exp := []string{ 150 "0f801f518ec853daff611e836528efcac6caa3db", 151 "cf3d4694c9f57b28cb4092c2eb832c67eb5e8957", 152 "c874011f0ab405110d02105534365d9472d7468f", 153 } 154 155 if !reflect.DeepEqual(fingerprints, exp) { 156 t.Fatalf("bad: got \n%#v\nexpected\n%#v\n", fingerprints, exp) 157 } 158 } 159 160 const pubKey1 = `mQENBFXbjPUBCADjNjCUQwfxKL+RR2GA6pv/1K+zJZ8UWIF9S0lk7cVIEfJiprzzwiMwBS5cD0da 161 rGin1FHvIWOZxujA7oW0O2TUuatqI3aAYDTfRYurh6iKLC+VS+F7H+/mhfFvKmgr0Y5kDCF1j0T/ 162 063QZ84IRGucR/X43IY7kAtmxGXH0dYOCzOe5UBX1fTn3mXGe2ImCDWBH7gOViynXmb6XNvXkP0f 163 sF5St9jhO7mbZU9EFkv9O3t3EaURfHopsCVDOlCkFCw5ArY+DUORHRzoMX0PnkyQb5OzibkChzpg 164 8hQssKeVGpuskTdz5Q7PtdW71jXd4fFVzoNH8fYwRpziD2xNvi6HABEBAAG0EFZhdWx0IFRlc3Qg 165 S2V5IDGJATgEEwECACIFAlXbjPUCGy8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