github.com/openshift-online/ocm-sdk-go@v0.1.473/authentication/securestore/pass_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 /* 5 Copyright (c) 2024 Red Hat, Inc. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package securestore 21 22 import ( 23 "fmt" 24 "os" 25 "os/exec" 26 "path/filepath" 27 "time" 28 29 . "github.com/onsi/ginkgo/v2" // nolint 30 . "github.com/onsi/gomega" // nolint 31 32 . "github.com/openshift-online/ocm-sdk-go/testing" // nolint 33 ) 34 35 // This test requires `pass` to be installed. 36 // macOS: `brew install pass` 37 // linux: `sudo apt-get install pass` or `sudo yum install pass` 38 39 const keyring_dir = "keyring-pass-test-*" 40 41 func runCmd(cmds ...string) { 42 cmd := exec.Command(cmds[0], cmds[1:]...) //nolint:gosec 43 out, err := cmd.CombinedOutput() 44 if err != nil { 45 fmt.Println(cmd) 46 fmt.Println(string(out)) 47 Fail(err.Error()) 48 } 49 } 50 51 var _ = Describe("Pass Keyring", Ordered, func() { 52 const backend = "pass" 53 54 BeforeAll(func() { 55 pwd, err := os.Getwd() 56 if err != nil { 57 Fail(err.Error()) 58 } 59 pwdParent := filepath.Dir(pwd) 60 61 // the default temp directory can't be used because gpg-agent complains with "socket name too long" 62 tmpdir, err := os.MkdirTemp("/tmp", keyring_dir) 63 if err != nil { 64 Fail(err.Error()) 65 66 } 67 tmpdirPass, err := os.MkdirTemp("/tmp", ".password-store-*") 68 if err != nil { 69 Fail(err.Error()) 70 } 71 72 // Initialise a blank GPG homedir; import & trust the test key 73 gnupghome := filepath.Join(tmpdir, ".gnupg") 74 err = os.Mkdir(gnupghome, os.FileMode(int(0700))) 75 if err != nil { 76 Fail(err.Error()) 77 } 78 os.Setenv("GNUPGHOME", gnupghome) 79 os.Setenv("PASSWORD_STORE_DIR", tmpdirPass) 80 os.Unsetenv("GPG_AGENT_INFO") 81 os.Unsetenv("GPG_TTY") 82 83 runCmd("gpg", "--batch", "--import", filepath.Join(pwdParent, "testdata", "test-gpg.key")) 84 runCmd("gpg", "--batch", "--import-ownertrust", filepath.Join(pwdParent, "testdata", "test-ownertrust-gpg.txt")) 85 runCmd("pass", "init", "ocm-devel@redhat.com") 86 87 DeferCleanup(func() { 88 os.Unsetenv("GNUPGHOME") 89 os.Unsetenv("PASSWORD_STORE_DIR") 90 os.RemoveAll(filepath.Join("/tmp", keyring_dir)) 91 }) 92 }) 93 94 BeforeEach(func() { 95 err := RemoveConfigFromKeyring(backend) 96 Expect(err).To(BeNil()) 97 }) 98 99 When("Listing Keyrings", func() { 100 It("Lists pass as a valid keyring", func() { 101 backends := AvailableBackends() 102 Expect(backends).To(ContainElement(backend)) 103 }) 104 }) 105 106 When("Using Pass", func() { 107 It("Stores/Removes configuration in Pass", func() { 108 // Create the token 109 accessToken := MakeTokenString("Bearer", 15*time.Minute) 110 111 // Run insert 112 err := UpsertConfigToKeyring(backend, []byte(accessToken)) 113 114 Expect(err).To(BeNil()) 115 116 // Check the content of the keyring 117 result, err := GetConfigFromKeyring(backend) 118 Expect(result).To(Equal([]byte(accessToken))) 119 Expect(err).To(BeNil()) 120 121 // Remove the configuration from the keyring 122 err = RemoveConfigFromKeyring(backend) 123 Expect(err).To(BeNil()) 124 125 // Ensure the keyring is empty 126 result, err = GetConfigFromKeyring(backend) 127 Expect(result).To(Equal([]byte(""))) 128 Expect(err).To(BeNil()) 129 }) 130 }) 131 })