I am getting data from the Hashicorp Vault API, and struggling to manipulate it because I have a hazy understanding of interfaces still :(
I am getting a TLS cert response from the PKI backend. Using go-dumper, it outputs this:
(0xc4203880c0) &Secret {
RequestID: "271c63ef-d7b6-a084-18a0-966dd6989f03",
LeaseID: "",
LeaseDuration: 0 (int),
Renewable: false,
Data: map[string]interface {} {
"serial_number": interface() ,
"ca_chain": interface() ,
"certificate": interface() ,
"issuing_ca": interface() ,
"private_key": interface() ,
"private_key_type": interface()
},
Warnings: nil ([]string),
Auth: nil (*api.SecretAuth),
WrapInfo: nil (*api.SecretWrapInfo)
}
What I'd like to do is print the "certficate" field using fmt
. So I did this:
Created a struct:
type TLSCredentialResp struct {
IssuingCA string `mapstructure:"issuing_ca"`
PrivateKey string `mapstructure:"private_key"`
CAChain string `mapstructure:"ca_chain"`
Cert string `mapstructure:"certificate"`
}
and then used mapstructure to decode into it:
var response TLSCredentialResp
if err := mapstructure.Decode(docker.Data, &response); err != nil {
log.Fatal("Error parsing vault's credential response: ", err)
}
fmt.Printf("Private Key: %s", response.PrivateKey)
However, when I try this, I get the following:
Error parsing vault's credential response: 1 error(s) decoding:
* 'ca_chain' expected type 'string', got unconvertible type '[]interface {}'
exit status 1
Now it's fairly clear to me I've gotten this wrong, as I'm trying to Decode an interface into a string. What isn't clear to me is how I deal with interface()
as a type? Simply put, how can I get the plain text cert value out of that?
So I'm an idiot, this was way easier than I expected.
I don't need to use mapstructure it seems, I can just do this:
fmt.Printf("Private Key: %s", docker.Data["private_key"])
fmt.Printf("Cert: %s", docker.Data["certificate"])
Although I don't know why..