fix init with empty options
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 5s
automerge / automerge (pull_request) Failing after 5s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 7s
prbuild / lint (pull_request) Failing after 5s
codeql / analyze (go) (pull_request) Failing after 6s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-06-03 23:19:38 +03:00
parent ab279308cd
commit 0015bbd209

View File

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