Home > Mobile >  golang unmarshal yaml from a vault file
golang unmarshal yaml from a vault file

Time:10-01

With the below code I can get the yaml from the Ansible vault file which results in:

---
dbtype: redis
vsad: go0v

When attempting to unmarshal the YAML I get only:

map[string]string(nil)

My desired goal is to unvault the file, edit the data, re-vault the file.

How do I achieve the unmarshal so as to edit the data?

package main

import (
    "fmt"
    "github.com/sosedoff/ansible-vault-go"
    "gopkg.in/yaml.v2"
)

type Props struct {
    values map[string]string
}
func main() {
    str, err := vault.DecryptFile("/tmp/tmpvlt", `.NunY4hb33zWx!)`)
    if err != nil {
        panic(err)
    }
    props := Props{}
    err2 := yaml.Unmarshal([]byte(str), &props)
    if err2 != nil {
        panic(err2)
    }
    fmt.Println(str)
    fmt.Printf("%#v\n",props.values)

}

CodePudding user response:

You want to do either

var props map[string]string

or

err2 := yaml.Unmarshal([]byte(str), &props.values)

In your current code, the unmarshaler cannot access the private field values, and even if you make it public by renaming it to Values, it would not be filled because your YAML doesn't have a top-level key named values.

  • Related