Skip to content

Commit

Permalink
add methods for secrets to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
havardelnan committed Feb 28, 2024
1 parent 5002cc5 commit 497314c
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/clients/vaultclient/vaultsecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,72 @@ func SetSecret(secretPath string, value []byte) (bool, error) {

return false, errors.New("could not set secret")
}

func (vc VaultClient) GetSecret(secretPath string) (map[string]interface{}, error) {
if secretPath == "" {
return nil, errors.New("secret path is nil or empty")
}

data, err := vc.Client.Read(vc.Context, secretPath)
if err != nil {
var vaultError *vault.ResponseError
errors.As(err, &vaultError)
msg := fmt.Sprintf("Could not get secret, StatusCode: %d", vaultError.StatusCode)
if vaultError.StatusCode == 404 {
rlog.Info(msg)
} else {
rlog.Error(msg, err)
}
return nil, fmt.Errorf(msg)
}
if data != nil {
return data.Data, nil
}

return nil, nil
}

func (vc VaultClient) GetSecretValue(secretPath string, key string) (string, error) {
if secretPath == "" {
return "", errors.New("secret path is nil or empty")
}

data, err := vc.Client.Read(vc.Context, secretPath)
if err != nil {
var err2 *vault.ResponseError
errors.As(err, &err2)
msg := fmt.Sprintf("Could not get secret, StatusCode: %d", err2.StatusCode)
if err2.StatusCode == 404 {
rlog.Info(msg)
} else {
rlog.Error(msg, err)
}
return "", fmt.Errorf(msg)
}
if data != nil {
vaultval, _ := data.Data["data"].(map[string]interface{})
vaultkey, _ := vaultval[key].(string)
return vaultkey, nil
}

return "", nil
}

func (vc VaultClient) SetSecret(secretPath string, value []byte) (bool, error) {
if len(secretPath) < 1 {
return false, fmt.Errorf("could not set secret, secret path is empty")
}

secret, err := vc.Client.WriteFromBytes(vc.Context, secretPath, value)
if err != nil {
msg := fmt.Sprintf("could not set secret on path: %s", secretPath)
rlog.Error(msg, err)
return false, errors.New(msg)
}

if secret.Data != nil {
return true, nil
}

return false, errors.New("could not set secret")
}

0 comments on commit 497314c

Please sign in to comment.