Merge pull request 'fix init with empty options' (#115) from initfix into v3
Some checks failed
build / test (push) Failing after 6s
build / lint (push) Failing after 5s
codeql / analyze (go) (push) Failing after 5s

Reviewed-on: #115
This commit is contained in:
Василий Толстов 2023-06-03 23:20:05 +03:00
commit 68177c9962

View File

@ -17,6 +17,9 @@ var DefaultStructTag = "vault"
type vaultConfig struct {
path string
token string
roleID string
secretID string
cli *api.Client
opts config.Options
}
@ -95,21 +98,14 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
}
c.cli = cli
c.path = path
c.token = token
c.roleID = roleID
c.secretID = secretID
if token != "" {
cli.SetToken(token)
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
if err = c.setToken(); err != nil && !c.opts.AllowFail {
return err
}
return nil
} else if roleID == "" || secretID == "" {
if !c.opts.AllowFail {
return fmt.Errorf("missing Token or RoleID and SecretID")
}
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
@ -117,21 +113,23 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
return nil
}
rsp, err := cli.Logical().Write("auth/approle/login", map[string]interface{}{
"role_id": roleID,
"secret_id": secretID,
func (c *vaultConfig) setToken() error {
if c.token != "" {
c.cli.SetToken(c.token)
}
if c.roleID != "" && c.secretID != "" {
rsp, err := c.cli.Logical().Write("auth/approle/login", map[string]interface{}{
"role_id": c.roleID,
"secret_id": c.secretID,
})
if err != nil {
if !c.opts.AllowFail {
return err
}
} else if err == nil {
cli.SetToken(rsp.Auth.ClientToken)
c.cli.SetToken(rsp.Auth.ClientToken)
}
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil