github.com/jenkins-x/jx/v2@v2.1.155/pkg/secreturl/fakevault/client.go (about) 1 package fakevault 2 3 import ( 4 "regexp" 5 6 "github.com/jenkins-x/jx/v2/pkg/secreturl" 7 "github.com/jenkins-x/jx/v2/pkg/util" 8 "github.com/pkg/errors" 9 ) 10 11 var fakeURIRegex = regexp.MustCompile(`:[\s"]*vault:[-_.\w\/:]*`) 12 13 // FakeClient a local file system based client loading/saving content from the given URL 14 type FakeClient struct { 15 data map[string]map[string]interface{} 16 } 17 18 // NewFakeClient create a new fake client 19 func NewFakeClient() secreturl.Client { 20 return &FakeClient{ 21 data: map[string]map[string]interface{}{}, 22 } 23 } 24 25 // Read reads a named secret from the vault 26 func (c *FakeClient) Read(secretName string) (map[string]interface{}, error) { 27 return c.data[secretName], nil 28 } 29 30 // ReadObject reads a generic named object from vault. 31 // The secret _must_ be serializable to JSON. 32 func (c *FakeClient) ReadObject(secretName string, secret interface{}) error { 33 m, err := c.Read(secretName) 34 if err != nil { 35 return errors.Wrapf(err, "reading the secret %q from vault", secretName) 36 } 37 err = util.ToStructFromMapStringInterface(m, &secret) 38 if err != nil { 39 return errors.Wrapf(err, "deserializing the secret %q from vault", secretName) 40 } 41 return nil 42 } 43 44 // Write writes a named secret to the vault with the data provided. Data can be a generic map of stuff, but at all points 45 // in the map, keys _must_ be strings (not bool, int or even interface{}) otherwise you'll get an error 46 func (c *FakeClient) Write(secretName string, data map[string]interface{}) (map[string]interface{}, error) { 47 c.data[secretName] = data 48 return c.Read(secretName) 49 } 50 51 // WriteObject writes a generic named object to the vault. 52 // The secret _must_ be serializable to JSON. 53 func (c *FakeClient) WriteObject(secretName string, secret interface{}) (map[string]interface{}, error) { 54 // TODO 55 return c.Read(secretName) 56 } 57 58 // ReplaceURIs will replace any local: URIs in a string 59 func (c *FakeClient) ReplaceURIs(s string) (string, error) { 60 return secreturl.ReplaceURIs(s, c, fakeURIRegex, "vault:") 61 }