@@ -80,25 +80,34 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
8080 }
8181 return err
8282 }
83+ c .cli = cli
84+ c .path = path
85+
86+ if token != "" {
87+ cli .SetToken (token )
88+ return nil
89+ }
8390
84- if len (token ) == 0 {
85- rsp , err := cli .Logical ().Write ("auth/approle/login" , map [string ]interface {}{
86- "role_id" : roleID ,
87- "secret_id" : secretID ,
88- })
89- if err != nil {
90- c .opts .Logger .Errorf (c .opts .Context , "vault init approle err: %v" , err )
91- if ! c .opts .AllowFail {
92- return err
93- }
94- } else if err == nil {
95- token = rsp .Auth .ClientToken
91+ if roleID == "" || secretID == "" {
92+ if ! c .opts .AllowFail {
93+ return fmt .Errorf ("missing Token or RoleID and SecretID" )
9694 }
95+ return nil
9796 }
98- cli .SetToken (token )
9997
100- c .cli = cli
101- c .path = path
98+ rsp , err := cli .Logical ().Write ("auth/approle/login" , map [string ]interface {}{
99+ "role_id" : roleID ,
100+ "secret_id" : secretID ,
101+ })
102+
103+ if err != nil {
104+ c .opts .Logger .Errorf (c .opts .Context , "vault init approle err: %v" , err )
105+ if ! c .opts .AllowFail {
106+ return err
107+ }
108+ } else if err == nil {
109+ cli .SetToken (rsp .Auth .ClientToken )
110+ }
102111
103112 return nil
104113}
@@ -108,14 +117,6 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
108117 return err
109118 }
110119
111- path := c .path
112- options := config .NewLoadOptions (opts ... )
113- if options .Context != nil {
114- if v , ok := options .Context .Value (pathKey {}).(string ); ok && v != "" {
115- path = v
116- }
117- }
118-
119120 if c .cli == nil {
120121 c .opts .Logger .Errorf (c .opts .Context , "vault load err: %v" , fmt .Errorf ("vault client not created" ))
121122 if ! c .opts .AllowFail {
@@ -124,19 +125,19 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
124125 return config .DefaultAfterLoad (ctx , c )
125126 }
126127
127- pair , err := c .cli .Logical ().Read (path )
128+ pair , err := c .cli .Logical ().Read (c . path )
128129 if err != nil {
129- c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , path , err )
130+ c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , c . path , err )
130131 if ! c .opts .AllowFail {
131132 return err
132133 }
133134 return config .DefaultAfterLoad (ctx , c )
134135 }
135136
136137 if pair == nil || pair .Data == nil {
137- c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , path , fmt .Errorf ("not found" ))
138+ c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , c . path , fmt .Errorf ("not found" ))
138139 if ! c .opts .AllowFail {
139- return fmt .Errorf ("vault path %s not found" , path )
140+ return fmt .Errorf ("vault path %s not found" , c . path )
140141 }
141142 return config .DefaultAfterLoad (ctx , c )
142143 }
@@ -145,7 +146,7 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
145146 var src interface {}
146147 data , err = json .Marshal (pair .Data ["data" ])
147148 if err != nil {
148- c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , path , err )
149+ c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , c . path , err )
149150 if ! c .opts .AllowFail {
150151 return err
151152 }
@@ -158,13 +159,14 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
158159 }
159160
160161 if err != nil {
161- c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , path , err )
162+ c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , c . path , err )
162163 if ! c .opts .AllowFail {
163164 return err
164165 }
165166 return config .DefaultAfterLoad (ctx , c )
166167 }
167168
169+ options := config .NewLoadOptions (opts ... )
168170 mopts := []func (* mergo.Config ){mergo .WithTypeCheck }
169171 if options .Override {
170172 mopts = append (mopts , mergo .WithOverride )
@@ -175,7 +177,7 @@ func (c *vaultConfig) Load(ctx context.Context, opts ...config.LoadOption) error
175177 err = mergo .Merge (c .opts .Struct , src , mopts ... )
176178
177179 if err != nil {
178- c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , path , err )
180+ c .opts .Logger .Errorf (c .opts .Context , "vault load path %s err: %v" , c . path , err )
179181 if ! c .opts .AllowFail {
180182 return err
181183 }
@@ -193,39 +195,6 @@ func (c *vaultConfig) Save(ctx context.Context, opts ...config.SaveOption) error
193195 return err
194196 }
195197
196- path := c .path
197- options := config .NewSaveOptions (opts ... )
198- if options .Context != nil {
199- if v , ok := options .Context .Value (pathKey {}).(string ); ok && v != "" {
200- path = v
201- }
202- }
203-
204- if c .cli == nil {
205- c .opts .Logger .Errorf (c .opts .Context , "vault save err: %v" , fmt .Errorf ("vault client not created" ))
206- if ! c .opts .AllowFail {
207- return fmt .Errorf ("vault client not created" )
208- }
209- return config .DefaultAfterSave (ctx , c )
210- }
211-
212- buf , err := c .opts .Codec .Marshal (c .opts .Struct )
213- if err != nil {
214- if ! c .opts .AllowFail {
215- return err
216- }
217- return config .DefaultAfterSave (ctx , c )
218- }
219-
220- _ , err = c .cli .Logical ().WriteBytes (path , buf )
221- if err != nil {
222- c .opts .Logger .Errorf (c .opts .Context , "vault save path %s err: %v" , path , err )
223- if ! c .opts .AllowFail {
224- return err
225- }
226- return config .DefaultAfterSave (ctx , c )
227- }
228-
229198 if err := config .DefaultAfterSave (ctx , c ); err != nil {
230199 return err
231200 }
@@ -242,17 +211,9 @@ func (c *vaultConfig) Name() string {
242211}
243212
244213func (c * vaultConfig ) Watch (ctx context.Context , opts ... config.WatchOption ) (config.Watcher , error ) {
245- path := c .path
246- options := config .NewWatchOptions (opts ... )
247- if options .Context != nil {
248- if v , ok := options .Context .Value (pathKey {}).(string ); ok && v != "" {
249- path = v
250- }
251- }
252-
253214 w := & vaultWatcher {
254215 cli : c .cli ,
255- path : path ,
216+ path : c . path ,
256217 opts : c .opts ,
257218 wopts : config .NewWatchOptions (opts ... ),
258219 done : make (chan struct {}),
0 commit comments