-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathcredCache_windows.go
300 lines (253 loc) · 9.44 KB
/
credCache_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright © 2017 Microsoft <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package common
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"sync"
"syscall"
"unsafe"
)
// CredCache manages credential caches.
type CredCache struct {
dpapiFilePath string
entropy *dataBlob
lock sync.Mutex
}
const azcopyverbose = "azcopyverbose"
const defaultTokenFileName = "accessToken.json"
// NewCredCache creates a cred cache.
func NewCredCache(options CredCacheOptions) *CredCache {
return &CredCache{
dpapiFilePath: options.DPAPIFilePath,
entropy: newDataBlob([]byte(azcopyverbose)),
}
}
// HasCachedToken returns if there is cached token for current executing user.
func (c *CredCache) HasCachedToken() (bool, error) {
c.lock.Lock()
has, err := c.hasCachedTokenInternal()
c.lock.Unlock()
return has, err
}
// RemoveCachedToken deletes the cached token.
func (c *CredCache) RemoveCachedToken() error {
c.lock.Lock()
err := c.removeCachedTokenInternal()
c.lock.Unlock()
return err
}
// SaveToken saves an oauth token.
func (c *CredCache) SaveToken(token OAuthTokenInfo) error {
c.lock.Lock()
err := c.saveTokenInternal(token)
c.lock.Unlock()
return err
}
// LoadToken gets the cached oauth token.
func (c *CredCache) LoadToken() (*OAuthTokenInfo, error) {
c.lock.Lock()
token, err := c.loadTokenInternal()
c.lock.Unlock()
return token, err
}
///////////////////////////////////////////////////////////////////////////////////////////////
// This internal method pattern is applied to avoid defer locks.
// The reason is:
// We use locks to protect shared state from being accessed from multiple threads/goroutines at the same time.
// If a bug is in this method that causes a panic,
// then the defer will unlock another thread/goroutine allowing it to access the shared state.
// BUT, if a panic happened, the shared state is hard to be decide whether in a good or corrupted state.
// So currently let the other threads/goroutines hang forever instead of letting them access the potentially corrupted shared state.
// Once having bad state, more bad state gets injected into app and figuring out how it happened and how to recover from it is near impossible.
// On the other hand, hanging threads is MUCH easier to detect and devs can fix the bug in code to make sure that the panic doesn't happen in the first place.
///////////////////////////////////////////////////////////////////////////////////////////////
// hasCachedTokenInternal returns if there is cached token in token manager.
func (c *CredCache) hasCachedTokenInternal() (bool, error) {
if _, err := os.Stat(c.tokenFilePath()); err == nil {
return true, nil
} else {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
}
// removeCachedTokenInternal deletes all the cached token.
func (c *CredCache) removeCachedTokenInternal() error {
tokenFilePath := c.tokenFilePath()
if _, err := os.Stat(tokenFilePath); err == nil {
// Cached token file existed
err = os.Remove(tokenFilePath)
if err != nil { // remove failed
return fmt.Errorf("failed to remove cached token file with path %q, %v", tokenFilePath, err)
}
// remove succeeded
} else {
if !os.IsNotExist(err) { // Failed to stat cached token file
return fmt.Errorf("failed to stat cached token file with path %q during removing, %v", tokenFilePath, err)
}
// token doesn't exist
return errors.New("no cached token found for current user")
}
return nil
}
// loadTokenInternal restores a Token object from file cache.
func (c *CredCache) loadTokenInternal() (*OAuthTokenInfo, error) {
tokenFilePath := c.tokenFilePath()
b, err := os.ReadFile(tokenFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read token file %q during loading token: %v", tokenFilePath, err)
}
decryptedB, err := decrypt(b, c.entropy)
if err != nil {
return nil, fmt.Errorf("failed to decrypt bytes during loading token: %v", err)
}
token, err := jsonToTokenInfo(decryptedB)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal token during loading token, %v", err)
}
return token, nil
}
// saveTokenInternal persists an oauth token on disk.
// It moves the new file into place so it can safely be used to replace an existing file
// that maybe accessed by multiple processes.
func (c *CredCache) saveTokenInternal(token OAuthTokenInfo) error {
tokenFilePath := c.tokenFilePath()
dir := filepath.Dir(tokenFilePath)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory %q to store token in, %v", dir, err)
}
newFile, err := os.CreateTemp(dir, "token")
if err != nil {
return fmt.Errorf("failed to create the temp file to write the token, %v", err)
}
tempPath := newFile.Name()
json, err := token.toJSON()
if err != nil {
return fmt.Errorf("failed to marshal token, %v", err)
}
b, err := encrypt(json, c.entropy)
if err != nil {
return fmt.Errorf("failed to encrypt token, %v", err)
}
if _, err = newFile.Write(b); err != nil {
return fmt.Errorf("failed to encode token to file %q while saving token, %v", tempPath, err)
}
if err := newFile.Close(); err != nil {
return fmt.Errorf("failed to close temp file %q, %v", tempPath, err)
}
// Atomic replace to avoid multi-writer file corruptions
if err := os.Rename(tempPath, tokenFilePath); err != nil {
return fmt.Errorf("failed to move temporary token to desired output location. src=%q dst=%q, %v", tempPath, tokenFilePath, err)
}
if err := os.Chmod(tokenFilePath, 0600); err != nil { // read/write for current user
return fmt.Errorf("failed to chmod the token file %q, %v", tokenFilePath, err)
}
return nil
}
func (c *CredCache) tokenFilePath() string {
if cacheFile := GetEnvironmentVariable(EEnvironmentVariable.LoginCacheName()); cacheFile != "" {
return path.Join(c.dpapiFilePath, "/", cacheFile)
}
return path.Join(c.dpapiFilePath, "/", defaultTokenFileName)
}
// ======================================================================================
// DPAPI facilities
// ======================================================================================
var dCrypt32 = syscall.NewLazyDLL("crypt32.dll") // lower case to tie in with Go's sysdll registration
var dKernel32 = syscall.NewLazyDLL("kernel32.dll")
// Refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa380261(v=vs.85).aspx for more details.
var mCryptProtectData = dCrypt32.NewProc("CryptProtectData")
// Refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa380882(v=vs.85).aspx for more details.
var mCryptUnprotectData = dCrypt32.NewProc("CryptUnprotectData")
// Refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730(v=vs.85).aspx for more details.
var mLocalFree = dKernel32.NewProc("LocalFree")
// dwFlags for protection. Remote situations where presenting a user interface (UI) is not an option.
// When this flag is set and a UI is specified for either the protect or unprotect operation, the operation fails and GetLastError returns the ERROR_PASSWORD_RESTRICTION code.
const cryptProtectUIForbidden = 0x1
type dataBlob struct {
cbData uint32
pbData *byte
}
func newDataBlob(d []byte) *dataBlob {
if len(d) == 0 {
return &dataBlob{}
}
return &dataBlob{
cbData: uint32(len(d)),
pbData: &d[0],
}
}
func (b dataBlob) toByteArray() []byte {
d := make([]byte, b.cbData)
copy(d, (*[1 << 30]byte)(unsafe.Pointer(b.pbData))[:])
return d
}
func encrypt(data []byte, entropy *dataBlob) ([]byte, error) {
if entropy == nil {
return nil, errors.New("entropy is enforced in AzCopy")
}
var outblob dataBlob
defer func() {
if outblob.pbData != nil {
_, _, _ = mLocalFree.Call(uintptr(unsafe.Pointer(outblob.pbData)))
}
}()
r, _, err := mCryptProtectData.Call(
uintptr(unsafe.Pointer(newDataBlob(data))),
0,
uintptr(unsafe.Pointer(entropy)),
0,
0,
cryptProtectUIForbidden,
uintptr(unsafe.Pointer(&outblob)))
if r == 0 {
return nil, err
}
return outblob.toByteArray(), nil
}
func decrypt(data []byte, entropy *dataBlob) ([]byte, error) {
if entropy == nil {
return nil, errors.New("entropy is enforced in AzCopy")
}
var outblob dataBlob
defer func() {
if outblob.pbData != nil {
_, _, _ = mLocalFree.Call(uintptr(unsafe.Pointer(outblob.pbData)))
}
}()
r, _, err := mCryptUnprotectData.Call(
uintptr(unsafe.Pointer(newDataBlob(data))),
0,
uintptr(unsafe.Pointer(entropy)),
0,
0,
cryptProtectUIForbidden,
uintptr(unsafe.Pointer(&outblob)))
if r == 0 {
return nil, err
}
return outblob.toByteArray(), nil
}