@@ -152,11 +152,10 @@ func (c *CachingTokenSource) tokenFromDisk() (*identityToken, error) {
152
152
153
153
func (c * CachingTokenSource ) tokenToDisk (tok * identityToken ) error {
154
154
cacheKey := c .cacheKey ()
155
-
156
155
jsonCachePath := path .Join (c .cacheDir , fmt .Sprintf ("%s.json" , cacheKey ))
157
- jsonCachePathTmp := jsonCachePath + ".tmp"
158
156
159
- jsonCacheFile , err := os .OpenFile (jsonCachePathTmp , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0600 )
157
+ jsonTmpPattern := fmt .Sprintf ("%s.json.tmp.*" , cacheKey )
158
+ jsonCacheFile , err := os .CreateTemp (c .cacheDir , jsonTmpPattern )
160
159
if err != nil {
161
160
return fmt .Errorf ("unable to open cache file: %w" , err )
162
161
}
@@ -167,16 +166,18 @@ func (c *CachingTokenSource) tokenToDisk(tok *identityToken) error {
167
166
return fmt .Errorf ("unable to encode token: %w" , err )
168
167
}
169
168
jsonCacheFile .Close ()
170
- err = os .Rename (jsonCachePathTmp , jsonCachePath )
169
+ err = os .Rename (jsonCacheFile . Name () , jsonCachePath )
171
170
if err != nil {
172
171
return fmt .Errorf ("unable to rename tmpfile: %w" , err )
173
172
}
174
173
175
174
// also write out the raw token for use in fallback
176
175
rawCachePath := c .GetAccessTokenPath ()
177
- rawCachePathTmp := rawCachePath + ".tmp"
178
-
179
- rawCacheFile , err := os .OpenFile (rawCachePathTmp , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0600 )
176
+ rawTmpPattern := fmt .Sprintf ("%s.tmp.*" , cacheKey )
177
+ rawCacheFile , err := os .CreateTemp (c .cacheDir , rawTmpPattern )
178
+ if err != nil {
179
+ return fmt .Errorf ("unable to open cache file: %w" , err )
180
+ }
180
181
if err != nil {
181
182
return fmt .Errorf ("unable to open cache file: %w" , err )
182
183
}
@@ -186,7 +187,7 @@ func (c *CachingTokenSource) tokenToDisk(tok *identityToken) error {
186
187
return fmt .Errorf ("unable to write token to cache file: %w" , err )
187
188
}
188
189
rawCacheFile .Close ()
189
- err = os .Rename (rawCachePathTmp , rawCachePath )
190
+ err = os .Rename (rawCacheFile . Name () , rawCachePath )
190
191
if err != nil {
191
192
return fmt .Errorf ("unable to rename tmpfile: %w" , err )
192
193
}
0 commit comments